-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdegreesmodel.cpp
More file actions
156 lines (138 loc) · 5.33 KB
/
degreesmodel.cpp
File metadata and controls
156 lines (138 loc) · 5.33 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
#include "degreesmodel.h"
#include "ctre.hpp"
#include <QDataStream>
#include <QFile>
#include <QTextStream>
DegreesModel::DegreesModel(QObject* parent)
: QAbstractTableModel{parent} {
}
int DegreesModel::rowCount(const QModelIndex&) const { return static_cast<int>(data_.size()); }
int DegreesModel::columnCount(const QModelIndex&) const { return 1; }
QVariant DegreesModel::data(const QModelIndex& index, int role) const {
if(role == Qt::DisplayRole)
return number(data_[index.row()], precision_);
else if(role == Qt::EditRole)
return data_[index.row()];
else if(role == Qt::TextAlignmentRole)
return Qt::AlignCenter;
return {};
}
QVariant DegreesModel::headerData(int section, Qt::Orientation orientation, int role) const {
static const QString chars(u"⁰¹²³⁴⁵⁶⁷⁸⁹"_s);
if(role == Qt::DisplayRole) {
if(orientation == Qt::Horizontal)
return u"Коэффициенты"_s;
else {
switch(section) {
case 0:
return u"A0"_s;
case 1:
return u"A1x"_s;
default: {
auto str{u"A%1x"_s.arg(section)};
for(char c: QByteArray::number(section))
str += chars[c - u'0'];
return str;
}
}
}
}
if(role == Qt::TextAlignmentRole)
return Qt::AlignCenter;
return QAbstractTableModel::headerData(section, orientation, role);
}
Qt::ItemFlags DegreesModel::flags(const QModelIndex&) const {
return Qt::ItemIsEnabled;
}
void DegreesModel::setCoeffData(const Degrees& data) {
auto lastSize{static_cast<int>(data_.size())};
data_ = data;
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, 0), createIndex(static_cast<int>(data_.size()) - 1, 0), {Qt::DisplayRole});
emit dataChanged_(data_);
}
void DegreesModel::setPrecision(int prec) {
precision_ = prec;
emit dataChanged(createIndex(0, 0), createIndex(static_cast<int>(data_.size()) - 1, 1), {Qt::DisplayRole});
}
void DegreesModel::load(const QString& fileName) {
qDebug(__FUNCTION__);
if(QFile file(fileName); file.open(QIODevice::ReadOnly | QIODevice::Text)) {
auto lastSize{static_cast<int>(data_.size())};
data_.clear();
if(0) {
Timer{{__FUNCTION__}};
QString line;
QTextStream stream(&file);
while(stream.readLineInto(&line)) {
data_.push_back(line.toDouble());
}
} else {
Timer{{__FUNCTION__}};
QByteArray fileData(file.readAll());
static constexpr ctll::fixed_string pattern{"\\s*(\\S+)\\s*\n"};
auto range = ctre::range<pattern>(std::string_view(fileData.data(), fileData.size()));
for(auto [wholeStr, xStr]: range) {
if(wholeStr) {
data_.push_back(xStr.size() ? QByteArray{xStr.data(), static_cast<int>(xStr.size())}.toDouble() : 0.0);
}
}
}
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, 0), createIndex(static_cast<int>(data_.size()) - 1, 0), {Qt::DisplayRole});
emit dataChanged_(data_);
}
}
void DegreesModel::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 << uint32_t(static_cast<int>(data_.size()));
for(auto&& val: data_)
out << val;
} else {
QTextStream out(&file);
for(size_t i = 0; i < data_.size(); ++i)
out << u"%1\n"_s.arg(data_[i], 0, u'G', precision_);
}
}
}
QString DegreesModel::copy() const {
QString cpyStr;
for(auto val: data_)
cpyStr += number(val, precision_) + u'\n';
return cpyStr;
}
void DegreesModel::paste(QString&& clipboardStr) {
if(clipboardStr.isEmpty())
return;
clipboardStr.replace(u',', u'.');
QStringList clipboardList{clipboardStr.split(u'\n', Qt::SkipEmptyParts)};
auto lastSize{static_cast<int>(data_.size())};
data_.clear();
data_.reserve(clipboardList.size());
for(auto&& str: clipboardList)
data_.emplace_back(str.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, 0), createIndex(static_cast<int>(data_.size()) - 1, 0), {Qt::DisplayRole});
emit dataChanged_(data_);
}