-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelstorage.cpp
More file actions
141 lines (109 loc) · 4.29 KB
/
modelstorage.cpp
File metadata and controls
141 lines (109 loc) · 4.29 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
#include "modelstorage.h"
#include <QDebug>
bool ModelStorage::load(const QString& fileName, QTableWidget* simplex_task, QTableWidget* simplex_restrictions, QSpinBox* variables, QSpinBox* restrictions,
QTableWidget* graph_task, QTableWidget* graph_restrictions, QSpinBox* restrictionsGraph) {
QFile loadFile(fileName);
if (!loadFile.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open save file.");
return false;
}
QByteArray saveData = loadFile.readAll();
QJsonDocument loadDoc = QJsonDocument::fromJson(saveData);
QJsonObject json = loadDoc.object();
simplex_task->clear();
const int rowCount = json["rowCountRestrictions"].toInt();
const int rowCountGraph = json["rowCountRestrictionsGraph"].toInt();
const int columnCount = json["columnCountTask"].toInt();
QJsonArray data = json["data_task"].toArray();
variables->setValue(columnCount-1);
restrictions->setValue(rowCount);
restrictionsGraph->setValue(rowCountGraph);
if(columnCount > 0 && rowCount > 0)
{
for (int i = 0; i < 1; i++)
{
QJsonArray row = data[i].toArray();
for (int j = 0; j < columnCount; j++) {
simplex_task->setItem(i, j, new QTableWidgetItem(row[j].toString()));
}
}
data = json["data_restrictions"].toArray();
for (int i = 0; i < rowCount; i++)
{
QJsonArray row = data[i].toArray();
for (int j = 0; j < columnCount; j++) {
simplex_restrictions->setItem(i, j, new QTableWidgetItem(row[j].toString()));
}
}
}
data = json["data_taskGraph"].toArray();
for (int i = 0; i < 1; i++)
{
QJsonArray row = data[i].toArray();
for (int j = 0; j < 3; j++) {
graph_task->setItem(i, j, new QTableWidgetItem(row[j].toString()));
}
}
data = json["data_restrictionsGraph"].toArray();
for (int i = 0; i < rowCountGraph; i++)
{
QJsonArray row = data[i].toArray();
for (int j = 0; j < 3; j++) {
graph_restrictions->setItem(i, j, new QTableWidgetItem(row[j].toString()));
}
}
return true;
}
bool ModelStorage::save(const QString& fileName,const QTableWidget* simplex_task, const QTableWidget* simplex_restrictions, const QTableWidget* graph_task, const QTableWidget* graph_restrictions) {
QFile saveFile(fileName);
if (!saveFile.open(QIODevice::WriteOnly)) {
qWarning("Couldn't open save file.");
return false;
}
QJsonObject json;
json["rowCountTask"] = simplex_task->rowCount();
json["columnCountTask"] = simplex_task->columnCount();
QJsonArray data;
for (int i = 0; i < simplex_task->rowCount(); i++) {
QJsonArray row;
for (int j = 0; j < simplex_task->columnCount(); j++) {
row.append(QJsonValue(simplex_task->item(i, j)->text()));
}
data.append(row);
}
json["data_task"] = data;
data = {};
for (int i = 0; i < graph_task->rowCount(); i++) {
QJsonArray row;
for (int j = 0; j < graph_task->columnCount(); j++) {
row.append(QJsonValue(graph_task->item(i, j)->text()));
}
data.append(row);
}
json["data_taskGraph"] = data;
data = {};
json["rowCountRestrictions"] = simplex_restrictions->rowCount();
json["columnCountRestrictions"] = simplex_restrictions->columnCount();
json["rowCountRestrictionsGraph"] = graph_restrictions->rowCount();
json["columnCountRestrictionsGraph"] = graph_restrictions->columnCount();
for (int i = 0; i < simplex_restrictions->rowCount(); i++) {
QJsonArray row;
for (int j = 0; j < simplex_restrictions->columnCount(); j++) {
row.append(QJsonValue(simplex_restrictions->item(i, j)->text()));
}
data.append(row);
}
json["data_restrictions"] = data;
data = {};
for (int i = 0; i < graph_restrictions->rowCount(); i++) {
QJsonArray row;
for (int j = 0; j < graph_restrictions->columnCount(); j++) {
row.append(QJsonValue(graph_restrictions->item(i, j)->text()));
}
data.append(row);
}
json["data_restrictionsGraph"] = data;
QJsonDocument saveDoc(json);
saveFile.write(saveDoc.toJson());
return true;
}