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.
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.
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.
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$
52
46
#include <qgalleryresultset.h>
48
#include <QtDeclarative/qdeclarativeinfo.h>
54
50
QTM_BEGIN_NAMESPACE
56
52
QDeclarativeGalleryQueryModel::QDeclarativeGalleryQueryModel(QObject *parent)
57
53
: QAbstractListModel(parent)
57
, m_updateStatus(Incomplete)
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()));
66
63
connect(&m_request, SIGNAL(resultSetChanged(QGalleryResultSet*)),
74
71
void QDeclarativeGalleryQueryModel::componentComplete()
79
m_request.setFilter(m_filter->filter());
73
m_updateStatus = NoUpdate;
76
connect(m_filter.data(), SIGNAL(filterChanged()), this, SLOT(deferredExecute()));
78
m_request.setFilter(m_filter.data()->filter());
80
80
m_request.execute();
83
qreal QDeclarativeGalleryQueryModel::progress() const
85
const int max = m_request.maximumProgress();
87
return max > 0 ? qreal(m_request.currentProgress()) / max : qreal(0.0);
90
void QDeclarativeGalleryQueryModel::setPropertyNames(const QStringList &names)
92
if (m_updateStatus == Incomplete) {
93
m_request.setPropertyNames(names);
95
emit propertyNamesChanged();
99
void QDeclarativeGalleryQueryModel::setSortPropertyNames(const QStringList &names)
101
if (m_request.sortPropertyNames() != names) {
102
m_request.setSortPropertyNames(names);
106
emit sortPropertyNamesChanged();
110
void QDeclarativeGalleryQueryModel::setAutoUpdate(bool enabled)
112
if (m_request.autoUpdate() != enabled) {
113
m_request.setAutoUpdate(enabled);
117
else if (m_status == Idle)
120
emit autoUpdateChanged();
124
void QDeclarativeGalleryQueryModel::setScope(Scope scope)
126
if (m_request.scope() != QGalleryQueryRequest::Scope(scope)) {
127
m_request.setScope(QGalleryQueryRequest::Scope(scope));
135
void QDeclarativeGalleryQueryModel::setRootItem(const QVariant &itemId)
137
if (m_request.rootItem() != itemId) {
138
m_request.setRootItem(itemId);
142
emit rootItemChanged();
146
void QDeclarativeGalleryQueryModel::setFilter(QDeclarativeGalleryFilterBase *filter)
149
disconnect(m_filter.data(), SIGNAL(filterChanged()), this, SLOT(deferredExecute()));
154
connect(m_filter.data(), SIGNAL(filterChanged()), this, SLOT(deferredExecute()));
158
emit filterChanged();
161
void QDeclarativeGalleryQueryModel::setOffset(int offset)
163
if (m_request.offset() != offset) {
164
m_request.setOffset(offset);
168
emit offsetChanged();
172
void QDeclarativeGalleryQueryModel::setLimit(int limit)
174
if (m_request.limit() != limit) {
175
m_request.setLimit(limit);
83
183
void QDeclarativeGalleryQueryModel::reload()
85
m_request.setFilter(m_filter ? m_filter->filter() : QGalleryFilter());
185
if (m_updateStatus == PendingUpdate)
186
m_updateStatus = CanceledUpdate;
188
m_request.setFilter(m_filter ? m_filter.data()->filter() : QGalleryFilter());
86
190
m_request.execute();
193
void QDeclarativeGalleryQueryModel::cancel()
195
if (m_updateStatus == PendingUpdate)
196
m_updateStatus = CanceledUpdate;
201
void QDeclarativeGalleryQueryModel::clear()
203
if (m_updateStatus == PendingUpdate)
204
m_updateStatus = CanceledUpdate;
90
210
int QDeclarativeGalleryQueryModel::rowCount(const QModelIndex &parent) const
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)
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()))
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
135
257
return !parent.isValid() && row >= 0 && row < m_rowCount && column == 0
136
258
? createIndex(row, column)
205
327
QScriptValueIterator it(values);
206
328
while (it.hasNext()) {
208
QScriptValue value = it.value();
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());
215
334
void QDeclarativeGalleryQueryModel::setProperty(
216
335
int index, const QString &property, const QVariant &value)
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);
228
void QDeclarativeGalleryQueryModel::_q_statusChanged()
230
Status status = m_status;
231
QString message = m_request.errorString();
233
m_status = Status(m_request.status());
235
qSwap(message, m_errorMessage);
237
if (m_status != status) {
240
emit statusChanged();
243
if (message != m_errorMessage)
244
emit errorMessageChanged();
348
void QDeclarativeGalleryQueryModel::deferredExecute()
350
if (m_updateStatus == NoUpdate) {
351
m_updateStatus = PendingUpdate;
353
QCoreApplication::postEvent(this, new QEvent(QEvent::UpdateRequest));
354
} else if (m_updateStatus == CanceledUpdate) {
355
m_updateStatus = PendingUpdate;
359
bool QDeclarativeGalleryQueryModel::event(QEvent *event)
361
if (event->type() == QEvent::UpdateRequest) {
362
UpdateStatus status = m_updateStatus;
363
m_updateStatus = NoUpdate;
365
if (status == PendingUpdate) {
366
m_request.setFilter(m_filter ? m_filter.data()->filter() : QGalleryFilter());
372
return QAbstractListModel::event(event);
376
void QDeclarativeGalleryQueryModel::_q_stateChanged()
378
m_status = Status(m_request.state());
380
if (m_status == Error) {
381
const QString message = m_request.errorString();
383
if (!message.isEmpty()) {
384
qmlInfo(this) << message;
386
switch (m_request.error()) {
387
case QDocumentGallery::ConnectionError:
388
qmlInfo(this) << tr("An error was encountered connecting to the document gallery");
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()));
396
case QDocumentGallery::ItemIdError:
397
qmlInfo(this) << tr("The value of rootItem is not a valid item ID");
399
case QDocumentGallery::FilterError:
400
qmlInfo(this) << tr("The value of filter is unsupported");
405
emit statusChanged();
406
} else if (m_status == Idle && !m_request.autoUpdate()) {
409
emit statusChanged();
247
413
void QDeclarativeGalleryQueryModel::_q_setResultSet(QGalleryResultSet *resultSet)
249
415
if (m_rowCount > 0) {
250
beginRemoveRows(QModelIndex(), 0, m_rowCount);
416
beginRemoveRows(QModelIndex(), 0, m_rowCount - 1);
252
418
m_resultSet = resultSet;
268
434
const int key = m_resultSet->propertyKey(*it);
270
roleNames.insert(key + MetaDataOffset, it->toLatin1());
271
m_propertyNames.append(qMakePair(key, *it));
437
roleNames.insert(key + MetaDataOffset, it->toLatin1());
438
m_propertyNames.append(qMakePair(key, *it));
273
441
roleNames.insert(ItemId, QByteArray("itemId"));
274
442
roleNames.insert(ItemType, QByteArray("itemType"));
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
411
\o Cancelled The query was cancelled.
577
\o Canceling The query was canceled but hasn't yet reached the
579
\o Canceled The query was canceled.
412
580
\o Error Information about a type could not be retrieved due to an error.