--- a/MqttMonitor/MqttMonitorWidget.py Tue Sep 04 19:42:24 2018 +0200 +++ b/MqttMonitor/MqttMonitorWidget.py Wed Sep 05 19:52:30 2018 +0200 @@ -16,6 +16,7 @@ import os import collections +import copy from PyQt5.QtCore import pyqtSlot, QTimer from PyQt5.QtGui import QTextCursor @@ -303,10 +304,7 @@ @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): @@ -315,15 +313,14 @@ dialog to edit connection profiles. """ 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) + profilesDict = dlg.getProfiles() + self.__plugin.setPreferences("BrokerProfiles", profilesDict) self.__populateProfileComboBox() else: from .MqttConnectionOptionsDialog import \ @@ -342,8 +339,7 @@ self.__client.disconnectFromServer() else: if self.__connectionModeProfile: - # TODO: implement this path - pass + self.__profileConnectToBroker else: self.__directConnectToBroker() @@ -509,6 +505,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] @@ -530,6 +528,8 @@ self.profileComboBox.clear() self.profileComboBox.addItems(sorted(profilesDict.keys())) + + self.__setConnectButtonState() def __updateUnsubscribeTopicComboBox(self): """ @@ -654,8 +654,19 @@ os.path.join("MqttMonitor", "icons", "quickopen.png"))) self.profileComboBox.setVisible(profileMode) - self.brokerComboBox.setVisible(not profileMode) - self.brokerPortComboBox.setVisible(not 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): """ @@ -682,5 +693,10 @@ """ profileName = self.profileComboBox.currentText() if profileName: - # TODO: implement connect by profile name - pass + 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)