9#include <QAbstractItemView>
11#include <QDoubleValidator>
14#include <QItemSelectionModel>
24int WS_INDEX_MAX(100000);
25double X_EXTENT(100000.0);
28QStringList
const COLUMN_HEADINGS({
"Name",
"WS Index",
"StartX",
"EndX"});
29QColor
const FUNCTION_INDEX_COLOR(QColor(30, 144, 255));
30QString
const TABLE_STYLESHEET(
"QTableWidget {\n"
32 " border: 1px solid #828790;\n"
35 "QTableWidget::item:selected {\n"
36 " background-color: #c7e0ff;\n"
40 "QTableWidget::item:hover {\n"
41 " background-color: #c7e0ff;\n"
44QValidator *createXValidator() {
45 auto validator =
new QDoubleValidator(-X_EXTENT, X_EXTENT, X_PRECISION);
46 validator->setNotation(QDoubleValidator::Notation::StandardNotation);
50QValidator *createWSIndexValidator() {
return new QIntValidator(WS_INDEX_MIN, WS_INDEX_MAX); }
52QTableWidgetItem *createTableItem(QString
const &
value, Qt::AlignmentFlag
const &alignment,
bool editable,
53 QColor
const &color = QColor(0, 0, 0)) {
54 auto item =
new QTableWidgetItem(
value);
55 item->setData(Qt::ForegroundRole, color);
56 item->setTextAlignment(alignment);
58 item->setFlags(item->flags() ^ Qt::ItemIsEditable);
62QTableWidgetItem *createWSIndexTableItem(
int value, Qt::AlignmentFlag
const &alignment,
bool editable) {
63 return createTableItem(QString::number(
value), alignment, editable);
66QTableWidgetItem *createXTableItem(
double value, Qt::AlignmentFlag
const &alignment,
bool editable) {
67 return createTableItem(QString::number(
value,
'f', X_PRECISION), alignment, editable);
71 return "f" + QString::number(
index.value) +
".";
83 : QTableWidget(parent), m_selectedRows(), m_selectedColumn(-1), m_selectedValue(0.0),
84 m_lastHoveredIndex(QPersistentModelIndex()) {
85 this->setSelectionBehavior(QAbstractItemView::SelectRows);
86 this->setSelectionMode(QAbstractItemView::ExtendedSelection);
87 this->setShowGrid(
false);
88 this->setColumnCount(COLUMN_HEADINGS.size());
90 this->horizontalHeader()->setHighlightSections(
false);
91 this->horizontalHeader()->setStretchLastSection(
true);
93 this->setColumnWidth(ColumnIndex::WorkspaceName, 280);
94 this->setColumnWidth(ColumnIndex::WorkspaceIndex, 80);
95 this->setColumnWidth(ColumnIndex::StartX, 100);
96 this->setColumnWidth(ColumnIndex::EndX, 100);
98 this->setHorizontalHeaderLabels(COLUMN_HEADINGS);
100 this->setStyleSheet(TABLE_STYLESHEET);
102 this->viewport()->installEventFilter(
this);
104 this->setItemDelegateForColumn(ColumnIndex::WorkspaceName,
new CustomItemDelegate(
this, ColumnIndex::WorkspaceName));
105 this->setItemDelegateForColumn(ColumnIndex::WorkspaceIndex,
107 this->setItemDelegateForColumn(ColumnIndex::StartX,
new CustomItemDelegate(
this, ColumnIndex::StartX));
108 this->setItemDelegateForColumn(ColumnIndex::EndX,
new CustomItemDelegate(
this, ColumnIndex::EndX));
110 connect(
this, SIGNAL(itemClicked(QTableWidgetItem *)),
this, SLOT(
handleItemClicked(QTableWidgetItem *)));
112 disconnect(this->verticalHeader(), SIGNAL(sectionPressed(
int)),
this, SLOT(selectRow(
int)));
116 if (widget == this->viewport()) {
125 return QTableWidget::eventFilter(widget, event);
136 auto *selectionModel = this->selectionModel();
138 if (!selectionModel->hasSelection()) {
139 this->blockSignals(
true);
144 auto itemSelection = selectionModel->selection();
146 this->selectRow(
static_cast<int>(selectedRow.value));
147 itemSelection.merge(selectionModel->selection(), QItemSelectionModel::Select);
149 selectionModel->clearSelection();
150 selectionModel->select(itemSelection, QItemSelectionModel::Select);
152 this->blockSignals(
false);
160 auto const eventType =
event->type();
161 if (eventType == QEvent::HoverMove)
162 index = QPersistentModelIndex(this->indexAt(
static_cast<QHoverEvent *
>(event)->pos()));
163 else if (eventType == QEvent::Leave)
164 index = QPersistentModelIndex(QModelIndex());
170 return getText(row, ColumnIndex::WorkspaceName).toStdString();
174 return getText(row, ColumnIndex::WorkspaceIndex).toInt();
178 return getText(row, ColumnIndex::StartX).toDouble();
182 return getText(row, ColumnIndex::EndX).toDouble();
186 std::vector<FitDomainIndex> rowIndices;
187 rowIndices.reserve(this->rowCount());
191 std::reverse(rowIndices.begin(), rowIndices.end());
196 std::vector<FitDomainIndex> rowIndices;
198 auto const selectionModel = this->selectionModel();
199 if (selectionModel->hasSelection()) {
200 const auto selectedRows = selectionModel->selectedRows();
203 [](
const auto &rowIndex) { return std::move(FitDomainIndex(static_cast<std::size_t>(rowIndex.row()))); });
204 std::reverse(rowIndices.begin(), rowIndices.end());
215 throw std::runtime_error(
"There is no currentRow as data has not been loaded yet.");
224 return this->verticalHeaderItem(
static_cast<int>(rows[0].
value))->text();
228 for (
auto rowIndex = 0; rowIndex < this->rowCount(); ++rowIndex) {
229 auto tableItem = this->item(rowIndex, ColumnIndex::WorkspaceName);
231 tableItem->setText(newName);
236 this->removeRow(
static_cast<int>(domainIndex.
value));
248 double startX,
double endX) {
249 this->blockSignals(
true);
251 auto const rowIndex = this->rowCount();
252 this->insertRow(rowIndex);
254 this->setVerticalHeaderItem(rowIndex, createTableItem(toFunctionIndex(
FitDomainIndex(rowIndex)), Qt::AlignCenter,
255 false, FUNCTION_INDEX_COLOR));
256 this->setItem(rowIndex, ColumnIndex::WorkspaceName, createTableItem(
workspaceName, Qt::AlignVCenter,
false));
257 this->setItem(rowIndex, ColumnIndex::WorkspaceIndex,
259 this->setItem(rowIndex, ColumnIndex::StartX, createXTableItem(
startX, Qt::AlignCenter,
true));
260 this->setItem(rowIndex, ColumnIndex::EndX, createXTableItem(
endX, Qt::AlignCenter,
true));
262 if (!this->selectionModel()->hasSelection()) {
264 this->selectRow(rowIndex);
267 this->blockSignals(
false);
272 this->setVerticalHeaderItem(
static_cast<int>(i.value),
273 createTableItem(toFunctionIndex(i), Qt::AlignCenter,
false, FUNCTION_INDEX_COLOR));
277 return this->item(
static_cast<int>(row.
value), column)->text();
288 this->verticalHeader()->setVisible(visible);
292 this->blockSignals(
true);
295 createXTableItem(xValue, Qt::AlignCenter,
true));
296 this->blockSignals(
false);
304 : QStyledItemDelegate(parent), m_tableWidget(parent), m_columnIndex(
index), m_hoveredIndex(-1) {
320 QModelIndex
const &
index)
const {
324 auto lineEdit =
new QLineEdit(parent);
326 case ColumnIndex::WorkspaceName:
328 case ColumnIndex::WorkspaceIndex:
329 lineEdit->setValidator(createWSIndexValidator());
331 case ColumnIndex::StartX:
332 lineEdit->setValidator(createXValidator());
334 case ColumnIndex::EndX:
335 lineEdit->setValidator(createXValidator());
343 auto opt = QStyleOptionViewItem(option);
345 opt.state |= QStyle::State_MouseOver;
346 QStyledItemDelegate::paint(painter, opt,
index);
double value
The value of the point.
std::map< DeltaEMode::Type, std::string > index