Repository navigation
Developer Notes
The Developer Notes attempts to present a guide of the NeXus Constructor. This assumes you have some basic knowledge of how to create programs with Python + QML. If not, it might be best to examine the QML and Qt for Python sections of the Wiki first.
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
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'sjsonpackage
The LabeledTextField consists of a TextField and a Label bundled into one item.
This section describes the custom validators used in the NeXus constructor and their purpose. Creating custom validators requires a combination of QML and Python which is discussed here. An overview of QML validators in general can be found here.
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.