Revize ab7ab78d
Přidáno uživatelem Jan Rach před téměř 4 roky(ů)
aswi2021vochomurka/view/main_view.py | ||
---|---|---|
1 | 1 |
import logging |
2 |
import math |
|
3 |
import random |
|
4 | 2 |
|
3 |
import matplotlib.pyplot as plt |
|
4 |
from PyQt5 import QtCore |
|
5 | 5 |
from PyQt5.QtCore import QSize, QThread, QObject, pyqtSignal |
6 |
from PyQt5.QtWidgets import QMainWindow, QPlainTextEdit, QDialog, QHBoxLayout
|
|
7 |
from numpy import pi, sin, cos, tan, exp
|
|
8 |
from matplotlib.pyplot import subplot
|
|
6 |
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout
|
|
7 |
from PyQt5.QtWidgets import QHBoxLayout, QGridLayout, QScrollArea, QWidget
|
|
8 |
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
|
|
9 | 9 |
|
10 | 10 |
from aswi2021vochomurka.model.Message import Message |
11 | 11 |
from aswi2021vochomurka.service.mqtt.mqtt_subscriber import MQTTSubscriber |
12 | 12 |
from aswi2021vochomurka.service.subscriber import Subscriber |
13 | 13 |
from aswi2021vochomurka.service.subscriber_callback import SubscriberCallback |
14 | 14 |
from aswi2021vochomurka.service.subscriber_params import SubscriberParams, ConnectionParams |
15 |
|
|
16 |
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
|
17 |
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar |
|
18 |
import matplotlib.pyplot as plt |
|
19 |
|
|
20 |
import sys |
|
21 |
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout |
|
22 |
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas |
|
23 |
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar |
|
24 |
import matplotlib.pyplot as plt |
|
25 |
import random |
|
26 |
|
|
27 | 15 |
from aswi2021vochomurka.view.logger_view import LoggerView |
28 | 16 |
|
29 | 17 |
|
... | ... | |
31 | 19 |
connected = pyqtSignal() |
32 | 20 |
disconnected = pyqtSignal() |
33 | 21 |
error = pyqtSignal(Exception) |
34 |
newMessage = pyqtSignal(str)
|
|
22 |
newMessage = pyqtSignal(Message)
|
|
35 | 23 |
subscriber: Subscriber = None |
36 | 24 |
|
37 | 25 |
params = SubscriberParams( |
38 |
["/home/1", "/home/2"], |
|
26 |
["/home/1", "/home/2", "/home/3", "/home/4", "/home/5", "/home/6", "/home/7", "/home/8"],
|
|
39 | 27 |
10, |
40 | 28 |
ConnectionParams("localhost", 1883, 60), |
41 | 29 |
True |
... | ... | |
55 | 43 |
self.error.emit() |
56 | 44 |
|
57 | 45 |
def onMessage(self, message: Message): |
58 |
self.newMessage.emit(message.topic)
|
|
59 |
self.window.plot(message) |
|
46 |
self.newMessage.emit(message) |
|
47 |
# self.window.plot(message)
|
|
60 | 48 |
|
61 | 49 |
def onCloseTopic(self, topic: str): |
62 | 50 |
pass |
... | ... | |
75 | 63 |
self.dataIndex = 0 |
76 | 64 |
self.dataDict = {} |
77 | 65 |
|
78 |
self.figure = plt.figure(figsize=([500,500])) |
|
66 |
self.canvasDict = {} |
|
67 |
self.figureDict = {} |
|
79 | 68 |
|
80 |
self.canvas = FigureCanvas(self.figure) |
|
81 |
self.toolbar = NavigationToolbar(self.canvas, self) |
|
69 |
# self.toolbar = NavigationToolbar(self.canvas, self) |
|
82 | 70 |
|
83 |
self.setMinimumSize(QSize(440, 240)) |
|
71 |
# self.setMinimumSize(QSize(440, 240)) |
|
72 |
self.setMinimumSize(QSize(1200, 800)) |
|
84 | 73 |
self.setWindowTitle("MQTT demo") |
85 | 74 |
|
86 | 75 |
# Add logger text field |
... | ... | |
92 | 81 |
|
93 | 82 |
layout = QVBoxLayout() |
94 | 83 |
layout.addWidget(logger.widget) |
95 |
layout.addWidget(self.toolbar) |
|
96 |
layout.addWidget(self.canvas) |
|
84 |
# layout.addWidget(self.toolbar) |
|
97 | 85 |
|
98 | 86 |
self.setLayout(layout) |
99 | 87 |
|
100 |
self.initSubscriber() |
|
88 |
scrollArea = QScrollArea(self) |
|
89 |
scrollArea.setWidgetResizable(True) |
|
90 |
scrollContent = QWidget() |
|
91 |
self.grid = QGridLayout(scrollContent) |
|
92 |
scrollArea.setWidget(scrollContent) |
|
93 |
layout.addWidget(scrollArea) |
|
101 | 94 |
|
102 |
def plot(self, message: Message): |
|
103 |
self.figure.clear() |
|
95 |
self.init_subscriber() |
|
104 | 96 |
|
97 |
def plot(self, message: Message): |
|
105 | 98 |
if message.topic in self.dataDict: |
106 | 99 |
self.dataDict[message.topic].append(message.value) |
100 |
|
|
101 |
figure = self.figureDict[message.topic] |
|
102 |
figure.clear() |
|
103 |
|
|
104 |
plt.figure(figure.number) |
|
105 |
plt.plot(self.dataDict[message.topic]) |
|
106 |
|
|
107 |
self.canvasDict[message.topic].draw() |
|
107 | 108 |
else: |
108 | 109 |
self.dataDict[message.topic] = [message.value] |
109 |
self.chartsNum += 1 |
|
110 | 110 |
|
111 |
rows = math.ceil(self.chartsNum / 2) |
|
111 |
figure = plt.figure(figsize=[500, 500]) |
|
112 |
canvas = FigureCanvas(figure) |
|
113 |
layout = QHBoxLayout() |
|
112 | 114 |
|
113 |
b = 0 |
|
114 |
for a in self.dataDict.values(): |
|
115 |
self.figure.add_subplot(rows, 2, b + 1) |
|
116 |
b += 1 |
|
117 |
plt.plot(a) |
|
115 |
plt.plot(self.dataDict[message.topic]) |
|
118 | 116 |
|
119 |
self.canvas.draw() |
|
117 |
self.canvasDict[message.topic] = canvas |
|
118 |
self.figureDict[message.topic] = figure |
|
119 |
|
|
120 |
widget = QWidget() |
|
121 |
widget.setLayout(layout) |
|
122 |
button = QPushButton(':') |
|
123 |
button.setFixedSize(QSize(40, 40)) |
|
124 |
layout.addWidget(canvas) |
|
125 |
layout.addWidget(button) |
|
126 |
layout.setAlignment(button, QtCore.Qt.AlignTop) |
|
127 |
widget.setMinimumSize(QSize(500, 500)) |
|
128 |
|
|
129 |
self.grid.addWidget(widget, int(self.chartsNum / 2), self.chartsNum % 2) |
|
130 |
|
|
131 |
self.chartsNum += 1 |
|
120 | 132 |
|
121 |
def initSubscriber(self):
|
|
133 |
def init_subscriber(self):
|
|
122 | 134 |
self.workerThread = QThread() |
123 | 135 |
self.worker = Worker() |
124 | 136 |
self.worker.moveToThread(self.workerThread) |
125 | 137 |
self.workerThread.started.connect(self.worker.start) |
126 |
# self.worker.newMessage.connect(
|
|
127 |
# lambda message: self.b.insertPlainText(message + "\n")
|
|
128 |
# )
|
|
138 |
self.worker.newMessage.connect( |
|
139 |
lambda message: self.plot(message)
|
|
140 |
) |
|
129 | 141 |
self.worker.window = self |
130 | 142 |
self.workerThread.start() |
Také k dispozici: Unified diff
Feature/8923 grid layout