1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
#
# This file is part of the PyMeasure package.
#
# Copyright (c) 2013-2024 PyMeasure Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
import logging
from os.path import basename
from .Qt import QtCore, QtGui, QtWidgets
from ..experiment import Procedure
log = logging.getLogger(__name__)
log.addHandler(logging.NullHandler())
class BaseBrowserItem:
""" Base class for an experiment's browser item. BaseBrowerItem outlines core functionality
for displaying progress of an experiment to the user.
"""
status_label = {
Procedure.QUEUED: 'Queued', Procedure.RUNNING: 'Running',
Procedure.FAILED: 'Failed', Procedure.ABORTED: 'Aborted',
Procedure.FINISHED: 'Finished'}
def setStatus(self, status):
raise NotImplementedError('Must be reimplemented by subclasses')
def setProgress(self, status):
raise NotImplementedError('Must be reimplemented by subclasses')
class BrowserItem(QtWidgets.QTreeWidgetItem, BaseBrowserItem):
""" Represent a row in the :class:`~pymeasure.display.browser.Browser` tree widget """
def __init__(self, results, color, parent=None):
super().__init__(parent)
pixelmap = QtGui.QPixmap(24, 24)
pixelmap.fill(color)
self.setIcon(0, QtGui.QIcon(pixelmap))
self.setFlags(self.flags() | QtCore.Qt.ItemFlag.ItemIsUserCheckable)
self.setCheckState(0, QtCore.Qt.CheckState.Checked)
self.setText(1, basename(results.data_filename))
self.setStatus(results.procedure.status)
self.progressbar = QtWidgets.QProgressBar()
self.progressbar.setRange(0, 100)
self.progressbar.setValue(0)
def setStatus(self, status):
self.setText(3, self.status_label[status])
if status == Procedure.FAILED or status == Procedure.ABORTED:
# Set progress bar color to red
return # Commented this out
self.progressbar.setStyleSheet("""
QProgressBar {
border: 1px solid #AAAAAA;
border-radius: 5px;
text-align: center;
}
QProgressBar::chunk {
background-color: red;
}
""")
def setProgress(self, progress):
self.progressbar.setValue(int(progress))
class Browser(QtWidgets.QTreeWidget):
"""Graphical list view of :class:`Experiment<pymeasure.display.manager.Experiment>`
objects allowing the user to view the status of queued Experiments as well as
loading and displaying data from previous runs.
In order that different Experiments be displayed within the same Browser,
they must have entries in `DATA_COLUMNS` corresponding to the
`measured_quantities` of the Browser.
"""
def __init__(self, procedure_class, display_parameters,
measured_quantities, sort_by_filename=False, parent=None):
super().__init__(parent)
self.display_parameters = display_parameters
self.procedure_class = procedure_class
self.measured_quantities = set(measured_quantities)
header_labels = ["Graph", "Filename", "Progress", "Status"]
for parameter in self.display_parameters:
header_labels.append(getattr(self.procedure_class, parameter).name)
self.setColumnCount(len(header_labels))
self.setHeaderLabels(header_labels)
self.setSortingEnabled(True)
if sort_by_filename:
self.sortItems(1, QtCore.Qt.SortOrder.AscendingOrder)
for i, width in enumerate([80, 140]):
self.header().resizeSection(i, width)
def add(self, experiment):
"""Add a :class:`Experiment<pymeasure.display.manager.Experiment>` object
to the Browser. This function checks to make sure that the Experiment
measures the appropriate quantities to warrant its inclusion, and then
adds a BrowserItem to the Browser, filling all relevant columns with
Parameter data.
"""
experiment_parameters = experiment.procedure.parameter_objects()
experiment_parameter_names = list(experiment_parameters.keys())
for measured_quantity in self.measured_quantities:
if measured_quantity not in experiment.procedure.DATA_COLUMNS:
raise Exception("Procedure does not measure the"
" %s quantity." % measured_quantity)
# Set the relevant fields within the BrowserItem if
# that Parameter is implemented
item = experiment.browser_item
for i, column in enumerate(self.display_parameters):
if column in experiment_parameter_names:
item.setText(i + 4, str(experiment_parameters[column]))
self.addTopLevelItem(item)
self.setItemWidget(item, 2, item.progressbar)
return item
|