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

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.

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

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

Custom Items in the NeXus Constructor

LabeledTextField

The LabeledTextField consists of a TextField and a Label bundled into one item.

Its fields can be accessed via the following aliases:

property alias labelText: label.text
property alias editorText: field.text
property alias editorWidth: field.width
property alias validator: field.validator
property alias readOnly: field.readOnly

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.

ActiveScrollBar

Custom Validators in the NeXus Constructor

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.

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