MqttMonitor/MqttConnectionOptionsDialog.py

changeset 10
7e0e921dc7ea
child 11
90d3ebed4cc0
equal deleted inserted replaced
9:f75a385e9127 10:7e0e921dc7ea
1 # -*- coding: utf-8 -*-
2
3 """
4 Module implementing a dialog to enter MQTT connection options.
5 """
6
7 from __future__ import unicode_literals
8
9 from PyQt5.QtCore import pyqtSlot
10 from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton
11
12 from .Ui_MqttConnectionOptionsDialog import Ui_MqttConnectionOptionsDialog
13
14
15 class MqttConnectionOptionsDialog(QDialog, Ui_MqttConnectionOptionsDialog):
16 """
17 Class implementing a dialog to enter MQTT connection options.
18 """
19 def __init__(self, client, options=None, parent=None):
20 """
21 Constructor
22
23 @param client reference to the MQTT client object
24 @type MqttClient
25 @param options dictionary containing the connection options to
26 populate the dialog with
27 @@type dict
28 @param parent reference to the parent widget
29 @type QWidget
30 """
31 super(MqttConnectionOptionsDialog, self).__init__(parent)
32 self.setupUi(self)
33
34 self.__client = client
35
36 self.__populateDefaults(options=options)
37
38
39 @pyqtSlot(QAbstractButton)
40 def on_buttonBox_clicked(self, button):
41 """
42 Private slot to handle the press of a button box button.
43
44 @param button button that has been pressed
45 @type QAbstractButton
46 """
47 if button == self.buttonBox.button(QDialogButtonBox.RestoreDefaults):
48 self.__populateDefaults(options=None)
49
50 def __populateDefaults(self, options=None):
51 """
52 Private method to populate the dialog.
53
54 If no options dictionary is given, the dialog will be populated with
55 default values.
56 """
57 if options is None:
58 options = self.__client.defaultConnectionOptions()
59
60 # general
61 self.clientIdEdit.setText(options["ClientId"])
62 self.keepaliveSpinBox.setValue(options["Keepalive"])
63 self.cleanSessionCheckBox.setChecked(options["CleanSession"])
64
65 # user credentials
66 self.usernameEdit.setText(options["Username"])
67 self.passwordEdit.setText(options["Password"])
68
69 # last will and testament
70 self.willQosSpinBox.setValue(options["WillQos"])
71 self.willRetainCheckBox.setChecked(options["WillRetain"])
72 self.willTopicEdit.setText(options["WillTopic"])
73 self.willMessageEdit.setPlainText(options["WillMessage"])
74
75 def getConnectionOptions(self):
76 """
77 Public method get the entered connection options.
78
79 @return dictionary containing the connection options. It has the keys
80 "ClientId", "Keepalive", "CleanSession", "Username", "Password",
81 "WillTopic", "WillMessage", "WillQos", "WillRetain"
82 @rtype tuple of (int, dict)
83 """
84 return {
85 "ClientId": self.clientIdEdit.text(),
86 "Keepalive": self.keepaliveSpinBox.value(),
87 "CleanSession": self.cleanSessionCheckBox.isChecked(),
88 "Username": self.usernameEdit.text(),
89 "Password": self.passwordEdit.text(),
90 "WillTopic": self.willTopicEdit.text(),
91 "WillMessage": self.willMessageEdit.toPlainText(),
92 "WillQos": self.willQosSpinBox.value(),
93 "WillRetain": self.willRetainCheckBox.isChecked(),
94 }

eric ide

mercurial