Debian Codegraph @

pymeasure/display/windows/plotter_window.py

raw
1 #
2 # This file is part of the PyMeasure package.
3 #
4 # Copyright (c) 2013-2024 PyMeasure Developers
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining a copy
7 # of this software and associated documentation files (the "Software"), to deal
8 # in the Software without restriction, including without limitation the rights
9 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 # copies of the Software, and to permit persons to whom the Software is
11 # furnished to do so, subject to the following conditions:
12 #
13 # The above copyright notice and this permission notice shall be included in
14 # all copies or substantial portions of the Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 # THE SOFTWARE.
23 #
24
25 import logging
26
27 import pyqtgraph as pg
28
29 from ..curves import ResultsCurve
30 from ..Qt import QtCore, QtWidgets
31 from ..widgets import (
32 PlotWidget,
33 )
34
35 log = logging.getLogger(__name__)
36 log.addHandler(logging.NullHandler())
37
38
39 class PlotterWindow(QtWidgets.QMainWindow):
40 """
41 A window for plotting experiment results. Should not be
42 instantiated directly, but only via the
43 :class:`~pymeasure.display.plotter.Plotter` class.
44
45 .. seealso::
46 Tutorial :ref:`tutorial-plotterwindow`
47 A tutorial and example code for using the Plotter and PlotterWindow.
48 .. attribute plot::
49 The `pyqtgraph.PlotItem`_ object for this window. Can be
50 accessed to further customise the plot view programmatically, e.g.,
51 display log-log or semi-log axes by default, change axis range, etc.
52 .. pyqtgraph.PlotItem: http://www.pyqtgraph.org/documentation/graphicsItems/plotitem.html
53 """
54
55 def __init__(self, plotter, refresh_time=0.1, linewidth=1, parent=None):
56 super().__init__(parent)
57 self.plotter = plotter
58 self.refresh_time = refresh_time
59 columns = plotter.results.procedure.DATA_COLUMNS
60
61 self.setWindowTitle('Results Plotter')
62 self.main = QtWidgets.QWidget(self)
63
64 vbox = QtWidgets.QVBoxLayout(self.main)
65 vbox.setSpacing(0)
66
67 hbox = QtWidgets.QHBoxLayout()
68 hbox.setSpacing(6)
69 hbox.setContentsMargins(-1, 6, -1, -1)
70
71 file_label = QtWidgets.QLabel(self.main)
72 file_label.setText('Data Filename:')
73
74 self.file = QtWidgets.QLineEdit(self.main)
75 self.file.setText(plotter.results.data_filename)
76
77 hbox.addWidget(file_label)
78 hbox.addWidget(self.file)
79 vbox.addLayout(hbox)
80
81 self.plot_widget = PlotWidget("Plotter", columns, refresh_time=self.refresh_time,
82 check_status=False, linewidth=linewidth)
83 self.plot = self.plot_widget.plot
84
85 vbox.addWidget(self.plot_widget)
86
87 self.main.setLayout(vbox)
88 self.setCentralWidget(self.main)
89 self.main.show()
90 self.resize(800, 600)
91
92 self.curve = ResultsCurve(plotter.results, columns[0], columns[1],
93 pen=pg.mkPen(color=pg.intColor(0), width=linewidth),
94 antialias=False)
95 self.plot.addItem(self.curve)
96
97 self.plot_widget.updated.connect(self.check_stop)
98
99 def quit(self, evt=None):
100 log.info("Quitting the Plotter")
101 self.close()
102 self.plotter.stop()
103
104 def check_stop(self):
105 """ Checks if the Plotter should stop and exits the Qt main loop if so
106 """
107 if self.plotter.should_stop():
108 QtCore.QCoreApplication.instance().quit()