MqttConnectionProfilesDialog: continued implementing the connections profile dialog. connection_profiles

Tue, 04 Sep 2018 19:42:24 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Tue, 04 Sep 2018 19:42:24 +0200
branch
connection_profiles
changeset 18
bbfe5866b6aa
parent 17
ee738a0efe9c
child 19
889a7c3c0e63

MqttConnectionProfilesDialog: continued implementing the connections profile dialog.

MqttMonitor/MqttConnectionOptionsDialog.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttConnectionProfilesDialog.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.py file | annotate | diff | comparison | revisions
MqttMonitor/MqttMonitorWidget.ui file | annotate | diff | comparison | revisions
MqttMonitor/icons/profiles.png file | annotate | diff | comparison | revisions
MqttMonitor/icons/quickopen.png file | annotate | diff | comparison | revisions
PluginMqttMonitor.py file | annotate | diff | comparison | revisions
--- a/MqttMonitor/MqttConnectionOptionsDialog.py	Mon Sep 03 19:57:59 2018 +0200
+++ b/MqttMonitor/MqttConnectionOptionsDialog.py	Tue Sep 04 19:42:24 2018 +0200
@@ -29,7 +29,7 @@
             populate the dialog with. It must have the keys "ClientId",
             "Keepalive", "CleanSession", "Username", "Password", "WillTopic",
             "WillMessage", "WillQos", "WillRetain".
-        @@type dict
+        @type dict
         @param parent reference to the parent widget
         @type QWidget
         """
--- a/MqttMonitor/MqttConnectionProfilesDialog.py	Mon Sep 03 19:57:59 2018 +0200
+++ b/MqttMonitor/MqttConnectionProfilesDialog.py	Tue Sep 04 19:42:24 2018 +0200
@@ -9,6 +9,8 @@
 
 from __future__ import unicode_literals
 
+import collections
+
 from PyQt5.QtCore import pyqtSlot
 from PyQt5.QtWidgets import QDialog, QAbstractButton, QListWidgetItem
 
@@ -19,15 +21,26 @@
     """
     Class implementing a dialog to edit the MQTT connection profiles.
     """
-    def __init__(self, parent=None):
+    def __init__(self, client, profiles, parent=None):
         """
         Constructor
         
+        @param client reference to the MQTT client object
+        @type MqttClient
+        @param profiles dictionary containing dictionaries containing the
+            connection parameters. Each entry must have the keys
+            "BrokerAddress", "BrokerPort", "ClientId",
+            "Keepalive", "CleanSession", "Username", "Password", "WillTopic",
+            "WillMessage", "WillQos", "WillRetain".
+        @type dict
         @param parent reference to the parent widget
         @type QWidget
         """
         super(MqttConnectionProfilesDialog, self).__init__(parent)
         self.setupUi(self)
+        
+        self.__profiles = collections.defaultdict(self.__defaultProfile)
+        self.__profiles.update(profiles)
     
     @pyqtSlot(str)
     def on_profileEdit_textChanged(self, p0):
@@ -79,3 +92,25 @@
         """
         # TODO: not implemented yet
         raise NotImplementedError
+    
+    def getProfiles(self):
+        """
+        Public method to return a dictionary of profiles.
+        
+        @return dictionary containing the defined connection profiles
+        @rtype dict
+        """
+        return {}
+    
+    def __defaultProfile(self):
+        """
+        Private method to populate non-existing profile items.
+        
+        @return default dictionary entry
+        @rtype dict
+        """
+        defaultProfile = self.__client.defaultConnectionOptions()
+        defaultProfile["BrokerAddress"] = ""
+        defaultProfile["BrokerPort"] = 1883
+        
+        return defaultProfile
--- 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
--- a/MqttMonitor/MqttMonitorWidget.ui	Mon Sep 03 19:57:59 2018 +0200
+++ b/MqttMonitor/MqttMonitorWidget.ui	Tue Sep 04 19:42:24 2018 +0200
@@ -35,12 +35,26 @@
     </layout>
    </item>
    <item>
-    <widget class="QGroupBox" name="groupBox">
+    <widget class="QGroupBox" name="brokerGroupBox">
      <property name="title">
       <string>Broker</string>
      </property>
      <layout class="QGridLayout" name="gridLayout">
       <item row="0" column="0">
