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.
Some of the fields in the TextField and Label can be accessed via aliases. Adding further aliases is quite simple.
Note: Setting readOnly to true is helpful in the case of things such as file selection where you only want the contents of the box to be changed by a FileDialog or something else in QML. Modifying the text can still be achieved programmatically in readOnly mode and is a safe bet for fields with "predictable" inputs.
The ActiveScrollBar is simply an ordinary ScrollBar plus a QML trick that causes the ScrollBar to always be visible if the contents of a Flickable are sufficiently long, and always be invisible if the contents of a Flickbable aren't sufficiently long.
By contrast, an ordinary ScrollBar with its policy set to ScrollBar.AsNeeded appears depending on whether the user's mouse is above the Flickable. This can result in a user being unaware that a list may be scrolled through unless they happen to move their mouse over it (something that can't be guaranteed). For this reason, ActiveScrollBar should be the preferred form of scroll bar that is used in the program.
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.
The ValidUnits singleton in ValidUnits.qml contains variables that are used by QML when carrying out unit validation. These consist of
-
validMeshUnits- Defaults to False. Set to true if a user opens a mesh file and provides a valid unit of length. -
validCylinderUnits- Defaults to False. Set to true if a user attempts to create a cylinder and provides a valid unit of length. -
showCylinderUnitMessage- Defaults to False. Set to True if unit validation fails. -
invalidUnitsText- Read-only string that contains the text displayed when unit validation fails.
These can then be used together through the following code:
text: ValidUnits.showCylinderUnitMessage ? ValidUnits.invalidUnitsText : ""In the above example, the text is either blank or contains a warning message depending on the state of showCylinderUnitMessage.
Set to False when the Mesh option is selected when adding a geometry. Is set to True after a file is selected with the FileDialog. If a user attempts to "Add" a mesh without selecting a file then this is used to display a warning message.