Newer
Older
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
// Mantid Repository : https://github.com/mantidproject/mantid
//
// Copyright © 2020 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 +
#include "MantidQtWidgets/Common/FitScriptGeneratorDataTable.h"
#include <QAbstractItemView>
#include <QDoubleValidator>
#include <QHeaderView>
#include <QHoverEvent>
#include <QLineEdit>
#include <QStringList>
#include <QValidator>
namespace {
int DOUBLE_PRECISION(5);
QValidator *createDoubleValidator() {
auto validator = new QDoubleValidator(-100000.0, 100000.0, DOUBLE_PRECISION);
validator->setNotation(QDoubleValidator::Notation::StandardNotation);
return validator;
}
QValidator *createIntValidator() { return new QIntValidator(0, 100000); }
QTableWidgetItem *createTableItem(QString const &value,
Qt::AlignmentFlag const &alignment,
bool editable) {
auto item = new QTableWidgetItem(value);
item->setTextAlignment(alignment);
if (!editable)
item->setFlags(item->flags() ^ Qt::ItemIsEditable);
return item;
}
QTableWidgetItem *createIntTableItem(int value,
Qt::AlignmentFlag const &alignment,
bool editable) {
return createTableItem(QString::number(value), alignment, editable);
}
QTableWidgetItem *createDoubleTableItem(double value,
Qt::AlignmentFlag const &alignment,
bool editable) {
return createTableItem(QString::number(value, 'f', DOUBLE_PRECISION),
alignment, editable);
}
} // namespace
namespace MantidQt {
namespace MantidWidgets {
using DelegateType = CustomItemDelegate::DelegateType;
/**
* FitScriptGeneratorDataTable class methods.
*/
FitScriptGeneratorDataTable::FitScriptGeneratorDataTable(QWidget *parent)
: QTableWidget(parent) {
this->setSelectionBehavior(QAbstractItemView::SelectRows);
this->setSelectionMode(QAbstractItemView::ExtendedSelection);
this->setShowGrid(false);
this->setColumnCount(4);
this->setRowCount(0);
this->verticalHeader()->setVisible(false);
this->horizontalHeader()->setHighlightSections(false);
this->horizontalHeader()->setStretchLastSection(true);
this->setColumnWidth(ColumnIndex::WorkspaceName, 280);
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
this->setColumnWidth(ColumnIndex::WorkspaceIndex, 80);
this->setColumnWidth(ColumnIndex::StartX, 100);
this->setColumnWidth(ColumnIndex::EndX, 100);
this->setHorizontalHeaderLabels(
QStringList({"Name", "WS Index", "StartX", "EndX"}));
this->setStyleSheet("QTableWidget {\n"
" font-size: 8pt;\n"
" border: 1px solid #828790;\n"
"}\n"
"\n"
"QTableWidget::item:selected {\n"
" background-color: #c7e0ff;\n"
" color: #000000;\n"
"}"
"\n"
"QTableWidget::item:hover {\n"
" background-color: #c7e0ff;\n"
" color: #000000;\n"
"}");
m_lastIndex = QPersistentModelIndex();
this->viewport()->installEventFilter(this);
this->setItemDelegateForColumn(
ColumnIndex::WorkspaceName,
new CustomItemDelegate(this, DelegateType::String));
this->setItemDelegateForColumn(
ColumnIndex::WorkspaceIndex,
new CustomItemDelegate(this, DelegateType::Int));
this->setItemDelegateForColumn(
ColumnIndex::StartX, new CustomItemDelegate(this, DelegateType::Double));
this->setItemDelegateForColumn(
ColumnIndex::EndX, new CustomItemDelegate(this, DelegateType::Double));
}
bool FitScriptGeneratorDataTable::eventFilter(QObject *widget, QEvent *event) {
if (widget == this->viewport()) {
auto index = hoveredRowIndex(event);
if (index != m_lastIndex) {
if (this->item(m_lastIndex.row(), m_lastIndex.column()))
emit itemExited(index.isValid() ? index.row() : -1);
m_lastIndex = QPersistentModelIndex(index);
}
}
return QTableWidget::eventFilter(widget, event);
}
QPersistentModelIndex
FitScriptGeneratorDataTable::hoveredRowIndex(QEvent *event) {
auto index = m_lastIndex;
switch (event->type()) {
case QEvent::HoverMove:
index = QPersistentModelIndex(
this->indexAt(static_cast<QHoverEvent *>(event)->pos()));
break;
case QEvent::Leave:
index = QPersistentModelIndex(QModelIndex());
break;
}
return index;
}
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
std::string
FitScriptGeneratorDataTable::workspaceName(FitDomainIndex row) const {
return getText(row, ColumnIndex::WorkspaceName).toStdString();
}
WorkspaceIndex
FitScriptGeneratorDataTable::workspaceIndex(FitDomainIndex row) const {
return getText(row, ColumnIndex::WorkspaceIndex).toInt();
}
double FitScriptGeneratorDataTable::startX(FitDomainIndex row) const {
return getText(row, ColumnIndex::StartX).toDouble();
}
double FitScriptGeneratorDataTable::endX(FitDomainIndex row) const {
return getText(row, ColumnIndex::EndX).toDouble();
}
std::vector<FitDomainIndex> FitScriptGeneratorDataTable::selectedRows() const {
std::vector<FitDomainIndex> rowIndices;
auto const selectionModel = this->selectionModel();
if (selectionModel->hasSelection()) {
for (auto const &rowIndex : selectionModel->selectedRows())
rowIndices.emplace_back(
FitDomainIndex(static_cast<std::size_t>(rowIndex.row())));
std::sort(rowIndices.rbegin(), rowIndices.rend());
}
return rowIndices;
}
void FitScriptGeneratorDataTable::removeDomain(
std::string const &workspaceName,
MantidWidgets::WorkspaceIndex workspaceIndex) {
this->removeRow(indexOfDomain(workspaceName, workspaceIndex));
}
void FitScriptGeneratorDataTable::addDomain(
QString const &workspaceName, MantidWidgets::WorkspaceIndex workspaceIndex,
double startX, double endX) {
auto const rowIndex = this->rowCount();
this->insertRow(rowIndex);
this->setItem(rowIndex, ColumnIndex::WorkspaceName,
createTableItem(workspaceName, Qt::AlignVCenter, false));
this->setItem(rowIndex, ColumnIndex::WorkspaceIndex,
createIntTableItem(static_cast<int>(workspaceIndex.value),
Qt::AlignCenter, false));
this->setItem(rowIndex, ColumnIndex::StartX,
createDoubleTableItem(startX, Qt::AlignCenter, true));
this->setItem(rowIndex, ColumnIndex::EndX,
createDoubleTableItem(endX, Qt::AlignCenter, true));
}
int FitScriptGeneratorDataTable::indexOfDomain(
std::string const &workspaceName,
MantidWidgets::WorkspaceIndex workspaceIndex) const {
for (auto rowIndex = 0; rowIndex < this->rowCount(); ++rowIndex) {
if (this->workspaceName(rowIndex) == workspaceName &&
this->workspaceIndex(rowIndex) == workspaceIndex)
return rowIndex;
}
return -1;
}
QString FitScriptGeneratorDataTable::getText(FitDomainIndex row,
int column) const {
return this->item(static_cast<int>(row.value), column)->text();
}
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**
* CustomItemDelegate class methods.
*/
CustomItemDelegate::CustomItemDelegate(FitScriptGeneratorDataTable *parent,
DelegateType const &type)
: QStyledItemDelegate(parent), m_tableWidget(parent), m_type(type) {
m_tableWidget = parent;
m_tableWidget->setMouseTracking(true);
connect(m_tableWidget, SIGNAL(itemEntered(QTableWidgetItem *)), this,
SLOT(handleItemEntered(QTableWidgetItem *)));
connect(m_tableWidget, SIGNAL(itemExited(int)), this,
SLOT(handleItemExited(int)));
m_hoveredIndex = -1;
}
void CustomItemDelegate::handleItemEntered(QTableWidgetItem *item) {
m_hoveredIndex = item->row();
m_tableWidget->viewport()->update();
}
void CustomItemDelegate::handleItemExited(int newRowIndex) {
m_hoveredIndex = newRowIndex;
}
QWidget *CustomItemDelegate::createEditor(QWidget *parent,
QStyleOptionViewItem const &option,
QModelIndex const &index) const {
auto lineEdit = new QLineEdit(parent);
switch (m_type) {
case DelegateType::Double:
lineEdit->setValidator(createDoubleValidator());
break;
case DelegateType::Int:
lineEdit->setValidator(createIntValidator());
break;
}
return lineEdit;
}
void CustomItemDelegate::paint(QPainter *painter,
QStyleOptionViewItem const &option,
QModelIndex const &index) const {
auto opt = QStyleOptionViewItem(option);
if (index.row() == m_hoveredIndex)
opt.state |= QStyle::State_MouseOver;
QStyledItemDelegate::paint(painter, opt, index);
}
} // namespace MantidWidgets
} // namespace MantidQt