MqttMonitor/MqttConnectionProfilesDialog.py

branch
connection_profiles
changeset 19
889a7c3c0e63
parent 18
bbfe5866b6aa
child 23
0b23bd856e43
equal deleted inserted replaced
18:bbfe5866b6aa 19:889a7c3c0e63
9 9
10 from __future__ import unicode_literals 10 from __future__ import unicode_literals
11 11
12 import collections 12 import collections
13 13
14 from PyQt5.QtCore import pyqtSlot 14 from PyQt5.QtCore import pyqtSlot, Qt, QUuid
15 from PyQt5.QtWidgets import QDialog, QAbstractButton, QListWidgetItem 15 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton, \
16 QListWidgetItem, QInputDialog, QLineEdit
17
18 from E5Gui import E5MessageBox
16 19
17 from .Ui_MqttConnectionProfilesDialog import Ui_MqttConnectionProfilesDialog 20 from .Ui_MqttConnectionProfilesDialog import Ui_MqttConnectionProfilesDialog
21
22 import UI.PixmapCache
18 23
19 24
20 class MqttConnectionProfilesDialog(QDialog, Ui_MqttConnectionProfilesDialog): 25 class MqttConnectionProfilesDialog(QDialog, Ui_MqttConnectionProfilesDialog):
21 """ 26 """
22 Class implementing a dialog to edit the MQTT connection profiles. 27 Class implementing a dialog to edit the MQTT connection profiles.
37 @type QWidget 42 @type QWidget
38 """ 43 """
39 super(MqttConnectionProfilesDialog, self).__init__(parent) 44 super(MqttConnectionProfilesDialog, self).__init__(parent)
40 self.setupUi(self) 45 self.setupUi(self)
41 46
47 self.__client = client
48
42 self.__profiles = collections.defaultdict(self.__defaultProfile) 49 self.__profiles = collections.defaultdict(self.__defaultProfile)
43 self.__profiles.update(profiles) 50 self.__profiles.update(profiles)
51
52 self.plusButton.setIcon(UI.PixmapCache.getIcon("plus.png"))
53 self.minusButton.setIcon(UI.PixmapCache.getIcon("minus.png"))
54
55 self.__populateProfilesList()
56
57 if len(self.__profiles) == 0:
58 self.minusButton.setEnabled(False)
59
60 self.__updateApplyButton()
61
62 self.profileTabWidget.setCurrentIndex(0)
44 63
45 @pyqtSlot(str) 64 @pyqtSlot(str)
46 def on_profileEdit_textChanged(self, p0): 65 def on_profileEdit_textChanged(self, name):
47 """ 66 """
48 Slot documentation goes here. 67 Private slot to handle changes of the profile name.
49 68
50 @param p0 DESCRIPTION 69 @param name name of the profile
51 @type str 70 @type str
52 """ 71 """
53 # TODO: not implemented yet 72 self.__updateApplyButton()
54 raise NotImplementedError
55 73
56 @pyqtSlot(QAbstractButton) 74 @pyqtSlot(QAbstractButton)
57 def on_profileButtonBox_clicked(self, button): 75 def on_profileButtonBox_clicked(self, button):
58 """ 76 """
59 Slot documentation goes here. 77 Private slot handling presses of the profile buttons.
60 78
61 @param button DESCRIPTION 79 @param button reference to the pressed button
62 @type QAbstractButton 80 @type QAbstractButton
63 """ 81 """
64 # TODO: not implemented yet 82 if button == self.profileButtonBox.button(QDialogButtonBox.Apply):
65 raise NotImplementedError 83 currentProfile = self.__applyProfile()
84 self.__populateProfilesList(currentProfile)
85
86 # TODO: not implemented other paths
66 87
67 @pyqtSlot(QListWidgetItem, QListWidgetItem) 88 @pyqtSlot(QListWidgetItem, QListWidgetItem)
68 def on_profilesList_currentItemChanged(self, current, previous): 89 def on_profilesList_currentItemChanged(self, current, previous):
69 """ 90 """
70 Slot documentation goes here. 91 Private slot to handle a change of the current profile.
71 92
72 @param current DESCRIPTION 93 @param current new current item
73 @type QListWidgetItem 94 @type QListWidgetItem
74 @param previous DESCRIPTION 95 @param previous previous current item
75 @type QListWidgetItem 96 @type QListWidgetItem
76 """ 97 """
77 # TODO: not implemented yet 98 self.minusButton.setEnabled(current is not None)
78 raise NotImplementedError 99 if current:
100 profileName = current.text()
101 self.__populateProfile(profileName)
79 102
80 @pyqtSlot() 103 @pyqtSlot()
81 def on_plusButton_clicked(self): 104 def on_plusButton_clicked(self):
82 """ 105 """
83 Slot documentation goes here. 106 Private slot to add a new empty profile entry.
84 """ 107 """
85 # TODO: not implemented yet 108 profileName, ok = QInputDialog.getText(
86 raise NotImplementedError 109 self,
110 self.tr("New Connection Profile"),
111 self.tr("Enter name for the new Connection Profile:"),
112 QLineEdit.Normal)
113 if ok and bool(profileName) and profileName not in self.__profiles:
114 itm = QListWidgetItem(profileName, self.profilesList)
115 self.profilesList.setCurrentItem(itm)
116 self.brokerAddressEdit.setFocus(Qt.OtherFocusReason)
87 117
88 @pyqtSlot() 118 @pyqtSlot()
89 def on_minusButton_clicked(self): 119 def on_minusButton_clicked(self):
90 """ 120 """
91 Slot documentation goes here. 121 Private slot to delete the selected entry.
92 """ 122 """
93 # TODO: not implemented yet 123 itm = self.profilesList.currentItem()
94 raise NotImplementedError 124 if itm:
125 profileName = itm.text()
126 yes = E5MessageBox.yesNo(
127 self,
128 self.tr("Delete Connection Profile"),
129 self.tr("""<p>Shall the Connection Profile <b>{0}</b>"""
130 """ really be deleted?</p>""").format(profileName)
131 )
132 if yes:
133 del self.__profiles[profileName]
134 self.__populateProfilesList()
95 135
96 def getProfiles(self): 136 def getProfiles(self):
97 """ 137 """
98 Public method to return a dictionary of profiles. 138 Public method to return a dictionary of profiles.
99 139
100 @return dictionary containing the defined connection profiles 140 @return dictionary containing dictionaries containing the defined
141 connection profiles. Each entry have the keys "BrokerAddress",
142 "BrokerPort", "ClientId", "Keepalive", "CleanSession", "Username",
143 "Password", "WillTopic", "WillMessage", "WillQos", "WillRetain".
101 @rtype dict 144 @rtype dict
102 """ 145 """
103 return {} 146 profilesDict = {}
147 profilesDict.update(self.__profiles)
148 return profilesDict
149
150 def __applyProfile(self):
151 """
152 Private method to apply the entered data to the list of profiles.
153
154 @return name of the applied profile
155 @rtype str
156 """
157 profileName = self.profileEdit.text()
158 profile = {
159 "BrokerAddress": self.brokerAddressEdit.text(),
160 "BrokerPort": self.brokerPortSpinBox.value(),
161 "ClientId": self.clientIdEdit.text(),
162 "Keepalive": self.keepaliveSpinBox.value(),
163 "CleanSession": self.cleanSessionCheckBox.isChecked(),
164 "Username": self.usernameEdit.text(),
165 "Password": self.passwordEdit.text(),
166 "WillTopic": self.willTopicEdit.text(),
167 "WillMessage": self.willMessageEdit.toPlainText(),
168 "WillQos": self.willQosSpinBox.value(),
169 "WillRetain": self.willRetainCheckBox.isChecked(),
170 }
171 self.__profiles[profileName] = profile
172
173 return profileName
104 174
105 def __defaultProfile(self): 175 def __defaultProfile(self):
106 """ 176 """
107 Private method to populate non-existing profile items. 177 Private method to populate non-existing profile items.
108 178
112 defaultProfile = self.__client.defaultConnectionOptions() 182 defaultProfile = self.__client.defaultConnectionOptions()
113 defaultProfile["BrokerAddress"] = "" 183 defaultProfile["BrokerAddress"] = ""
114 defaultProfile["BrokerPort"] = 1883 184 defaultProfile["BrokerPort"] = 1883
115 185
116 return defaultProfile 186 return defaultProfile
187
188 def __populateProfilesList(self, currentProfile=""):
189 """
190 Private method to populate the list of defined profiles.
191
192 @param currentProfile name of the current profile
193 @type str
194 """
195 if not currentProfile:
196 currentItem = self.profilesList.currentItem()
197 if currentItem:
198 currentProfile = currentItem.text()
199
200 self.profilesList.clear()
201 self.profilesList.addItems(sorted(self.__profiles.keys()))
202
203 if currentProfile:
204 items = self.profilesList.findItems(
205 currentProfile, Qt.MatchExactly)
206 if items:
207 self.profilesList.setCurrentItem(items[0])
208
209 def __populateProfile(self, profileName):
210 """
211 Private method to populate the profile data entry fields.
212
213 @param profileName name of the profile to get data from
214 @type str
215 """
216 if profileName:
217 profile = self.__profiles[profileName]
218 else:
219 profile = self.__defaultProfile()
220
221 self.profileEdit.setText(profileName)
222 self.brokerAddressEdit.setText(profile["BrokerAddress"])
223 self.brokerPortSpinBox.setValue(profile["BrokerPort"])
224 self.clientIdEdit.setText(profile["ClientId"])
225 self.keepaliveSpinBox.setValue(profile["Keepalive"])
226 self.cleanSessionCheckBox.setChecked(profile["CleanSession"])
227 self.usernameEdit.setText(profile["Username"])
228 self.passwordEdit.setText(profile["Password"])
229 self.willTopicEdit.setText(profile["WillTopic"])
230 self.willMessageEdit.setPlainText(profile["WillMessage"])
231 self.willQosSpinBox.setValue(profile["WillQos"])
232 self.willRetainCheckBox.setChecked(profile["WillRetain"])
233
234 self.__updateApplyButton()
235
236 def __updateApplyButton(self):
237 """
238 Private method to set the state of the Apply button.
239 """
240 enable = (bool(self.profileEdit.text()) and
241 bool(self.brokerAddressEdit.text()))
242 self.profileButtonBox.button(QDialogButtonBox.Apply).setEnabled(enable)
243
244 @pyqtSlot(str)
245 def on_brokerAddressEdit_textChanged(self, address):
246 """
247 Private slot handling a change of the broker address.
248
249 @param address broker address
250 @type str
251 """
252 self.__updateApplyButton()
253
254 @pyqtSlot()
255 def on_generateIdButton_clicked(self):
256 """
257 Private slot to generate a client ID.
258 """
259 uuid = QUuid.createUuid()
260 self.clientIdEdit.setText(uuid.toString(QUuid.WithoutBraces))

eric ide

mercurial