forked from npshub/mantid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi_plotting_widget.py
217 lines (180 loc) · 7.96 KB
/
multi_plotting_widget.py
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source,
# Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
# SPDX - License - Identifier: GPL - 3.0 +
from qtpy import QtWidgets, QtCore
from copy import deepcopy
from MultiPlotting.subplot.subplot import subplot
from MultiPlotting.QuickEdit.quickEdit_widget import QuickEditWidget
from MultiPlotting.multi_plotting_context import *
class MultiPlotWindow(QtWidgets.QMainWindow):
windowClosedSignal = QtCore.Signal()
def __init__(self, window_title="plotting"):
super(MultiPlotWindow, self).__init__()
self.plot_context = PlottingContext()
self.multi_plot = MultiPlotWidget(self.plot_context, self)
self.setCentralWidget(self.multi_plot)
self.setWindowTitle(window_title)
def set_window_title(self, window_title):
self.setWindowTitle(window_title)
def closeEvent(self, event):
self.multi_plot.removeSubplotDisonnect()
self.multi_plot.plots._ADSObserver.unsubscribe()
self.multi_plot.plots._ADSObserver = None
self.windowClosedSignal.emit()
class MultiPlotWidget(QtWidgets.QWidget):
closeSignal = QtCore.Signal()
def __init__(self, context, parent=None):
super(MultiPlotWidget, self).__init__()
self._context = context
layout = QtWidgets.QVBoxLayout()
splitter = QtWidgets.QSplitter(QtCore.Qt.Vertical)
self.quickEdit = QuickEditWidget(self, auto_btn = True)
self.quickEdit.connect_x_range_changed(self._x_range_changed)
self.quickEdit.connect_y_range_changed(self._y_range_changed)
self.quickEdit.connect_errors_changed(self._errors_changed)
self.quickEdit.connect_autoscale_changed(self._autoscale_changed)
self.quickEdit.connect_plot_selection(self._selection_changed)
# add some dummy plot
self.plots = subplot(self._context)
self.plots.connect_quick_edit_signal(self._update_quick_edit)
self.plots.connect_rm_subplot_signal(self._update_quick_edit)
# create GUI layout
splitter.addWidget(self.plots)
splitter.addWidget(self.quickEdit.widget)
layout.addWidget(splitter)
self.setLayout(layout)
""" plotting """
def add_subplot(self, name):
self.plots.add_subplot(name, len(self.quickEdit.get_subplots()))
self.quickEdit.add_subplot(name)
def plot(self, subplot_name, ws, color=None, spec_num=1):
self.plots.plot(subplot_name, ws, color=color, spec_num=spec_num)
def remove_subplot(self, name):
self.plots._remove_subplot(name)
def get_subplots(self):
return list(self._context.subplots.keys())
def add_vline_and_annotate(self, subplotName, xvalue, label, color):
self.add_annotate(subplotName, label)
self.add_vline(subplotName, xvalue, label.text, color)
def rm_vline_and_annotate(self, subplotName, name):
self.rm_annotate(subplotName, name)
self.rm_vline(subplotName, name)
def add_annotate(self, subplotName, label):
self.plots.add_annotate(subplotName, label)
def add_vline(self, subplotName, xvalue, name, color):
self.plots.add_vline(subplotName, xvalue, name, color)
def rm_annotate(self, subplotName, name):
self.plots.rm_annotate(subplotName, name)
def rm_vline(self, subplotName, name):
self.plots.rm_vline(subplotName, name)
def remove_line(self, subplot_name, ws_name, spec=1):
name = '{}: spec {}'.format(ws_name, spec)
self.plots.remove_lines(subplot_name, [name])
# gets initial values for quickEdit
def set_all_values(self):
names = self.quickEdit.get_selection()
xrange = list(self._context.subplots[names[0]].xbounds)
yrange = list(self._context.subplots[names[0]].ybounds)
for name in names:
xbounds = self._context.subplots[name].xbounds
ybounds = self._context.subplots[name].ybounds
if xrange[0] > xbounds[0]:
xrange[0] = deepcopy(xbounds[0])
if xrange[1] < xbounds[1]:
xrange[1] = deepcopy(xbounds[1])
if yrange[0] > ybounds[0]:
yrange[0] = deepcopy(ybounds[0])
if yrange[1] < ybounds[1]:
yrange[1] = deepcopy(ybounds[1])
self._context.set_xBounds(xrange)
self._context.set_yBounds(yrange)
self._x_range_changed(xrange)
self._y_range_changed(yrange)
# get tick boxes correct
errors = self._check_all_errors(names)
self.quickEdit.set_errors(errors)
self._change_errors(errors, names)
def connectCloseSignal(self, slot):
self.closeSignal.connect(slot)
def remove_subplot_connection(self, slot):
self.plots.connect_rm_subplot_signal(slot)
def remove_line_connection(self, slot):
self.plots.connect_rm_line_signal(slot)
def disconnectCloseSignal(self):
self.closeSignal.disconnect()
def removeSubplotDisonnect(self):
self.plots.disconnect_rm_subplot_signal()
""" update GUI """
def _if_empty_close(self):
if not self._context.subplots:
self.closeSignal.emit()
self.close()
def _update_quick_edit(self, subplotName):
names = self.quickEdit.get_selection()
if subplotName not in self._context.subplots.keys():
self.quickEdit.rm_subplot(subplotName)
self._if_empty_close()
return
xrange = self._context.subplots[subplotName].xbounds
yrange = self._context.subplots[subplotName].ybounds
if len(names) == 0:
return
# if all selected update everyone
if len(names) > 1:
self.quickEdit.set_plot_x_range(xrange)
self.quickEdit.set_plot_y_range(yrange)
# if changed current selection
elif names[0] == subplotName:
self.quickEdit.set_plot_x_range(xrange)
self.quickEdit.set_plot_y_range(yrange)
# if a different plot changed
else:
pass
def _selection_changed(self, index):
names = self.quickEdit.get_selection()
xrange = self._context.get_xBounds()
yrange = self._context.get_yBounds()
errors = True
if len(names) == 1:
xrange = self._context.subplots[names[0]].xbounds
yrange = self._context.subplots[names[0]].ybounds
errors = self._context.subplots[names[0]].errors
else:
errors = self._check_all_errors(names)
# update values
self.quickEdit.set_errors(errors)
self._change_errors(errors, names)
self.quickEdit.set_plot_x_range(xrange)
self.quickEdit.set_plot_y_range(yrange)
# force update of plots if selection is all
if len(names) > 1:
self._x_range_changed(xrange)
self._y_range_changed(yrange)
def _autoscale_changed(self, _):
names = self.quickEdit.get_selection()
self.plots.set_y_autoscale(names, True)
def _change_errors(self, state, names):
self.plots.change_errors(state, names)
def _errors_changed(self, state):
names = self.quickEdit.get_selection()
self._change_errors(state, names)
def _x_range_changed(self, xRange):
names = self.quickEdit.get_selection()
if len(names) > 1:
self._context.set_xBounds(xRange)
self.plots.set_plot_x_range(names, xRange)
self.quickEdit.set_plot_x_range(xRange)
def _y_range_changed(self, yRange):
names = self.quickEdit.get_selection()
if len(names) > 1:
self._context.set_yBounds(yRange)
self.plots.set_plot_y_range(names, yRange)
self.quickEdit.set_plot_y_range(yRange)
def _check_all_errors(self, names):
for name in names:
if self._context.subplots[name].errors is False:
return False
return True