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