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