Changed code to remember the most recently subscribed topics. eric7

Sat, 24 Jul 2021 16:12:05 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 24 Jul 2021 16:12:05 +0200
branch
eric7
changeset 106
84498095d511
parent 105
36ec7431ad04
child 107
639445679db1

Changed code to remember the most recently subscribed topics.

MqttMonitor/ConfigurationPage/MqttPage.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.ui file | annotate | diff | comparison | revisions
PluginMqttMonitor.py file | annotate | diff | comparison | revisions
--- a/MqttMonitor/ConfigurationPage/MqttPage.py	Fri Jul 23 19:48:14 2021 +0200
+++ b/MqttMonitor/ConfigurationPage/MqttPage.py	Sat Jul 24 16:12:05 2021 +0200
@@ -60,8 +60,9 @@
             # should never happen
             protocol = MqttProtocols.MQTTv311
         
-        self.__plugin.setPreferences("DefaultProtocol", protocol)
-        self.__plugin.setPreferences("RecentBrokersNumber",
-            self.recentBrokersSpinBox.value())
-        self.__plugin.setPreferences("RecentTopicsNumber",
-            self.recentTopicsSpinBox.value())
+        self.__plugin.setPreferences(
+            "DefaultProtocol", protocol)
+        self.__plugin.setPreferences(
+            "RecentBrokersNumber", self.recentBrokersSpinBox.value())
+        self.__plugin.setPreferences(
+            "RecentTopicsNumber", self.recentTopicsSpinBox.value())
--- a/MqttMonitor/MqttMonitorWidget.py	Fri Jul 23 19:48:14 2021 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Sat Jul 24 16:12:05 2021 +0200
@@ -69,6 +69,9 @@
             EricPathPickerModes.OPEN_FILE_MODE)
         self.publishPayloadFilePicker.setFilters(self.tr("All Files (*)"))
         
+        self.brokerComboBox.lineEdit().setClearButtonEnabled(True)
+        self.publishTopicComboBox.lineEdit().setClearButtonEnabled(True)
+        
         self.__messagesFormat = self.messagesEdit.currentCharFormat()
         self.__messagesTopicFormat = self.messagesEdit.currentCharFormat()
         self.__messagesTopicFormat.setFontWeight(QFont.Weight.Bold)
@@ -140,6 +143,11 @@
         self.clearWillButton.setIcon(
             UI.PixmapCache.getIcon("certificateDelete"))
         
