85
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description: caitemmodel.cpp
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
86
|
18 |
#include <HbIcon>
|
85
|
19 |
|
|
20 |
#include "caitemmodel.h"
|
|
21 |
#include "caitemmodel_p.h"
|
|
22 |
#include "canotifier.h"
|
|
23 |
#include "canotifierfilter.h"
|
87
|
24 |
#include "caclienttest_global.h"
|
85
|
25 |
|
|
26 |
// Constants
|
|
27 |
const QSize defaultIconSize(30, 30);
|
|
28 |
|
|
29 |
// ======== MEMBER FUNCTIONS ========
|
|
30 |
|
|
31 |
/*!
|
|
32 |
* \class CaItemModel
|
|
33 |
*
|
|
34 |
* \brief This class provides model containing CaEntry class objects.
|
|
35 |
*
|
|
36 |
* To create instance of CaItemModel object, you need to pass CaQuery
|
|
37 |
* object in constructor. CaQuery should describe items that you want
|
|
38 |
* to have in a model.
|
|
39 |
*
|
|
40 |
* \example
|
|
41 |
* \code
|
|
42 |
*
|
|
43 |
* CaQuery query;
|
|
44 |
* query.setFlagsOn(VisibleEntryFlag);
|
|
45 |
* query.setParentId(collectionId);
|
|
46 |
* CaItemModel* model = new CaItemModel(query, this);
|
|
47 |
*
|
|
48 |
* \endcode
|
|
49 |
*/
|
|
50 |
|
|
51 |
/*!
|
|
52 |
Constructor.
|
|
53 |
\param query query describing entries that should be present in a model.
|
|
54 |
\param parent parent of a model
|
|
55 |
*/
|
87
|
56 |
CaItemModel::CaItemModel(const CaQuery &query, QObject *parent) :
|
85
|
57 |
QAbstractItemModel(parent), m_d(new CaItemModelPrivate(query, this))
|
|
58 |
{
|
|
59 |
|
|
60 |
}
|
|
61 |
|
|
62 |
/*!
|
|
63 |
Destructor
|
|
64 |
*/
|
|
65 |
CaItemModel::~CaItemModel()
|
|
66 |
{
|
|
67 |
delete m_d;
|
|
68 |
}
|
|
69 |
|
|
70 |
/*!
|
|
71 |
Returns number of columns
|
|
72 |
\param parent not used
|
|
73 |
\retval 1
|
|
74 |
|
|
75 |
\code
|
|
76 |
...
|
|
77 |
// to get number of columns in a model
|
|
78 |
int columns = model->columnCount();
|
|
79 |
...
|
|
80 |
\endcode
|
|
81 |
|
|
82 |
*/
|
|
83 |
int CaItemModel::columnCount(const QModelIndex &parent) const
|
|
84 |
{
|
|
85 |
Q_UNUSED(parent);
|
|
86 |
//model keeps entries in a column 0 and a parent entry in a column 1
|
|
87 |
//parent entry is treated as an invisible root item,
|
|
88 |
//so column count is always 1
|
|
89 |
return 1;
|
|
90 |
}
|
|
91 |
|
|
92 |
/*!
|
|
93 |
Returns number of rows
|
|
94 |
\param parent not used
|
|
95 |
\retval number of rows
|
|
96 |
|
|
97 |
\code
|
|
98 |
...
|
|
99 |
// to get number of rows in a model
|
|
100 |
int rows = model->rowCount();
|
|
101 |
...
|
|
102 |
\endcode
|
|
103 |
*/
|
|
104 |
int CaItemModel::rowCount(const QModelIndex &parent) const
|
|
105 |
{
|
|
106 |
Q_UNUSED(parent);
|
|
107 |
return m_d->rowCount();
|
|
108 |
}
|
|
109 |
|
|
110 |
/*!
|
|
111 |
Returns QModelIndex of an item
|
|
112 |
\param row row in which an item is placed
|
|
113 |
\param column not used
|
|
114 |
\param parent not used
|
|
115 |
\retval index of item in model
|
|
116 |
|
|
117 |
\code
|
|
118 |
...
|
|
119 |
// to get model index of an item
|
|
120 |
QModelIndex modelIndex = model->index(5);
|
|
121 |
...
|
|
122 |
\endcode
|
|
123 |
|
|
124 |
*/
|
|
125 |
QModelIndex CaItemModel::index(int row, int column,
|
87
|
126 |
const QModelIndex &parent) const
|
85
|
127 |
{
|
|
128 |
Q_UNUSED(column);
|
|
129 |
Q_UNUSED(parent);
|
|
130 |
return m_d->index(row);
|
|
131 |
}
|
|
132 |
|
|
133 |
/*!
|
|
134 |
Returns parent item
|
|
135 |
\param child index (ignored)
|
|
136 |
\retval parent index. in case of CaItemModel it is always QModelIndex()
|
|
137 |
|
|
138 |
\code
|
|
139 |
...
|
|
140 |
// to get model index of a parent of an item
|
|
141 |
QModelIndex parentModelIndex = model->parent(childModelIndex);
|
|
142 |
...
|
|
143 |
\endcode
|
|
144 |
*/
|
|
145 |
QModelIndex CaItemModel::parent(const QModelIndex &index) const
|
|
146 |
{
|
|
147 |
Q_UNUSED(index);
|
|
148 |
return QModelIndex();
|
|
149 |
}
|
|
150 |
|
|
151 |
/*!
|
|
152 |
Returns root item model index
|
|
153 |
\retval root item model index
|
|
154 |
|
|
155 |
\code
|
|
156 |
...
|
|
157 |
// to get model index of a root item
|
|
158 |
QModelIndex rootIndex = model->root();
|
|
159 |
...
|
|
160 |
\endcode
|
|
161 |
*/
|
|
162 |
QModelIndex CaItemModel::root() const
|
|
163 |
{
|
|
164 |
return m_d->root();
|
|
165 |
}
|
|
166 |
|
|
167 |
/*!
|
|
168 |
Returns appropiate model's data
|
|
169 |
\param index model index
|
|
170 |
\param role which data role to return
|
|
171 |
\retval models data
|
|
172 |
|
|
173 |
\code
|
|
174 |
...
|
|
175 |
// to get data for model item
|
|
176 |
QVariant = model->data(itemModelIndex, Qt::DisplayRole);
|
|
177 |
...
|
|
178 |
\endcode
|
|
179 |
|
|
180 |
*/
|
|
181 |
QVariant CaItemModel::data(const QModelIndex &index, int role) const
|
|
182 |
{
|
|
183 |
return m_d->data(index, role);
|
|
184 |
}
|
|
185 |
|
|
186 |
/*!
|
|
187 |
Disables or enables auto-update feature of the model
|
|
188 |
\param autoUpdate true to enable autoupdate, false to disable
|
|
189 |
|
|
190 |
\code
|
|
191 |
...
|
|
192 |
// to enable model auto update
|
|
193 |
model->setAutoUpdate(true);
|
|
194 |
...
|
|
195 |
\endcode
|
|
196 |
|
|
197 |
*/
|
|
198 |
void CaItemModel::setAutoUpdate(bool autoUpdate)
|
|
199 |
{
|
|
200 |
m_d->setAutoUpdate(autoUpdate);
|
|
201 |
}
|
|
202 |
|
|
203 |
/*!
|
|
204 |
Disables or enables secondline feature of the model
|
|
205 |
\param secondLine enable or disable second line
|
|
206 |
|
|
207 |
\code
|
|
208 |
...
|
|
209 |
// to enable model second line visibility
|
|
210 |
model->setSecondLineVisibility(true);
|
|
211 |
...
|
|
212 |
\endcode
|
|
213 |
|
|
214 |
*/
|
|
215 |
void CaItemModel::setSecondLineVisibility(bool secondLineVisible)
|
|
216 |
{
|
|
217 |
m_d->setSecondLineVisibility(secondLineVisible);
|
|
218 |
}
|
|
219 |
|
|
220 |
/*!
|
|
221 |
Gets second line visibility attribute
|
|
222 |
\retrun second line visibility attribute
|
|
223 |
|
|
224 |
\code
|
|
225 |
...
|
|
226 |
// to check second line visibility attribute
|
|
227 |
bool visibility = model->secondLineVisibility();
|
|
228 |
...
|
|
229 |
\endcode
|
|
230 |
|
|
231 |
*/
|
|
232 |
bool CaItemModel::secondLineVisibility() const
|
|
233 |
{
|
|
234 |
return m_d->secondLineVisibility();
|
|
235 |
}
|
|
236 |
/*!
|
|
237 |
Returns auto update status
|
|
238 |
\retval true if autoupdate is on, false if not
|
|
239 |
|
|
240 |
\code
|
|
241 |
...
|
|
242 |
// to check auto update attribute
|
|
243 |
bool autoUpdate = model->isAutoUpdate();
|
|
244 |
...
|
|
245 |
\endcode
|
|
246 |
|
|
247 |
*/
|
|
248 |
bool CaItemModel::isAutoUpdate() const
|
|
249 |
{
|
|
250 |
return m_d->notifierExists();
|
|
251 |
}
|
|
252 |
|
|
253 |
/*!
|
|
254 |
Method for setting sorting order on model
|
|
255 |
\param sortAttribute sort attribute (by name, timestamp, ect...)
|
|
256 |
\param sortOrder sort order (ascending, descending)
|
|
257 |
|
|
258 |
\code
|
|
259 |
...
|
|
260 |
// to set sort order in a model
|
|
261 |
model->setSort(NameSortAttribute, Qt::Ascending);
|
|
262 |
...
|
|
263 |
\endcode
|
|
264 |
|
|
265 |
*/
|
|
266 |
void CaItemModel::setSort(SortAttribute sortAttribute,
|
87
|
267 |
Qt::SortOrder sortOrder)
|
85
|
268 |
{
|
|
269 |
m_d->setSort(sortAttribute, sortOrder);
|
|
270 |
}
|
|
271 |
|
|
272 |
/*!
|
|
273 |
Method for setting icon size
|
|
274 |
\param size icon size to display
|
|
275 |
|
|
276 |
\code
|
|
277 |
...
|
|
278 |
// to set an icon size in a model
|
|
279 |
QSize iconSize(50,50);
|
|
280 |
model->setIconSize(iconSize);
|
|
281 |
...
|
|
282 |
\endcode
|
|
283 |
|
|
284 |
*/
|
|
285 |
void CaItemModel::setIconSize(const QSize &size)
|
|
286 |
{
|
|
287 |
m_d->setIconSize(size);
|
|
288 |
}
|
|
289 |
|
|
290 |
/*!
|
|
291 |
Method for getting icon size
|
|
292 |
\param size icon size to display
|
|
293 |
*/
|
|
294 |
QSize CaItemModel::getIconSize() const
|
|
295 |
{
|
|
296 |
return m_d->getIconSize();
|
|
297 |
}
|
|
298 |
|
|
299 |
/*!
|
|
300 |
Updates model with fresh entries
|
|
301 |
|
|
302 |
\code
|
|
303 |
...
|
|
304 |
// to refresh a model
|
|
305 |
model->updateModel();
|
|
306 |
...
|
|
307 |
\endcode
|
|
308 |
|
|
309 |
*/
|
|
310 |
void CaItemModel::updateModel()
|
|
311 |
{
|
|
312 |
m_d->updateModel();
|
|
313 |
}
|
|
314 |
|
|
315 |
/*!
|
|
316 |
Sets parent and fetch children items from a storage
|
|
317 |
\param parentId id of a collection
|
|
318 |
|
|
319 |
\code
|
|
320 |
...
|
|
321 |
// to set a new parent id
|
|
322 |
int newParentId = 10;
|
|
323 |
model->setParentId(newParentId);
|
|
324 |
...
|
|
325 |
\endcode
|
|
326 |
|
|
327 |
*/
|
|
328 |
void CaItemModel::setParentId(int parentId)
|
|
329 |
{
|
|
330 |
m_d->setParentId(parentId);
|
|
331 |
}
|
|
332 |
|
|
333 |
/*!
|
87
|
334 |
Sets flags to mQuery which should be enabled.
|
|
335 |
This method does not update current model - only mQuery member.
|
|
336 |
Now to do this setParentId have to be invoke.
|
|
337 |
\param onFlags flags.
|
|
338 |
|
|
339 |
\code
|
|
340 |
...
|
|
341 |
// to set a flags enabled
|
|
342 |
model->setFlagsOn(RemovableEntryFlag);
|
|
343 |
...
|
|
344 |
\endcode
|
|
345 |
|
|
346 |
*/
|
|
347 |
void CaItemModel::setFlagsOn(const EntryFlags &onFlags)
|
|
348 |
{
|
|
349 |
m_d->setFlagsOn(onFlags);
|
|
350 |
}
|
|
351 |
|
|
352 |
/*!
|
|
353 |
Sets flags to mQuery which should be disabled.
|
|
354 |
This method does not update current model - only mQuery member.
|
|
355 |
Now to do this setParentId have to be invoke.
|
|
356 |
\param offFlags flags.
|
|
357 |
|
|
358 |
\code
|
|
359 |
...
|
|
360 |
// to set a flags disabled
|
|
361 |
model->setFlagsOff(RemovableEntryFlag);
|
|
362 |
...
|
|
363 |
\endcode
|
|
364 |
|
|
365 |
*/
|
|
366 |
void CaItemModel::setFlagsOff(const EntryFlags &offFlags)
|
|
367 |
{
|
|
368 |
m_d->setFlagsOff(offFlags);
|
|
369 |
}
|
|
370 |
|
|
371 |
/*!
|
85
|
372 |
Returns an entry from model
|
|
373 |
\param index of entry in model
|
|
374 |
\retval pointer to an entry
|
|
375 |
|
|
376 |
\code
|
|
377 |
...
|
|
378 |
// to get an entry from a model
|
|
379 |
CaEntry* entry = model->entry(entryModelIndex);
|
|
380 |
...
|
|
381 |
delete entry;
|
|
382 |
\endcode
|
|
383 |
|
|
384 |
*/
|
92
|
385 |
QSharedPointer<CaEntry> CaItemModel::entry(const QModelIndex &index) const
|
85
|
386 |
{
|
|
387 |
return m_d->entry(index);
|
|
388 |
}
|
|
389 |
|
|
390 |
/*!
|
|
391 |
Constructor
|
|
392 |
\param query needed to create model
|
|
393 |
\param pointer to public implementation object connected
|
|
394 |
*/
|
|
395 |
CaItemModelPrivate::CaItemModelPrivate(const CaQuery &query,
|
87
|
396 |
CaItemModel *itemModelPublic) :
|
92
|
397 |
QObject(), m_q(itemModelPublic), mParentEntry(), mQuery(query),
|
85
|
398 |
mService(CaService::instance()), mEntries(mService), mNotifier(NULL),
|
|
399 |
mSize(defaultIconSize), mSecondLineVisibility(true)
|
|
400 |
{
|
|
401 |
updateModel();
|
|
402 |
setAutoUpdate(true);
|
|
403 |
}
|
|
404 |
|
|
405 |
/*!
|
|
406 |
Destructor
|
|
407 |
*/
|
|
408 |
CaItemModelPrivate::~CaItemModelPrivate()
|
|
409 |
{
|
|
410 |
mEntries.clear();
|
|
411 |
delete mNotifier;
|
|
412 |
}
|
|
413 |
|
|
414 |
/*!
|
|
415 |
Returns count of rows in model
|
|
416 |
\retval number of rows
|
|
417 |
*/
|
|
418 |
int CaItemModelPrivate::rowCount() const
|
|
419 |
{
|
|
420 |
return mEntries.count();
|
|
421 |
}
|
|
422 |
|
|
423 |
/*!
|
|
424 |
Returns QModelIndex of an item
|
|
425 |
\param row row
|
|
426 |
\retval QModelIndex of item in model
|
|
427 |
*/
|
|
428 |
QModelIndex CaItemModelPrivate::index(int row)
|
|
429 |
{
|
|
430 |
if ((row >= 0) && (row < mEntries.count())) {
|
|
431 |
return m_q->createIndex(row, 0);
|
|
432 |
} else {
|
|
433 |
return QModelIndex();
|
|
434 |
}
|
|
435 |
}
|
|
436 |
|
|
437 |
/*!
|
|
438 |
Returns appropiate model's data
|
|
439 |
\param modelIndex model index
|
|
440 |
\param role which data role to return
|
|
441 |
\retval models data as QVariant
|
|
442 |
*/
|
|
443 |
QVariant CaItemModelPrivate::data(const QModelIndex &modelIndex,
|
87
|
444 |
int role) const
|
85
|
445 |
{
|
87
|
446 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::data");
|
|
447 |
QVariant variant;
|
|
448 |
if (modelIndex.isValid()) {
|
|
449 |
switch (role) {
|
|
450 |
case Qt::DisplayRole:
|
|
451 |
variant = displayRole(modelIndex);
|
|
452 |
break;
|
|
453 |
case Qt::DecorationRole:
|
|
454 |
variant = QVariant(HbIcon(entry(modelIndex)->makeIcon(mSize)));
|
|
455 |
break;
|
|
456 |
case CaItemModel::IdRole:
|
|
457 |
variant = QVariant(entry(modelIndex)->id());
|
|
458 |
break;
|
|
459 |
case CaItemModel::TypeRole:
|
|
460 |
variant = QVariant(entry(modelIndex)->entryTypeName());
|
|
461 |
break;
|
|
462 |
case CaItemModel::FlagsRole:
|
|
463 |
variant = qVariantFromValue(entry(modelIndex)->flags());
|
|
464 |
break;
|
|
465 |
case CaItemModel::TextRole:
|
|
466 |
variant = QVariant(entry(modelIndex)->text());
|
|
467 |
break;
|
92
|
468 |
case CaItemModel::FullTextRole:
|
|
469 |
variant = QVariant(entry(modelIndex)->text() + QString(" ")
|
|
470 |
+ entry(modelIndex)->description());
|
|
471 |
break;
|
87
|
472 |
default:
|
|
473 |
variant = QVariant(QVariant::Invalid);
|
|
474 |
}
|
85
|
475 |
}
|
87
|
476 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::data");
|
|
477 |
return variant;
|
85
|
478 |
}
|
|
479 |
|
|
480 |
/*!
|
|
481 |
Disables or enables auto-update feature of the model
|
|
482 |
\param autoUpdate (HsMenuAutoUpdate)
|
|
483 |
*/
|
|
484 |
void CaItemModelPrivate::setAutoUpdate(bool autoUpdate)
|
|
485 |
{
|
87
|
486 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::setAutoUpdate");
|
85
|
487 |
if (autoUpdate) {
|
|
488 |
if (!mNotifier) {
|
|
489 |
CaNotifierFilter filter(mQuery);
|
|
490 |
mNotifier = mService->createNotifier(filter);
|
|
491 |
connectSlots();
|
|
492 |
}
|
|
493 |
} else {
|
|
494 |
disconnectSlots();
|
|
495 |
delete mNotifier;
|
|
496 |
mNotifier = NULL;
|
|
497 |
}
|
87
|
498 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::setAutoUpdate");
|
85
|
499 |
}
|
|
500 |
|
|
501 |
/*!
|
|
502 |
Method for setting sorting order on model
|
|
503 |
(probably will be moved to MenuService)
|
|
504 |
\param sortOrder sorting order (SortAttribute)
|
|
505 |
*/
|
|
506 |
void CaItemModelPrivate::setSort(SortAttribute sortAttribute,
|
87
|
507 |
Qt::SortOrder sortOrder)
|
85
|
508 |
{
|
87
|
509 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::setSort");
|
85
|
510 |
mQuery.setSort(sortAttribute, sortOrder);
|
|
511 |
updateLayout();
|
87
|
512 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::setSort");
|
85
|
513 |
}
|
|
514 |
|
|
515 |
/*!
|
|
516 |
Method for setting icon size
|
|
517 |
\param size icon size to display
|
|
518 |
*/
|
|
519 |
void CaItemModelPrivate::setIconSize(const QSize &size)
|
|
520 |
{
|
|
521 |
if (mSize == size)
|
|
522 |
return;
|
|
523 |
m_q->layoutAboutToBeChanged();
|
|
524 |
mSize = size;
|
|
525 |
m_q->layoutChanged();
|
|
526 |
}
|
|
527 |
|
|
528 |
/*!
|
|
529 |
Method for getting icon size
|
|
530 |
\retval icon size to display
|
|
531 |
*/
|
|
532 |
QSize CaItemModelPrivate::getIconSize() const
|
|
533 |
{
|
|
534 |
return mSize;
|
|
535 |
}
|
|
536 |
|
|
537 |
|
|
538 |
/*!
|
|
539 |
Gets index of the parent item
|
|
540 |
\retval QModelIndex representing root item
|
|
541 |
*/
|
|
542 |
QModelIndex CaItemModelPrivate::root()
|
|
543 |
{
|
|
544 |
if (mQuery.parentId()) {
|
|
545 |
return m_q->createIndex(0, 1);
|
|
546 |
} else {
|
|
547 |
return QModelIndex();
|
|
548 |
}
|
|
549 |
}
|
|
550 |
|
|
551 |
/*!
|
|
552 |
Returns an entry from model
|
|
553 |
\param modelIndex index of entry in model
|
|
554 |
\retval pointer to an entry
|
|
555 |
*/
|
92
|
556 |
QSharedPointer<CaEntry> CaItemModelPrivate::entry(const QModelIndex &modelIndex) const
|
85
|
557 |
{
|
|
558 |
if (modelIndex.column() == 1) {
|
|
559 |
return mParentEntry;
|
|
560 |
} else {
|
|
561 |
return mEntries.at(modelIndex.row());
|
|
562 |
}
|
|
563 |
}
|
|
564 |
|
|
565 |
/*!
|
|
566 |
Disables or enables secondline feature of the model
|
|
567 |
\param secondLine disables or enables second line
|
|
568 |
*/
|
|
569 |
void CaItemModelPrivate::setSecondLineVisibility(bool secondLineVisibility)
|
|
570 |
{
|
|
571 |
if (mSecondLineVisibility == secondLineVisibility)
|
|
572 |
return;
|
|
573 |
m_q->layoutAboutToBeChanged();
|
|
574 |
mSecondLineVisibility = secondLineVisibility;
|
|
575 |
m_q->layoutChanged();
|
|
576 |
}
|
|
577 |
|
|
578 |
/*!
|
|
579 |
Gets second line visibility attribute
|
|
580 |
\retrun second line visibility attribute
|
|
581 |
*/
|
|
582 |
bool CaItemModelPrivate::secondLineVisibility() const
|
|
583 |
{
|
|
584 |
return mSecondLineVisibility;
|
|
585 |
}
|
|
586 |
|
|
587 |
/*!
|
|
588 |
Returns proper display role for item
|
|
589 |
\param modelIndex item index
|
|
590 |
\retval QVariant containing display role
|
|
591 |
*/
|
|
592 |
QVariant CaItemModelPrivate::displayRole(const QModelIndex &modelIndex) const
|
|
593 |
{
|
87
|
594 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::displayRole")
|
|
595 |
|
85
|
596 |
QVariant result;
|
|
597 |
if (mSecondLineVisibility) {
|
|
598 |
if (entry(modelIndex)->description().isEmpty()) {
|
|
599 |
result = entry(modelIndex)->text();
|
|
600 |
} else {
|
|
601 |
QList<QVariant> text;
|
|
602 |
text << entry(modelIndex)->text();
|
|
603 |
text << entry(modelIndex)->description();
|
|
604 |
result = QVariant(text);
|
|
605 |
}
|
|
606 |
} else {
|
|
607 |
result = entry(modelIndex)->text();
|
|
608 |
}
|
87
|
609 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::displayRole")
|
85
|
610 |
return result;
|
|
611 |
}
|
|
612 |
|
|
613 |
/*!
|
|
614 |
Sets parent
|
|
615 |
\param parentId
|
|
616 |
*/
|
|
617 |
void CaItemModelPrivate::setParentId(int parentId)
|
|
618 |
{
|
87
|
619 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::setParentId");
|
85
|
620 |
mQuery.setParentId(parentId);
|
|
621 |
if (mNotifier) {
|
|
622 |
delete mNotifier;
|
|
623 |
mNotifier = mService->createNotifier(CaNotifierFilter(mQuery));
|
|
624 |
reconnectSlots();
|
|
625 |
}
|
|
626 |
updateModel();
|
87
|
627 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::setParentId");
|
|
628 |
|
|
629 |
}
|
|
630 |
|
|
631 |
/*!
|
|
632 |
Sets flags to mQuery which should be enabled.
|
|
633 |
\param onFlags flags.
|
|
634 |
|
|
635 |
*/
|
|
636 |
void CaItemModelPrivate::setFlagsOn(const EntryFlags &onFlags)
|
|
637 |
{
|
|
638 |
mQuery.setFlagsOn(onFlags);
|
85
|
639 |
}
|
|
640 |
|
87
|
641 |
/*!
|
|
642 |
Sets flags to mQuery which should be disabled.
|
|
643 |
\param offFlags flags.
|
|
644 |
|
|
645 |
\code
|
|
646 |
...
|
|
647 |
// to set a new parent id
|
|
648 |
model->setFlagsOff(RemovableEntryFlag);
|
|
649 |
...
|
|
650 |
\endcode
|
|
651 |
|
|
652 |
*/
|
|
653 |
void CaItemModelPrivate::setFlagsOff(const EntryFlags &offFlags)
|
|
654 |
{
|
|
655 |
mQuery.setFlagsOff(offFlags);
|
|
656 |
}
|
85
|
657 |
|
|
658 |
/*!
|
|
659 |
Checks if notifier exists
|
|
660 |
\retval true if notifier exists otherwise false
|
|
661 |
*/
|
|
662 |
bool CaItemModelPrivate::notifierExists() const
|
|
663 |
{
|
|
664 |
if (mNotifier) {
|
|
665 |
return true;
|
|
666 |
}
|
|
667 |
return false;
|
|
668 |
}
|
|
669 |
|
|
670 |
|
|
671 |
/*!
|
|
672 |
Updates model with fresh entries and resets model
|
|
673 |
*/
|
|
674 |
void CaItemModelPrivate::updateModel()
|
|
675 |
{
|
87
|
676 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateModel");
|
|
677 |
|
85
|
678 |
mEntries.reloadEntries(mQuery);
|
|
679 |
updateParentEntry();
|
|
680 |
m_q->reset();
|
87
|
681 |
|
|
682 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateModel");
|
85
|
683 |
}
|
|
684 |
|
|
685 |
/*!
|
|
686 |
Updates parent entry
|
|
687 |
*/
|
|
688 |
void CaItemModelPrivate::updateParentEntry()
|
|
689 |
{
|
87
|
690 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateParentEntry");
|
|
691 |
|
85
|
692 |
if (mQuery.parentId()) {
|
|
693 |
mParentEntry = mService->getEntry(mQuery.parentId());
|
|
694 |
}
|
87
|
695 |
|
|
696 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateParentEntry");
|
|
697 |
|
85
|
698 |
}
|
|
699 |
|
|
700 |
/*!
|
|
701 |
Updates model item with fresh data
|
|
702 |
\param id id of item to update
|
|
703 |
*/
|
|
704 |
void CaItemModelPrivate::updateItemData(int id)
|
|
705 |
{
|
87
|
706 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateItemData");
|
|
707 |
|
85
|
708 |
mEntries.updateEntry(id);
|
|
709 |
|
|
710 |
QList<int> ids = mService->getEntryIds(mQuery);
|
92
|
711 |
if (mEntries.indexOf(id) >= 0
|
89
|
712 |
&& ids.indexOf(id) == mEntries.indexOf(id)) {
|
85
|
713 |
emit m_q->dataChanged(index(mEntries.indexOf(id)), index(
|
87
|
714 |
mEntries.indexOf(id)));
|
89
|
715 |
} else if (ids.indexOf(id) < 0){
|
|
716 |
removeItem(id);
|
|
717 |
} else if (mEntries.indexOf(id) < 0){
|
92
|
718 |
addItem(id);
|
89
|
719 |
} else if (mParentEntry && id == mParentEntry->id()) {
|
|
720 |
updateParentEntry();
|
|
721 |
m_q->reset();
|
85
|
722 |
} else {
|
89
|
723 |
updateModel();
|
85
|
724 |
}
|
87
|
725 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateItemData");
|
85
|
726 |
}
|
|
727 |
|
|
728 |
/*!
|
|
729 |
Adds new item to model
|
|
730 |
\param id id of item to add
|
|
731 |
*/
|
|
732 |
void CaItemModelPrivate::addItem(int id)
|
|
733 |
{
|
87
|
734 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::addItem");
|
|
735 |
|
85
|
736 |
int row = itemRow(id);
|
|
737 |
//we use beginInsertRows and endInsertRows to emit proper signal
|
|
738 |
//(see Qt documentation of QAbstractItemModel)
|
|
739 |
if (mEntries.indexOf(id) < 0 && row >= 0) {
|
|
740 |
m_q->beginInsertRows(QModelIndex(), row, row);
|
|
741 |
mEntries.insert(row, id);
|
|
742 |
m_q->endInsertRows();
|
|
743 |
}
|
87
|
744 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::addItem");
|
85
|
745 |
}
|
|
746 |
|
|
747 |
/*!
|
92
|
748 |
Adds new item block to model
|
|
749 |
Use in cases when inserting / appending an adjacent block of items
|
|
750 |
\param itemsList list of adjacent items
|
|
751 |
*/
|
|
752 |
void CaItemModelPrivate::addItemBlock(const QList<int> &itemsList)
|
|
753 |
{
|
|
754 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::addItemBlock");
|
|
755 |
if (!itemsList.isEmpty()) {
|
|
756 |
int firstRow = itemRow(itemsList.first());
|
|
757 |
int lastRow = itemRow(itemsList.last());
|
|
758 |
m_q->beginInsertRows(QModelIndex(), firstRow, lastRow);
|
|
759 |
for (int i = 0; i < itemsList.count(); ++i) {
|
|
760 |
mEntries.insert(firstRow + i, itemsList.at(i));
|
|
761 |
}
|
|
762 |
m_q->endInsertRows();
|
|
763 |
emit m_q->scrollTo(firstRow, QAbstractItemView::PositionAtTop);
|
|
764 |
}
|
|
765 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::addItemBlock");
|
|
766 |
}
|
|
767 |
|
|
768 |
/*!
|
85
|
769 |
Adds new items to model
|
|
770 |
\param itemsList current items list
|
|
771 |
*/
|
92
|
772 |
void CaItemModelPrivate::handleAddItems(const QList<int> &itemsList)
|
85
|
773 |
{
|
87
|
774 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::handleAddItems");
|
92
|
775 |
const int oldItemCount(mEntries.count());
|
|
776 |
if (oldItemCount) {
|
|
777 |
const int newItemCount(itemsList.count());
|
|
778 |
if (newItemCount == oldItemCount) {
|
|
779 |
// count is the same - check if item order changed
|
|
780 |
if (itemsList == mEntries.orderedIdList()) {
|
|
781 |
// assume that if the order has not changed
|
|
782 |
// it had to be the secondary lines
|
|
783 |
updateModel();
|
|
784 |
} else {
|
89
|
785 |
updateLayout();
|
|
786 |
}
|
92
|
787 |
} else {
|
85
|
788 |
int i = 0;
|
92
|
789 |
QList<int> oldList = mEntries.orderedIdList();
|
|
790 |
//we loop through items to find first added
|
|
791 |
while (i < oldList.count()) {
|
|
792 |
if (oldList[i] != itemsList[i]) {
|
|
793 |
oldList.takeAt(i);
|
|
794 |
} else {
|
|
795 |
++i;
|
85
|
796 |
}
|
|
797 |
}
|
89
|
798 |
updateModel();
|
92
|
799 |
//i is the index of first added item
|
|
800 |
emit m_q->scrollTo(i, QAbstractItemView::PositionAtTop);
|
|
801 |
}
|
85
|
802 |
} else {
|
92
|
803 |
// items added to an empty list - add all as a single block
|
|
804 |
addItemBlock(itemsList);
|
85
|
805 |
}
|
87
|
806 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::handleAddItems");
|
85
|
807 |
}
|
|
808 |
|
|
809 |
/*!
|
|
810 |
Gets index/row for new item
|
|
811 |
\param id of item to add
|
|
812 |
\retval row in model list for new item to insert
|
|
813 |
*/
|
|
814 |
int CaItemModelPrivate::itemRow(int id)
|
|
815 |
{
|
87
|
816 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::itemRow");
|
85
|
817 |
QList<int> ids = mService->getEntryIds(mQuery);
|
87
|
818 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::itemRow");
|
85
|
819 |
return ids.indexOf(id);
|
|
820 |
}
|
|
821 |
|
|
822 |
/*!
|
|
823 |
Removes item from model
|
|
824 |
\param id of item to remove
|
|
825 |
*/
|
|
826 |
void CaItemModelPrivate::removeItem(int id)
|
|
827 |
{
|
87
|
828 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::removeItem");
|
85
|
829 |
int row = mEntries.indexOf(id);
|
|
830 |
if (row >= 0) {
|
|
831 |
m_q->beginRemoveRows(QModelIndex(), mEntries.indexOf(id),
|
87
|
832 |
mEntries.indexOf(id));
|
85
|
833 |
mEntries.remove(id);
|
|
834 |
m_q->endRemoveRows();
|
87
|
835 |
} else {
|
89
|
836 |
updateModel();
|
85
|
837 |
}
|
87
|
838 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::removeItem");
|
85
|
839 |
}
|
|
840 |
|
|
841 |
/*!
|
|
842 |
Removes missing items from model
|
|
843 |
\param itemsList current items list
|
|
844 |
*/
|
|
845 |
void CaItemModelPrivate::removeItems(const QList<int> &itemsList)
|
|
846 |
{
|
87
|
847 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::removeItems");
|
85
|
848 |
int i = 0;
|
|
849 |
for (i = 0; i < itemsList.count(); i++) {
|
|
850 |
if (itemsList[i] != mEntries[i]) {
|
|
851 |
removeItem(mEntries[i]);
|
|
852 |
}
|
|
853 |
}
|
|
854 |
while (i < mEntries.count()) {
|
|
855 |
removeItem(mEntries[i]);
|
|
856 |
i++;
|
|
857 |
}
|
87
|
858 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::removeItems");
|
85
|
859 |
}
|
|
860 |
|
|
861 |
/*!
|
|
862 |
Layout update
|
89
|
863 |
NOTE: this method should be called only if the entries get rearranged
|
|
864 |
and do not change their contents!
|
85
|
865 |
*/
|
|
866 |
void CaItemModelPrivate::updateLayout()
|
|
867 |
{
|
87
|
868 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateLayout");
|
85
|
869 |
m_q->layoutAboutToBeChanged();
|
89
|
870 |
|
|
871 |
// get the ID list from before the update
|
|
872 |
QList<int> oldOrderedIdList = mEntries.orderedIdList();
|
|
873 |
|
|
874 |
// do the update, the entries should only get rearranged
|
85
|
875 |
mEntries.updateEntries(mQuery);
|
89
|
876 |
|
|
877 |
// get the new ID list after the entries got rearranged
|
|
878 |
QList<int> newOrderedIdList = mEntries.orderedIdList();
|
|
879 |
// this list will contain the new position indices
|
|
880 |
QList<int> newPositionsList;
|
|
881 |
int entry;
|
|
882 |
foreach (entry, oldOrderedIdList) {
|
|
883 |
newPositionsList << newOrderedIdList.indexOf(entry);
|
|
884 |
}
|
|
885 |
|
|
886 |
// now check which items in the previous persistent index list changed
|
|
887 |
// their positions, make new indices for them and store in the new
|
|
888 |
// persistent index list
|
|
889 |
QModelIndexList oldPersistentIndexList = m_q->persistentIndexList();
|
|
890 |
QModelIndexList newPersistentIndexList;
|
|
891 |
QModelIndex index;
|
|
892 |
foreach (index, oldPersistentIndexList) {
|
|
893 |
newPersistentIndexList <<
|
|
894 |
m_q->createIndex(
|
92
|
895 |
newPositionsList.at(index.row()), 0, index.internalPointer());
|
89
|
896 |
}
|
92
|
897 |
m_q->changePersistentIndexList(
|
|
898 |
oldPersistentIndexList, newPersistentIndexList);
|
89
|
899 |
|
85
|
900 |
m_q->layoutChanged();
|
87
|
901 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateLayout");
|
85
|
902 |
}
|
|
903 |
|
|
904 |
/*!
|
|
905 |
Connects slots
|
|
906 |
*/
|
|
907 |
void CaItemModelPrivate::connectSlots()
|
|
908 |
{
|
87
|
909 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::connectSlots");
|
85
|
910 |
connect(mNotifier, SIGNAL(entryChanged(int,ChangeType)),
|
87
|
911 |
this, SLOT(updateModelItem(int,ChangeType)));
|
85
|
912 |
|
|
913 |
if (mQuery.parentId() > 0) {
|
|
914 |
connect(mNotifier, SIGNAL(groupContentChanged(int)),
|
87
|
915 |
this, SLOT(updateModelContent(int)));
|
85
|
916 |
}
|
87
|
917 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::connectSlots");
|
85
|
918 |
}
|
|
919 |
|
|
920 |
/*!
|
|
921 |
Disconnects slots
|
|
922 |
*/
|
|
923 |
void CaItemModelPrivate::disconnectSlots()
|
|
924 |
{
|
87
|
925 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::disconnectSlots");
|
85
|
926 |
disconnect(mNotifier, SIGNAL(entryChanged(int,ChangeType)),
|
87
|
927 |
this, SLOT(updateModelItem(int,ChangeType)));
|
85
|
928 |
if (mQuery.parentId() > 0) {
|
|
929 |
disconnect(mNotifier, SIGNAL(groupContentChanged(int)),
|
87
|
930 |
this, SLOT(updateModelContent(int)));
|
85
|
931 |
}
|
87
|
932 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::disconnectSlots");
|
85
|
933 |
}
|
|
934 |
|
|
935 |
/*!
|
|
936 |
Reconnects slots
|
|
937 |
*/
|
|
938 |
void CaItemModelPrivate::reconnectSlots()
|
|
939 |
{
|
87
|
940 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::reconnectSlots");
|
85
|
941 |
disconnectSlots();
|
|
942 |
connectSlots();
|
87
|
943 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::reconnectSlots");
|
85
|
944 |
}
|
|
945 |
|
|
946 |
/*!
|
|
947 |
Updates model with fresh entries
|
|
948 |
\param id of item to handle
|
|
949 |
\param changeType change type
|
|
950 |
*/
|
|
951 |
void CaItemModelPrivate::updateModelItem(int id, ChangeType changeType)
|
|
952 |
{
|
87
|
953 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateModelItem");
|
85
|
954 |
switch (changeType) {
|
|
955 |
case AddChangeType:
|
|
956 |
addItem(id);
|
|
957 |
break;
|
|
958 |
case RemoveChangeType:
|
|
959 |
removeItem(id);
|
|
960 |
break;
|
|
961 |
default:
|
|
962 |
updateItemData(id);
|
|
963 |
break;
|
|
964 |
}
|
87
|
965 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateModelItem");
|
85
|
966 |
}
|
|
967 |
|
|
968 |
/*!
|
|
969 |
Updates model with fresh entries
|
|
970 |
\param id of parent
|
|
971 |
*/
|
|
972 |
void CaItemModelPrivate::updateModelContent(int id)
|
|
973 |
{
|
|
974 |
Q_UNUSED(id);
|
87
|
975 |
CACLIENTTEST_FUNC_ENTRY("CaItemModelPrivate::updateModelContent");
|
85
|
976 |
QList<int> ids = mService->getEntryIds(mQuery);
|
|
977 |
|
|
978 |
if (ids.count() >= mEntries.count()) {
|
|
979 |
handleAddItems(ids);
|
|
980 |
} else {
|
|
981 |
removeItems(ids);
|
|
982 |
}
|
87
|
983 |
CACLIENTTEST_FUNC_EXIT("CaItemModelPrivate::updateModelContent");
|
85
|
984 |
}
|