commit 3b3b9e9b462f1564ec1e7a4278d9d85f40337acf
Author: MFori <forejt.martin97@gmail.com>
Date:   Mon May 17 17:36:28 2021 +0200

    Re: #8921 - use configuration from QSettings

diff --git a/aswi2021vochomurka/service/mqtt/mqtt_subscriber.py b/aswi2021vochomurka/service/mqtt/mqtt_subscriber.py
index 7afccac..fb07edb 100644
--- a/aswi2021vochomurka/service/mqtt/mqtt_subscriber.py
+++ b/aswi2021vochomurka/service/mqtt/mqtt_subscriber.py
@@ -49,7 +49,6 @@ class MQTTSubscriber(Subscriber):
 
         if not self.params.anonymous:
             logging.info('Using credentials, username=' + self.params.username + ', password=' + self.params.password)
-            client.tls_set()
             client.username_pw_set(self.params.username, self.params.password)
 
         try:
diff --git a/aswi2021vochomurka/view/main_view.py b/aswi2021vochomurka/view/main_view.py
index eef4738..19e3c1c 100644
--- a/aswi2021vochomurka/view/main_view.py
+++ b/aswi2021vochomurka/view/main_view.py
@@ -3,7 +3,7 @@ import math
 
 import matplotlib.pyplot as plt
 from PyQt5 import QtGui
-from PyQt5.QtCore import QSize, QThread, QObject, pyqtSignal
+from PyQt5.QtCore import QSize, QThread, QObject, pyqtSignal, QSettings
 from PyQt5.QtWidgets import QMainWindow, QWidget, QMenuBar, QAction
 from PyQt5.QtWidgets import QVBoxLayout
 from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
@@ -17,22 +17,20 @@ from aswi2021vochomurka.service.subscriber_params import SubscriberParams, Conne
 from aswi2021vochomurka.view.about import AboutDialog
 from aswi2021vochomurka.view.help import HelpDialog
 from aswi2021vochomurka.view.logger_view import LoggerView
-from aswi2021vochomurka.view.settings import SettingsDialog
+from aswi2021vochomurka.view.settings import SettingsDialog, DEFAULT_HOST, DEFAULT_PORT, DEFAULT_KEEPALIVE, \
+    DEFAULT_ANONYMOUS, DEFAULT_USERNAME, DEFAULT_TIMEOUT, DEFAULT_TOPICS
 
 
 class Worker(QObject, SubscriberCallback):
     connected = pyqtSignal()
     disconnected = pyqtSignal()
-    error = pyqtSignal(Exception)
     newMessage = pyqtSignal(Message)
     subscriber: Subscriber = None
+    params: SubscriberParams
 
-    params = SubscriberParams(
-        ["/home/1", "/home/2"],
-        10,
-        ConnectionParams("localhost", 1883, 60),
-        True
-    )
+    def __init__(self, params: SubscriberParams) -> None:
+        super().__init__()
+        self.params = params
 
     def start(self):
         self.subscriber = MQTTSubscriber(self, self.params)
@@ -48,7 +46,7 @@ class Worker(QObject, SubscriberCallback):
         self.disconnected.emit()
 
     def onError(self):
-        self.error.emit()
+        pass
 
     def onMessage(self, message: Message):
         self.newMessage.emit(message)
@@ -71,7 +69,7 @@ class MainView(QMainWindow):
         self.dataIndex = 0
         self.dataDict = {}
 
-        self.figure = plt.figure(figsize=([500,500]))
+        self.figure = plt.figure(figsize=([500, 500]))
 
         self.canvas = FigureCanvas(self.figure)
         self.toolbar = NavigationToolbar(self.canvas, self)
@@ -156,14 +154,35 @@ class MainView(QMainWindow):
 
     def reconnect(self):
         self.disconnect()
+        self.worker.params = self.getConfigParams()
         self.workerThread.start()
 
     def initSubscriber(self):
         self.workerThread = QThread()
-        self.worker = Worker()
+        self.worker = Worker(self.getConfigParams())
         self.worker.moveToThread(self.workerThread)
         self.workerThread.started.connect(self.worker.start)
         self.worker.newMessage.connect(
             lambda message: self.plot(message)
         )
         self.workerThread.start()
+
+    def getConfigParams(self) -> SubscriberParams:
+        settings = QSettings("Vochomurka", "MQTTClient")
+
+        connection = ConnectionParams(
+            settings.value("connection_host", DEFAULT_HOST, str),
+            settings.value("connection_port", DEFAULT_PORT, int),
+            settings.value("connection_keepalive", DEFAULT_KEEPALIVE, int)
+        )
+
+        params = SubscriberParams(
+            settings.value("topics_items", DEFAULT_TOPICS),
+            settings.value("topics_timeout", DEFAULT_TIMEOUT),
+            connection,
+            settings.value("connection_anonymous", DEFAULT_ANONYMOUS, bool),
+            settings.value("connection_username", DEFAULT_USERNAME, str),
+            settings.value("connection_password", DEFAULT_USERNAME, str),
+        )
+
+        return params
diff --git a/aswi2021vochomurka/view/settings.py b/aswi2021vochomurka/view/settings.py
index 742cb44..f6e4f3e 100644
--- a/aswi2021vochomurka/view/settings.py
+++ b/aswi2021vochomurka/view/settings.py
@@ -1,12 +1,19 @@
 from PyQt5 import QtCore
 from PyQt5.QtCore import QSettings, QSize
