aboutsummaryrefslogtreecommitdiff
path: root/demos/widgetsgallery/phonebookmodel.cpp
blob: 23f0c413a4f09b4ab2517d204bcb45c4795c441b (plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/***************************************************************************
**
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (directui@nokia.com)
**
** This file is part of libmeegotouch.
**
** If you have questions regarding the use of this file, please contact
** Nokia at directui@nokia.com.
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation
** and appearing in the file LICENSE.LGPL included in the packaging
** of this file.
**
****************************************************************************/

#include "phonebookmodel.h"
#include "utils.h"
#include <QFile>
#include <QDir>
#include <QStringList>
#include <QTextStream>
#include <QTimer>

#include <MTheme>
#include <MSortFilterProxyModel>

PhoneBookModel::PhoneBookModel()
    : MAbstractItemModel(),
      phoneBookEntries(),
      namesList(),
      imageIdList(),
      defaultThumbnail(),
      groups(),
      groupsSize(),
      itemGroupCache()
{
    namesList = loadFakeNames();
    imageIdList = loadFakeImageIds();
}

PhoneBookModel::~PhoneBookModel()
{
    qDeleteAll(phoneBookEntries);
}

QStringList PhoneBookModel::loadFakeNames()
{
    QStringList listNames;

    // loads the names from file, read each line splitting by space
    QFile fileNames(Utils::contactsDir().append("names.txt"));
    if (fileNames.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&fileNames);
        in.setCodec("UTF-8");
        while (!in.atEnd()) {
            QString line = in.readLine();
            if (!line.isEmpty()) {
                listNames << line.split(' ');
            }
        }
    }

    // fallback
    if (listNames.isEmpty())
        listNames << "Pasi" << "Arto" << "Kiki" << "Keitty" << "Kitti" << "Adam" << "Tomas" << "Jakub" << "Sergiy" << "Rocks";

    return listNames;
}

QStringList PhoneBookModel::loadFakeImageIds()
{
    QString contactsDir = Utils::contactsDir();

    // gets all thumbnail contacts
    QDir imagesDir(contactsDir);
    imagesDir.setNameFilters(QStringList() << "*.png");

    QStringList imageContacts = imagesDir.entryList(QDir::Files);
    QStringList imageIds;
    foreach(const QString & imageName, imageContacts) {
        imageIds << QFileInfo(imageName).baseName();
    }

    return imageIds;
}

QString PhoneBookModel::generatePhoneNumber()
{
    QString phoneNumber("+358 ");

    for (int n = 0; n < 8; ++n)
        phoneNumber.append(QString::number(qrand() % 10));

    return phoneNumber;
}

PhoneBookEntry *PhoneBookModel::generateEntry()
{
    PhoneBookEntry *entry = new PhoneBookEntry;
    entry->firstName = namesList[qrand() % namesList.size()];
    entry->lastName = namesList[qrand() % namesList.size()];
    entry->fullName = QString("%1, %2").arg(entry->lastName).arg(entry->firstName);
    entry->phoneNumber = generatePhoneNumber();
    entry->thumbnail = defaultThumbnail;

    if (imageIdList.size() > 0)
        entry->thumbnailId = imageIdList[qrand() % imageIdList.size()];

    return entry;
}

int PhoneBookModel::groupCount() const
{
    return groups.count();
}

int PhoneBookModel::rowCountInGroup(int group) const
{
    if (group < groupsSize.count() && group >= 0)
        return groupsSize[group];
    else if (group == -1)
        return phoneBookEntries.count();
    
    return 0;
}

QString PhoneBookModel::groupTitle(int group) const
{
    if (group < groups.count())
        return groups[group];
    
    return QString();
}

QVariant PhoneBookModel::itemData(int row, int group, int role) const
{
    int flatRow = row;
    
    if (group >= 0 && row >= 0)
        flatRow = itemGroupCache[group][row];

    // This function will be called many times during fast panning, lets
    // check boundaries and validnes only in debug mode
    Q_ASSERT(flatRow >= 0);
    Q_ASSERT(flatRow < phoneBookEntries.size());
    
    if (role == Qt::DisplayRole)
        return QVariant::fromValue(static_cast<void *>(phoneBookEntries[flatRow]));
    else if (role == PhoneBookSortRole)
        return QVariant::fromValue(phoneBookEntries[flatRow]->lastName);
    else if (role == PhoneBookFilterRole)
        return QVariant::fromValue(phoneBookEntries[flatRow]->fullName);
    
    return QVariant();
}

bool PhoneBookModel::insertRows(int row, int count, const QModelIndex &parent)
{
    emit layoutAboutToBeChanged();
    
    beginInsertRows(parent, row, row + count - 1, false);

    for (int i = phoneBookEntries.size(); i < count; i++) {
        PhoneBookEntry *entry = generateEntry();
        phoneBookEntries.append(entry);
    }
    
    regenerateModel();

    endInsertRows();
    
    emit layoutChanged();
    return true;
}

bool PhoneBookModel::removeRows(int row, int count, const QModelIndex &parent)
{
    emit layoutAboutToBeChanged();
    
    if (count <= 0)
        return true; //Successfully removed 0 rows.
    
    int flatRow = row;
    int group = parent.row();
            
    if (isGrouped() && group >= 0)
        flatRow = itemGroupCache[group][row];

    beginRemoveRows(parent, row, row + count - 1, true);
    
    qDeleteAll(phoneBookEntries.begin() + flatRow, phoneBookEntries.begin() + flatRow + count - 1);
    phoneBookEntries.remove(flatRow, count);

    regenerateModel();
    
    endRemoveRows();

    emit layoutChanged();
    return true;
}

