Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# TODO's:
# * Solve the legend update. That isn't working at all.
# * Change labels "All" and "None" into buttons.
# * Make it work with the main_window (probably requires a rebase or merge)
import re
import sys
from matplotlib.backends.backend_qt5agg import (
FigureCanvasQTAgg as FigureCanvas,
)
from matplotlib.figure import Figure
from matplotlib.ticker import MaxNLocator
from qtpy.QtCore import Qt
from qtpy.QtGui import QKeySequence
# Intereme imports for the Plot class:
from qtpy.QtWidgets import ( # QFrame,; QScrollArea,; QLineEdit,
QApplication,
QCheckBox,
QDialog,
QFileDialog,
QHBoxLayout,
QLabel,
QListWidget,
QListWidgetItem,
QPushButton,
QShortcut,
QSizePolicy,
QVBoxLayout,
)
class PlotCanvas(FigureCanvas):
def __init__(self, logger, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
super().__init__(fig)
self.axes = fig.add_subplot(111)
self.axes.xaxis.set_major_locator(MaxNLocator(integer=True))
self.legend = self.axes.legend()
self.logger = logger
FigureCanvas.updateGeometry(self)
self.save_figure = QShortcut(QKeySequence("Ctrl+S"), self)
self.save_figure.activated.connect(self._save_plot_figure)
def _save_plot_figure(self):
self.logger.info(f"Saving plot of figure: {self.sfg.name}.")
file_choices = "PNG (*.png)|*.png"
path, ext = QFileDialog.getSaveFileName(
self, "Save file", "", file_choices
)
path = path.encode("utf-8")
if not path[-4:] == file_choices[-4:].encode("utf-8"):
path += file_choices[-4:].encode("utf-8")
if path:
self.print_figure(path.decode(), dpi=self.dpi)
self.logger.info(f"Saved plot: {self.sfg.name} to path: {path}.")
class PlotWindow(QDialog):
def __init__(
self,
window,
sfg_name,
sim_result,
logger,
parent=None,
width=5,
height=4,
dpi=100,
):
super().__init__()
self._window = window
self.setWindowFlags(Qt.WindowTitleHint | Qt.WindowCloseButtonHint)
self.setWindowTitle("Simulation result")
self.sim_result = sim_result
self._auto_redraw = False
# Categorise sim_results into inputs, outputs, delays, others
sim_res_ins = {}
sim_res_outs = {}
sim_res_delays = {}
sim_res_others = {}
for key in sim_result:
if re.fullmatch("in[0-9]+", key):
sim_res_ins[key] = sim_result[key]
elif re.fullmatch("[0-9]+", key):
sim_res_outs[key] = sim_result[key]
elif re.fullmatch("t[0-9]+", key):
sim_res_delays[key] = sim_result[key]
else:
sim_res_others[key] = sim_result[key]
# Layout: ############################################
# | list | |
# | ... | plot |
# | misc | |
self.dialog_layout = QHBoxLayout()
self.setLayout(self.dialog_layout)
listlayout = QVBoxLayout()
self.plotcanvas = PlotCanvas(
logger=logger, parent=self, width=5, height=4, dpi=100
)
self.dialog_layout.addLayout(listlayout)
self.dialog_layout.addWidget(self.plotcanvas)
########### Plot: ##############
# Do this before the list layout, as the list layout will re/set visibility
# Note: The order is of importens. Interesting lines last, to be on top.
self._lines = {}
for key in (
sim_res_others | sim_res_delays | sim_res_ins | sim_res_outs
):
line = self.plotcanvas.axes.plot(
sim_result[key], visible=False, label=key
)
self._lines[key] = line
########### List layout: ##############
# Add two labels for selecting all/none:
hlayout = QHBoxLayout()
labelAll = QLabel("All")
labelAll.mousePressEvent = self.labelAll_click
labelNone = QLabel("None")
labelNone.mousePressEvent = self.labelNone_click
hlayout.addWidget(labelAll)
hlayout.addWidget(labelNone)
listlayout.addLayout(hlayout)
# Add the entire list
self.checklist = QListWidget()
self.checklist.itemChanged.connect(self.item_change)
listitems = {}
for key in (
sim_res_ins | sim_res_outs | sim_res_delays | sim_res_others
):
listitem = QListWidgetItem(key)
listitems[key] = listitem
self.checklist.addItem(listitem)
listitem.setCheckState(
Qt.Unchecked
) # CheckState: Qt.{Unchecked, PartiallyChecked, Checked}
for key in sim_res_outs:
listitems[key].setCheckState(
Qt.Checked
) # CheckState: Qt.{Unchecked, PartiallyChecked, Checked}
self.checklist.setFixedWidth(150)
listlayout.addWidget(self.checklist)
# Add a "legend" checkbox, connected to the plot.
self.legend_checkbox = QCheckBox("&Legend")
self.legend_checkbox.stateChanged.connect(self.legend_checkbox_change)
self.legend_checkbox.setCheckState(Qt.Checked)
listlayout.addWidget(self.legend_checkbox)
# Add "Close" buttons
buttonClose = QPushButton("&Close", self)
buttonClose.clicked.connect(self.close)
listlayout.addWidget(buttonClose)
# Done. Tell the functions below to redraw the canvas when needed.
# self.plotcanvas.draw()
self._auto_redraw = True
def legend_checkbox_change(self, checkState):
self.plotcanvas.legend.set(visible=(checkState == Qt.Checked))
if self._auto_redraw:
if checkState == Qt.Checked:
self.plotcanvas.legend = self.plotcanvas.axes.legend()
self.plotcanvas.draw()
# self.plotcanvas.legend
# if checkState == Qt.Checked:
# print('legend on')
# else:
# print('legend off')
def labelAll_click(self, event):
for x in range(self.checklist.count()):
self.checklist.item(x).setCheckState(Qt.Checked)
def labelNone_click(self, event):
for x in range(self.checklist.count()):
self.checklist.item(x).setCheckState(Qt.Unchecked)
def item_change(self, listitem):
key = listitem.text()
self._lines[key][0].set(visible=(listitem.checkState() == Qt.Checked))
if self._auto_redraw:
if self.legend_checkbox.checkState == Qt.Checked:
self.plotcanvas.legend = self.plotcanvas.axes.legend()
self.plotcanvas.draw()
# print(f"lines[{key}].set(visible={listitem.checkState() == Qt.Checked}). autodraw={self._auto_redraw}")
# print("Arg:", listitem)
# Simple test of the dialog
if __name__ == "__main__":
app = QApplication(sys.argv)
# sim_res = {"c1": [3, 6, 7], "c2": [4, 5, 5], "bfly1.0": [7, 0, 0], "bfly1.1": [-1, 0, 2], "0": [1, 2, 3]}
sim_res = {
'0': [0.5, 0.5, 0, 0],
'add1': [0.5, 0.5, 0, 0],
'cmul1': [0, 0.5, 0, 0],
'cmul2': [0.5, 0, 0, 0],
'in1': [1, 0, 0, 0],
't1': [0, 1, 0, 0],
}
win = PlotWindow(
window=None, sim_result=sim_res, sfg_name="hej", logger=print
)
win.exec_()
# win.show()
# This is the original, used as a quick reference. Do not instantiate this:
class Plot(FigureCanvas):
def __init__(
self, simulation, sfg, window, parent=None, width=5, height=4, dpi=100
):
self.simulation = simulation
self.sfg = sfg
self.dpi = dpi
self._window = window
fig = Figure(figsize=(width, height), dpi=dpi)
fig.suptitle(sfg.name, fontsize=20)
self.axes = fig.add_subplot(111)
FigureCanvas.__init__(self, fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(
self, QSizePolicy.Expanding, QSizePolicy.Expanding
)
FigureCanvas.updateGeometry(self)
self.save_figure = QShortcut(QKeySequence("Ctrl+S"), self)
self.save_figure.activated.connect(self._save_plot_figure)
self._plot_values_sfg()
def _save_plot_figure(self):
self._window.logger.info(f"Saving plot of figure: {self.sfg.name}.")
file_choices = "PNG (*.png)|*.png"
path, ext = QFileDialog.getSaveFileName(
self, "Save file", "", file_choices
)
path = path.encode("utf-8")
if not path[-4:] == file_choices[-4:].encode("utf-8"):
path += file_choices[-4:].encode("utf-8")
if path:
self.print_figure(path.decode(), dpi=self.dpi)
self._window.logger.info(
f"Saved plot: {self.sfg.name} to path: {path}."
)
def _plot_values_sfg(self):
x_axis = list(range(len(self.simulation.results["0"])))
for _output in range(self.sfg.output_count):
y_axis = self.simulation.results[str(_output)]
self.axes.plot(x_axis, y_axis)