-from PyQt5.QtGui import QStandardItemModel, QStandardItem
 from PyQt5.QtWidgets import QDialog, QVBoxLayout, QDialogButtonBox, QGroupBox, QFormLayout, QLabel, QLineEdit, QSpinBox, \
-    QCheckBox, QListView, QWidget, QPushButton, QHBoxLayout, QListWidget, QListWidgetItem, QTableWidget
+    QCheckBox, QPushButton, QListWidget, QListWidgetItem
 
+DEFAULT_HOST = "localhost"
+DEFAULT_PORT = 1883
+DEFAULT_KEEPALIVE = 60
+DEFAULT_ANONYMOUS = True
+DEFAULT_USERNAME = ""
+DEFAULT_PASSWORD = ""
+DEFAULT_TOPICS = ["/home/1", "/home/2"]
+DEFAULT_TIMEOUT = 60
 
 class SettingsDialog(QDialog):
-    topics = ['/home/1', '/home/2']
+    topics = DEFAULT_TOPICS
 
     def __init__(self):
         super(SettingsDialog, self).__init__()
@@ -16,23 +23,23 @@ class SettingsDialog(QDialog):
 
         connectionGroupBox = QGroupBox("Connection")
         connectionLayout = QFormLayout()
-        self.hostInput = QLineEdit(self.settings.value("connection_host", "localhost", str))
+        self.hostInput = QLineEdit(self.settings.value("connection_host", DEFAULT_HOST, str))
         connectionLayout.addRow(QLabel("Host:"), self.hostInput)
         self.portInput = QSpinBox()
         self.portInput.setMaximum(65535)
-        self.portInput.setValue(self.settings.value("connection_port", 1883, int))
+        self.portInput.setValue(self.settings.value("connection_port", DEFAULT_PORT, int))
         connectionLayout.addRow(QLabel("Port:"), self.portInput)
         self.keepaliveInput = QSpinBox()
         self.keepaliveInput.setMaximum(1000)
-        self.keepaliveInput.setValue(self.settings.value("connection_keepalive", 60, int))
+        self.keepaliveInput.setValue(self.settings.value("connection_keepalive", DEFAULT_KEEPALIVE, int))
         connectionLayout.addRow(QLabel("Keepalive(s):"), self.keepaliveInput)
         self.anonymousInput = QCheckBox()
-        self.anonymousInput.setChecked(self.settings.value("connection_anonymous", True, bool))
+        self.anonymousInput.setChecked(self.settings.value("connection_anonymous", DEFAULT_ANONYMOUS, bool))
         self.anonymousInput.stateChanged.connect(self.anonymousChanged)
         connectionLayout.addRow(QLabel("Anonymous:"), self.anonymousInput)
-        self.usernameInput = QLineEdit(self.settings.value("connection_username", "", str))
+        self.usernameInput = QLineEdit(self.settings.value("connection_username", DEFAULT_USERNAME, str))
         connectionLayout.addRow(QLabel("Username:"), self.usernameInput)
-        self.passwordInput = QLineEdit(self.settings.value("connection_password", "", str))
+        self.passwordInput = QLineEdit(self.settings.value("connection_password", DEFAULT_PASSWORD, str))
         connectionLayout.addRow(QLabel("Password:"), self.passwordInput)
         self.anonymousChanged()
         connectionGroupBox.setLayout(connectionLayout)
@@ -40,6 +47,7 @@ class SettingsDialog(QDialog):
         topicsGroupBox = QGroupBox("Topics")
         topicsLayout = QFormLayout()
 
+        self.topics = self.settings.value("topics_items", DEFAULT_TOPICS, list)
         self.topicsListWidget = QListWidget()
         for topic in self.topics:
             item = QListWidgetItem()
@@ -63,7 +71,7 @@ class SettingsDialog(QDialog):
         timeoutLabel = QLabel("Topic timeout(s):")
         timeoutLabel.setToolTip("Unsubscribe topic and close file when there is not new message after this "
                                 "timeout (in seconds) expires")
-        self.timeoutInput.setValue(self.settings.value("topic_timeout", 60, int))
+        self.timeoutInput.setValue(self.settings.value("topics_timeout", DEFAULT_TIMEOUT, int))
         topicsLayout.addRow(timeoutLabel, self.timeoutInput)
 
         topicsGroupBox.setLayout(topicsLayout)
@@ -96,8 +104,12 @@ class SettingsDialog(QDialog):
 
     def accept(self) -> None:
         super().accept()
-        self.topics = self.topicsListWidget.item(0).text()
-        print(self.topics)
+        self.topics = []
+        for index in range(self.topicsListWidget.count()):
+            self.topics.append(self.topicsListWidget.item(index).text())
+
+        self.settings.setValue("topics_items", self.topics)
+        self.settings.setValue("topics_timeout", self.timeoutInput.value())
         self.settings.setValue("connection_host", self.hostInput.text())
         self.settings.setValue("connection_port", self.portInput.value())
         self.settings.setValue("connection_keepalive", self.keepaliveInput.value())
