-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServersListModel.cpp
executable file
·224 lines (180 loc) · 5.86 KB
/
ServersListModel.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include "ServersListModel.h"
#include "Encrypter.h"
ServersListModel::ServersListModel(SettingsModel* poSettingsModel, QObject *poParent) : QAbstractListModel(poParent)
{
qDebug() << __FUNCTION__;
this->poSettingsModel = poSettingsModel;
}
ServersListModel::~ServersListModel()
{
qDebug() << __FUNCTION__;
}
QHash<int,QByteArray> ServersListModel::roleNames() const
{
qDebug() << __FUNCTION__;
return {
{ IsEnabledRole, "isEnabled" },
{ HostRole, "host" },
{ PortRole, "port" }
};
}
QVariant ServersListModel::data(const QModelIndex &oIndex, int iRole) const
{
qDebug() << __FUNCTION__;
if (!oIndex.isValid())
return QVariant();
QJsonArray oServersJsonArray = this->poSettingsModel->fnGetJsonArrayValue("servers");
if (oIndex.row() >= oServersJsonArray.size())
return QVariant();
QJsonObject oJsonObject = oServersJsonArray.at(oIndex.row()).toObject();
if (iRole == IsEnabledRole) {
return oJsonObject["isEnabled"].toBool();
} if (iRole == HostRole) {
return oJsonObject["host"].toString();
} if (iRole == PortRole) {
return oJsonObject["port"].toString();
} else
return QVariant();
}
bool ServersListModel::setData(const QModelIndex &oIndex, const QVariant &oValue, int iRole)
{
qDebug() << __FUNCTION__;
if (oIndex.isValid() /*&& iRole == Qt::EditRole*/) {
QJsonArray oServersJsonArray = this->poSettingsModel->fnGetJsonArrayValue("servers");
if (oIndex.row() < oServersJsonArray.size()) {
QJsonObject oJsonObject = oServersJsonArray.at(oIndex.row()).toObject();
if (iRole == IsEnabledRole) {
oJsonObject["isEnabled"] = oValue.toBool();
}
if (iRole == HostRole) {
oJsonObject["host"] = oValue.toString();
}
if (iRole == PortRole) {
oJsonObject["port"] = oValue.toString();
}
QJsonValue oJsonValue(oJsonObject);
oServersJsonArray.replace(oIndex.row(), oJsonValue);
this->poSettingsModel->fnUpdateJsonArrayValue("servers", oServersJsonArray);
}
emit dataChanged(oIndex, oIndex, { iRole });
this->fnSave();
return true;
}
return false;
}
QVariant ServersListModel::headerData(
int iSection,
Qt::Orientation oOrientation,
int iRole
) const
{
qDebug() << __FUNCTION__;
if (iRole != Qt::DisplayRole)
return QVariant();
if (oOrientation == Qt::Horizontal)
return QString("Column %1").arg(iSection);
else
return QString("Row %1").arg(iSection);
}
Qt::ItemFlags ServersListModel::flags(const QModelIndex &oIndex) const
{
qDebug() << __FUNCTION__;
if (!oIndex.isValid())
return Qt::ItemIsEnabled;
return QAbstractItemModel::flags(oIndex) | Qt::ItemIsEditable;
}
bool ServersListModel::insertRows(int iPosition, int iRows, const QModelIndex &oParent)
{
qDebug() << __FUNCTION__;
beginInsertRows(QModelIndex(), iPosition, iPosition+iRows-1);
QJsonArray oServersJsonArray = this->poSettingsModel->fnGetJsonArrayValue("servers");
for (int iRow = 0; iRow < iRows; ++iRow) {
QJsonObject oJsonObject;
QJsonValue oJsonValue(oJsonObject);
oServersJsonArray.insert(iPosition, oJsonValue);
}
this->poSettingsModel->fnUpdateJsonArrayValue("servers", oServersJsonArray);
endInsertRows();
this->fnSave();
return true;
}
bool ServersListModel::removeRows(int iPosition, int iRows, const QModelIndex &oParent)
{
qDebug() << __FUNCTION__;
beginRemoveRows(QModelIndex(), iPosition, iPosition+iRows-1);
QJsonArray oServersJsonArray = this->poSettingsModel->fnGetJsonArrayValue("servers");
for (int iRow = iPosition; iRow < iPosition+iRows; ++iRow) {
oServersJsonArray.removeAt(iPosition);
}
this->poSettingsModel->fnUpdateJsonArrayValue("servers", oServersJsonArray);
endRemoveRows();
this->fnSave();
return true;
}
QModelIndex ServersListModel::index(int iRow, int iColumn, const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
return createIndex(iRow, iColumn);
//return QModelIndex();
}
QModelIndex ServersListModel::parent(const QModelIndex &oChild) const
{
qDebug() << __FUNCTION__;
return QModelIndex();
}
int ServersListModel::rowCount(const QModelIndex &oParent) const
{
int iRows = this->poSettingsModel->fnGetJsonArrayValue("servers").size();
qDebug() << __FUNCTION__ << iRows;
return iRows;
}
int ServersListModel::columnCount(const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
Q_UNUSED(oParent);
return 1;
}
bool ServersListModel::hasChildren(const QModelIndex &oParent) const
{
qDebug() << __FUNCTION__;
return false;
}
void ServersListModel::fnUpdate()
{
beginResetModel();
endResetModel();
}
QVariant ServersListModel::fnLoad()
{
qDebug() << __FUNCTION__;
return this->poSettingsModel->fnLoad();
}
QVariant ServersListModel::fnSave()
{
qDebug() << __FUNCTION__;
return this->poSettingsModel->fnSave();
}
void ServersListModel::fnRemoveRow(int iIndex)
{
this->removeRows(iIndex, 1);
}
QVariant ServersListModel::fnAddRow()
{
this->insertRows(this->rowCount(), 1);
return this->rowCount()-1;
}
QVariant ServersListModel::fnSize()
{
return this->rowCount();
}
QVariant ServersListModel::fnGetStringValue(int iIndex, QVariant sName, QVariant sDefaultValue)
{
QJsonObject oJsonObject = this->poSettingsModel->fnGetJsonArrayValue("servers")[iIndex].toObject();
QString sResult = oJsonObject[sName.toString()].toString();
return sResult.isEmpty() ? sDefaultValue : sResult;
}
QVariant ServersListModel::fnGetBoolValue(int iIndex, QVariant sName)
{
QJsonObject oJsonObject = this->poSettingsModel->fnGetJsonArrayValue("servers")[iIndex].toObject();
return oJsonObject[sName.toString()].toBool();
}