-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpointsmodel.cpp
More file actions
269 lines (225 loc) · 8.14 KB
/
Copy pathpointsmodel.cpp
File metadata and controls
269 lines (225 loc) · 8.14 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
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
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
267
268
269
#include "pointsmodel.h"
#include <limits>
#include <QFileDialog>
#include <QByteArray>
#include <QTextStream>
PointsModel::PointsModel(QObject *parent) : QAbstractListModel(parent) {
}
PointData *PointsModel::source() const { return _source; }
void PointsModel::setSource(PointData *s) {
if (_source == s) return;
if (_source) {
disconnect(_source, &PointData::pointsChanged, this, &PointsModel::onPointsChanged);
}
_source = s;
if (_source) {
connect(_source, &PointData::pointsChanged, this, &PointsModel::onPointsChanged);
onPointsChanged();
}
emit sourceChanged();
}
FitController *PointsModel::fit() const { return _fit; }
void PointsModel::setFit(FitController *f) {
if (_fit == f) return;
if (_fit) {
disconnect(_fit, &FitController::resultChanged, this, &PointsModel::onFitUpdated);
}
_fit = f;
if (_fit) {
connect(_fit, &FitController::resultChanged, this, &PointsModel::onFitUpdated);
onFitUpdated();
}
emit fitChanged();
}
int PointsModel::rowCount(const QModelIndex &parent) const {
if (parent.isValid()) return 0;
return _selectionActive
? static_cast<int>(_filteredIndices.size())
: static_cast<int>(_points.size());
}
int PointsModel::totalCount() const { return static_cast<int>(_points.size()); }
QVariant PointsModel::data(const QModelIndex &index, const int role) const {
if (!index.isValid() || index.row() >= rowCount()) return {};
const int actual = resolveRow(index.row());
if (actual < 0 || actual >= static_cast<int>(_points.size())) return {};
const auto &p = _points.at(actual);
switch (role) {
case XRole: return p.x();
case YRole: return p.y();
case SourceIndexRole: return actual;
case ErrorRole:
return _fit
? _fit->residualFor(p)
: std::numeric_limits<double>::quiet_NaN();
default: return {};
}
}
bool PointsModel::setData(const QModelIndex &index, const QVariant &value, int role) {
if (!index.isValid() || index.row() >= rowCount()) return false;
const int actual = resolveRow(index.row());
if (actual < 0 || actual >= static_cast<int>(_points.size())) return false;
auto &p = _points[actual];
switch (role) {
case XRole: p.setX(value.toReal());
break;
case YRole: p.setY(value.toReal());
break;
default: return false;
}
emit dataChanged(index, index, {role});
return true;
}
Qt::ItemFlags PointsModel::flags(const QModelIndex &index) const {
if (!index.isValid()) return Qt::NoItemFlags;
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable;
}
QHash<int, QByteArray> PointsModel::roleNames() const {
return {
{XRole, "px"},
{YRole, "py"},
{ErrorRole, "residual"},
{SourceIndexRole, "sourceIndex"},
};
}
bool PointsModel::selectionActive() const { return _selectionActive; }
void PointsModel::setSelectionActive(const bool active) {
if (_selectionActive == active) return;
_selectionActive = active;
rebuildFilter();
emit selectionActiveChanged();
}
QRectF PointsModel::selectionRect() const { return _selectionRect; }
void PointsModel::setSelectionRect(const QRectF &rect) {
_selectionRect = rect;
emit selectionRectChanged();
if (_selectionActive) rebuildFilter();
}
void PointsModel::appendPoint(const qreal x, const qreal y) {
if (!_source) return;
_selfModifying = true;
const QPointF pt{x, y};
const int newActualIdx = static_cast<int>(_points.size());
if (!_selectionActive || _selectionRect.contains(pt)) {
const int visualRow = _selectionActive
? static_cast<int>(_filteredIndices.size())
: newActualIdx;
beginInsertRows({}, visualRow, visualRow);
_points.append(pt);
if (_selectionActive) _filteredIndices.append(newActualIdx);
endInsertRows();
} else {
_points.append(pt);
}
_source->addPoint(pt);
_selfModifying = false;
emit countChanged();
emit totalCountChanged();
}
void PointsModel::removePoint(const qint64 visualRow) {
if (!_source || visualRow < 0 || visualRow >= rowCount()) return;
const int actual = resolveRow(static_cast<int>(visualRow));
_selfModifying = true;
beginRemoveRows({}, static_cast<int>(visualRow), static_cast<int>(visualRow));
_points.removeAt(actual);
if (_selectionActive) {
_filteredIndices.removeAt(static_cast<int>(visualRow));
for (int &fi: _filteredIndices) if (fi > actual) --fi;
}
endRemoveRows();
_source->removePoint(actual);
_selfModifying = false;
emit countChanged();
emit totalCountChanged();
}
void PointsModel::setPoint(const qint64 visualRow, qreal x, qreal y) {
if (!_source || visualRow < 0 || visualRow >= rowCount()) return;
const int actual = resolveRow(static_cast<int>(visualRow));
_selfModifying = true;
_points[actual] = {x, y};
const QModelIndex mi = index(static_cast<int>(visualRow));
emit dataChanged(mi, mi, {XRole, YRole});
_source->setPoint(actual, {x, y});
_selfModifying = false;
}
QPointF PointsModel::pointAt(const int visualRow) const {
if (visualRow < 0 || visualRow >= static_cast<int>(_filteredIndices.size())) return {};
return _points.at(_filteredIndices.at(visualRow));
}
void PointsModel::onPointsChanged() {
if (_selfModifying) return;
beginResetModel();
_points = _source->allPoints();
if (_selectionActive) {
_filteredIndices.clear();
for (int i = 0; i < static_cast<int>(_points.size()); ++i)
if (_selectionRect.contains(_points[i]))
_filteredIndices.append(i);
}
endResetModel();
emit countChanged();
emit totalCountChanged();
}
void PointsModel::onFitUpdated() {
if (_selfModifying || rowCount() == 0) return;
emit dataChanged(index(0), index(rowCount() - 1), {ErrorRole});
}
void PointsModel::rebuildFilter() {
beginResetModel();
_filteredIndices.clear();
if (_selectionActive)
for (int i = 0; i < static_cast<int>(_points.size()); ++i)
if (_selectionRect.contains(_points[i]))
_filteredIndices.append(i);
endResetModel();
emit countChanged();
}
int PointsModel::resolveRow(const int visualRow) const {
return _selectionActive ? _filteredIndices.at(visualRow) : visualRow;
}
void PointsModel::exportToFile() const
{
QByteArray data;
QTextStream out(&data);
for (const auto &pt : _points)
out << pt.x() << ',' << pt.y() << '\n';
QFileDialog::saveFileContent(data, "points.txt");
}
void PointsModel::importFromFile()
{
QFileDialog::getOpenFileContent(
"Text files (*.txt)",
[this](const QString &fileName, const QByteArray &fileContent) {
if (fileName.isEmpty()) {
return; // user cancelled
}
QVector<QPair<double, double>> parsed;
const QStringList lines = QString::fromUtf8(fileContent).split('\n');
for (const QString &rawLine : lines) {
const QString line = rawLine.trimmed();
if (line.isEmpty())
continue;
const QStringList parts = line.split(',');
if (parts.size() != 2) {
emit importFailed("Malformed line: " + line);
return;
}
bool okX = false, okY = false;
const double x = parts[0].trimmed().toDouble(&okX);
const double y = parts[1].trimmed().toDouble(&okY);
if (!okX || !okY) {
emit importFailed("Invalid number on line: " + line);
return;
}
parsed.append({x, y});
}
if (!_source) {
emit importFailed("No source model attached.");
return;
}
while (rowCount() > 0)
removePoint(0);
for (const auto &p : parsed)
appendPoint(p.first, p.second);
emit importSucceeded();
});
}