Skip to content
Snippets Groups Projects
Commit 5a527216 authored by Petter Källström's avatar Petter Källström
Browse files

Making functions private, fixing docstring etc

parent 069abe95
Branches
Tags
1 merge request!203Simulation window
# 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)
# * Make it work with the main_window
import re
import sys
......@@ -15,7 +15,7 @@ from qtpy.QtCore import Qt
from qtpy.QtGui import QKeySequence
# Intereme imports for the Plot class:
from qtpy.QtWidgets import ( # QFrame,; QScrollArea,; QLineEdit,
from qtpy.QtWidgets import ( # QFrame,; QScrollArea,; QLineEdit,; QSizePolicy,
QApplication,
QCheckBox,
QDialog,
......@@ -26,12 +26,13 @@ from qtpy.QtWidgets import ( # QFrame,; QScrollArea,; QLineEdit,
QListWidgetItem,
QPushButton,
QShortcut,
QSizePolicy,
QVBoxLayout,
)
class PlotCanvas(FigureCanvas):
"""PlotCanvas is used as a part in the PlotWindow."""
def __init__(self, logger, parent=None, width=5, height=4, dpi=100):
fig = Figure(figsize=(width, height), dpi=dpi)
super().__init__(fig)
......@@ -60,12 +61,14 @@ class PlotCanvas(FigureCanvas):
class PlotWindow(QDialog):
"""Dialog for plotting the result of a simulation."""
def __init__(
self,
window,
sfg_name,
sim_result,
logger,
sfg_name="{sfg_name}",
window=None,
logger=print,
parent=None,
width=5,
height=4,
......@@ -127,16 +130,16 @@ class PlotWindow(QDialog):
# Add two labels for selecting all/none:
hlayout = QHBoxLayout()
labelAll = QLabel("All")
labelAll.mousePressEvent = self.labelAll_click
labelAll.mousePressEvent = self._label_all_click
labelNone = QLabel("None")
labelNone.mousePressEvent = self.labelNone_click
labelNone.mousePressEvent = self._label_none_click
hlayout.addWidget(labelAll)
hlayout.addWidget(labelNone)
listlayout.addLayout(hlayout)
# Add the entire list
self.checklist = QListWidget()
self.checklist.itemChanged.connect(self.item_change)
self.checklist.itemChanged.connect(self._item_change)
listitems = {}
for key in (
sim_res_ins | sim_res_outs | sim_res_delays | sim_res_others
......@@ -145,19 +148,17 @@ class PlotWindow(QDialog):
listitems[key] = listitem
self.checklist.addItem(listitem)
listitem.setCheckState(
Qt.Unchecked
) # CheckState: Qt.{Unchecked, PartiallyChecked, Checked}
Qt.CheckState.Unchecked # CheckState: Qt.CheckState.{Unchecked, PartiallyChecked, Checked}
)
for key in sim_res_outs:
listitems[key].setCheckState(
Qt.Checked
) # CheckState: Qt.{Unchecked, PartiallyChecked, Checked}
listitems[key].setCheckState(Qt.CheckState.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)
self.legend_checkbox.stateChanged.connect(self._legend_checkbox_change)
self.legend_checkbox.setCheckState(Qt.CheckState.Checked)
listlayout.addWidget(self.legend_checkbox)
# Add "Close" buttons
......@@ -169,34 +170,38 @@ class PlotWindow(QDialog):
# self.plotcanvas.draw()
self._auto_redraw = True
def legend_checkbox_change(self, checkState):
self.plotcanvas.legend.set(visible=(checkState == Qt.Checked))
def _legend_checkbox_change(self, checkState):
self.plotcanvas.legend.set(
visible=(checkState == Qt.CheckState.Checked)
)
if self._auto_redraw:
if checkState == Qt.Checked:
if checkState == Qt.CheckState.Checked:
self.plotcanvas.legend = self.plotcanvas.axes.legend()
self.plotcanvas.draw()
# self.plotcanvas.legend
# if checkState == Qt.Checked:
# if checkState == Qt.CheckState.Checked:
# print('legend on')
# else:
# print('legend off')
def labelAll_click(self, event):
def _label_all_click(self, event):
for x in range(self.checklist.count()):
self.checklist.item(x).setCheckState(Qt.Checked)
self.checklist.item(x).setCheckState(Qt.CheckState.Checked)
def labelNone_click(self, event):
def _label_none_click(self, event):
for x in range(self.checklist.count()):
self.checklist.item(x).setCheckState(Qt.Unchecked)
self.checklist.item(x).setCheckState(Qt.CheckState.Unchecked)
def item_change(self, listitem):
def _item_change(self, listitem):
key = listitem.text()
self._lines[key][0].set(visible=(listitem.checkState() == Qt.Checked))
self._lines[key][0].set(
visible=(listitem.checkState() == Qt.CheckState.Checked)
)
if self._auto_redraw:
if self.legend_checkbox.checkState == Qt.Checked:
if self.legend_checkbox.checkState == Qt.CheckState.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(f"lines[{key}].set(visible={listitem.checkState() == Qt.CheckState.Checked}). autodraw={self._auto_redraw}")
# print("Arg:", listitem)
......@@ -217,51 +222,3 @@ if __name__ == "__main__":
)
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)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment