Newer
Older
# Mantid Repository : https://github.com/mantidproject/mantid
#
# Copyright © 2019 ISIS Rutherford Appleton Laboratory UKRI,
# NScD Oak Ridge National Laboratory, European Spallation Source
# & Institut Laue - Langevin
# SPDX - License - Identifier: GPL - 3.0 +
# This file is part of the mantid workbench.
from __future__ import (absolute_import, unicode_literals)
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
from mantidqt.widgets.plotconfigdialog import get_axes_names_dict
from mantidqt.widgets.plotconfigdialog.curvestabwidget import CurveProperties
from mantidqt.widgets.plotconfigdialog.curvestabwidget.curvestabwidgetview import CurvesTabWidgetView
class BlockQSignals:
def __init__(self, qobject):
self.qobject = qobject
def __enter__(self):
self.qobject.blockSignals(True)
def __exit__(self, exc_type, exc_val, exc_tb):
self.qobject.blockSignals(False)
class CurvesTabWidgetPresenter:
def __init__(self, fig, view=None, parent=None):
self.fig = fig
if not view:
self.view = CurvesTabWidgetView(parent)
else:
self.view = view
self.axes_names_dict = get_axes_names_dict(self.fig)
self.populate_select_axes_combo_box()
self.populate_select_curve_combo_box()
self.set_selected_curve_view_properties()
# Signals
self.view.select_axes_combo_box.currentIndexChanged.connect(
self.populate_curve_combo_box_and_update_properties
)
self.view.select_curve_combo_box.currentIndexChanged.connect(
self.set_selected_curve_view_properties
)
self.view.remove_curve_button.clicked.connect(
self.remove_selected_curve
)
def apply_properties(self):
props = self.view.get_properties()
line = self.get_selected_curve()
line.set_visible(not props.hide_curve)
self.set_curve_label(line, props.label)
def set_curve_label(self, line, label):
line.set_label(label)
self.view.set_current_curve_selector_text(label)
def remove_selected_curve(self):
self.get_selected_curve().remove()
ax = self.get_selected_ax()
ax.figure.canvas.draw()
with BlockQSignals(self.view.select_curve_combo_box):
self.view.remove_select_curve_combo_box_current_item()
if self.view.select_curve_combo_box.count() == 0:
self.view.remove_select_axes_combo_box_current_item()
def clear_select_curves_combo_box_signal_blocking(self):
self.view.select_curve_combo_box.blockSignals(True)
self.view.select_curve_combo_box.clear()
self.view.select_curve_combo_box.blockSignals(False)
def get_selected_ax(self):
try:
return self.axes_names_dict[self.view.get_selected_ax_name()]
except KeyError:
return None
def get_selected_curve(self):
for curve in self.get_selected_ax().get_lines():
if curve.get_label() == self.view.get_selected_curve_name():
return curve
def get_selected_curve_properties(self):
return CurveProperties.from_curve(self.get_selected_curve())
def populate_select_axes_combo_box(self):
# Sort names by axes position
names = sorted(self.axes_names_dict.keys(),
key=lambda x: x[x.rfind("("):])
self.view.populate_select_axes_combo_box(names)
def populate_select_curve_combo_box(self):
with BlockQSignals(self.view.select_curve_combo_box):
self.clear_select_curves_combo_box_signal_blocking()
curve_names = []
selected_ax = self.get_selected_ax()
if not selected_ax:
self.view.close()
return False
lines = self.get_selected_ax().get_lines()
for line in lines:
curve_names.append(line.get_label())
self.view.populate_select_curve_combo_box(curve_names)
def populate_curve_combo_box_and_update_properties(self):
if self.populate_select_curve_combo_box():
self.set_selected_curve_view_properties()
def set_selected_curve_view_properties(self):
self.view.set_properties(self.get_selected_curve_properties())