|
1 # -*- coding: utf-8 -*- |
|
2 |
|
3 # Copyright (c) 2018 Detlev Offenbach <detlev@die-offenbachs.de> |
|
4 # |
|
5 |
|
6 """ |
|
7 Module implementing the MQTT Monitor plug-in. |
|
8 """ |
|
9 |
|
10 from __future__ import unicode_literals |
|
11 |
|
12 import os |
|
13 |
|
14 from PyQt5.QtCore import Qt, QObject, QTranslator, QCoreApplication |
|
15 from PyQt5.QtGui import QKeySequence |
|
16 |
|
17 from E5Gui.E5Application import e5App |
|
18 from E5Gui.E5Action import E5Action |
|
19 |
|
20 import UI.PixmapCache |
|
21 |
|
22 # Start-Of-Header |
|
23 name = "MQTT Monitor Plugin" |
|
24 author = "Detlev Offenbach <detlev@die-offenbachs.de>" |
|
25 autoactivate = True |
|
26 deactivateable = True |
|
27 version = "1.x" |
|
28 className = "MqttMonitorPlugin" |
|
29 packageName = "MqttMonitor" |
|
30 shortDescription = "Plug-in implementing a tool to connect to a MQTT broker" |
|
31 longDescription = ( |
|
32 """Plug-in implementing a tool to connect to a MQTT broker, subscribe""" |
|
33 """ to topics, present received messages and publish messages.\n\n""" |
|
34 """Note: The paho-mqtt Python package must be installed.""" |
|
35 ) |
|
36 needsRestart = False |
|
37 pyqtApi = 2 |
|
38 python2Compatible = True |
|
39 # End-Of-Header |
|
40 |
|
41 error = "" |
|
42 |
|
43 |
|
44 def exeDisplayData(): |
|
45 """ |
|
46 Module function to support the display of some executable info. |
|
47 |
|
48 @return dictionary containing the data to query the presence of |
|
49 the executable |
|
50 @rtype dict |
|
51 """ |
|
52 try: |
|
53 import paho.mqtt |
|
54 version = paho.mqtt.__version__ |
|
55 except ImportError: |
|
56 version = QCoreApplication.translate( |
|
57 "MqttMonitorPlugin", "package not available") |
|
58 |
|
59 data = { |
|
60 "programEntry": False, |
|
61 "header": QCoreApplication.translate("MqttMonitorPlugin", "MQTT"), |
|
62 "text": QCoreApplication.translate("MqttMonitorPlugin", "paho-mqtt"), |
|
63 "version": version, |
|
64 } |
|
65 |
|
66 return data |
|
67 |
|
68 |
|
69 class MqttMonitorPlugin(QObject): |
|
70 """ |
|
71 Class implementing the MQTT Monitor plug-in. |
|
72 """ |
|
73 def __init__(self, ui): |
|
74 """ |
|
75 Constructor |
|
76 |
|
77 @param ui reference to the user interface object |
|
78 @type UI.UserInterface |
|
79 """ |
|
80 super(MqttMonitorPlugin, self).__init__(ui) |
|
81 self.__ui = ui |
|
82 self.__initialize() |
|
83 |
|
84 self.__translator = None |
|
85 self.__loadTranslator() |
|
86 |
|
87 def __initialize(self): |
|
88 """ |
|
89 Private slot to (re)initialize the plugin. |
|
90 """ |
|
91 self.__widget = None |
|
92 |
|
93 def activate(self): |
|
94 """ |
|
95 Public method to activate this plug-in. |
|
96 |
|
97 @return tuple of None and activation status |
|
98 @rtype bool |
|
99 """ |
|
100 global error |
|
101 error = "" # clear previous error |
|
102 |
|
103 try: |
|
104 import paho.mqtt # __IGNORE_WARNING__ |
|
105 except ImportError: |
|
106 error = self.tr("The 'paho-mqtt' package is not available.") |
|
107 return None, False |
|
108 |
|
109 ## self.__object = MqttMonitorWidget(self, self.__ui) |
|
110 ## self.__object.activate() |
|
111 ## e5App().registerPluginObject("MqttMonitor", self.__object) |
|
112 from MqttMonitor.MqttMonitorWidget import MqttMonitorWidget |
|
113 |
|
114 self.__widget = MqttMonitorWidget()##self.__ui) |
|
115 self.__ui.addSideWidget( |
|
116 self.__ui.RightSide, self.__widget, |
|
117 UI.PixmapCache.getIcon( |
|
118 os.path.join("MqttMonitor", "icons", "mqtt22.png")), |
|
119 self.tr("MQTT Monitor")) |
|
120 |
|
121 self.__activateAct = E5Action( |
|
122 self.tr('MQTT Monitor'), |
|
123 self.tr('M&QTT Monitor'), |
|
124 QKeySequence(self.tr("Alt+Shift+Q")), |
|
125 0, self, |
|
126 'mqtt_monitor_activate') |
|
127 self.__activateAct.setStatusTip(self.tr( |
|
128 "Switch the input focus to the MQTT Monitor window.")) |
|
129 self.__activateAct.setWhatsThis(self.tr( |
|
130 """<b>Activate MQTT Monitor</b>""" |
|
131 """<p>This switches the input focus to the MQTT Monitor""" |
|
132 """ window.</p>""" |
|
133 )) |
|
134 self.__activateAct.triggered.connect(self.__activateWidget) |
|
135 |
|
136 self.__ui.addE5Actions([self.__activateAct], 'ui') |
|
137 menu = self.__ui.getMenu("subwindow") |
|
138 menu.addAction(self.__activateAct) |
|
139 |
|
140 return None, True |
|
141 |
|
142 def deactivate(self): |
|
143 """ |
|
144 Public method to deactivate this plug-in. |
|
145 """ |
|
146 ## e5App().unregisterPluginObject("TimeTracker") |
|
147 ## self.__object.deactivate() |
|
148 menu = self.__ui.getMenu("subwindow") |
|
149 menu.removeAction(self.__activateAct) |
|
150 self.__ui.removeE5Actions([self.__activateAct], 'ui') |
|
151 self.__ui.removeSideWidget(self.__widget) |
|
152 |
|
153 self.__initialize() |
|
154 |
|
155 def __loadTranslator(self): |
|
156 """ |
|
157 Private method to load the translation file. |
|
158 """ |
|
159 if self.__ui is not None: |
|
160 loc = self.__ui.getLocale() |
|
161 if loc and loc != "C": |
|
162 locale_dir = os.path.join( |
|
163 os.path.dirname(__file__), "MqttMonitor", "i18n") |
|
164 translation = "mqttmonitor{0}".format(loc) |
|
165 translator = QTranslator(None) |
|
166 loaded = translator.load(translation, locale_dir) |
|
167 if loaded: |
|
168 self.__translator = translator |
|
169 e5App().installTranslator(self.__translator) |
|
170 else: |
|
171 print("Warning: translation file '{0}' could not be" |
|
172 " loaded.".format(translation)) |
|
173 print("Using default.") |
|
174 |
|
175 def __activateWidget(self): |
|
176 """ |
|
177 Private slot to handle the activation of the MQTT Monitor. |
|
178 """ |
|
179 try: |
|
180 uiLayoutType = self.__ui.getLayoutType() |
|
181 except AttributeError: |
|
182 # backward compatibility for eric < 18.08 |
|
183 uiLayoutType = self.__ui.layoutType |
|
184 |
|
185 if uiLayoutType == "Toolboxes": |
|
186 self.__ui.rToolboxDock.show() |
|
187 self.__ui.rToolbox.setCurrentWidget(self.__widget) |
|
188 elif uiLayoutType == "Sidebars": |
|
189 self.__ui.rightSidebar.show() |
|
190 self.__ui.rightSidebar.setCurrentWidget(self.__widget) |
|
191 else: |
|
192 self.__widget.show() |
|
193 self.__widget.setFocus(Qt.ActiveWindowFocusReason) |