-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatamodel.cpp
More file actions
171 lines (151 loc) · 6.17 KB
/
datamodel.cpp
File metadata and controls
171 lines (151 loc) · 6.17 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
#include "datamodel.h"
#include "ctre.hpp"
#include <QDataStream>
#include <QFile>
#include <QRegularExpression>
#include <QTextStream>
DataModel::DataModel(QObject* parent)
: QAbstractTableModel{parent} {
data_.reserve(1000000);
}
DataModel::~DataModel() { qDebug(__FUNCTION__); }
int DataModel::rowCount(const QModelIndex&) const { return data_.size(); }
int DataModel::columnCount(const QModelIndex&) const { return ColumnCount; }
QVariant DataModel::data(const QModelIndex& index, int role) const {
if(role == Qt::DisplayRole)
switch(index.column()) {
case ColumnX : return number(data_[index.row()].x(), precision_);
case ColumnY : return number(data_[index.row()].y(), precision_);
case ColumnComp : return number(dataComp_[index.row()].y(), precision_);
case ColumnDelta: return number(data_[index.row()].y() - dataComp_[index.row()].y(), precision_);
}
else if(role == Qt::EditRole)
switch(index.column()) {
case ColumnX : return data_[index.row()].x();
case ColumnY : return data_[index.row()].y();
case ColumnComp : return dataComp_[index.row()].y();
case ColumnDelta: return data_[index.row()].y() - dataComp_[index.row()].y();
}
else if(role == Qt::TextAlignmentRole)
return Qt::AlignCenter;
return {};
}
bool DataModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if(role == Qt::EditRole)
switch(index.column()) {
case ColumnX:
data_[index.row()].setX(value.toDouble());
emit dataChanged_(data_);
return true;
case ColumnY:
data_[index.row()].setY(value.toDouble());
emit dataChanged_(data_);
return true;
default:
return {};
}
return {};
}
QVariant DataModel::headerData(int section, Qt::Orientation orientation, int role) const {
static QStringList hd{u"X"_s, u"Y"_s, u"Расчёт"_s, u"Δ"_s};
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
return hd[section];
return QAbstractTableModel::headerData(section, orientation, role);
}
Qt::ItemFlags DataModel::flags(const QModelIndex& index) const {
if(index.column() < ColumnComp)
return Qt::ItemIsEnabled | Qt::ItemIsEditable;
return Qt::ItemIsEnabled;
}
const Data& DataModel::data() const noexcept { return data_; }
void DataModel::setNewData(const Data& data) {
int maxIndex = std::min(data.size(), dataComp_.size());
for(int i = 0; i < maxIndex; ++i)
dataComp_[i].ry() = data[i].y();
emit dataChanged(createIndex(0, ColumnComp), createIndex(data_.size() - 1, ColumnComp), {Qt::DisplayRole});
}
void DataModel::setPrecision(int prec) {
precision_ = prec;
emit dataChanged(createIndex(0, 0), createIndex(data_.size() - 1, 1), {Qt::DisplayRole});
}
void DataModel::load(const QString& fileName) {
if(QFile file(fileName); file.open(QIODevice::ReadOnly)) {
auto lastSize{data_.size()};
data_.clear();
dataComp_.clear();
if(fileName.endsWith(u"bin"_s)) {
QDataStream in{&file};
in >> data_;
in >> dataComp_;
dataComp_.resize(data_.size());
} else {
QTextStream in{&file};
while(!in.atEnd()) {
auto& val = data_.emplace_back();
in >> val.rx() >> val.ry();
val = dataComp_.emplace_back();
in >> val.ry();
}
}
if(lastSize > static_cast<int>(data_.size())) {
beginRemoveRows({}, static_cast<int>(data_.size()), lastSize - 1);
endRemoveRows();
} else if(lastSize < static_cast<int>(data_.size())) {
beginInsertRows({}, lastSize, static_cast<int>(data_.size()) - 1);
endInsertRows();
} else
emit dataChanged(createIndex(0, ColumnX), createIndex(data_.size() - 1, ColumnX), {Qt::DisplayRole});
emit dataChanged_(data_);
}
}
void DataModel::save(const QString& fileName) const {
qDebug(__FUNCTION__);
if(QFile file(fileName); file.open(QIODevice::WriteOnly)) {
if(fileName.endsWith(u"bin"_s)) {
QDataStream out{&file};
out << data_;
out << dataComp_;
} else {
QTextStream out{&file};
for(int i{}; auto&& val: data_)
out << u"%1\t%2\t%3\n"_s
.arg(val.x(), 0, u'G', precision_)
.arg(val.y(), 0, u'G', precision_)
.arg(dataComp_[i++].y(), 0, u'G', precision_);
}
}
}
QString DataModel::copy() const {
QString cpyStr;
for(auto&& [val, cmp]: v::zip(data_, dataComp_)) {
cpyStr += number(val.x(), precision_) + u'\t';
cpyStr += number(val.y(), precision_) + u'\t';
cpyStr += number(cmp.y(), precision_) + u'\t';
cpyStr += number(val.y() - cmp.y(), precision_) + u'\n';
}
return cpyStr;
}
void DataModel::paste(QString&& clipboardStr) {
if(clipboardStr.isEmpty())
return;
clipboardStr.replace(u',', u'.');
auto clipboardList{clipboardStr.split(u'\n', Qt::SkipEmptyParts)};
auto lastSize{data_.size()};
data_.resize(clipboardList.size());
dataComp_.resize(clipboardList.size());
for(int row{}; auto&& [val, cmp]: v::zip(data_, dataComp_)) {
auto&& list = clipboardList[row++].split(QRegularExpression{u"\\s"_s});
val.rx() = list.value(0).toDouble();
val.ry() = list.value(1).toDouble();
cmp.ry() = list.value(2).toDouble();
}
if(lastSize > static_cast<int>(data_.size())) {
beginRemoveRows({}, static_cast<int>(data_.size()), lastSize - 1);
endRemoveRows();
} else if(lastSize < static_cast<int>(data_.size())) {
beginInsertRows({}, lastSize, static_cast<int>(data_.size()) - 1);
endInsertRows();
} else
emit dataChanged(createIndex(0, ColumnX), createIndex(data_.size() - 1, ColumnX), {Qt::DisplayRole});
emit dataChanged_(data_);
}