| 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 |
import logging |
| 25 |
|
| 26 |
from os import path |
| 27 |
import json |
| 28 |
|
| 29 |
from pyqtgraph.dockarea import Dock, DockArea |
| 30 |
from pyqtgraph.dockarea.Dock import DockLabel |
| 31 |
import pyqtgraph as pg |
| 32 |
|
| 33 |
from .plot_widget import PlotWidget, PlotFrame |
| 34 |
from ..Qt import QtWidgets |
| 35 |
from .tab_widget import TabWidget |
| 36 |
|
| 37 |
log = logging.getLogger(__name__) |
| 38 |
log.addHandler(logging.NullHandler()) |
| 39 |
|
| 40 |
|
| 41 |
class DockWidget(TabWidget, QtWidgets.QWidget): |
| 42 |
""" |
| 43 |
Widget that contains a DockArea with a number of Docks as determined by the length of |
| 44 |
the longest x_axis_labels or y_axis_labels list. |
| 45 |
|
| 46 |
:param name: Name for the TabWidget |
| 47 |
:param procedure_class: procedure class describing the experiment (see |
| 48 |
:class:`~pymeasure.experiment.procedure.Procedure`) |
| 49 |
:param x_axis_labels: List of data column(s) for the x-axis of the plot. If the list is shorter |
| 50 |
than y_axis_labels the last item in the list to match y_axis_labels length. |
| 51 |
:param y_axis_labels: List of data column(s) for the y-axis of the plot. If the list is shorter |
| 52 |
than x_axis_labels the last item in the list to match x_axis_labels length. |
| 53 |
:param linewidth: line width for plots in |
| 54 |
:class:`~pymeasure.display.widgets.plot_widget.PlotWidget` |
| 55 |
:param layout_path: Directory path to save dock layout state. Default is './' |
| 56 |
:param layout_filename: Optional filename for dock layout file. |
| 57 |
Default: *current procedure class* + "_dock_layout.json" |
| 58 |
:param parent: Passed on to QtWidgets.QWidget. Default is None |
| 59 |
""" |
| 60 |
|
| 61 |
def __init__(self, name, procedure_class, x_axis_labels=None, y_axis_labels=None, linewidth=1, |
| 62 |
layout_path='./', layout_filename='', parent=None): |
| 63 |
super().__init__(name, parent) |
| 64 |
|
| 65 |
self.procedure_class = procedure_class |
| 66 |
if layout_filename: |
| 67 |
self.dock_layout_filename = path.join(layout_path, layout_filename) |
| 68 |
else: |
| 69 |
self.dock_layout_filename = path.join(layout_path, |
| 70 |
procedure_class.__name__ + '_dock_layout.json') |
| 71 |
self.x_axis_labels = x_axis_labels |
| 72 |
self.y_axis_labels = y_axis_labels |
| 73 |
self.num_plots = max(len(self.x_axis_labels), len(self.y_axis_labels)) |
| 74 |
self.linewidth = linewidth |
| 75 |
|
| 76 |
self.dock_area = DockArea() |
| 77 |
self.docks = [] |
| 78 |
self.plot_frames = [] |
| 79 |
|
| 80 |
self._setup_ui() |
| 81 |
self._layout() |
| 82 |
|
| 83 |
def save_dock_layout(self): |
| 84 |
""" |
| 85 |
Save the current layout of the docks and the plot settings. |
| 86 |
When running the GUI you can access this function by right-clicking in the |
| 87 |
widget area to bring up the context menu and selecting "Save Dock Layout" |
| 88 |
""" |
| 89 |
|
| 90 |
layout = { |
| 91 |
'docks': self.dock_area.saveState(), |
| 92 |
'plots': [i.plot_frame.plot_widget.saveState() for i in self.plot_frames] |
| 93 |
} |
| 94 |
with open(self.dock_layout_filename, 'w') as f: |
| 95 |
f.write(json.dumps(layout)) |
| 96 |
log.info('Saved dock layout to file %s' % self.dock_layout_filename) |
| 97 |
|
| 98 |
def save_dock_action(self): |
| 99 |
save_dock_action = QtWidgets.QWidgetAction(self) |
| 100 |
save_dock_action.setText("Save Dock Layout") |
| 101 |
save_dock_action.triggered.connect(self.save_dock_layout) |
| 102 |
return save_dock_action |
| 103 |
|
| 104 |
def contextMenuEvent(self, event): |
| 105 |
position = event.pos() |
| 106 |
# Create menu outside pyqtgraph.PlotWidget position |
| 107 |
if isinstance(self.childAt(position), (PlotWidget, DockLabel, QtWidgets.QLabel, PlotFrame)): |
| 108 |
menu = QtWidgets.QMenu(self) |
| 109 |
menu.addAction(self.save_dock_action()) |
| 110 |
menu.exec(self.mapToGlobal(position)) |
| 111 |
|
| 112 |
def _setup_ui(self): |
| 113 |
for i in range(self.num_plots): |
| 114 |
# Set the default label for current dock from x_axis_labels and y_axis_labels |
| 115 |
# However, if list is shorter than num_plots, repeat last item in the list. |
| 116 |
x_label = self.x_axis_labels[min(i, len(self.x_axis_labels) - 1)] |
| 117 |
y_label = self.y_axis_labels[min(i, len(self.y_axis_labels) - 1)] |
| 118 |
|
| 119 |
dock = Dock("Dock " + str(i + 1), closable=False, size=(200, 50)) |
| 120 |
self.dock_area.addDock(dock) |
| 121 |
self.plot_frames.append( |
| 122 |
PlotWidget("Results Graph", self.procedure_class.DATA_COLUMNS, x_label, |
| 123 |
y_label, linewidth=self.linewidth)) |
| 124 |
self.plot_frames[i].plot_frame.plot_widget.scene().contextMenu.append( |
| 125 |
self.save_dock_action()) |
| 126 |
dock.addWidget(self.plot_frames[i]) |
| 127 |
self.docks.append(dock) |
| 128 |
|
| 129 |
def _layout(self): |
| 130 |
|
| 131 |
vbox = QtWidgets.QVBoxLayout(self) |
| 132 |
vbox.setSpacing(0) |
| 133 |
vbox.addWidget(self.dock_area) |
| 134 |
self.setLayout(vbox) |
| 135 |
|
| 136 |
# Load dock layout file if it exists in the directory of the current procedure |
| 137 |
if path.exists(self.dock_layout_filename): |
| 138 |
with open(self.dock_layout_filename, 'r') as f: |
| 139 |
dock_layout = f.read() |
| 140 |
layout = json.loads(dock_layout) |
| 141 |
docks = layout['docks'] |
| 142 |
plots = layout['plots'] |
| 143 |
# Make sure number of plots in the file matches num_plots |
| 144 |
if len(plots) == self.num_plots: |
| 145 |
self.dock_area.restoreState(docks) |
| 146 |
for idx, i in enumerate(self.plot_frames): |
| 147 |
i.plot_frame.plot_widget.restoreState(plots[idx]) |
| 148 |
log.info('Loaded dock layout from file %s' % self.dock_layout_filename) |
| 149 |
else: |
| 150 |
log.warning( |
| 151 |
'Number of displayed docks does not match number of docks in layout file %s' |
| 152 |
% self.dock_layout_filename) |
| 153 |
|
| 154 |
def new_curve(self, results, color=pg.intColor(0), **kwargs): |
| 155 |
if 'pen' not in kwargs: |
| 156 |
kwargs['pen'] = pg.mkPen(color=color, width=self.linewidth) |
| 157 |
if 'antialias' not in kwargs: |
| 158 |
kwargs['antialias'] = False |
| 159 |
curves = [] |
| 160 |
for i in range(self.num_plots): |
| 161 |
curves.append(self.plot_frames[i].new_curve(results, color=color, **kwargs)) |
| 162 |
return curves |
| 163 |
|
| 164 |
def clear(self): |
| 165 |
for i in range(self.num_plots): |
| 166 |
self.plot_frames[i].plot.clear() |