+       <widget class="QToolButton" name="modeButton">
+        <property name="toolTip">
+         <string>Press to switch the mode between profiles and direct connection</string>
+        </property>
+       </widget>
+      </item>
+      <item row="0" column="1">
+       <widget class="QComboBox" name="profileComboBox">
+        <property name="toolTip">
+         <string>Select the profile to be used to connect to the broker</string>
+        </property>
+       </widget>
+      </item>
+      <item row="0" column="2">
        <widget class="E5ClearableComboBox" name="brokerComboBox">
         <property name="sizePolicy">
          <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
@@ -57,23 +71,6 @@
        </widget>
       </item>
       <item row="0" column="3">
-       <widget class="QToolButton" name="connectButton">
-        <property name="toolTip">
-         <string>Press to connect to/disconnect from the broker</string>
-        </property>
-        <property name="text">
-         <string/>
-        </property>
-       </widget>
-      </item>
-      <item row="1" column="0" colspan="4">
-       <widget class="QLabel" name="brokerStatusLabel">
-        <property name="wordWrap">
-         <bool>true</bool>
-        </property>
-       </widget>
-      </item>
-      <item row="0" column="1">
        <widget class="QComboBox" name="brokerPortComboBox">
         <property name="toolTip">
          <string>Enter the broker port to connect to</string>
@@ -86,13 +83,30 @@
         </property>
        </widget>
       </item>
-      <item row="0" column="2">
+      <item row="0" column="4">
        <widget class="QToolButton" name="brokerConnectionOptionsButton">
         <property name="toolTip">
          <string>Press to open a dialog to enter connection options</string>
         </property>
        </widget>
       </item>
+      <item row="0" column="5">
+       <widget class="QToolButton" name="connectButton">
+        <property name="toolTip">
+         <string>Press to connect to/disconnect from the broker</string>
+        </property>
+        <property name="text">
+         <string/>
+        </property>
+       </widget>
+      </item>
+      <item row="1" column="0" colspan="6">
+       <widget class="QLabel" name="brokerStatusLabel">
+        <property name="wordWrap">
+         <bool>true</bool>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
@@ -421,8 +435,8 @@
            <rect>
             <x>0</x>
             <y>0</y>
-            <width>344</width>
-            <height>840</height>
+            <width>339</width>
+            <height>670</height>
            </rect>
           </property>
           <layout class="QFormLayout" name="formLayout">
@@ -1156,6 +1170,8 @@
   </customwidget>
  </customwidgets>
  <tabstops>
+  <tabstop>modeButton</tabstop>
+  <tabstop>profileComboBox</tabstop>
   <tabstop>brokerComboBox</tabstop>
   <tabstop>brokerPortComboBox</tabstop>
   <tabstop>brokerConnectionOptionsButton</tabstop>
@@ -1167,9 +1183,10 @@
   <tabstop>unsubscribeTopicComboBox</tabstop>
   <tabstop>unsubscribeButton</tabstop>
   <tabstop>publishTopicComboBox</tabstop>
+  <tabstop>publishPayloadEdit</tabstop>
   <tabstop>publishQosSpinBox</tabstop>
   <tabstop>publishRetainCheckBox</tabstop>
-  <tabstop>publishPayloadEdit</tabstop>
+  <tabstop>publishClearButton</tabstop>
   <tabstop>publishButton</tabstop>
   <tabstop>messagesEdit</tabstop>
   <tabstop>pushButton</tabstop>
Binary file MqttMonitor/icons/profiles.png has changed
Binary file MqttMonitor/icons/quickopen.png has changed
--- a/PluginMqttMonitor.py	Mon Sep 03 19:57:59 2018 +0200
+++ b/PluginMqttMonitor.py	Tue Sep 04 19:42:24 2018 +0200
@@ -94,6 +94,7 @@
         
         self.__defaults = {
             "RecentBrokersWithPort": "[]",      # JSON formatted empty list
+            "BrokerProfiles": "{}",             # JSON formatted empty dict
         }
         
         self.__translator = None
@@ -209,7 +210,7 @@
         @param key the key of the value to get
         @return the requested setting
         """
-        if key in ["RecentBrokersWithPort"]:
+        if key in ["RecentBrokersWithPort", "BrokerProfiles"]:
             return json.loads(Preferences.Prefs.settings.value(
                 self.PreferencesKey + "/" + key, self.__defaults[key]))
         else:
@@ -223,7 +224,7 @@
         @param key the key of the setting to be set (string)
         @param value the value to be set
         """
-        if key in ["RecentBrokersWithPort"]:
+        if key in ["RecentBrokersWithPort", "BrokerProfiles"]:
             Preferences.Prefs.settings.setValue(
                 self.PreferencesKey + "/" + key, json.dumps(value))
         else:

eric ide

mercurial