-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger.cpp
146 lines (133 loc) · 4.17 KB
/
trigger.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
/*
* 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 "trigger.h"
Trigger::Trigger(const QTime& time, const QBitArray& weekdays, QModelIndex profile){
_time = time;
_weekdays = weekdays;
_profile = QPersistentModelIndex(profile);
}
Trigger::Trigger(const QJsonObject data, ProfileModel* profiles){
if (!data.contains("time") || !data.contains("monday") || !data.contains("tuesday") || !data.contains("wednesday") || !data.contains("thursday") || !data.contains("friday") || !data.contains("saturday") || !data.contains("sunday") || !data.contains("profile"))
return;
if (data["time"].isString()){
_time = QTime::fromString(data["time"].toString(), "hh:mm:ss");
if (!_time.isValid())
return;
}
else
return;
_weekdays = QBitArray(7, false);
if (data["monday"].isBool())
_weekdays[0] = data["monday"].toBool();
else
return;
if (data["tuesday"].isBool())
_weekdays[1] = data["tuesday"].toBool();
else
return;
if (data["wednesday"].isBool())
_weekdays[2] = data["wednesday"].toBool();
else
return;
if (data["thursday"].isBool())
_weekdays[3] = data["thursday"].toBool();
else
return;
if (data["friday"].isBool())
_weekdays[4] = data["friday"].toBool();
else
return;
if (data["saturday"].isBool())
_weekdays[5] = data["saturday"].toBool();
else
return;
if (data["sunday"].isBool())
_weekdays[6] = data["sunday"].toBool();
else
return;
if (data["profile"].isString()){
QModelIndex index = profiles->find(data["profile"].toString());
_profile = QPersistentModelIndex(index);
} else
return;
}
QJsonObject Trigger::toJson() const{
QJsonObject result;
result.insert("time", QJsonValue(_time.toString("HH:mm:ss")));
result.insert("monday", _weekdays[0]);
result.insert("tuesday", _weekdays[1]);
result.insert("wednesday", _weekdays[2]);
result.insert("thursday", _weekdays[3]);
result.insert("friday", _weekdays[4]);
result.insert("saturday", _weekdays[5]);
result.insert("sunday", _weekdays[6]);
result.insert("profile", _profile.data(Qt::DisplayRole).toString());
return result;
}
QTime Trigger::time() const{
return _time;
}
bool Trigger::weekday(uint8_t day) const{
return _weekdays[day];
}
QPersistentModelIndex Trigger::profile() const{
return _profile;
}
QString Trigger::name() const{
QString profileName;
if (_profile.isValid())
profileName = _profile.data(Qt::DisplayRole).toString();
else
profileName = "???";
QString weekdays;
if (_weekdays[0])
weekdays += " Mo";
if (_weekdays[1])
weekdays += " Tu";
if (_weekdays[2])
weekdays += " We";
if (_weekdays[3])
weekdays += " Th";
if (_weekdays[4])
weekdays += " Fr";
if (_weekdays[5])
weekdays += " Sat";
if (_weekdays[6])
weekdays += " Sun";
QString time = _time.toString("HH:mm");
return profileName+":"+weekdays+" "+time;
}
bool Trigger::valid() const{
return _profile.isValid();
}
void Trigger::setTime(const QTime &time){
_time = time;
}
bool Trigger::setWeekday(uint8_t day, bool value){
if (day < 7){
_weekdays[day] = value;
return true;
} else
return false;
}
bool Trigger::setProfile(const QModelIndex profile){
if (profile.isValid()){
_profile = QPersistentModelIndex(profile);
return true;
} else
return false;
}