Debian Codegraph @

pymeasure/display/browser.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 from os.path import basename
28
29 from .Qt import QtCore, QtGui, QtWidgets
30
31 from ..experiment import Procedure
32
33 log = logging.getLogger(__name__)
34 log.addHandler(logging.NullHandler())
35
36
37 class BaseBrowserItem:
38 """ Base class for an experiment's browser item. BaseBrowerItem outlines core functionality
39 for displaying progress of an experiment to the user.
40 """
41
42 status_label = {
43 Procedure.QUEUED: 'Queued', Procedure.RUNNING: 'Running',
44 Procedure.FAILED: 'Failed', Procedure.ABORTED: 'Aborted',
45 Procedure.FINISHED: 'Finished'}
46
47 def setStatus(self, status):
48 raise NotImplementedError('Must be reimplemented by subclasses')
49
50 def setProgress(self, status):
51 raise NotImplementedError('Must be reimplemented by subclasses')
52
53
54 class BrowserItem(QtWidgets.QTreeWidgetItem, BaseBrowserItem):
55 """ Represent a row in the :class:`~pymeasure.display.browser.Browser` tree widget """
56
57 def __init__(self, results, color, parent=None):
58 super().__init__(parent)
59
60 pixelmap = QtGui.QPixmap(24, 24)
61 pixelmap.fill(color)
62 self.setIcon(0, QtGui.QIcon(pixelmap))
63 self.setFlags(self.flags() | QtCore.Qt.ItemFlag.ItemIsUserCheckable)
64 self.setCheckState(0, QtCore.Qt.CheckState.Checked)
65 self.setText(1, basename(results.data_filename))
66
67 self.setStatus(results.procedure.status)
68
69 self.progressbar = QtWidgets.QProgressBar()
70 self.progressbar.setRange(0, 100)
71 self.progressbar.setValue(0)
72
73 def setStatus(self, status):
74 self.setText(3, self.status_label[status])
75
76 if status == Procedure.FAILED or status == Procedure.ABORTED:
77 # Set progress bar color to red
78 return # Commented this out
79 self.progressbar.setStyleSheet("""
80 QProgressBar {
81 border: 1px solid #AAAAAA;
82 border-radius: 5px;
83 text-align: center;
84 }
85 QProgressBar::chunk {
86 background-color: red;
87 }
88 """)
89
90 def setProgress(self, progress):
91 self.progressbar.setValue(int(progress))
92
93
94 class Browser(QtWidgets.QTreeWidget):
95 """Graphical list view of :class:`Experiment<pymeasure.display.manager.Experiment>`
96 objects allowing the user to view the status of queued Experiments as well as
97 loading and displaying data from previous runs.
98
99 In order that different Experiments be displayed within the same Browser,
100 they must have entries in `DATA_COLUMNS` corresponding to the
101 `measured_quantities` of the Browser.
102 """
103
104 def __init__(self, procedure_class, display_parameters,
105 measured_quantities, sort_by_filename=False, parent=None):
106 super().__init__(parent)
107 self.display_parameters = display_parameters
108 self.procedure_class = procedure_class
109 self.measured_quantities = set(measured_quantities)
110
111 header_labels = ["Graph", "Filename", "Progress", "Status"]
112 for parameter in self.display_parameters:
113 header_labels.append(getattr(self.procedure_class, parameter).name)
114
115 self.setColumnCount(len(header_labels))
116 self.setHeaderLabels(header_labels)
117 self.setSortingEnabled(True)
118 if sort_by_filename:
119 self.sortItems(1, QtCore.Qt.SortOrder.AscendingOrder)
120
121 for i, width in enumerate([80, 140]):
122 self.header().resizeSection(i, width)
123
124 def add(self, experiment):
125 """Add a :class:`Experiment<pymeasure.display.manager.Experiment>` object
126 to the Browser. This function checks to make sure that the Experiment
127 measures the appropriate quantities to warrant its inclusion, and then
128 adds a BrowserItem to the Browser, filling all relevant columns with
129 Parameter data.
130 """
131 experiment_parameters = experiment.procedure.parameter_objects()
132 experiment_parameter_names = list(experiment_parameters.keys())
133
134 for measured_quantity in self.measured_quantities:
135 if measured_quantity not in experiment.procedure.DATA_COLUMNS:
136 raise Exception("Procedure does not measure the"
137 " %s quantity." % measured_quantity)
138
139 # Set the relevant fields within the BrowserItem if
140 # that Parameter is implemented
141 item = experiment.browser_item
142 for i, column in enumerate(self.display_parameters):
143 if column in experiment_parameter_names:
144 item.setText(i + 4, str(experiment_parameters[column]))
145
146 self.addTopLevelItem(item)
147 self.setItemWidget(item, 2, item.progressbar)
148 return item