~ubuntu-branches/ubuntu/quantal/qtmobility/quantal

« back to all changes in this revision

Viewing changes to plugins/declarative/gallery/qdeclarativegalleryquerymodel.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-11-16 16:18:07 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20101116161807-k2dzt2nyse975r3l
Tags: 1.1.0-0ubuntu1
* New upstream release
* Syncronise with Debian, no remaining changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
8
8
**
9
9
** $QT_BEGIN_LICENSE:LGPL$
10
10
** Commercial Usage
11
 
** Licensees holding valid Qt Commercial licenses may use this file in
12
 
** accordance with the Qt Solutions Commercial License Agreement provided
13
 
** with the Software or, alternatively, in accordance with the terms
 
11
** Licensees holding valid Qt Commercial licenses may use this file in 
 
12
** accordance with the Qt Commercial License Agreement provided with
 
13
** the Software or, alternatively, in accordance with the terms
14
14
** contained in a written agreement between you and Nokia.
15
15
**
16
16
** GNU Lesser General Public License Usage
33
33
** ensure the GNU General Public License version 3.0 requirements will be
34
34
** met: http://www.gnu.org/copyleft/gpl.html.
35
35
**
36
 
** Please note Third Party Software included with Qt Solutions may impose
37
 
** additional restrictions and it is the user's responsibility to ensure
38
 
** that they have met the licensing requirements of the GPL, LGPL, or Qt
39
 
** Solutions Commercial license and the relevant license of the Third
40
 
** Party Software they are using.
41
 
**
42
36
** If you are unsure which license is appropriate for your use, please
43
37
** contact the sales department at qt-sales@nokia.com.
44
38
** $QT_END_LICENSE$
51
45
 
52
46
#include <qgalleryresultset.h>
53
47
 
 
48
#include <QtDeclarative/qdeclarativeinfo.h>
 
49
 
54
50
QTM_BEGIN_NAMESPACE
55
51
 
56
52
QDeclarativeGalleryQueryModel::QDeclarativeGalleryQueryModel(QObject *parent)
57
53
    : QAbstractListModel(parent)
58
54
    , m_resultSet(0)
59
55
    , m_status(Null)
60
 
    , m_complete(false)
 
56
    , m_rowCount(0)
 
57
    , m_updateStatus(Incomplete)
