MqttMonitor/MqttConnectionOptionsDialog.py

Sat, 01 Sep 2018 20:18:11 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 01 Sep 2018 20:18:11 +0200
changeset 10
7e0e921dc7ea
child 11
90d3ebed4cc0
permissions
-rw-r--r--

Started to implement the connection options dialog and methods to specify these connection options connecting to the server.

# -*- coding: utf-8 -*-

"""
Module implementing a dialog to enter MQTT connection options.
"""

from __future__ import unicode_literals

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QAbstractButton

from .Ui_MqttConnectionOptionsDialog import Ui_MqttConnectionOptionsDialog


class MqttConnectionOptionsDialog(QDialog, Ui_MqttConnectionOptionsDialog):
    """
    Class implementing a dialog to enter MQTT connection options.
    """
    def __init__(self, client, options=None, parent=None):
        """
        Constructor
        
        @param client reference to the MQTT client object
        @type MqttClient
        @param options dictionary containing the connection options to
            populate the dialog with
        @@type dict
        @param parent reference to the parent widget
        @type QWidget
        """
        super(MqttConnectionOptionsDialog, self).__init__(parent)
        self.setupUi(self)
        
        self.__client = client
        
        self.__populateDefaults(options=options)
        
    
    @pyqtSlot(QAbstractButton)
    def on_buttonBox_clicked(self, button):
        """
        Private slot to handle the press of a button box button.
        
        @param button button that has been pressed
        @type QAbstractButton
        """
        if button == self.buttonBox.button(QDialogButtonBox.RestoreDefaults):
            self.__populateDefaults(options=None)
    
    def __populateDefaults(self, options=None):
        """
        Private method to populate the dialog.
        
        If no options dictionary is given, the dialog will be populated with
        default values.
        """
        if options is None:
            options = self.__client.defaultConnectionOptions()
        
        # general
        self.clientIdEdit.setText(options["ClientId"])
        self.keepaliveSpinBox.setValue(options["Keepalive"])
        self.cleanSessionCheckBox.setChecked(options["CleanSession"])
        
        # user credentials
        self.usernameEdit.setText(options["Username"])
        self.passwordEdit.setText(options["Password"])
        
        # last will and testament
        self.willQosSpinBox.setValue(options["WillQos"])
        self.willRetainCheckBox.setChecked(options["WillRetain"])
        self.willTopicEdit.setText(options["WillTopic"])
        self.willMessageEdit.setPlainText(options["WillMessage"])
    
    def getConnectionOptions(self):
        """
        Public method get the entered connection options.
        
        @return dictionary containing the connection options. It has the keys
            "ClientId", "Keepalive", "CleanSession", "Username", "Password",
            "WillTopic", "WillMessage", "WillQos", "WillRetain"
        @rtype tuple of (int, dict)
        """
        return {
            "ClientId": self.clientIdEdit.text(),
            "Keepalive": self.keepaliveSpinBox.value(),
            "CleanSession": self.cleanSessionCheckBox.isChecked(),
            "Username": self.usernameEdit.text(),
            "Password": self.passwordEdit.text(),
            "WillTopic": self.willTopicEdit.text(),
            "WillMessage": self.willMessageEdit.toPlainText(),
            "WillQos": self.willQosSpinBox.value(),
            "WillRetain": self.willRetainCheckBox.isChecked(),
        }

eric ide

mercurial