MqttMonitor/MqttMonitorWidget.py

branch
eric7
changeset 106
84498095d511
parent 105
36ec7431ad04
child 112
b287c1d2cc78
--- 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.

eric ide

mercurial