-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodel.cpp
125 lines (105 loc) · 3.01 KB
/
model.cpp
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
#include <iostream>
#include "model.h"
using std::vector;
Model::Model(QObject *parent) : QAbstractTableModel(parent)
{
std::vector<std::vector<double>> mat{{1}};
std::vector<std::string> matr{"a"};
std::vector<std::string> matc{"x"};
DataMatrix x(mat, matr, matc);
table = x;
}
int Model::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return table.getRowCount();
}
int Model::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent)
return table.getColumnCount();
}
QVariant Model::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
qreal temp = table.getData()->at(index.row()).at(index.column());
return temp;
}
return QVariant();
}
DataMatrix Model::getTable() const
{
return table;
}
void Model::readJson(std::string path)
{
table.read(path);
}
void Model::writeJson(std::string path)
{
table.write(path);
}
QVariant Model::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (Qt::Orientation::Vertical == orientation)
{
if (((int)table.getRowCount()) > section)
return QString::fromStdString(table.getRowLabel()->at(section));
}
else{
if (((int)table.getColumnCount()) > section)
return QString::fromStdString(table.getColumnLabel()->at(section));
}
}
return QVariant();
}
Qt::ItemFlags Model::flags(const QModelIndex &index) const
{
return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
}
bool Model::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole)
{
table.getData()->at(index.row()).at(index.column()) = value.toDouble();
emit dataChanged(index, index);
}
return true;
}
void Model::insertRow(unsigned int i, std::string label)
{
vector<double> v(table.getColumnCount(), 0);
table.addRow(v, i, label);
}
void Model::insertColumn(unsigned int i, std::string label)
{
vector<double> v(table.getRowCount(), 0);
table.addColumn(v, i, label);
}
void Model::removeRow(unsigned int i) { table.deleteRow(i); }
void Model::removeColumn(unsigned int i) { table.deleteColumn(i); }
void Model::newModel(std::string rowLabel, std::string columnLabel)
{
if(table.getData())
{
for (long unsigned int i = 0; i < table.getData()->size(); ++i)
{
table.getData()[i].clear();
table.getData()[i].shrink_to_fit();
}
table.getData()->clear();
table.getData()->shrink_to_fit();
table.getRowLabel()->clear();
table.getRowLabel()->shrink_to_fit();
table.getColumnLabel()->clear();
table.getColumnLabel()->shrink_to_fit();
}
std::vector<std::vector<double>> mat{{0}};
std::vector<std::string> matr{rowLabel};
std::vector<std::string> matc{columnLabel};
DataMatrix x(mat, matr, matc);
table = x;
}