Debian Codegraph @

pymeasure/display/widgets/image_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 from ..curves import ResultsImage
29 from ..Qt import QtCore, QtWidgets
30 from .tab_widget import TabWidget
31 from .image_frame import ImageFrame
32
33 log = logging.getLogger(__name__)
34 log.addHandler(logging.NullHandler())
35
36
37 class ImageWidget(TabWidget, QtWidgets.QWidget):
38 """ Extends the :class:`ImageFrame<pymeasure.display.widgets.image_frame.ImageFrame>`
39 to allow different columns of the data to be dynamically chosen
40 """
41
42 def __init__(self, name, columns, x_axis, y_axis, z_axis=None, refresh_time=0.2,
43 check_status=True, parent=None):
44 super().__init__(name, parent)
45 self.columns = columns
46 self.refresh_time = refresh_time
47 self.check_status = check_status
48 self.x_axis = x_axis
49 self.y_axis = y_axis
50 self._setup_ui()
51 self._layout()
52 if z_axis is not None:
53 self.columns_z.setCurrentIndex(self.columns_z.findText(z_axis))
54 self.image_frame.change_z_axis(z_axis)
55
56 def _setup_ui(self):
57 self.columns_z_label = QtWidgets.QLabel(self)
58 self.columns_z_label.setMaximumSize(QtCore.QSize(45, 16777215))
59 self.columns_z_label.setText('Z Axis:')
60
61 self.columns_z = QtWidgets.QComboBox(self)
62 for column in self.columns:
63 self.columns_z.addItem(column)
64 self.columns_z.activated.connect(self.update_z_column)
65
66 self.image_frame = ImageFrame(
67 self.x_axis,
68 self.y_axis,
69 self.columns[0],
70 self.refresh_time,
71 self.check_status
72 )
73 self.updated = self.image_frame.updated
74 self.plot = self.image_frame.plot
75 self.columns_z.setCurrentIndex(2)
76
77 def _layout(self):
78 vbox = QtWidgets.QVBoxLayout(self)
79 vbox.setSpacing(0)
80
81 hbox = QtWidgets.QHBoxLayout()
82 hbox.setSpacing(10)
83 hbox.setContentsMargins(-1, 6, -1, 6)
84 hbox.addWidget(self.columns_z_label)
85 hbox.addWidget(self.columns_z)
86
87 vbox.addLayout(hbox)
88 vbox.addWidget(self.image_frame)
89 self.setLayout(vbox)
90
91 def sizeHint(self):
92 return QtCore.QSize(300, 600)
93
94 def new_curve(self, results, color=pg.intColor(0), **kwargs):
95 """ Creates a new image """
96 image = ResultsImage(results,
97 wdg=self,
98 x=self.image_frame.x_axis,
99 y=self.image_frame.y_axis,
100 z=self.image_frame.z_axis,
101 **kwargs
102 )
103 return image
104
105 def update_z_column(self, index):
106 axis = self.columns_z.itemText(index)
107 self.image_frame.change_z_axis(axis)
108
109 def load(self, curve):
110 curve.z = self.columns_z.currentText()
111 curve.update_data()
112 self.plot.addItem(curve)
113
114 def remove(self, curve):
115 self.plot.removeItem(curve)