MqttMonitor/MqttMonitorWidget.py

branch
connection_profiles
changeset 18
bbfe5866b6aa
parent 11
90d3ebed4cc0
child 19
889a7c3c0e63
diff -r ee738a0efe9c -r bbfe5866b6aa MqttMonitor/MqttMonitorWidget.py
--- a/MqttMonitor/MqttMonitorWidget.py	Mon Sep 03 19:57:59 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.py	Tue Sep 04 19:42:24 2018 +0200
@@ -61,6 +61,10 @@
         
         self.brokerWidget.setCurrentIndex(0)
         
+        self.__connectionModeProfile = True
+        self.__setConnectionMode(True)  # initial mode is 'profile connection'
+        self.__populateProfileComboBox()
+        
         self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
         self.brokerConnectionOptionsButton.setIcon(UI.PixmapCache.getIcon(
             os.path.join("MqttMonitor", "icons", "connectionOptions.png")))
@@ -283,6 +287,14 @@
         self.brokerStatusLabel.show()
         QTimer.singleShot(5000, self.brokerStatusLabel.hide)
     
+    @pyqtSlot()
+    def on_modeButton_clicked(self):
+        """
+        Private slot to switch between connection profiles and direct
+        connection mode.
+        """
+        self.__setConnectionMode(not self.__connectionModeProfile)
+    
     @pyqtSlot(str)
     def on_brokerComboBox_editTextChanged(self, host):
         """
@@ -299,13 +311,27 @@
     @pyqtSlot()
     def on_brokerConnectionOptionsButton_clicked(self):
         """
-        Private slot to show a dialog to modify connection options.
+        Private slot to show a dialog to modify connection options or a
+        dialog to edit connection profiles.
         """
-        from .MqttConnectionOptionsDialog import MqttConnectionOptionsDialog
-        dlg = MqttConnectionOptionsDialog(
-            self.__client, self.__connectionOptions, parent=self)
-        if dlg.exec_() == QDialog.Accepted:
-            self.__connectionOptions = dlg.getConnectionOptions()
+        if self.__connectionModeProfile:
+            # TODO: implement this path
+            from .MqttConnectionProfilesDialog import \
+                MqttConnectionProfilesDialog
+            dlg = MqttConnectionProfilesDialog(
+                self.__client, self.__plugin.getPreferences("BrokerProfiles"),
+                parent=self)
+            if dlg.exec_() == QDialog.Accepted:
+                profiles = dlg.getProfiles()
+                self.__plugin.setPreferences("BrokerProfiles", profiles)
+                self.__populateProfileComboBox()
+        else:
+            from .MqttConnectionOptionsDialog import \
+                MqttConnectionOptionsDialog
+            dlg = MqttConnectionOptionsDialog(
+                self.__client, self.__connectionOptions, parent=self)
+            if dlg.exec_() == QDialog.Accepted:
+                self.__connectionOptions = dlg.getConnectionOptions()
     
     @pyqtSlot()
     def on_connectButton_clicked(self):
@@ -315,20 +341,11 @@
         if self.__connectedToBroker:
             self.__client.disconnectFromServer()
         else:
-            host = self.brokerComboBox.currentText()
-            port = self.brokerPortComboBox.currentText().strip()
-            try:
-                port = int(port)
-            except ValueError:
-                # use standard port at 1883
-                port = 1883
-            if host:
-                self.__addBrokerToRecent(host, port)
-                if self.__connectionOptions is None:
-                    self.__client.connectToServer(host, port=port)
-                else:
-                    self.__client.connectToServerWithOptions(
-                        host, port=port, options=self.__connectionOptions)
+            if self.__connectionModeProfile:
+                # TODO: implement this path
+                pass
+            else:
+                self.__directConnectToBroker()
     
     @pyqtSlot(str)
     def on_subscribeTopicEdit_textChanged(self, topic):
@@ -505,6 +522,15 @@
         index = self.brokerPortComboBox.findText(currentPortStr)
         self.brokerPortComboBox.setCurrentIndex(index)
     
+    def __populateProfileComboBox(self):
+        """
+        Private method to populate the profiles selection box.
+        """
+        profilesDict = self.__plugin.getPreferences("BrokerProfiles")
+        
+        self.profileComboBox.clear()
+        self.profileComboBox.addItems(sorted(profilesDict.keys()))
+    
     def __updateUnsubscribeTopicComboBox(self):
         """
         Private method to update the unsubcribe topic combo box.
@@ -611,3 +637,50 @@
             "5min": "-",
             "15min": "-",
         }
+    
+    def __setConnectionMode(self, profileMode):
+        """
+        Private method to set the connection mode.
+        
+        @param profileMode flag indicating the profile connection mode
+        @type bool
+        """
+        self.__connectionModeProfile = profileMode
+        if profileMode:
+            self.modeButton.setIcon(UI.PixmapCache.getIcon(
+                os.path.join("MqttMonitor", "icons", "profiles.png")))
+        else:
+            self.modeButton.setIcon(UI.PixmapCache.getIcon(
+                os.path.join("MqttMonitor", "icons", "quickopen.png")))
+        
+        self.profileComboBox.setVisible(profileMode)
+        self.brokerComboBox.setVisible(not profileMode)
+        self.brokerPortComboBox.setVisible(not profileMode)
+    
+    def __directConnectToBroker(self):
+        """
+        Private method to connect to the broker with entered data.
+        """
+        host = self.brokerComboBox.currentText()
+        port = self.brokerPortComboBox.currentText().strip()
+        try:
+            port = int(port)
+        except ValueError:
+            # use standard port at 1883
+            port = 1883
+        if host:
+            self.__addBrokerToRecent(host, port)
+            if self.__connectionOptions is None:
+                self.__client.connectToServer(host, port=port)
+            else:
+                self.__client.connectToServerWithOptions(
+                    host, port=port, options=self.__connectionOptions)
+    
+    def __profileConnectToBroker(self):
+        """
+        Private method to connect to the broker with selected profile.
+        """
+        profileName = self.profileComboBox.currentText()
+        if profileName:
+            # TODO: implement connect by profile name
+            pass

eric ide

mercurial