-
Notifications
You must be signed in to change notification settings - Fork 122
/
view.py
239 lines (183 loc) · 9.69 KB
/
view.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# 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
# SPDX - License - Identifier: GPL - 3.0 +
# This file is part of the mantidqt package.
from __future__ import (absolute_import, division, print_function)
import sys
from functools import partial
from qtpy import QtGui
from qtpy.QtCore import QVariant, Qt, Signal, Slot
from qtpy.QtGui import QKeySequence
from qtpy.QtWidgets import (QAction, QHeaderView, QItemEditorFactory, QMenu, QMessageBox,
QStyledItemDelegate, QTableWidget)
import mantidqt.icons
from mantidqt.widgets.workspacedisplay.table.plot_type import PlotType
class PreciseDoubleFactory(QItemEditorFactory):
def __init__(self):
QItemEditorFactory.__init__(self)
def createEditor(self, user_type, parent):
widget = super(PreciseDoubleFactory, self).createEditor(user_type, parent)
if user_type == QVariant.Double:
widget.setFrame(True)
widget.setDecimals(16)
widget.setRange(sys.float_info.min, sys.float_info.max)
return widget
class TableWorkspaceDisplayView(QTableWidget):
repaint_signal = Signal()
def __init__(self, presenter, parent=None):
super(TableWorkspaceDisplayView, self).__init__(parent)
self.presenter = presenter
self.COPY_ICON = mantidqt.icons.get_icon("fa.files-o")
self.DELETE_ROW = mantidqt.icons.get_icon("fa.minus-square-o")
self.STATISTICS_ON_ROW = mantidqt.icons.get_icon('fa.fighter-jet')
self.GRAPH_ICON = mantidqt.icons.get_icon('fa.line-chart')
self.TBD = mantidqt.icons.get_icon('fa.question')
item_delegate = QStyledItemDelegate(self)
item_delegate.setItemEditorFactory(PreciseDoubleFactory())
self.setItemDelegate(item_delegate)
self.setAttribute(Qt.WA_DeleteOnClose, True)
self.repaint_signal.connect(self._run_repaint)
header = self.horizontalHeader()
header.sectionDoubleClicked.connect(self.handle_double_click)
def resizeEvent(self, event):
QTableWidget.resizeEvent(self, event)
header = self.horizontalHeader()
# resizes the column headers to fit the contents,
# currently this overwrites any manual changes to the widths of the columns
header.resizeSections(QHeaderView.ResizeToContents)
# then allows the users to resize the headers manually
header.setSectionResizeMode(QHeaderView.Interactive)
def emit_repaint(self):
self.repaint_signal.emit()
@Slot()
def _run_repaint(self):
self.viewport().update()
def handle_double_click(self, section):
header = self.horizontalHeader()
header.resizeSection(section, header.defaultSectionSize())
def keyPressEvent(self, event):
if event.matches(QKeySequence.Copy):
self.presenter.action_keypress_copy()
return
elif event.key() == Qt.Key_F2 or event.key() == Qt.Key_Return or event.key() == Qt.Key_Enter:
self.edit(self.currentIndex())
return
def set_context_menu_actions(self, table):
"""
Sets up the context menu actions for the table
:type table: QTableView
:param table: The table whose context menu actions will be set up.
:param ws_read_function: The read function used to efficiently retrieve data directly from the workspace
"""
copy_action = QAction(self.COPY_ICON, "Copy", table)
copy_action.triggered.connect(self.presenter.action_copy_cells)
table.setContextMenuPolicy(Qt.ActionsContextMenu)
table.addAction(copy_action)
horizontalHeader = table.horizontalHeader()
horizontalHeader.setContextMenuPolicy(Qt.CustomContextMenu)
horizontalHeader.customContextMenuRequested.connect(self.custom_context_menu)
verticalHeader = table.verticalHeader()
verticalHeader.setContextMenuPolicy(Qt.ActionsContextMenu)
verticalHeader.setSectionResizeMode(QHeaderView.Fixed)
copy_spectrum_values = QAction(self.COPY_ICON, "Copy", verticalHeader)
copy_spectrum_values.triggered.connect(self.presenter.action_copy_spectrum_values)
delete_row = QAction(self.DELETE_ROW, "Delete Row", verticalHeader)
delete_row.triggered.connect(self.presenter.action_delete_row)
separator2 = self.make_separator(verticalHeader)
verticalHeader.addAction(copy_spectrum_values)
verticalHeader.addAction(separator2)
verticalHeader.addAction(delete_row)
def custom_context_menu(self, position):
menu_main = QMenu()
plot = QMenu("Plot...", menu_main)
plot_line = QAction("Line", plot)
plot_line.triggered.connect(partial(self.presenter.action_plot, PlotType.LINEAR))
plot_line_with_yerr = QAction("Line with Y Errors", plot)
plot_line_with_yerr.triggered.connect(partial(self.presenter.action_plot, PlotType.LINEAR_WITH_ERR))
plot_scatter = QAction("Scatter", plot)
plot_scatter.triggered.connect(partial(self.presenter.action_plot, PlotType.SCATTER))
plot_scatter_with_yerr = QAction("Scatter with Y Errors", plot)
plot_scatter_with_yerr.triggered.connect(partial(self.presenter.action_plot, PlotType.SCATTER_WITH_ERR))
plot_line_and_points = QAction("Line + Symbol", plot)
plot_line_and_points.triggered.connect(partial(self.presenter.action_plot, PlotType.LINE_AND_SYMBOL))
plot.addAction(plot_line)
plot.addAction(plot_line_with_yerr)
plot.addAction(plot_scatter)
plot.addAction(plot_scatter_with_yerr)
plot.addAction(plot_line_and_points)
menu_main.addMenu(plot)
copy_bin_values = QAction(self.COPY_ICON, "Copy", menu_main)
copy_bin_values.triggered.connect(self.presenter.action_copy_bin_values)
set_as_x = QAction("Set as X", menu_main)
set_as_x.triggered.connect(self.presenter.action_set_as_x)
set_as_y = QAction("Set as Y", menu_main)
set_as_y.triggered.connect(self.presenter.action_set_as_y)
set_as_none = QAction("Set as None", menu_main)
set_as_none.triggered.connect(self.presenter.action_set_as_none)
statistics_on_columns = QAction("Statistics on Columns", menu_main)
statistics_on_columns.triggered.connect(self.presenter.action_statistics_on_columns)
hide_selected = QAction("Hide Selected", menu_main)
hide_selected.triggered.connect(self.presenter.action_hide_selected)
show_all_columns = QAction("Show All Columns", menu_main)
show_all_columns.triggered.connect(self.presenter.action_show_all_columns)
sort_ascending = QAction("Sort Ascending", menu_main)
sort_ascending.triggered.connect(partial(self.presenter.action_sort, True))
sort_descending = QAction("Sort Descending", menu_main)
sort_descending.triggered.connect(partial(self.presenter.action_sort, False))
menu_main.addAction(copy_bin_values)
menu_main.addAction(self.make_separator(menu_main))
menu_main.addAction(set_as_x)
menu_main.addAction(set_as_y)
marked_y_cols = self.presenter.get_columns_marked_as_y()
num_y_cols = len(marked_y_cols)
# If any columns are marked as Y then generate the set error menu
if num_y_cols > 0:
menu_set_as_y_err = QMenu("Set error for Y...")
for label_index in range(num_y_cols):
set_as_y_err = QAction("Y{}".format(label_index), menu_main)
# This is the column index of the Y column for which a YERR column is being added.
# The column index is relative to the whole table, this is necessary
# so that later the data of the column marked as error can be retrieved
related_y_column = marked_y_cols[label_index]
# label_index here holds the index in the LABEL (multiple Y columns have labels Y0, Y1, YN...)
# this is NOT the same as the column relative to the WHOLE table
set_as_y_err.triggered.connect(
partial(self.presenter.action_set_as_y_err, related_y_column, label_index))
menu_set_as_y_err.addAction(set_as_y_err)
menu_main.addMenu(menu_set_as_y_err)
menu_main.addAction(set_as_none)
menu_main.addAction(self.make_separator(menu_main))
menu_main.addAction(statistics_on_columns)
menu_main.addAction(self.make_separator(menu_main))
menu_main.addAction(hide_selected)
menu_main.addAction(show_all_columns)
menu_main.addAction(self.make_separator(menu_main))
menu_main.addAction(sort_ascending)
menu_main.addAction(sort_descending)
menu_main.exec_(self.mapToGlobal(position))
def make_separator(self, horizontalHeader):
separator1 = QAction(horizontalHeader)
separator1.setSeparator(True)
return separator1
@staticmethod
def copy_to_clipboard(data):
"""
Uses the QGuiApplication to copy to the system clipboard.
:type data: str
:param data: The data that will be copied to the clipboard
:return:
"""
cb = QtGui.QGuiApplication.clipboard()
cb.setText(data, mode=cb.Clipboard)
def ask_confirmation(self, message, title="Mantid Workbench"):
"""
:param message:
:return:
"""
reply = QMessageBox.question(self, title, message, QMessageBox.Yes, QMessageBox.No)
return True if reply == QMessageBox.Yes else False
def show_warning(self, message, title="Mantid Workbench"):
QMessageBox.warning(self, title, message)