Skip to content

How to create a hardware object and brick in Qt4

Ivars Karpics edited this page Jan 17, 2019 · 13 revisions

This tutorial describes how to create a simple hardware objects and brick in the Qt version of MXCuBE. During the tutorial you will create a configuration xml for a hardware object, write a simple hardware object and add a brick to the graphical user interface. To start the tutorial a running mxcube version with Qt graphics needs to be installed.

1. Create configuration xml file

Location of configuration files is passed with keyword argument --hardwareRepository=. By default configuration files are located in the HardwareRepository/configuration/xml-qt directory. With text editor create a new xml file.

vi test-config.xml
<object class="TestHardwareObject">
  <property_one>123</property_one>
</object>

In this example configuration xml describes object with a class name TestHardwareObject and one property with name property_one.

2. Create hardware object

Hardware objects are located in the HardwareObjects directory. The name of the file and the class name of the hardware object has to match with the object name in the xml.

vi TestHardwareObject.py
from HardwareRepository.BaseHardwareObjects import HardwareObject

class TestHardwareObject(HardwareObject):

    def __init__(self, name):
        HardwareObject.__init__(self, name)

        self.property_one = None

    def init(self):
        self.property_one = self.getProperty("property_one")

    def get_property_one(self):
        return self.property_one

In the __init__ method all attributes should be listed and in init method initialized.

3. Create brick

Bricks are located in the Bricks directory. The name of the file should end with Brick to allow importing bricks in the gui designer.

vi TestBrick.py
import QtImport

from gui.BaseComponents import BaseWidget

__category__ = "General"

class TestBrick(BaseWidget):

    def __init__(self, *args):
        BaseWidget.__init__(self, *args)

        # Hardware objects ----------------------------------------------------
        self.test_hwobj = None

        # Internal variables --------------------------------------------------

        # Properties ----------------------------------------------------------
        self.add_property('mnemonic', 'string', '')

        # Signals ------------------------------------------------------------

        # Slots ---------------------------------------------------------------

        # Graphic elements ----------------------------------------------------
        _main_gbox = QtImport.QGroupBox("Test brick", self)
        self.test_ledit = QtImport.QLineEdit("", _main_gbox)

        # Layout --------------------------------------------------------------
        _main_gbox_vlayout = QtImport.QVBoxLayout(_main_gbox)
        _main_gbox_vlayout.addWidget(self.test_ledit)
        _main_gbox_vlayout.setSpacing(2)
        _main_gbox_vlayout.setContentsMargins(0, 0, 0, 0)

        _main_vlayout = QtImport.QVBoxLayout(self)
        _main_vlayout.addWidget(_main_gbox)
        _main_vlayout.addStretch()
        _main_vlayout.setSpacing(0)
        _main_vlayout.setContentsMargins(0, 0, 0, 0)

        # SizePolicies --------------------------------------------------------

        # Qt signal/slot connections ------------------------------------------

        # Other ---------------------------------------------------------------

    def property_changed(self, property_name, old_values, new_value):
        if property_name == "mnemonic":
            self.test_hwobj = self.get_hardware_object(new_value)
            if self.test_hwobj is not None:
                self.test_ledit.setText(str(self.test_hwobj.get_property_one()))

Brick contains a group box with title "Test brick" and a LineEdit to display some value.

4. Add brick to the gui

Start mxcube in designer mode (keyword argument -d) and find the name of the brick in the list of bricks. In this case you have to search for the name Test (postfix Brick is removed). __category__ of a brick sorts all bricks in categories.

In the tree locate necessary place for the brick and by double clicking on the name, add the new brick to the gui. Save the gui file.

After adding the brick to the gui, you can change the name of the brick and edit its properties. Properties added in the code appear on the gui builder. For example mnemonic is a property used to link hardware object with a brick.

        # Properties ----------------------------------------------------------
        self.add_property('mnemonic', 'string', '')

By clicking on the brick on the tree or on the gui preview, a table with properties is displayed. Set the value of property mnemonic to test-config (name of xml config file).

    def property_changed(self, property_name, old_values, new_value):
        if property_name == "mnemonic":
            self.test_hwobj = self.get_hardware_object(new_value)
            if self.test_hwobj:
                self.test_ledit.setText(str(self.test_hwobj.get_property_one()))

Method property_changed is called once on the startup and is used to customize the brick. In this case property mnemonic is used to initialize hardware object and display value 123 on the LineEdit.

More tutorials to be added...