void PhoneBookModel::regenerateModel()
{
    groups.clear();
    groupsSize.clear();
    itemGroupCache.clear();
    for (int i = 0; i < phoneBookEntries.count(); i++) {
        PhoneBookEntry *entry = phoneBookEntries[i];
        if (!entry)
            continue;
        
        QChar group = entry->fullName[0];
        if (!groups.contains(group)) {
            groups.append(group);
            groupsSize.append(1);
        } else {
            groupsSize[groups.indexOf(group)] ++ ;
        }
        itemGroupCache[groups.indexOf(group)].append(i);
    }
}

void PhoneBookModel::thumbnailWasLoaded(const QModelIndex &index)
{
    emit dataChanged(index, index);
}

void PhoneBookModel::updateData(const QModelIndex &first, const QModelIndex &last)
{
    emit dataChanged(first, last);
}

PhoneBookImageLoader::PhoneBookImageLoader()
    : QObject(),
      thumbnailLoadingJobs()
{

}

PhoneBookImageLoader::~PhoneBookImageLoader()
{
    stopLoadingPictures();
}

void PhoneBookImageLoader::loadPictures(const QModelIndex &firstVisibleRow, const QModelIndex &lastVisibleRow)
{
    if (!firstVisibleRow.isValid() || !lastVisibleRow.isValid())
        return;

    QModelIndex topLeftRow = firstVisibleRow;
    QModelIndex bottomRightRow = lastVisibleRow;

    if (firstVisibleRow.parent().isValid())
        topLeftRow = firstVisibleRow.parent();

    if (lastVisibleRow.parent().isValid())
        bottomRightRow = lastVisibleRow.parent();

    int rowCount = bottomRightRow.row();

    for (int i = topLeftRow.row(); i <= rowCount; i++) {
        QModelIndex index(topLeftRow.sibling(i, 0));

        if (index.isValid()) {
            if (index.model()->hasChildren(index) && !index.parent().isValid()) {
                for (int ci = 0; ci < index.model()->rowCount(index); ci++) {
                    QModelIndex cIndex(index.model()->index(ci, 0, index));
                    if (cIndex.isValid())
                        addJob(cIndex);
                }
            } else
                addJob(index);
        }
    }

    // processJobQueue will issue single shots to continue loading images
    if (thumbnailLoadingJobs.count() != 0)
        QTimer::singleShot(0, this, SLOT(processJobQueue()));
}

void PhoneBookImageLoader::addJob(const QModelIndex &index)
{
    QVariant data = index.data(Qt::DisplayRole);
    PhoneBookEntry *entry = static_cast<PhoneBookEntry *>(data.value<void *>());

    // May happen if size of list model was changed
    if (entry == NULL)
        return;
    
    foreach (const Job& job, thumbnailLoadingJobs) {
        if (job.entry == entry)
            return;
    }

    if (entry->thumbnailId.isEmpty())
        return;

    if (!entry->thumbnail.isNull())
        return;

    Job job;
    job.entry = entry;
    job.row = index;
    thumbnailLoadingJobs << job;
}

void PhoneBookImageLoader::stopLoadingPictures()
{
    // Lets not process any more requests to load image because list is panning now
    thumbnailLoadingJobs.clear();
}

void PhoneBookImageLoader::processJobQueue()
{
    if (thumbnailLoadingJobs.isEmpty())
        return;

    Job job = thumbnailLoadingJobs.takeFirst();

    PhoneBookEntry *entry = job.entry;
    entry->thumbnail = QImage(Utils::contactsDir() + entry->thumbnailId + ".png");
    // indicate that thumbnail was loaded
    entry->thumbnailId = "";

    if (job.row.parent().isValid())
        notifyModel(job.row.model()->index(job.row.row(), 0, job.row.parent()));
    else
        notifyModel(job.row);

    // Continue loading and letting UI thread do something
    if (thumbnailLoadingJobs.count() > 0)
        QTimer::singleShot(0, this, SLOT(processJobQueue()));
}

// There is no need to cast if you attach model to the image loader
// but this is one way to achieve same result.
void PhoneBookImageLoader::notifyModel(const QModelIndex &index)
{
    QModelIndex realIndex = index;
    QAbstractItemModel *model = const_cast<QAbstractItemModel *>(index.model());
    Q_ASSERT(model);
    if (model) {
        MSortFilterProxyModel *sortModel = dynamic_cast<MSortFilterProxyModel *>(model);
        Q_ASSERT(sortModel);
        PhoneBookModel *phoneBookModel = dynamic_cast<PhoneBookModel *>(sortModel->sourceModel());
        if (phoneBookModel == NULL) {
            // Looks like live filtering is enabled
            // one more cast step needed to get the
            // the correct source index.
            MSortFilterProxyModel *filterModel = dynamic_cast<MSortFilterProxyModel *>(sortModel->sourceModel());
            if (filterModel) {
                phoneBookModel = dynamic_cast<PhoneBookModel *>(filterModel->sourceModel());
                realIndex = sortModel->mapToSource(index);
                sortModel = filterModel;
            }
        }
        if (phoneBookModel != NULL && sortModel != NULL) {
            phoneBookModel->thumbnailWasLoaded(sortModel->mapToSource(realIndex));
        }
    }
}