-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtablemodel.cpp
More file actions
137 lines (108 loc) · 4.1 KB
/
tablemodel.cpp
File metadata and controls
137 lines (108 loc) · 4.1 KB
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
// tablemodel.cpp
#include "tablemodel.h"
#include <QMimeData>
#include <QIODevice>
TableModel::TableModel(QObject* parent)
: QAbstractTableModel(parent),
columnHeaders({"Time (min)", "[Start] (mM)", "[End] (mM)"}) {}
int TableModel::rowCount(const QModelIndex& parent) const {
if (parent.isValid()) return 0;
return static_cast<int>(tableData.size());
}
int TableModel::columnCount(const QModelIndex& parent) const {
if (parent.isValid()) return 0;
return static_cast<int>(columnHeaders.size());
}
QVariant TableModel::data(const QModelIndex& index, int role) const {
if (!index.isValid()) return QVariant();
if (role == Qt::DisplayRole) {
return tableData[index.row()][index.column()];
}
return QVariant();
}
QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role != Qt::DisplayRole) return QVariant();
if (orientation == Qt::Horizontal) {
return columnHeaders[section];
} else {
return QString::number(section);
}
}
bool TableModel::insertRows(int row, int count, const QModelIndex& parent) {
beginInsertRows(parent, row, row + count - 1);
for (int i = 0; i < count; ++i) {
tableData.insert(tableData.begin() + row, QVector<QString>(columnHeaders.size(), "<empty>"));
}
endInsertRows();
return true;
}
bool TableModel::moveRows(const QModelIndex &sourceParent, int sourceRow, int count,
const QModelIndex &destinationParent, int destinationChild) {
if (sourceRow < 0 || destinationChild < 0 ||
sourceRow >= static_cast<int>(tableData.size()) ||
destinationChild > static_cast<int>(tableData.size()) ||
sourceRow == destinationChild || count != 1) {
return false;
}
beginMoveRows(sourceParent, sourceRow, sourceRow, destinationParent, destinationChild);
auto movedRow = tableData[sourceRow];
tableData.erase(tableData.begin() + sourceRow);
// adjust destination index if it follows the removed row
if (destinationChild > sourceRow)
destinationChild -= 1;
tableData.insert(tableData.begin() + destinationChild, movedRow);
endMoveRows();
return true;
}
bool TableModel::removeRows(int position, int rows, const QModelIndex& parent) {
if (position < 0 || position + rows > static_cast<int>(tableData.size())) return false;
beginRemoveRows(parent, position, position + rows - 1);
tableData.erase(tableData.begin() + position, tableData.begin() + position + rows);
endRemoveRows();
return true;
}
void TableModel::addSegment(double timeMinutes, int startConc, int endConc, int insertRow) {
// Clamp the insert row
if (insertRow < 0 || insertRow > static_cast<int>(tableData.size()))
insertRow = static_cast<int>(tableData.size());
beginInsertRows(QModelIndex(), insertRow, insertRow);
QVector<QString> row;
row.push_back(QString::number(timeMinutes));
row.push_back(QString::number(startConc));
row.push_back(QString::number(endConc));
tableData.insert(tableData.begin() + insertRow, row);
endInsertRows();
emit segmentsChanged();
}
void TableModel::removeSegment(int pos) {
if (tableData.empty()) return;
if (pos < 0 || pos >= static_cast<int>(tableData.size())) {
pos = static_cast<int>(tableData.size()) - 1;
}
beginRemoveRows(QModelIndex(), pos, pos);
tableData.erase(tableData.begin() + pos);
endRemoveRows();
emit segmentsChanged();
}
QVector<QVector<double>> TableModel::getSegments() const {
QVector<QVector<double>> numericSegments;
for (const auto& row : tableData) {
QVector<double> numericRow;
for (const auto& cell : row) {
bool ok;
double val = cell.toDouble(&ok);
numericRow.push_back(ok ? val : 0.0); // Optionally handle invalid values
}
numericSegments.push_back(numericRow);
}
return numericSegments;
}
void TableModel::clearSegments() {
beginResetModel();
tableData.clear();
endResetModel();
emit segmentsChanged();
}
void TableModel::updateSegments() {
emit segmentsChanged();
}