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
|
/****************************************************************************
**
** Copyright (C) 1992-$THISYEAR$ $TROLLTECH$. All rights reserved.
**
** This file is part of the $MODULE$ of the Qt Toolkit.
**
** $TROLLTECH_DUAL_LICENSE$
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/
#include "contactmodel.h"
#include "qmltypes.h"
ContactModel::ContactModel(QObject *parent) : QListModelInterface(parent)
{
QFile f("../contacts/contacts.txt");
f.open(QIODevice::ReadOnly);
QTextStream ts(&f);
QString text = ts.readLine();
while(!text.isEmpty()) {
Contact *c = new Contact;
QStringList list = text.split(" ");
c->setFirstName(list[0]);
c->setLastName(list[1]);
for (int i = 2; i < list.count(); ++i)
c->addEmail(list[i]);
//contactList.append(c);
insertContact(c);
text = ts.readLine();
}
f.close();
}
ContactModel::~ContactModel()
{
while (!contactList.isEmpty()) {
Contact *c = contactList.takeFirst();
delete c;
}
}
int ContactModel::count() const
{
return contactList.count();
}
QHash<int,QVariant> ContactModel::data(int index, const QList<int> &roles) const
{
QHash<int,QVariant> returnHash;
for (int i = 0; i < roles.size(); ++i) {
int role = roles.at(i);
QVariant info;
switch(role) {
case PortraitRole:
info = "contact.png";
break;
case FirstNameRole:
info = contactList.at(index)->firstName();
break;
case LastNameRole:
info = contactList.at(index)->lastName();
break;
case CompanyRole:
info = contactList.at(index)->company();
break;
case EmailsRole:
info = contactList.at(index)->emails();
break;
case AddressesRole:
//returns QVariant BOOL
info = QVariant::fromValue(contactList.at(index)->addresses());
break;
case NumbersRole:
info = QVariant::fromValue(contactList.at(index)->numbers());
break;
default:
break;
}
returnHash.insert(role, info);
}
return returnHash;
}
QString ContactModel::toString(int role) const
{
switch(role) {
case PortraitRole:
return "portrait";
case FirstNameRole:
return "firstName";
case LastNameRole:
return "lastName";
case CompanyRole:
return "company";
case EmailsRole:
return "emails";
case AddressesRole:
return "addresses";
case NumbersRole:
return "numbers";
default:
return "";
}
}
QList<int> ContactModel::roles() const
{
return QList<int>() << PortraitRole << FirstNameRole << LastNameRole << CompanyRole << EmailsRole << AddressesRole << NumbersRole;
}
void ContactModel::deleteContact(int index)
{
delete contactList.takeAt(index);
emit itemsRemoved(index, 1);
}
int ContactModel::insertContact(Contact *contact)
{
int index = 0;
QString fullName = contact->lastName();
index = findIndex(fullName);
contactList.insert(index, contact);
emit itemsInserted(index, 1);
return index;
}
//search - binary search algorithm lastname only
int ContactModel::findIndex(QString &searchName) const
{
int start = 0;
int end = contactList.size()-1;
int middle = 0;
QString middleString;
while (start <= end)
{
middle = (start+end)/2;
middleString = contactList.at(middle)->lastName();
if (isAfter(searchName, middleString) < 0) start = middle+1;
else if( isAfter(middleString, searchName) < 0) end = middle-1;
else return middle;
}
return start;
}
int ContactModel::isAfter(QString &name1, QString &name2) const
{
//if c1 is after c2 alphabetically, return positive
int compString = QString::compare(name1, name2, Qt::CaseInsensitive);
return -compString;
}
|