Skip to content
This repository was archived by the owner on Nov 7, 2024. It is now read-only.

Developer Notes

Dolica Akello-Egwel edited this page Mar 29, 2019 · 42 revisions

Python and QML

The data classes that internally model an instrument's geometry are found in the nexus_constructor.data_model module. The data that these classes contain is then exposed to the QML interface through a series of model classes defined in nexus_constructor.qml_models. These models create a set of named properties, or Roles, that can be read and edited through the QML UI.

For these custom models to be accessible in QML, they need to be registered to a module. This is done in nexus_constructor/application.py alongside the class that loads the QML files.

To use a custom model and its properties, import its qml module, create an instance, and use it as the model for a ListView component.

...
import MyModels 1.0

ListView {
    width: 100; height: 200

    model: InstrumentModel {}
    delegate: Text {
        text: name
    }
}

Methods in custom QML/python classes can be made callable using slot and signal annotations

QML sizing

Each item in QML has two sets of sizing properties.

  • width and height are the actual on screen size of a component in pixels
  • implicitWidth and implicitHeight are the recommended minimum/default size a component should be in pixels

For some components (Pane and Frame), implicit sizes are calculated automatically based on their padding values, and their assigned contentWidth and contentHeight parameters.

Components with a set implicit size can be larger, for instance if they have been anchored to other components that are wider.

Rectangle {
    id: rectangle1
    width: 200
}
Rectangle {
    implicitWidth: 100
    anchors.left: rectangle1.left
    anchors.right: rectangle1.right
}

A key use of the distinction between the types of width is used in the editor windows. Each section has its own implicit minimum width. Their container is set to use the largest implicit width as its actual width, and each component is then anchored to the sides of the container so that they all appear the same width.

Were just width properties to be used, a qml binding loop error would occur, as width would be in a circular dependency with the width of its parent item.

JSON formats

The NeXus Constructor supports two different json formats, each with a different purpose.

Nexus Constructor json is designed to mirror the structure of the InstrumentModel and data_model classes. Thus it can be used to store and reload identical representations of the data in them for saving and returning to at a later time. Its format is defined in Instrument.schema.json

Nexus FileWriter json stores an instrument's data in the format required by ESS-DMSC's Nexus Filewriter to produce a standard compliant nexus file. As a result, certain transformations are made to the data before it goes into this format which cannot be reversed, making saving and reloading into the nexus constructor from this format slightly lossy. This is mostly due to floating point calculation inaccuracies, but also includes explicitly stating the dependent transform in a parent if the model was implicitly using the last one before exporting.

Each format has a package that contains methods for writing and parsing json. These packages expose the following methods:

  • generate_json(model: InstrumentModel) produces a json string from the given model
  • load_json_object_into_instrument_model(json_object: dict, model: InstrumentModel) populates a model with data from a json_object, which can be obtained from the load methods in python's json package

Validator Overview

UnitValidator

The UnitValidator uses the pint library to check if a text input matches a valid unit of length. It will return QValidator.Intermediate and emit the ValidationFailed signal if:

  • the input could not be converted to a known unit
  • the input is a unit of time/volume/area/etc
  • the input has no units and simply consists of a number
  • the input has a magnitude other than 1 (e.g. "40 cm")

Some of these inputs are required because pint is flexible about what it regards as an acceptable physical quantity object. If given an empty string, pint will convert this to 1 dimensionless instead of throwing an Exception. This behaviour can then cause problems if a dimensionless object is allowed to propagate further into the program and be passed to functions that assume it is a length. In light of this, some care may be required when using pint for checking lengths as you might assume it will reject inputs that it is actually able to accept.

If the input passes all of the above checks then the UnitValidator will return QValidator.Acceptable and emit the ValidationSuccess signal.

Clone this wiki locally