This repository was archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlarmModel.cpp
More file actions
112 lines (100 loc) · 2.61 KB
/
AlarmModel.cpp
File metadata and controls
112 lines (100 loc) · 2.61 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
#include "AlarmModel.h"
#include <AlarmData.h>
#include <AlarmFileManager.h>
AlarmModel::AlarmModel(QObject *parent) :
QAbstractListModel{parent} {}
const QList<AlarmData>& AlarmModel::rawData() const
{
return m_data;
}
int AlarmModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid())
return 0;
return m_data.size();
}
QVariant AlarmModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid())
return QVariant();
if (index.row() < 0 || index.row() >= m_data.size())
return QVariant();
auto& ret = m_data[index.row()];
switch (role)
{
case AlarmDataRoles::HourRole:
return ret.hour;
case AlarmDataRoles::MinuteRole:
return ret.minute;
case AlarmDataRoles::TitleRole:
return ret.title;
case AlarmDataRoles::ActiveDaysRole:
return ret.activeDays;
case AlarmDataRoles::IsActiveRole:
return ret.isActive;
default:
return QVariant::fromValue(ret);
}
}
QHash<int, QByteArray> AlarmModel::roleNames() const
{
auto hash = QHash<int, QByteArray>();
hash[AlarmDataRoles::HourRole] = "hour";
hash[AlarmDataRoles::MinuteRole] = "minute";
hash[AlarmDataRoles::TitleRole] = "title";
hash[AlarmDataRoles::ActiveDaysRole] = "activeDays";
hash[AlarmDataRoles::IsActiveRole] = "isActive";
return hash;
}
void AlarmModel::append(const AlarmData& data)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_data.emplace_back(data);
endInsertRows();
}
void AlarmModel::append(int hour, int minute, bool isActive, int activeDays, const QString& title)
{
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_data.emplace_back(hour, minute, activeDays, title, isActive);
endInsertRows();
}
void AlarmModel::setData(int row, const AlarmData& data)
{
if (row < 0 || row >= m_data.size())
return;
if (m_data.at(row) == data)
return;
m_data[row] = data;
emit dataChanged(index(row), index(row));
}
void AlarmModel::setData(int row, int hour, int minute, bool isActive, int activeDays, const QString& title)
{
if (row < 0 || row >= m_data.size())
return;
setData(row, {hour, minute, activeDays, title, isActive});
}
void AlarmModel::setIsActive(int row, bool isActive)
{
if (row < 0 || row >= m_data.size())
return;
if (m_data[row].isActive == isActive)
return;
m_data[row].isActive = isActive;
emit dataChanged(index(row), index(row), {AlarmModel::IsActiveRole});
}
void AlarmModel::remove(int row)
{
if (row < 0 || row >= m_data.size())
return;
beginRemoveRows(QModelIndex(), row, row);
m_data.remove(row);
endRemoveRows();
}
void AlarmModel::clear()
{
if (rowCount() == 0)
return;
beginRemoveRows(QModelIndex(), 0, rowCount() - 1);
m_data.clear();
endRemoveRows();
}