summaryrefslogtreecommitdiffstats
path: root/src/declarative/util
diff options
context:
space:
mode:
authorQt Continuous Integration System <qt-info@nokia.com>2010-05-24 04:36:01 (GMT)
committerQt Continuous Integration System <qt-info@nokia.com>2010-05-24 04:36:01 (GMT)
commit2e2ce693e16899dcfd1f39374a78bf2ca3a0e016 (patch)
treee6fe4b5938c3a4a40c66301b16794d0b16c4e5f9 /src/declarative/util
parentcb23151d3abfdbdf291abe8a1510592db2011927 (diff)
parentff27b0e6a7161db17335dc4aa8724cae75cec39c (diff)
downloadQt-2e2ce693e16899dcfd1f39374a78bf2ca3a0e016.zip
Qt-2e2ce693e16899dcfd1f39374a78bf2ca3a0e016.tar.gz
Qt-2e2ce693e16899dcfd1f39374a78bf2ca3a0e016.tar.bz2
Merge branch '4.7' of scm.dev.nokia.troll.no:qt/qt-qml into 4.7-integration
* '4.7' of scm.dev.nokia.troll.no:qt/qt-qml: Check QML files for license headers too. Make compile: include script module for test Move copies of toLocalFileOrQrc() to QDeclarativeEnginePrivate Allow resource files to be loaded in WorkerScript Fix TextEdit alignment. Component::createObject() don't attempt to set parent of null object Factor out initialization effects from declarative benchmarks. Remove license headers from test data. Fix FolderListModel parentFolder property's file drive handling Add license headers for .qml files. Ensure QML Global Qt object functions appear in the documentation index doc Also show file/line numbers on XML query errors. Print warnings for xml query syntax errors Clean up Add XmlListModel::get()
Diffstat (limited to 'src/declarative/util')
-rw-r--r--src/declarative/util/qdeclarativefontloader.cpp12
-rw-r--r--src/declarative/util/qdeclarativepixmapcache.cpp12
-rw-r--r--src/declarative/util/qdeclarativexmllistmodel.cpp145
-rw-r--r--src/declarative/util/qdeclarativexmllistmodel_p.h4
4 files changed, 104 insertions, 69 deletions
diff --git a/src/declarative/util/qdeclarativefontloader.cpp b/src/declarative/util/qdeclarativefontloader.cpp
index adfcd62..c73f827 100644
--- a/src/declarative/util/qdeclarativefontloader.cpp
+++ b/src/declarative/util/qdeclarativefontloader.cpp
@@ -53,6 +53,7 @@
#include <QFontDatabase>
#include <private/qobject_p.h>
+#include <private/qdeclarativeengine_p.h>
#include <qdeclarativeinfo.h>
QT_BEGIN_NAMESPACE
@@ -98,15 +99,6 @@ QDeclarativeFontLoader::~QDeclarativeFontLoader()
{
}
-static QString toLocalFileOrQrc(const QUrl& url)
-{
- QString r = url.toLocalFile();
- if (r.isEmpty() && url.scheme() == QLatin1String("qrc"))
- r = QLatin1Char(':') + url.path();
- return r;
-}
-
-
/*!
\qmlproperty url FontLoader::source
The url of the font to load.
@@ -127,7 +119,7 @@ void QDeclarativeFontLoader::setSource(const QUrl &url)
d->status = Loading;
emit statusChanged();
#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML
- QString lf = toLocalFileOrQrc(d->url);
+ QString lf = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(d->url);
if (!lf.isEmpty()) {
int id = QFontDatabase::addApplicationFont(lf);
if (id != -1) {
diff --git a/src/declarative/util/qdeclarativepixmapcache.cpp b/src/declarative/util/qdeclarativepixmapcache.cpp
index d9ce42c..a4ddf46 100644
--- a/src/declarative/util/qdeclarativepixmapcache.cpp
+++ b/src/declarative/util/qdeclarativepixmapcache.cpp
@@ -66,14 +66,6 @@ static const int maxImageRequestCount = 8;
QT_BEGIN_NAMESPACE
-static QString toLocalFileOrQrc(const QUrl& url)
-{
- QString r = url.toLocalFile();
- if (r.isEmpty() && url.scheme() == QLatin1String("qrc"))
- r = QLatin1Char(':') + url.path();
- return r;
-}
-
class QDeclarativeImageReaderEvent : public QEvent
{
public:
@@ -269,7 +261,7 @@ bool QDeclarativeImageRequestHandler::event(QEvent *event)
}
QCoreApplication::postEvent(runningJob, new QDeclarativeImageReaderEvent(errorCode, errorStr, image));
} else {
- QString lf = toLocalFileOrQrc(url);
+ QString lf = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
if (!lf.isEmpty()) {
// Image is local - load/decode immediately
QImage image;
@@ -613,7 +605,7 @@ QDeclarativePixmapReply::Status QDeclarativePixmapCache::get(const QUrl& url, QP
#ifndef QT_NO_LOCALFILE_OPTIMIZED_QML
if (!async) {
- QString lf = toLocalFileOrQrc(url);
+ QString lf = QDeclarativeEnginePrivate::urlToLocalFileOrQrc(url);
if (!lf.isEmpty()) {
status = QDeclarativePixmapReply::Ready;
if (!QPixmapCache::find(strKey,pixmap)) {
diff --git a/src/declarative/util/qdeclarativexmllistmodel.cpp b/src/declarative/util/qdeclarativexmllistmodel.cpp
index cae1d3a..d08e37b 100644
--- a/src/declarative/util/qdeclarativexmllistmodel.cpp
+++ b/src/declarative/util/qdeclarativexmllistmodel.cpp
@@ -42,7 +42,7 @@
#include "private/qdeclarativexmllistmodel_p.h"
#include <qdeclarativecontext.h>
-#include <qdeclarativeengine.h>
+#include <qdeclarativeengine_p.h>
#include <QDebug>
#include <QStringList>
@@ -83,12 +83,14 @@ typedef QPair<int, int> QDeclarativeXmlListRange;
The name for the role. This name is used to access the model data for this role from Qml.
\qml
- XmlRole { name: "title"; query: "title/string()" }
-
- ...
+ XmlListModel {
+ id: xmlModel
+ ...
+ XmlRole { name: "title"; query: "title/string()" }
+ }
- Component {
- id: myDelegate
+ ListView {
+ model: xmlModel
Text { text: title }
}
\endqml
@@ -122,11 +124,11 @@ struct XmlQueryJob
QString query;
QString namespaces;
QStringList roleQueries;
+ QList<void*> roleQueryErrorId; // the ptr to send back if there is an error
QStringList keyRoleQueries;
QStringList keyRoleResultsCache;
};
-
class QDeclarativeXmlQuery : public QThread
{
Q_OBJECT
@@ -166,6 +168,7 @@ public:
continue;
}
job.roleQueries << roleObjects->at(i)->query();
+ job.roleQueryErrorId << static_cast<void*>(roleObjects->at(i));
if (roleObjects->at(i)->isKey())
job.keyRoleQueries << job.roleQueries.last();
}
@@ -181,6 +184,7 @@ public:
Q_SIGNALS:
void queryCompleted(const QDeclarativeXmlQueryResult &);
+ void error(void*, const QString&);
protected:
void run() {
@@ -346,26 +350,24 @@ void QDeclarativeXmlQuery::doSubQueryJob()
}
job.keyRoleResultsCache = keyRoleResults;
-
// Get the new values for each role.
//### we might be able to condense even further (query for everything in one go)
const QStringList &queries = job.roleQueries;
for (int i = 0; i < queries.size(); ++i) {
- if (queries[i].isEmpty()) {
- QList<QVariant> resultList;
- for (int j = 0; j < m_size; ++j)
- resultList << QVariant();
- m_modelData << resultList;
- continue;
- }
- subquery.setQuery(m_prefix + QLatin1String("(let $v := ") + queries[i] + QLatin1String(" return if ($v) then ") + queries[i] + QLatin1String(" else \"\")"));
- QXmlResultItems resultItems;
- subquery.evaluateTo(&resultItems);
- QXmlItem item(resultItems.next());
QList<QVariant> resultList;
- while (!item.isNull()) {
- resultList << item.toAtomicValue(); //### we used to trim strings
- item = resultItems.next();
+ if (!queries[i].isEmpty()) {
+ subquery.setQuery(m_prefix + QLatin1String("(let $v := ") + queries[i] + QLatin1String(" return if ($v) then ") + queries[i] + QLatin1String(" else \"\")"));
+ if (subquery.isValid()) {
+ QXmlResultItems resultItems;
+ subquery.evaluateTo(&resultItems);
+ QXmlItem item(resultItems.next());
+ while (!item.isNull()) {
+ resultList << item.toAtomicValue(); //### we used to trim strings
+ item = resultItems.next();
+ }
+ } else {
+ emit error(job.roleQueryErrorId.at(i), queries[i]);
+ }
}
//### should warn here if things have gone wrong.
while (resultList.count() < m_size)
@@ -412,6 +414,16 @@ public:
, reply(0), status(QDeclarativeXmlListModel::Null), progress(0.0)
, queryId(-1), roleObjects(), redirectCount(0) {}
+
+ void notifyQueryStarted(bool remoteSource) {
+ Q_Q(QDeclarativeXmlListModel);
+ progress = remoteSource ? 0.0 : 1.0;
+ status = QDeclarativeXmlListModel::Loading;
+ errorString.clear();
+ emit q->progressChanged(progress);
+ emit q->statusChanged(status);
+ }
+
bool isComponentComplete;
QUrl src;
QString xml;
@@ -556,6 +568,8 @@ QDeclarativeXmlListModel::QDeclarativeXmlListModel(QObject *parent)
{
connect(globalXmlQuery(), SIGNAL(queryCompleted(QDeclarativeXmlQueryResult)),
this, SLOT(queryCompleted(QDeclarativeXmlQueryResult)));
+ connect(globalXmlQuery(), SIGNAL(error(void*,QString)),
+ this, SLOT(queryError(void*,QString)));
}
QDeclarativeXmlListModel::~QDeclarativeXmlListModel()
@@ -727,6 +741,42 @@ void QDeclarativeXmlListModel::setNamespaceDeclarations(const QString &declarati
}
/*!
+ \qmlmethod object XmlListModel::get(int index)
+
+ Returns the item at \a index in the model.
+
+ For example, for a model like this:
+
+ \qml
+ XmlListModel {
+ id: model
+ source: "http://mysite.com/feed.xml"
+ query: "/feed/entry"
+ XmlRole { name: "title"; query: "title/string()" }
+ }
+ \endqml
+
+ This will access the \c title value for the first item in the model:
+
+ \qml
+ var title = model.get(0).title;
+ \endqml
+*/
+QScriptValue QDeclarativeXmlListModel::get(int index) const
+{
+ Q_D(const QDeclarativeXmlListModel);
+
+ QScriptEngine *sengine = QDeclarativeEnginePrivate::getScriptEngine(qmlContext(this)->engine());
+ if (index < 0 || index >= count())
+ return sengine->undefinedValue();
+
+ QScriptValue sv = sengine->newObject();
+ for (int i=0; i<d->roleObjects.count(); i++)
+ sv.setProperty(d->roleObjects[i]->name(), qScriptValueFromValue(sengine, d->data.value(i).value(index)));
+ return sv;
+}
+
+/*!
\qmlproperty enumeration XmlListModel::status
Specifies the model loading status, which can be one of the following:
@@ -824,36 +874,21 @@ void QDeclarativeXmlListModel::reload()
if (!d->xml.isEmpty()) {
d->queryId = globalXmlQuery()->doQuery(d->query, d->namespaces, d->xml.toUtf8(), &d->roleObjects, d->keyRoleResultsCache);
- d->progress = 1.0;
- d->status = Loading;
- d->errorString.clear();
- emit progressChanged(d->progress);
- emit statusChanged(d->status);
- return;
- }
+ d->notifyQueryStarted(false);
- if (d->src.isEmpty()) {
+ } else if (d->src.isEmpty()) {
d->queryId = XMLLISTMODEL_CLEAR_ID;
- d->progress = 1.0;
- d->status = Loading;
- d->errorString.clear();
- emit progressChanged(d->progress);
- emit statusChanged(d->status);
+ d->notifyQueryStarted(false);
QTimer::singleShot(0, this, SLOT(dataCleared()));
- return;
- }
- d->progress = 0.0;
- d->status = Loading;
- d->errorString.clear();
- emit progressChanged(d->progress);
- emit statusChanged(d->status);
-
- QNetworkRequest req(d->src);
- d->reply = qmlContext(this)->engine()->networkAccessManager()->get(req);
- QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(requestFinished()));
- QObject::connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)),
- this, SLOT(requestProgress(qint64,qint64)));
+ } else {
+ d->notifyQueryStarted(true);
+ QNetworkRequest req(d->src);
+ d->reply = qmlContext(this)->engine()->networkAccessManager()->get(req);
+ QObject::connect(d->reply, SIGNAL(finished()), this, SLOT(requestFinished()));
+ QObject::connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)),
+ this, SLOT(requestProgress(qint64,qint64)));
+ }
}
#define XMLLISTMODEL_MAX_REDIRECT 16
@@ -929,6 +964,19 @@ void QDeclarativeXmlListModel::dataCleared()
queryCompleted(r);
}
+void QDeclarativeXmlListModel::queryError(void* object, const QString& error)
+{
+ // Be extra careful, object may no longer exist, it's just an ID.
+ Q_D(QDeclarativeXmlListModel);
+ for (int i=0; i<d->roleObjects.count(); i++) {
+ if (d->roleObjects.at(i) == static_cast<QDeclarativeXmlListModelRole*>(object)) {
+ qmlInfo(d->roleObjects.at(i)) << QObject::tr("invalid query: \"%1\"").arg(error);
+ return;
+ }
+ }
+ qmlInfo(this) << QObject::tr("invalid query: \"%1\"").arg(error);
+}
+
void QDeclarativeXmlListModel::queryCompleted(const QDeclarativeXmlQueryResult &result)
{
Q_D(QDeclarativeXmlListModel);
@@ -960,7 +1008,6 @@ void QDeclarativeXmlListModel::queryCompleted(const QDeclarativeXmlQueryResult &
}
} else {
-
for (int i=0; i<result.removed.count(); i++)
emit itemsRemoved(result.removed[i].first, result.removed[i].second);
for (int i=0; i<result.inserted.count(); i++)
diff --git a/src/declarative/util/qdeclarativexmllistmodel_p.h b/src/declarative/util/qdeclarativexmllistmodel_p.h
index 7101c57..8d848c8 100644
--- a/src/declarative/util/qdeclarativexmllistmodel_p.h
+++ b/src/declarative/util/qdeclarativexmllistmodel_p.h
@@ -47,6 +47,7 @@
#include <QtCore/qurl.h>
#include <QtCore/qstringlist.h>
+#include <QtScript/qscriptvalue.h>
#include <private/qlistmodelinterface_p.h>
@@ -109,6 +110,8 @@ public:
QString namespaceDeclarations() const;
void setNamespaceDeclarations(const QString&);
+ Q_INVOKABLE QScriptValue get(int index) const;
+
enum Status { Null, Ready, Loading, Error };
Status status() const;
qreal progress() const;
@@ -139,6 +142,7 @@ private Q_SLOTS:
void requestProgress(qint64,qint64);
void dataCleared();
void queryCompleted(const QDeclarativeXmlQueryResult &);
+ void queryError(void* object, const QString& error);
private:
Q_DECLARE_PRIVATE(QDeclarativeXmlListModel)