-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofilemodel.cpp
151 lines (139 loc) · 4.71 KB
/
profilemodel.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
/*
* Powersched
* Copyright (C) 2019 Jiri Babocky
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "profilemodel.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonValue>
ProfileModel::ProfileModel(QObject *parent):QAbstractListModel(parent){
}
ProfileModel::ProfileModel(const QString& filename, QObject *parent):QAbstractListModel(parent){
QFile file(filename);
if (!file.open(QIODevice::ReadOnly))
return;
QByteArray bytes = file.readAll();
file.close();
QJsonDocument document = QJsonDocument::fromJson(bytes);
if (!document.isArray())
return;
for (QJsonValueRef value:document.array()){
if (value.isObject()){
Profile profile(value.toObject());
if (profile.name() != "" && !contains(profile.name()))
_data.append(profile);
}
}
}
bool ProfileModel::contains(const QString &name) const{
bool found = false;
for (const Profile& profile:_data){
if (profile.name() == name){
found = true;
break;
}
}
return found;
}
QModelIndex ProfileModel::find(const QString& name) const{
int row = 0;
for (const Profile& profile:_data){
if (profile.name() == name)
return index(row);
row++;
}
return QModelIndex();
}
bool ProfileModel::save(const QString &filename) const{
QJsonArray array;
for (const Profile& profile:_data)
array.append(QJsonValue(profile.toJson()));
QJsonDocument document(array);
QByteArray bytes = document.toJson(QJsonDocument::Indented);
QFile file(filename);
if (!file.open(QIODevice::WriteOnly))
return false;
if (file.write(bytes) == -1){
file.close();
return false;
}
file.close();
return true;
}
QVariant ProfileModel::headerData(int section, Qt::Orientation orientation, int role) const{
if (section != 0 || orientation != Qt::Horizontal || role != Qt::DisplayRole)
return QVariant();
else
return QVariant("name");
}
int ProfileModel::rowCount(const QModelIndex &parent) const{
if (parent.isValid())
return 0;
return _data.size();
}
QVariant ProfileModel::data(const QModelIndex &index, int role) const{
if (!index.isValid())
return QVariant();
if (role == Qt::DisplayRole)
return QVariant(_data[index.row()].name());
else if (role == StandbyRole)
return QVariant(_data[index.row()].standby());
else if (role == SuspendRole)
return QVariant(_data[index.row()].suspend());
else if (role == OffRole)
return QVariant(_data[index.row()].off());
else if (role == AwakeRole)
return QVariant(_data[index.row()].awake());
return QVariant();
}
bool ProfileModel::setData(const QModelIndex &index, const QVariant &value, int role){
if (!index.isValid() || index.column() != 0 || index.row() >= _data.size())
return false;
if (role == Qt::DisplayRole){
_data[index.row()].setName(value.toString());
emit dataChanged(index, index);
return true;
} else if (role == ProfileRole::StandbyRole){
_data[index.row()].setStandby(value.toUInt());
emit dataChanged(index, index);
return true;
} else if (role == ProfileRole::SuspendRole){
_data[index.row()].setSuspend(value.toUInt());
emit dataChanged(index, index);
return true;
} else if (role == ProfileRole::OffRole){
_data[index.row()].setOff(value.toUInt());
emit dataChanged(index, index);
return true;
} else if (role == ProfileRole::AwakeRole){
_data[index.row()].setAwake(value.toBool());
emit dataChanged(index, index);
return true;
}
return false;
}
bool ProfileModel::removeRows(int row, int count, const QModelIndex &parent){
beginRemoveRows(parent, row, row + count - 1);
for (int i = 0; i < count; i++)
_data.removeAt(row);
endRemoveRows();
return true;
}
void ProfileModel::append(const Profile &profile){
beginInsertRows(QModelIndex(), _data.length(), _data.length()+1);
_data.append(profile);
endInsertRows();
}