+        self.subscribeTopicComboBox.lineEdit().setClearButtonEnabled(True)
+        self.subscribeTopicComboBox.lineEdit().returnPressed.connect(
+            self.on_subscribeButton_clicked)
+        self.__populateSubscribeTopicComboBox()
+        
         self.subscribeButton.setIcon(UI.PixmapCache.getIcon("plus"))
         self.subscribeButton.setEnabled(False)
         self.subscribePropertiesButton.setIcon(
@@ -483,12 +491,10 @@
         @param mid ID of the subscribe request
         @type int
         """
-        # TODO: remember the successfully subscribed topic
-        # TODO: max. number of recent topics as a config item
         if mid in self.__topicQueue:
             topic = self.__topicQueue.pop(mid)
             self.__subscribedTopics.append(topic)
-            self.subscribeTopicEdit.clear()
+            self.__addTopicToRecent(topic)
             
             self.__updateUnsubscribeTopicComboBox()
             self.__updatePublishTopicComboBox()
@@ -653,7 +659,7 @@
         """
         Private slot to edit the subscribe user properties.
         """
-        topic = self.subscribeTopicEdit.text()
+        topic = self.subscribeTopicComboBox.currentText()
         self.__editProperties(
             "subscribe",
             self.tr("SUBSCRIBE: User Properties for '{0}'").format(topic),
@@ -661,7 +667,7 @@
         )
     
     @pyqtSlot(str)
-    def on_subscribeTopicEdit_textChanged(self, topic):
+    def on_subscribeTopicComboBox_editTextChanged(self, topic):
         """
         Private slot to handle a change of the entered topic.
         
@@ -672,19 +678,11 @@
         self.subscribePropertiesButton.setEnabled(bool(topic))
     
     @pyqtSlot()
-    def on_subscribeTopicEdit_returnPressed(self):
-        """
-        Private slot handling the user pressing the return button to subscribe
-        a topic.
-        """
-        self.on_subscribeButton_clicked()
-    
-    @pyqtSlot()
     def on_subscribeButton_clicked(self):
         """
         Private slot to subscribe to the entered topic.
         """
-        topic = self.subscribeTopicEdit.text()
+        topic = self.subscribeTopicComboBox.currentText()
         qos = self.subscribeQosSpinBox.value()
         if topic:
             if topic.startswith(MqttMonitorWidget.BrokerStatusTopicPrefix):
@@ -1034,9 +1032,9 @@
         if hostAndPort in brokerList:
             brokerList.remove(hostAndPort)
         brokerList.insert(0, hostAndPort)
-        # limit to most recently used 20 entries
-        # TODO: make the amount of recent brokers a config item
-        brokerList = brokerList[:20]
+        # limit the most recently used entries
+        maxBrokers = self.__plugin.getPreferences("RecentBrokersNumber")
+        brokerList = brokerList[:maxBrokers]
         self.__plugin.setPreferences("RecentBrokersWithPort", brokerList)
         
         self.__populateBrokerComboBoxes()
@@ -1087,6 +1085,35 @@
         
         self.__setConnectButtonState()
     
+    def __addTopicToRecent(self, topic):
+        """
+        Private method to add a topic to the list of recently subscribed
+        topics.
+        
+        @param topic subscribed topic
+        @type str
+        """
+        topicsList = self.__plugin.getPreferences("RecentTopics")
+        if topic in topicsList:
+            topicsList.remove(topic)
+        topicsList.insert(0, topic)
+        # limit the most recently used entries
+        maxTopics = self.__plugin.getPreferences("RecentTopicsNumber")
+        topicsList = topicsList[:maxTopics]
+        self.__plugin.setPreferences("RecentTopics", topicsList)
+        
+        self.__populateSubscribeTopicComboBox()
+    
+    def __populateSubscribeTopicComboBox(self):
+        """
+        Private method to populate the subscribe topic combo box.
+        """
+        topicsList = self.__plugin.getPreferences("RecentTopics")
+        
+        self.subscribeTopicComboBox.clear()
+        self.subscribeTopicComboBox.addItems(sorted(topicsList))
+        self.subscribeTopicComboBox.clearEditText()
+    
     def __updateUnsubscribeTopicComboBox(self):
         """
         Private method to update the unsubcribe topic combo box.
--- a/MqttMonitor/MqttMonitorWidget.ui	Fri Jul 23 19:48:14 2021 +0200
+++ b/MqttMonitor/MqttMonitorWidget.ui	Sat Jul 24 16:12:05 2021 +0200
@@ -188,11 +188,17 @@
            </widget>
           </item>
           <item>
-           <widget class="QLineEdit" name="subscribeTopicEdit">
+           <widget class="QComboBox" name="subscribeTopicComboBox">
+            <property name="sizePolicy">
+             <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+              <horstretch>0</horstretch>
+              <verstretch>0</verstretch>
+             </sizepolicy>
+            </property>
             <property name="toolTip">
              <string>Enter the topic</string>
             </property>
-            <property name="clearButtonEnabled">
+            <property name="editable">
              <bool>true</bool>
             </property>
            </widget>
@@ -636,9 +642,9 @@
           <property name="geometry">
            <rect>
             <x>0</x>
-            <y>0</y>
-            <width>339</width>
-            <height>670</height>
+            <y>-358</y>
+            <width>344</width>
+            <height>840</height>
            </rect>
           </property>
           <layout class="QFormLayout" name="formLayout">
@@ -1150,7 +1156,7 @@
              <item>
               <widget class="QLabel" name="label_29">
                <property name="text">
-                <string>&lt;b&gt;Load&lt;/b&gt;</string>
+                <string>&lt;b&gt;Load&lt;/b&gt; (1 min / 5 min / 15 min)</string>
                </property>
               </widget>
              </item>
@@ -1492,7 +1498,7 @@
   <tabstop>connectButton</tabstop>
   <tabstop>clearWillButton</tabstop>
   <tabstop>brokerWidget</tabstop>
-  <tabstop>subscribeTopicEdit</tabstop>
+  <tabstop>subscribeTopicComboBox</tabstop>
   <tabstop>subscribeQosSpinBox</tabstop>
   <tabstop>subscribeButton</tabstop>
   <tabstop>subscribePropertiesButton</tabstop>
--- a/PluginMqttMonitor.py	Fri Jul 23 19:48:14 2021 +0200
+++ b/PluginMqttMonitor.py	Sat Jul 24 16:12:05 2021 +0200
@@ -143,6 +143,7 @@
             "DefaultProtocol": MqttProtocols.MQTTv311,
             "RecentBrokersNumber": 20,
             "RecentTopicsNumber": 20,
+            "RecentTopics": "[]",               # JSON formatted empty list
         }
         
         self.__translator = None
@@ -165,7 +166,6 @@
         error = ""     # clear previous error
         mqttPluginObject = self
         
-        
         try:
             import paho.mqtt        # __IGNORE_WARNING__
         except ImportError:
@@ -264,7 +264,7 @@
         """
         if key in ("RecentBrokersWithPort", "BrokerProfiles",
                    "SubscribeProperties", "UnsubscribeProperties",
-                   "PublishProperties"):
+                   "PublishProperties", "RecentTopics"):
             return json.loads(Preferences.Prefs.settings.value(
                 self.PreferencesKey + "/" + key, self.__defaults[key]))
         elif key in ("DefaultProtocol", ):
@@ -289,7 +289,7 @@
         """
         if key in ("RecentBrokersWithPort", "BrokerProfiles",
                    "SubscribeProperties", "UnsubscribeProperties",
-                   "PublishProperties"):
+                   "PublishProperties", "RecentTopics"):
             Preferences.Prefs.settings.setValue(
                 self.PreferencesKey + "/" + key, json.dumps(value))
         elif key in ("DefaultProtocol", ):

eric ide

mercurial