Debian Codegraph @

pymeasure/display/widgets/plot_widget.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 .tab_widget import TabWidget
32 from .plot_frame import PlotFrame
33
34 log = logging.getLogger(__name__)
35 log.addHandler(logging.NullHandler())
36
37
38 class PlotWidget(TabWidget, QtWidgets.QWidget):
39 """ Extends :class:`PlotFrame<pymeasure.display.widgets.plot_frame.PlotFrame>`
40 to allow different columns of the data to be dynamically chosen
41 """
42
43 def __init__(self, name, columns, x_axis=None, y_axis=None, refresh_time=0.2,
44 check_status=True, linewidth=1, parent=None):
45 super().__init__(name, parent)
46 self.columns = columns
47 self.refresh_time = refresh_time
48 self.check_status = check_status
49 self.linewidth = linewidth
50 self._setup_ui()
51 self._layout()
52 if x_axis is not None:
53 self.columns_x.setCurrentIndex(self.columns_x.findText(x_axis))
54 self.plot_frame.change_x_axis(x_axis)
55 if y_axis is not None:
56 self.columns_y.setCurrentIndex(self.columns_y.findText(y_axis))
57 self.plot_frame.change_y_axis(y_axis)
58
59 def _setup_ui(self):
60 self.columns_x_label = QtWidgets.QLabel(self)
61 self.columns_x_label.setMaximumSize(QtCore.QSize(45, 16777215))
62 self.columns_x_label.setText('X Axis:')
63 self.columns_y_label = QtWidgets.QLabel(self)
64 self.columns_y_label.setMaximumSize(QtCore.QSize(45, 16777215))
65 self.columns_y_label.setText('Y Axis:')
66
67 self.columns_x = QtWidgets.QComboBox(self)
68 self.columns_y = QtWidgets.QComboBox(self)
69 for column in self.columns:
70 self.columns_x.addItem(column)
71 self.columns_y.addItem(column)
72 self.columns_x.activated.connect(self.update_x_column)
73 self.columns_y.activated.connect(self.update_y_column)
74
75 self.plot_frame = PlotFrame(
76 self.columns[0],
77 self.columns[1],
78 self.refresh_time,
79 self.check_status,
80 parent=self,
81 )
82 self.updated = self.plot_frame.updated
83 self.plot = self.plot_frame.plot
84 self.columns_x.setCurrentIndex(0)
85 self.columns_y.setCurrentIndex(1)
86
87 def _layout(self):
88 vbox = QtWidgets.QVBoxLayout(self)
89 vbox.setSpacing(0)
90
91 hbox = QtWidgets.QHBoxLayout()
92 hbox.setSpacing(10)
93 hbox.setContentsMargins(-1, 6, -1, 6)
94 hbox.addWidget(self.columns_x_label)
95 hbox.addWidget(self.columns_x)
96 hbox.addWidget(self.columns_y_label)
97 hbox.addWidget(self.columns_y)
98
99 vbox.addLayout(hbox)
100 vbox.addWidget(self.plot_frame)
101 self.setLayout(vbox)
102
103 def sizeHint(self):
104 return QtCore.QSize(300, 600)
105
106 def new_curve(self, results, color=pg.intColor(0), **kwargs):
107 if 'pen' not in kwargs:
108 kwargs['pen'] = pg.mkPen(color=color, width=self.linewidth)
109 if 'antialias' not in kwargs:
110 kwargs['antialias'] = False
111 curve = ResultsCurve(results,
112 wdg=self,
113 x=self.plot_frame.x_axis,
114 y=self.plot_frame.y_axis,
115 **kwargs,
116 )
117 curve.setSymbol(None)
118 curve.setSymbolBrush(None)
119 return curve
120
121 def update_x_column(self, index):
122 axis = self.columns_x.itemText(index)
123 self.plot_frame.change_x_axis(axis)
124
125 def update_y_column(self, index):
126 axis = self.columns_y.itemText(index)
127 self.plot_frame.change_y_axis(axis)
128
129 def load(self, curve):
130 curve.x = self.columns_x.currentText()
131 curve.y = self.columns_y.currentText()
132 curve.update_data()
133 self.plot.addItem(curve)
134
135 def remove(self, curve):
136 self.plot.removeItem(curve)
137
138 def set_color(self, curve, color):
139 """ Change the color of the pen of the curve """
140 curve.set_color(color)
141
142 def preview_widget(self, parent=None):
143 """ Return a widget suitable for preview during loading """
144 return PlotWidget("Plot preview",
145 self.columns,
146 self.plot_frame.x_axis,
147 self.plot_frame.y_axis,
148 parent=parent,
149 )
150
151 def clear_widget(self):
152 self.plot.clear()