-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcppinterface.cpp
133 lines (105 loc) · 3.12 KB
/
cppinterface.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
124
125
126
127
128
129
130
131
132
133
#include <QSettings>
#include <QFile>
#include <QDataStream>
#include <QJsonDocument>
#include "cppinterface.h"
CPPInterface::CPPInterface()
{
// counter in setting
QSettings settings("Hooshan", "AyCounter");
QString data = settings.value("variables/counter", "null").toString();
if (data == "null")
m_counter = 0;
else
m_counter = data.toInt();
// name in file
QFile file("file.dat");
if (file.open(QIODevice::ReadOnly)) {
QDataStream in(&file);
in >> m_name;
} else {
qDebug() << "fail to open file!!!!!";
m_name = "default name";
}
// json in file
m_personModel = new PersonModel;
// TEST
QFile personFile("person" + QString::number(0) + ".json");
if (!personFile.open(QIODevice::ReadOnly)) {
QVector<QString> tags1 = {"dangerous", "far", "magical"};
QVector<QString> tags2 = {"dangerous"};
Address address1 = Address{"Diagon", "London", tags1};
Address address2 = Address{"Koooche", "Gangcity", tags2};
QVector<QString> phoneNumbers1 = {"09003434", "091234567"};
QVector<Address> addresses = {address1, address2};
Person *person1 = new Person{"ali", "alavi", 20, phoneNumbers1, addresses};
m_personModel->appendRow(person1);
}
// ENDTEST
// reading json from file
int i = 0;
while (true) {
QFile personFile("person" + QString::number(i) + ".json");
if (!personFile.open(QIODevice::ReadOnly))
break;
QByteArray savedData = personFile.readAll();
QJsonDocument loadDoc = QJsonDocument::fromJson(savedData);
Person person = PersonModel::fromJson(loadDoc.object());
Person *personToAppend = new Person(person);
m_personModel->appendRow(personToAppend);
i++;
}
}
int CPPInterface::counter() const
{
return m_counter;
}
void CPPInterface::setcounter(int newCounter)
{
if (m_counter == newCounter)
return;
m_counter = newCounter;
emit counterChanged();
}
void CPPInterface::saveCounterToSetting() {
QSettings settings("Hooshan", "AyCounter");
settings.setValue("variables/counter", m_counter);
}
void CPPInterface::saveNameToFile()
{
QFile file("file.dat");
file.open(QIODevice::WriteOnly);
QDataStream out(&file);
out << m_name;
}
void CPPInterface::savePersonToFile()
{
for (int i = 0; i < m_personModel->rowCount(); ++i) {
QFile file("person" + QString::number(i) + ".json");
file.open(QIODevice::WriteOnly);
QJsonObject jsonObject = PersonModel::toJson(m_personModel->getPerson(i));
file.write(QJsonDocument(jsonObject).toJson());
}
}
PersonModel *CPPInterface::personModel() const
{
return m_personModel;
}
void CPPInterface::setPersonModel(PersonModel *newPersonModel)
{
if (m_personModel == newPersonModel)
return;
m_personModel = newPersonModel;
emit personModelChanged();
}
QString CPPInterface::name() const
{
return m_name;
}
void CPPInterface::setname(const QString &newName)
{
if (m_name == newName)
return;
m_name = newName;
emit nameChanged();
}