Revize e402bc71
Přidáno uživatelem Pavel Průcha před téměř 4 roky(ů)
aswi2021vochomurka/view/logger_view.py | ||
---|---|---|
5 | 5 |
|
6 | 6 |
|
7 | 7 |
class LoggerView(logging.Handler, QObject): |
8 |
""" |
|
9 |
LoggerView represents console in gui application. |
|
10 |
""" |
|
8 | 11 |
append = pyqtSignal(str) |
9 | 12 |
|
10 | 13 |
def __init__(self, parent): |
14 |
""" |
|
15 |
Constructor |
|
16 |
""" |
|
11 | 17 |
super().__init__() |
12 | 18 |
super(QObject, self).__init__() |
13 | 19 |
|
... | ... | |
19 | 25 |
) |
20 | 26 |
|
21 | 27 |
def emit(self, record): |
28 |
""" |
|
29 |
Emit message from record |
|
30 |
:param record: record |
|
31 |
""" |
|
22 | 32 |
msg = self.format(record) |
23 | 33 |
self.append.emit(msg) |
24 | 34 |
|
25 | 35 |
def appendMessage(self, msg): |
36 |
""" |
|
37 |
Append message |
|
38 |
:param msg: message |
|
39 |
""" |
|
26 | 40 |
self.widget.appendPlainText(msg) |
27 | 41 |
self.widget.verticalScrollBar().setValue(self.widget.verticalScrollBar().maximum()) |
28 | 42 |
|
aswi2021vochomurka/view/main_view.py | ||
---|---|---|
19 | 19 |
|
20 | 20 |
|
21 | 21 |
class Worker(QObject, SubscriberCallback): |
22 |
""" |
|
23 |
Worker representing thread |
|
24 |
""" |
|
22 | 25 |
connected = pyqtSignal() |
23 | 26 |
disconnected = pyqtSignal() |
24 | 27 |
error = pyqtSignal(Exception) |
... | ... | |
28 | 31 |
params: SubscriberParams |
29 | 32 |
|
30 | 33 |
def __init__(self, params: SubscriberParams) -> None: |
34 |
""" |
|
35 |
Constructor |
|
36 |
""" |
|
31 | 37 |
super().__init__() |
32 | 38 |
self.params = params |
33 | 39 |
|
34 | 40 |
def start(self): |
41 |
""" |
|
42 |
Start worker |
|
43 |
""" |
|
35 | 44 |
self.subscriber = MQTTSubscriber(self, self.params) |
36 | 45 |
self.subscriber.start() |
37 | 46 |
|
38 | 47 |
def stop(self): |
48 |
""" |
|
49 |
Stop worker |
|
50 |
""" |
|
39 | 51 |
self.subscriber.stop() |
40 | 52 |
|
41 | 53 |
def onConnected(self): |
54 |
""" |
|
55 |
Emit connection signal |
|
56 |
""" |
|
42 | 57 |
self.connected.emit() |
43 | 58 |
|
44 | 59 |
def onDisconnected(self): |
60 |
""" |
|
61 |
Emit disconnection signal |
|
62 |
""" |
|
45 | 63 |
self.disconnected.emit() |
46 | 64 |
|
47 | 65 |
def onError(self): |
48 | 66 |
pass |
49 | 67 |
|
50 | 68 |
def onMessage(self, message: Message): |
69 |
""" |
|
70 |
Emit message signal |
|
71 |
:param message: message |
|
72 |
""" |
|
51 | 73 |
self.newMessage.emit(message) |
52 | 74 |
|
53 | 75 |
def onCloseTopic(self, topic: str): |
76 |
""" |
|
77 |
Emit close topic signal |
|
78 |
:param topic: topic |
|
79 |
""" |
|
54 | 80 |
print("Close topic") |
55 | 81 |
self.closeTopic.emit(topic) |
56 |
#pass |
|
57 | 82 |
|
58 | 83 |
|
59 | 84 |
class MainView(QMainWindow): |
85 |
""" |
|
86 |
Main window of application. |
|
87 |
Displays all parts of the application. |
|
88 |
""" |
|
60 | 89 |
worker: Worker = None |
61 | 90 |
workerThread: QThread = None |
62 | 91 |
|
63 | 92 |
def __init__(self): |
93 |
""" |
|
94 |
Constructor - displays all parts of the application. |
|
95 |
""" |
|
64 | 96 |
super(MainView, self).__init__() |
65 | 97 |
|
66 | 98 |
self.chartsNum = 0 |
67 |
self.arrayData = [] |
|
68 |
|
|
69 |
self.dataIndex = 0 |
|
70 | 99 |
self.dataDict = {} |
71 | 100 |
self.canvasDict = {} |
72 | 101 |
self.figureDict = {} |
73 | 102 |
self.widgetDict = {} |
74 |
|
|
75 | 103 |
self.widgetList = [] |
76 | 104 |
|
77 |
# self.toolbar = NavigationToolbar(self.canvas, self) |
|
78 |
|
|
79 |
# self.setMinimumSize(QSize(440, 240)) |
|
80 | 105 |
self.setMinimumSize(QSize(1200, 800)) |
81 | 106 |
self.setWindowTitle("MQTT client") |
82 | 107 |
|
83 | 108 |
logger = self._createLoggerView() |
84 | 109 |
layout = QVBoxLayout() |
85 | 110 |
layout.addWidget(logger.widget) |
86 |
# layout.addWidget(self.toolbar) |
|
87 | 111 |
|
88 | 112 |
widget = QWidget() |
89 | 113 |
widget.setLayout(layout) |
... | ... | |
100 | 124 |
self.init_subscriber() |
101 | 125 |
|
102 | 126 |
def _createLoggerView(self): |
127 |
""" |
|
128 |
Create logger view |
|
129 |
""" |
|
103 | 130 |
logger = LoggerView(self) |
104 | 131 |
formatter = logging.Formatter('%(asctime)s %(message)s', '%H:%M') |
105 | 132 |
logger.setFormatter(formatter) |
... | ... | |
108 | 135 |
return logger |
109 | 136 |
|
110 | 137 |
def _createMenuBar(self): |
138 |
""" |
|
139 |
Creates menu bar |
|
140 |
""" |
|
111 | 141 |
menuBar = QMenuBar(self) |
112 | 142 |
settingsAction = QAction("&Settings", self) |
113 | 143 |
settingsAction.triggered.connect(self.settings) |
... | ... | |
115 | 145 |
self.setMenuBar(menuBar) |
116 | 146 |
|
117 | 147 |
def plot(self, message: Message): |
148 |
""" |
|
149 |
Plots new charts or updates old ones |
|
150 |
:param message: message |
|
151 |
""" |
|
118 | 152 |
if message.topic in self.dataDict: |
153 |
# topic already exists |
|
119 | 154 |
self.dataDict[message.topic].append(message.value) |
120 | 155 |
|
121 | 156 |
figure = self.figureDict[message.topic] |
... | ... | |
127 | 162 |
|
128 | 163 |
self.canvasDict[message.topic].draw() |
129 | 164 |
else: |
165 |
# new topic |
|
130 | 166 |
self.dataDict[message.topic] = [message.value] |
131 | 167 |
|
132 | 168 |
figure = plt.figure(figsize=[500, 500]) |
... | ... | |
155 | 191 |
self.chartsNum += 1 |
156 | 192 |
|
157 | 193 |
def deletePlot(self, topic: str): |
194 |
""" |
|
195 |
Deletes plot |
|
196 |
:param topic: topic |
|
197 |
""" |
|
158 | 198 |
widget = self.widgetDict[topic] |
159 | 199 |
self.widgetList.remove(widget) |
160 | 200 |
widget.setParent(None) |
... | ... | |
167 | 207 |
self.reorganizePlots() |
168 | 208 |
|
169 | 209 |
def reorganizePlots(self): |
210 |
""" |
|
211 |
Reorganize plots |
|
212 |
""" |
|
170 | 213 |
count = 0 |
171 | 214 |
for widget in self.widgetList: |
172 | 215 |
self.grid.addWidget(widget, int(count / 2), count % 2) |
... | ... | |
178 | 221 |
self.worker.stop() |
179 | 222 |
|
180 | 223 |
def settings(self): |
224 |
""" |
|
225 |
Opens settings dialog |
|
226 |
""" |
|
181 | 227 |
dialog = SettingsDialog() |
182 | 228 |
if dialog.exec_(): |
183 | 229 |
self.reconnect() |
184 | 230 |
|
185 | 231 |
def disconnect(self): |
232 |
""" |
|
233 |
Disconnect |
|
234 |
""" |
|
186 | 235 |
self.worker.stop() |
187 | 236 |
self.workerThread.quit() |
188 | 237 |
self.workerThread.wait() |
189 | 238 |
|
190 | 239 |
def reconnect(self): |
240 |
""" |
|
241 |
Reconnect |
|
242 |
""" |
|
191 | 243 |
self.disconnect() |
192 | 244 |
self.worker.params = self.getConfigParams() |
193 | 245 |
self.workerThread.start() |
194 | 246 |
|
195 | 247 |
def init_subscriber(self): |
248 |
""" |
|
249 |
Initialization of subscriber |
|
250 |
""" |
|
196 | 251 |
self.workerThread = QThread() |
197 | 252 |
self.worker = Worker(self.getConfigParams()) |
198 | 253 |
self.worker.moveToThread(self.workerThread) |
... | ... | |
207 | 262 |
self.workerThread.start() |
208 | 263 |
|
209 | 264 |
def getConfigParams(self) -> SubscriberParams: |
265 |
""" |
|
266 |
Returns config parameters |
|
267 |
:return: config parameters |
|
268 |
""" |
|
210 | 269 |
settings = get_settings() |
211 | 270 |
|
212 | 271 |
connection = ConnectionParams( |
aswi2021vochomurka/view/settings.py | ||
---|---|---|
19 | 19 |
|
20 | 20 |
|
21 | 21 |
class SettingsDialog(QDialog): |
22 |
""" |
|
23 |
Settings dialog. |
|
24 |
In settings dialog is possible to change settings of application. |
|
25 |
""" |
|
22 | 26 |
topics = DEFAULT_TOPICS |
23 | 27 |
|
24 | 28 |
def __init__(self): |
29 |
""" |
|
30 |
Constructor |
|
31 |
""" |
|
25 | 32 |
super(SettingsDialog, self).__init__(None, |
26 | 33 |
QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.WindowSystemMenuHint | QtCore.Qt.WindowTitleHint) |
27 | 34 |
self.settings = get_settings() |
... | ... | |
96 | 103 |
self.setLayout(mainLayout) |
97 | 104 |
|
98 | 105 |
def addTopic(self): |
106 |
""" |
|
107 |
Add topic |
|
108 |
""" |
|
99 | 109 |
item = QListWidgetItem() |
100 | 110 |
item.setText("/topic") |
101 | 111 |
item.setFlags(item.flags() | QtCore.Qt.ItemIsEditable) |
102 | 112 |
self.topicsListWidget.addItem(item) |
103 | 113 |
|
104 | 114 |
def removeTopic(self): |
115 |
""" |
|
116 |
Remove topic |
|
117 |
""" |
|
105 | 118 |
for item in self.topicsListWidget.selectedItems(): |
106 | 119 |
self.topicsListWidget.takeItem(self.topicsListWidget.row(item)) |
107 | 120 |
|
108 | 121 |
def anonymousChanged(self): |
122 |
""" |
|
123 |
Changing anonymous/user status |
|
124 |
""" |
|
109 | 125 |
self.usernameInput.setEnabled(not self.anonymousInput.isChecked()) |
110 | 126 |
self.passwordInput.setEnabled(not self.anonymousInput.isChecked()) |
111 | 127 |
|
112 | 128 |
def accept(self) -> None: |
129 |
""" |
|
130 |
Accept changes |
|
131 |
""" |
|
113 | 132 |
super().accept() |
114 | 133 |
self.topics = [] |
115 | 134 |
for index in range(self.topicsListWidget.count()): |
Také k dispozici: Unified diff
Re: #8998 - refactoring, comments