Skip to content

Latest commit

 

History

History
109 lines (90 loc) · 2.38 KB

calendarcontactmodel.cpp

File metadata and controls

109 lines (90 loc) · 2.38 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
#include "calendarcontactmodel.h"
CalendarContactModel::CalendarContactModel(QObject *parent)
: QAbstractListModel(parent)
{
}
CalendarContactModel::~CalendarContactModel()
{
}
int CalendarContactModel::rowCount(const QModelIndex &index) const
{
if (index.isValid()) {
return 0;
}
return m_contacts.length();
}
QVariant CalendarContactModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() < 0 || index.row() >= m_contacts.length())
return QVariant();
switch(role) {
case NameRole:
return m_contacts.at(index.row()).name;
case EmailRole:
return m_contacts.at(index.row()).email;
default:
return QVariant();
}
}
int CalendarContactModel::count() const
{
return m_contacts.length();
}
void CalendarContactModel::remove(int index)
{
if (index < 0 || index >= m_contacts.length()) {
return;
}
beginRemoveRows(QModelIndex(), index, index);
m_contacts.removeAt(index);
endRemoveRows();
emit countChanged();
}
bool CalendarContactModel::hasEmail(const QString &email) const
{
for (auto &contact : m_contacts) {
if (contact.email == email) {
return true;
}
}
return false;
}
QString CalendarContactModel::name(int index) const
{
if (index < 0 || index >= m_contacts.length()) {
return QString();
}
return m_contacts.at(index).name;
}
QString CalendarContactModel::email(int index) const
{
if (index < 0 || index >= m_contacts.length()) {
return QString();
}
return m_contacts.at(index).email;
}
May 24, 2019
May 24, 2019
80
QList<CalendarData::EmailContact> CalendarContactModel::getList()
81
82
83
84
{
return m_contacts;
}
May 27, 2019
May 27, 2019
85
86
87
88
89
90
91
92
93
94
void CalendarContactModel::append(const QString &name, const QString &email)
{
beginInsertRows(QModelIndex(), m_contacts.length(), m_contacts.length());
m_contacts.append(CalendarData::EmailContact(name, email));
endInsertRows();
emit countChanged();
}
void CalendarContactModel::prepend(const QString &name, const QString &email)
95
96
{
beginInsertRows(QModelIndex(), 0, 0);
May 24, 2019
May 24, 2019
97
m_contacts.prepend(CalendarData::EmailContact(name, email));
98
99
100
101
102
103
104
105
106
107
108
109
endInsertRows();
emit countChanged();
}
QHash<int, QByteArray> CalendarContactModel::roleNames() const
{
QHash<int, QByteArray> roleNames;
roleNames[NameRole] = "name";
roleNames[EmailRole] = "email";
return roleNames;
}