# -*- coding: utf-8 -*-
"""
Created on Mon Jan 3 09:19:17 2022
TODO:
Bug: no bugs known, but in work.....
"""
import sys
# import numpy as np
import os
from PyQt5 import QtWidgets
from pylab_ml.gui.instruments.base_instrument import Gui as Guibase
from pylab_ml.gui.instruments.base_instrument import load_ui
__author__ = "Zlin526F"
__credits__ = ["Zlin526F"]
__email__ = "Zlin526F@github"
__version__ = "0.0.0"
[docs]
class Gui(Guibase):
"""
Template Gui.
Inherited from base_instrument
status
"""
_states = {
"connect": ["connect", "color: rgb(0, 255, 0)"], # overwrite the definitions in base_instrument
"disconnect": ["disconnect", "color: rgb(255, 0, 0)"],
"on": ["running", "color: rgb(0, 255, 0)"],
"off": ["waiting for start...", "color: palette(HighlightedText)"],
}
[docs]
def __init__(self, parent=None, name='scope', channel=None):
"""
Initialize the GUI, load the UI file, adjust the UI elements, and set up MQTT connections.
Parameters
----------
parent : object, optional
The parent object to which this GUI belongs. Default is None.
name : str, optional
The name of the instrument. Default is 'scope'.
channel : int, optional
The channel number. Default is None.
"""
super().__init__(grandparent=parent, name=name)
self.myframe = load_ui(self.gui.myframe, __file__)
# bgcolor = self.gui.palette().color(QtGui.QPalette.Background).name() # getRgb()
self.myadjustUI()
self.gui.closeEvent = self.close
self.parent = parent
self.mqtt_initlist = ['all attributes do you need. Widgets start with MQTT will add automaticaly']
[docs]
def myadjustUI(self):
""" Adjust the UI elements, set icons, create and connect menu entries, and connect MQTT widgets. """
# set icons:
self.gui.runToolBar.setVisible(False)
# self.add_menuicon('onoff') # add existing icon and connection from the base-instrument
# create and connect Menue entries
#self.gui.actionexcel = self.add_menue("open registermaster in excel", self.openexcel, False)
# set connection fom Gui-object to functions:
#self.myframe.Dsamples.valueChanged.connect(self.rangeSamples)
#self.myframe.Echname.editingFinished.connect(self.editchname)
self.mqttConnectWidgets() # connect all widget which start with MQTT_
geometry = self.gui.geometry()
wh = self.myframe.geometry()
self.gui.setWindowTitle(f' {os.path.basename(__file__).split(".")[0]} (Version: {__version__})')
self.myframe.setGeometry(geometry.x(), geometry.y(), wh.width()+100, wh.height()+250)
# ======================================================
# attributes which connect to an extern call (mqtt-command)
[docs]
def mqttreceive(self, instName, msg):
"""
Common MQTT receive messages, get raw MQTT data for more information.
Parameters
----------
instName : str
The name of the instrument that received the MQTT message.
msg : dict
The MQTT message received, expected to contain 'cmd' and 'payload' keys for processing.
"""
self.logger.debug(f"{instName}.mqttreceive: {msg} ")
if super().mqttreceive(instName, msg):
return
# if you change a widget with the mqtt command, you have to use the widget.blockSignals(True/False)
if 'cmd' in tuple(msg.keys()) and 'payload' in tuple(msg.keys()):
if msg['cmd'] == 'my attribute': # special handling from mqtt commands
value = msg['payload']
self.logger.info(f"{instName}: {msg['cmd']} should do something with value = {value} -> implement it")
else:
self.logger.warning(f"{instName} {msg['cmd']} not found -> do nothing")
# ======================================================================================
# definitions from complex mqtt commands, if you need only a connection between the mqtt-command and a widget, start the name of your widget with MQTT_
# end extern calling attributes
# ======================================================================================
#
# connected GUI-function to buttons or menues
#
[docs]
def close(self, event=None):
""" Close the GUI, publish an 'off' message, and call the close method of the base class. """
self.publish('off()')
super().close()
if __name__ == "__main__":
# from pylab_ml.common.mqtt_client import mqtt_init
# broker = 127.0.0.1'
# message_client = 'ate/DT1604092/matrix'
# mqttclient = mqtt_init() # prepare mqtt
# mqttclient.init(None, message_client=message_client.split('/')[0]) # mqtt client connect to broker
# mqttclient.init()
app = QtWidgets.QApplication(sys.argv)
window = Gui(app)
app.exec_()
# window.scope.anim.event_source.stop()