61
58
{
62
 
    connect(&m_request, SIGNAL(statusChanged(QGalleryAbstractRequest::Status)),
63
 
            this, SLOT(_q_statusChanged()));
 
59
    connect(&m_request, SIGNAL(stateChanged(QGalleryAbstractRequest::State)),
 
60
            this, SLOT(_q_stateChanged()));
64
61
    connect(&m_request, SIGNAL(progressChanged(int,int)), this, SIGNAL(progressChanged()));
65
62
 
66
63
    connect(&m_request, SIGNAL(resultSetChanged(QGalleryResultSet*)),
73
70
 
74
71
void QDeclarativeGalleryQueryModel::componentComplete()
75
72
{
76
 
    m_complete = true;
77
 
 
78
 
    if (m_filter)
79
 
        m_request.setFilter(m_filter->filter());
 
73
    m_updateStatus = NoUpdate;
 
74
 
 
75
    if (m_filter) {
 
76
        connect(m_filter.data(), SIGNAL(filterChanged()), this, SLOT(deferredExecute()));
 
77
 
 
78
        m_request.setFilter(m_filter.data()->filter());
 
79
    }
80
80
    m_request.execute();
81
81
}
82
82
 
 
83
qreal QDeclarativeGalleryQueryModel::progress() const
 
84
{
 
85
    const int max = m_request.maximumProgress();
 
86
 
 
87
    return max > 0 ? qreal(m_request.currentProgress()) / max : qreal(0.0);
 
88
}
 
89
 
 
90
void QDeclarativeGalleryQueryModel::setPropertyNames(const QStringList &names)
 
91
{
 
92
    if (m_updateStatus == Incomplete) {
 
93
        m_request.setPropertyNames(names);
 
94
 
 
95
        emit propertyNamesChanged();
 
96
    }
 
97
}
 
98
 
 
99
void QDeclarativeGalleryQueryModel::setSortPropertyNames(const QStringList &names)
 
100
{
 
101
    if (m_request.sortPropertyNames() != names) {
 
102
        m_request.setSortPropertyNames(names);
 
103
 
 
104
        deferredExecute();
 
105
 
 
106
        emit sortPropertyNamesChanged();
 
107
    }
 
108
}
 
109
 
 
110
void QDeclarativeGalleryQueryModel::setAutoUpdate(bool enabled)
 
111
{
 
112
    if (m_request.autoUpdate() != enabled) {
 
113
        m_request.setAutoUpdate(enabled);
 
114
 
 
115
        if (enabled)
 
116
            deferredExecute();
 
117
        else if (m_status == Idle)
 
118
            m_request.cancel();
 
119
 
 
120
        emit autoUpdateChanged();
 
121
    }
 
122
}
 
123
 
 
124
void QDeclarativeGalleryQueryModel::setScope(Scope scope)
 
125
{
 
126
    if (m_request.scope() != QGalleryQueryRequest::Scope(scope)) {
 
127
        m_request.setScope(QGalleryQueryRequest::Scope(scope));
 
128
 
 
129
        deferredExecute();
 
130
 
 
131
        emit scopeChanged();
 
132
    }
 
133
}
 
134
 
 
135
void QDeclarativeGalleryQueryModel::setRootItem(const QVariant &itemId)
 
136
{
 
137
    if (m_request.rootItem() != itemId) {
 
138
        m_request.setRootItem(itemId);
 
139
 
 
140
        deferredExecute();
 
141
 
 
142
        emit rootItemChanged();
 
143
    }
 
144
}
 
145
 
 
146
void QDeclarativeGalleryQueryModel::setFilter(QDeclarativeGalleryFilterBase *filter)
 
147
{
 
148
    if (m_filter)
 
149
        disconnect(m_filter.data(), SIGNAL(filterChanged()), this, SLOT(deferredExecute()));
 
150
 
 
151
    m_filter = filter;
 
152
 
 
153
    if (m_filter)
 
154
        connect(m_filter.data(), SIGNAL(filterChanged()), this, SLOT(deferredExecute()));
 
155
 
 
156
    deferredExecute();
 
157
 
 
158
    emit filterChanged();
 
159
}
 
160
 
 
161
void QDeclarativeGalleryQueryModel::setOffset(int offset)
 
162
{
 
163
    if (m_request.offset() != offset) {
 
164
        m_request.setOffset(offset);
 
165
 
 
166
        deferredExecute();
 
167
 
 
168
        emit offsetChanged();
 
169
    }
 
170
}
 
171
 
 
172
void QDeclarativeGalleryQueryModel::setLimit(int limit)
 
173
{
 
174
    if (m_request.limit() != limit) {
 
175
        m_request.setLimit(limit);
 
176
 
 
177
        deferredExecute();
 
178
 
 
179
        emit limitChanged();
 
180
    }
 
181
}
 
182
 
83
183
void QDeclarativeGalleryQueryModel::reload()
84
184
{
85
 
    m_request.setFilter(m_filter ? m_filter->filter() : QGalleryFilter());
 
185
    if (m_updateStatus == PendingUpdate)
 
186
        m_updateStatus = CanceledUpdate;
 
187
 
 
188
    m_request.setFilter(m_filter ? m_filter.data()->filter() : QGalleryFilter());
 
189
 
86
190
    m_request.execute();
87
191
}
88
192
 
 
193
void QDeclarativeGalleryQueryModel::cancel()
 
194
{
 
195
    if (m_updateStatus == PendingUpdate)
 
196
        m_updateStatus = CanceledUpdate;
 
197
 
 
198
    m_request.cancel();
 
199
}
 
200
 
 
201
void QDeclarativeGalleryQueryModel::clear()
 
202
{
 
203
    if (m_updateStatus == PendingUpdate)
 
204
        m_updateStatus = CanceledUpdate;
 
205
 
 
206
    m_request.clear();
 
207
}
 
208
 
89
209
 
90
210
int QDeclarativeGalleryQueryModel::rowCount(const QModelIndex &parent) const
91
211
{
117
237
    }
118
238
}
119
239
 
120
 
bool QDeclarativeGalleryQueryModel::setData(const QModelIndex &index, const QVariant &value, int role)
 
240
bool QDeclarativeGalleryQueryModel::setData(
 
241
        const QModelIndex &index, const QVariant &value, int role)
121
242
{
122
 
    if (index.isValid() && (role -= MetaDataOffset) > 0) {
 
243
    if (index.isValid() && (role -= MetaDataOffset) >= 0) {
123
244
        if (m_resultSet->currentIndex() != index.row() && !m_resultSet->fetch(index.row()))
124
245
            return false;
125
246
 
130
251
 
131
252
}
132
253
 
133
 
QModelIndex QDeclarativeGalleryQueryModel::index(int row, int column, const QModelIndex &parent) const
 
254
QModelIndex QDeclarativeGalleryQueryModel::index(
 
255
        int row, int column, const QModelIndex &parent) const
134
256
{
135
257
    return !parent.isValid() && row >= 0 && row < m_rowCount && column == 0
136
258
            ? createIndex(row, column)
182
304
    if (property == QLatin1String("itemId")) {
183
305
        return m_resultSet->itemId();
184
306
    } else if (property == QLatin1String("itemType")) {
185
 
        return m_resultSet->itemType();
 
307
        return itemType(m_resultSet->itemType());
186
308
    } else {
187
309
        const int propertyKey = m_resultSet->propertyKey(property);
188
310
 
205
327
    QScriptValueIterator it(values);
206
328
    while (it.hasNext()) {
207
329
        it.next();
208
 
        QScriptValue value = it.value();
209
 
 
210
 
        if (value.isVariant())
211
 
            m_resultSet->setMetaData(m_resultSet->propertyKey(it.name()), value.toVariant());
 
330
        m_resultSet->setMetaData(m_resultSet->propertyKey(it.name()), it.value().toVariant());
212
331
    }
213
332
}
214
333
 
215
334
void QDeclarativeGalleryQueryModel::setProperty(
216
335
        int index, const QString &property, const QVariant &value)
217
336
{
218
 
 
219
337
    if (index < 0
220
338
            || index >= m_rowCount
221
339
            || (m_resultSet->currentIndex() != index && !m_resultSet->fetch(index))) {
225
343
    m_resultSet->setMetaData(m_resultSet->propertyKey(property), value);
226
344
}
227
345
 
228
 
void QDeclarativeGalleryQueryModel::_q_statusChanged()
229
 
{
230
 
    Status status = m_status;
231
 
    QString message = m_request.errorString();
232
 
 
233
 
    m_status = Status(m_request.status());
234
 
 
235
 
    qSwap(message, m_errorMessage);
236
 
 
237
 
    if (m_status != status) {
238
 
        m_status = status;
239
 
 
240
 
        emit statusChanged();
241
 
    }
242
 
 
243
 
    if (message != m_errorMessage)
244
 
        emit errorMessageChanged();
 
346
 
 
347
 
 
348
void QDeclarativeGalleryQueryModel::deferredExecute()
 
349
{
 
350
    if (m_updateStatus == NoUpdate) {
 
351
        m_updateStatus = PendingUpdate;
 
352
 
 
353
        QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
 
354
    } else if (m_updateStatus == CanceledUpdate) {
 
355
        m_updateStatus = PendingUpdate;
 
356
    }
 
357
}
 
358
 
 
359
bool QDeclarativeGalleryQueryModel::event(QEvent *event)
 
360
{
 
361
    if (event->type() == QEvent::UpdateRequest) {
 
362
        UpdateStatus status = m_updateStatus;
 
363
        m_updateStatus = NoUpdate;
 
364
 
 
365
        if (status == PendingUpdate) {
 
366
            m_request.setFilter(m_filter ? m_filter.data()->filter() : QGalleryFilter());
 
367
            m_request.execute();
 
368
        }
 
369
 
 
370
        return true;
 
371
    } else {
 
372
        return QAbstractListModel::event(event);
 
373
    }
 
374
}
 
375
 
 
376
void QDeclarativeGalleryQueryModel::_q_stateChanged()
 
377
{
 
378
    m_status = Status(m_request.state());
 
379
 
 
380
    if (m_status == Error) {
 
381
        const QString message = m_request.errorString();
 
382
 
 
383
        if (!message.isEmpty()) {
 
384
            qmlInfo(this) << message;
 
385
        } else {
 
386
            switch (m_request.error()) {
 
387
            case QDocumentGallery::ConnectionError:
 
388
                qmlInfo(this) << tr("An error was encountered connecting to the document gallery");
 
389
                break;
 
390
            case QDocumentGallery::ItemTypeError:
 
391
                qmlInfo(this) << (m_request.rootType().isEmpty()
 
392
                        ? tr("DocumentGallery.InvalidType is not a supported item type")
 
393
                        : tr("DocumentGallery.%1 is not a supported item type")
 
394
                                .arg(m_request.rootType()));
 
395
                break;
 
396
            case QDocumentGallery::ItemIdError:
 
397
                qmlInfo(this) << tr("The value of rootItem is not a valid item ID");
 
398
                break;
 
399
            case QDocumentGallery::FilterError:
 
400
                qmlInfo(this) << tr("The value of filter is unsupported");
 
401
            default:
 
402
                break;
 
403
            }
 
404
        }
 
405
        emit statusChanged();
 
406
    } else if (m_status == Idle && !m_request.autoUpdate()) {
 
407
        m_request.cancel();
 
408
    } else {
 
409
        emit statusChanged();
 
410
    }
245
411
}
246
412
 
247
413
void QDeclarativeGalleryQueryModel::_q_setResultSet(QGalleryResultSet *resultSet)
248
414
{
249
415
    if (m_rowCount > 0) {
250
 
        beginRemoveRows(QModelIndex(), 0, m_rowCount);
 
416
        beginRemoveRows(QModelIndex(), 0, m_rowCount - 1);
251
417
        m_rowCount = 0;
252
418
        m_resultSet = resultSet;
253
419
        endRemoveRows();
267
433
                ++it) {
268
434
            const int key = m_resultSet->propertyKey(*it);
269
435
 
270
 
            roleNames.insert(key + MetaDataOffset, it->toLatin1());
271
 
            m_propertyNames.append(qMakePair(key, *it));
 
436
            if (key >= 0) {
 
437
                roleNames.insert(key + MetaDataOffset, it->toLatin1());
 
438
                m_propertyNames.append(qMakePair(key, *it));
 
439
            }
272
440
        }
273
441
        roleNames.insert(ItemId, QByteArray("itemId"));
274
442
        roleNames.insert(ItemType, QByteArray("itemType"));
329
497
 
330
498
    \inmodule QtGallery
331
499
 
332
 
    \brief The GalleryQueryRequest element is used to specify a model
 
500
    \brief The DocumentGalleryModel element is used to specify a model
333
501
    containing items from the document gallery.
334
502
 
335
503
    \ingroup qml-gallery
406
574
    \o Finished The query has finished
407
575
    \o Idle The query is finished and will be automatically updated as new
408
576
    items become available.
409
 
    \o Cancelling The query was cancelled but hasn't yet reached the
410
 
    cancelled status.
411
 
    \o Cancelled The query was cancelled.
 
577
    \o Canceling The query was canceled but hasn't yet reached the
 
578
    canceled status.
 
579
    \o Canceled The query was canceled.
412
580
    \o Error Information about a type could not be retrieved due to an error.
413
581
    \endlist
414
582
*/
485
653
 
486
654
void QDeclarativeDocumentGalleryModel::setRootType(QDeclarativeDocumentGallery::ItemType itemType)
487
655
{
488
 
    if (!m_complete) {
 
656
    if (m_updateStatus == Incomplete) {
489
657
        m_request.setRootType(QDeclarativeDocumentGallery::toString(itemType));
490
658
 
491
659
        emit rootTypeChanged();
537
705
*/
538
706
 
539
707
/*!
540
 
    \qmlsignal DocumentGalleryModel::onCancelled()
 
708
    \qmlsignal DocumentGalleryModel::onCanceled()
541
709
 
542
 
    Signals that a query was cancelled.
 
710
    Signals that a query was canceled.
543
711
*/
544
712
 
545
713
/*!
555
723
*/
556
724
 
557
725
/*!
558
 
    \qmlproperty DocumentGalleryModel::count
 
726
    \qmlproperty int DocumentGalleryModel::count
559
727
 
560
728
    This property holds the number of results returned by a query.
561
729
*/