PluginMqttMonitor.py

changeset 1
bf1a17419d44
parent 0
0a65b0f02567
child 3
82845c0fd550
--- a/PluginMqttMonitor.py	Sun Aug 26 15:16:33 2018 +0200
+++ b/PluginMqttMonitor.py	Sun Aug 26 17:27:45 2018 +0200
@@ -0,0 +1,193 @@
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2018 Detlev Offenbach <detlev@die-offenbachs.de>
+#
+
+"""
+Module implementing the MQTT Monitor plug-in.
+"""
+
+from __future__ import unicode_literals
+
+import os
+
+from PyQt5.QtCore import Qt, QObject, QTranslator, QCoreApplication
+from PyQt5.QtGui import QKeySequence
+
+from E5Gui.E5Application import e5App
+from E5Gui.E5Action import E5Action
+
+import UI.PixmapCache
+
+# Start-Of-Header
+name = "MQTT Monitor Plugin"
+author = "Detlev Offenbach <detlev@die-offenbachs.de>"
+autoactivate = True
+deactivateable = True
+version = "1.x"
+className = "MqttMonitorPlugin"
+packageName = "MqttMonitor"
+shortDescription = "Plug-in implementing a tool to connect to a MQTT broker"
+longDescription = (
+    """Plug-in implementing a tool to connect to a MQTT broker, subscribe"""
+    """ to topics, present received messages and publish messages.\n\n"""
+    """Note: The paho-mqtt Python package must be installed."""
+)
+needsRestart = False
+pyqtApi = 2
+python2Compatible = True
+# End-Of-Header
+
+error = ""
+    
+
+def exeDisplayData():
+    """
+    Module function to support the display of some executable info.
+    
+    @return dictionary containing the data to query the presence of
+        the executable
+    @rtype dict
+    """
+    try:
+        import paho.mqtt
+        version = paho.mqtt.__version__
+    except ImportError:
+        version = QCoreApplication.translate(
+            "MqttMonitorPlugin", "package not available")
+    
+    data = {
+        "programEntry": False,
+        "header": QCoreApplication.translate("MqttMonitorPlugin", "MQTT"),
+        "text": QCoreApplication.translate("MqttMonitorPlugin", "paho-mqtt"),
+        "version": version,
+    }
+    
+    return data
+
+
+class MqttMonitorPlugin(QObject):
+    """
+    Class implementing the MQTT Monitor plug-in.
+    """
+    def __init__(self, ui):
+        """
+        Constructor
+        
+        @param ui reference to the user interface object
+        @type UI.UserInterface
+        """
+        super(MqttMonitorPlugin, self).__init__(ui)
+        self.__ui = ui
+        self.__initialize()
+        
+        self.__translator = None
+        self.__loadTranslator()
+    
+    def __initialize(self):
+        """
+        Private slot to (re)initialize the plugin.
+        """
+        self.__widget = None
+    
+    def activate(self):
+        """
+        Public method to activate this plug-in.
+        
+        @return tuple of None and activation status
+        @rtype bool
+        """
+        global error
+        error = ""     # clear previous error
+        
+        try:
+            import paho.mqtt        # __IGNORE_WARNING__
+        except ImportError:
+            error = self.tr("The 'paho-mqtt' package is not available.")
+            return None, False
+        
+##        self.__object = MqttMonitorWidget(self, self.__ui)
+##        self.__object.activate()
+##        e5App().registerPluginObject("MqttMonitor", self.__object)
+        from MqttMonitor.MqttMonitorWidget import MqttMonitorWidget
+        
+        self.__widget = MqttMonitorWidget()##self.__ui)
+        self.__ui.addSideWidget(
+            self.__ui.RightSide, self.__widget,
+            UI.PixmapCache.getIcon(
+                os.path.join("MqttMonitor", "icons", "mqtt22.png")),
+            self.tr("MQTT Monitor"))
+        
+        self.__activateAct = E5Action(
+            self.tr('MQTT Monitor'),
+            self.tr('M&QTT Monitor'),
+            QKeySequence(self.tr("Alt+Shift+Q")),
+            0, self,
+            'mqtt_monitor_activate')
+        self.__activateAct.setStatusTip(self.tr(
+            "Switch the input focus to the MQTT Monitor window."))
+        self.__activateAct.setWhatsThis(self.tr(
+            """<b>Activate MQTT Monitor</b>"""
+            """<p>This switches the input focus to the MQTT Monitor"""
+            """ window.</p>"""
+        ))
+        self.__activateAct.triggered.connect(self.__activateWidget)
+        
+        self.__ui.addE5Actions([self.__activateAct], 'ui')
+        menu = self.__ui.getMenu("subwindow")
+        menu.addAction(self.__activateAct)
+        
+        return None, True
+    
+    def deactivate(self):
+        """
+        Public method to deactivate this plug-in.
+        """
+##        e5App().unregisterPluginObject("TimeTracker")
+##        self.__object.deactivate()
+        menu = self.__ui.getMenu("subwindow")
+        menu.removeAction(self.__activateAct)
+        self.__ui.removeE5Actions([self.__activateAct], 'ui')
+        self.__ui.removeSideWidget(self.__widget)
+        
+        self.__initialize()
+    
+    def __loadTranslator(self):
+        """
+        Private method to load the translation file.
+        """
+        if self.__ui is not None:
+            loc = self.__ui.getLocale()
+            if loc and loc != "C":
+                locale_dir = os.path.join(
+                    os.path.dirname(__file__), "MqttMonitor", "i18n")
+                translation = "mqttmonitor{0}".format(loc)
+                translator = QTranslator(None)
+                loaded = translator.load(translation, locale_dir)
+                if loaded:
+                    self.__translator = translator
+                    e5App().installTranslator(self.__translator)
+                else:
+                    print("Warning: translation file '{0}' could not be"
+                          " loaded.".format(translation))
+                    print("Using default.")
+    
+    def __activateWidget(self):
+        """
+        Private slot to handle the activation of the MQTT Monitor.
+        """
+        try:
+            uiLayoutType = self.__ui.getLayoutType()
+        except AttributeError:
+            # backward compatibility for eric < 18.08
+            uiLayoutType = self.__ui.layoutType
+        
+        if uiLayoutType == "Toolboxes":
+            self.__ui.rToolboxDock.show()
+            self.__ui.rToolbox.setCurrentWidget(self.__widget)
+        elif uiLayoutType == "Sidebars":
+            self.__ui.rightSidebar.show()
+            self.__ui.rightSidebar.setCurrentWidget(self.__widget)
+        else:
+            self.__widget.show()
+        self.__widget.setFocus(Qt.ActiveWindowFocusReason)

eric ide

mercurial