diff -r aeb276d76ec7 -r 0f02baed8308 MqttMonitor/MqttMonitorWidget.py --- a/MqttMonitor/MqttMonitorWidget.py Sat Sep 08 16:51:39 2018 +0200 +++ b/MqttMonitor/MqttMonitorWidget.py Sat Sep 08 16:55:42 2018 +0200 @@ -16,6 +16,7 @@ import os import collections +import copy from PyQt5.QtCore import pyqtSlot, QTimer from PyQt5.QtGui import QTextCursor @@ -61,6 +62,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 +288,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): """ @@ -291,21 +304,41 @@ @param host host name of the broker @type str """ - if not self.__connectedToBroker and not host: - self.connectButton.setEnabled(False) - else: - self.connectButton.setEnabled(True) + self.__setConnectButtonState() @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: + from .MqttConnectionProfilesDialog import \ + MqttConnectionProfilesDialog + dlg = MqttConnectionProfilesDialog( + self.__client, self.__plugin.getPreferences("BrokerProfiles"), + parent=self) + if dlg.exec_() == QDialog.Accepted: + profilesDict = dlg.getProfiles() + self.__plugin.setPreferences("BrokerProfiles", profilesDict) + self.__populateProfileComboBox() + else: + from .MqttConnectionOptionsDialog import \ + MqttConnectionOptionsDialog + dlg = MqttConnectionOptionsDialog( + self.__client, self.__connectionOptions, parent=self) + if dlg.exec_() == QDialog.Accepted: + self.__connectionOptions = dlg.getConnectionOptions() + if self.__connectionOptions["TlsEnable"]: + port = self.brokerPortComboBox.currentText().strip() + if port == "1883": + # it is default non-encrypted port => set to TLS port + self.brokerPortComboBox.setEditText("8883") + else: + port = self.brokerPortComboBox.currentText().strip() + if port == "8883": + # it is default TLS port => set to non-encrypted port + self.brokerPortComboBox.setEditText("1883") @pyqtSlot() def on_connectButton_clicked(self): @@ -315,20 +348,10 @@ 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: + self.__profileConnectToBroker() + else: + self.__directConnectToBroker() @pyqtSlot(str) def on_subscribeTopicEdit_textChanged(self, topic): @@ -502,6 +525,8 @@ # step 2a: populate the broker name list self.brokerComboBox.addItems([b[0].strip() for b in brokerList]) + self.__setConnectButtonState() + # step 2b: populate the broker ports list if brokerList: currentPort = brokerList[0][1] @@ -515,6 +540,17 @@ 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())) + + self.__setConnectButtonState() + def __updateUnsubscribeTopicComboBox(self): """ Private method to update the unsubcribe topic combo box. @@ -630,3 +666,66 @@ "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.brokerConnectionWidget.setVisible(not profileMode) + self.__setConnectButtonState() + + def __setConnectButtonState(self): + """ + Private method to set the enabled state of the connect button. + """ + if self.__connectionModeProfile: + self.connectButton.setEnabled( + bool(self.profileComboBox.currentText())) + else: + self.connectButton.setEnabled( + bool(self.brokerComboBox.currentText())) + + 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: + profilesDict = self.__plugin.getPreferences("BrokerProfiles") + profile = copy.copy(profilesDict[profileName]) # play it save + host = profile["BrokerAddress"] + port = profile["BrokerPort"] + + self.__client.connectToServerWithOptions(host, port=port, + options=profile)