summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/src/declarative/extending.qdoc5
-rw-r--r--doc/src/declarative/qdeclarativemodels.qdoc23
-rw-r--r--src/imports/dirmodel/dirmodel.pro26
-rw-r--r--src/imports/dirmodel/plugin.cpp65
-rw-r--r--src/imports/dirmodel/qdeclarativefolderlistmodel.cpp (renamed from tools/qml/qdeclarativefolderlistmodel.cpp)86
-rw-r--r--src/imports/dirmodel/qdeclarativefolderlistmodel.h (renamed from tools/qml/qdeclarativefolderlistmodel.h)23
-rw-r--r--src/imports/dirmodel/qmldir1
-rw-r--r--src/imports/imports.pro2
-rw-r--r--tools/qml/content/Browser.qml1
-rw-r--r--tools/qml/main.cpp2
-rw-r--r--tools/qml/qml.pri2
-rw-r--r--tools/qml/qmlruntime.cpp1
12 files changed, 162 insertions, 75 deletions
diff --git a/doc/src/declarative/extending.qdoc b/doc/src/declarative/extending.qdoc
index 5b695f7..574b0b2 100644
--- a/doc/src/declarative/extending.qdoc
+++ b/doc/src/declarative/extending.qdoc
@@ -61,8 +61,11 @@ QML for their own independent use.
The QML snippet shown above instantiates one \c Person instance and sets
the \c name and \c shoeSize properties on it. Everything in QML ultimately comes down
to either instantiating an object instance, or assigning a property a value.
+
QML relies heavily on Qt's meta object system and can only instantiate classes
-that derive from QObject.
+that derive from QObject. For visual element types, this will usually mean a subclass
+of QDeclarativeItem; for models used with the view elements, a subclass of QAbstractItemModel;
+and for abitrary objects with properties, a direct subclass of QObject.
The QML engine has no intrinsic knowledge of any class types. Instead the
programmer must register the C++ types with their corresponding QML names.
diff --git a/doc/src/declarative/qdeclarativemodels.qdoc b/doc/src/declarative/qdeclarativemodels.qdoc
index 788d417..109d390 100644
--- a/doc/src/declarative/qdeclarativemodels.qdoc
+++ b/doc/src/declarative/qdeclarativemodels.qdoc
@@ -99,7 +99,8 @@ There are a number of QML elements that operate using data models:
\endlist
QML supports several types of data model, which may be provided by QML
-or C++ (via QDeclarativeContext::setContextProperty(), for example).
+or C++ (via QDeclarativeContext::setContextProperty() or as plugin types,
+for example).
\section1 QML Data Models
@@ -210,8 +211,13 @@ will be positioned by the view.
\section1 C++ Data Models
+Models defined in C++ can be made available to QML either from a C++ application or from a
+\l{QDeclarativeExtensionPlugin}{QML C++ plugin}.
+
\section2 QAbstractItemModel
+A model can be defined by subclassing QAbstractItemModel.
+
QAbstractItemModel provides the roles set via the QAbstractItemModel::setRoleNames() method.
The default role names set by Qt are:
@@ -227,7 +233,18 @@ The default role names set by Qt are:
\o decoration
\endtable
-QAbstractItemModel presents a heirachy of tables. Views currently provided by QML
+The model could be made available to QML either directly:
+
+\code
+QDeclarativeContext *ctxt = view.rootContext();
+MyModel *model = new MyModel; // subclass of QAbstractItemModel
+ctxt->setContextProperty("myModel", model);
+\endcode
+
+or by registering the subclass as a new QML type in
+a \l{QDeclarativeExtensionPlugin}{QML C++ plugin}.
+
+QAbstractItemModel presents a heirachy of tables, but views currently provided by QML
can only display list data.
In order to display child lists of a heirachical model
the VisualDataModel element provides several properties and functions for use
@@ -242,7 +259,7 @@ with models of type QAbstractItemModel:
\section2 QStringList
-QStringList provides the contents of the list via the \e modelData role:
+A model may be a simple QStringList, which provides the contents of the list via the \e modelData role:
\table
\row
diff --git a/src/imports/dirmodel/dirmodel.pro b/src/imports/dirmodel/dirmodel.pro
new file mode 100644
index 0000000..03f3a1a
--- /dev/null
+++ b/src/imports/dirmodel/dirmodel.pro
@@ -0,0 +1,26 @@
+TARGET = qmlviewerplugin
+TARGETPATH = Qt/labs/folderlistmodel
+include(../qimportbase.pri)
+
+QT += declarative script
+
+SOURCES += qdeclarativefolderlistmodel.cpp plugin.cpp
+HEADERS += qdeclarativefolderlistmodel.h
+
+QTDIR_build:DESTDIR = $$QT_BUILD_TREE/imports/$$TARGETPATH
+target.path = $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
+
+qmldir.files += $$PWD/qmldir
+qmldir.path += $$[QT_INSTALL_IMPORTS]/$$TARGETPATH
+
+symbian:{
+ load(data_caging_paths)
+ include($$QT_SOURCE_TREE/demos/symbianpkgrules.pri)
+
+ importFiles.sources = qmlviewerplugin.dll qmldir
+ importFiles.path = $$QT_IMPORTS_BASE_DIR/$$TARGETPATH
+
+ DEPLOYMENT = importFiles
+}
+
+INSTALLS += target qmldir
diff --git a/src/imports/dirmodel/plugin.cpp b/src/imports/dirmodel/plugin.cpp
new file mode 100644
index 0000000..61bf354
--- /dev/null
+++ b/src/imports/dirmodel/plugin.cpp
@@ -0,0 +1,65 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the plugins of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtDeclarative/qdeclarativeextensionplugin.h>
+#include <QtDeclarative/qdeclarative.h>
+
+#include "qdeclarativefolderlistmodel.h"
+
+QT_BEGIN_NAMESPACE
+
+class QmlViewerPlugin : public QDeclarativeExtensionPlugin
+{
+ Q_OBJECT
+public:
+ virtual void registerTypes(const char *uri)
+ {
+ Q_ASSERT(QLatin1String(uri) == QLatin1String("Qt.labs.folderlistmodel"));
+ qmlRegisterType<QDeclarativeFolderListModel>(uri,1,0,"FolderListModel");
+ }
+};
+
+QT_END_NAMESPACE
+
+#include "plugin.moc"
+
+Q_EXPORT_PLUGIN2(qmlviewerplugin, QT_PREPEND_NAMESPACE(QmlViewerPlugin));
+
diff --git a/tools/qml/qdeclarativefolderlistmodel.cpp b/src/imports/dirmodel/qdeclarativefolderlistmodel.cpp
index 7ac25d6..50ef4ae 100644
--- a/tools/qml/qdeclarativefolderlistmodel.cpp
+++ b/src/imports/dirmodel/qdeclarativefolderlistmodel.cpp
@@ -44,8 +44,6 @@
#include <QDebug>
#include <qdeclarativecontext.h>
-QT_BEGIN_NAMESPACE
-
class QDeclarativeFolderListModelPrivate
{
public:
@@ -112,8 +110,13 @@ public:
*/
QDeclarativeFolderListModel::QDeclarativeFolderListModel(QObject *parent)
- : QListModelInterface(parent)
+ : QAbstractListModel(parent)
{
+ QHash<int, QByteArray> roles;
+ roles[FileNameRole] = "fileName";
+ roles[FilePathRole] = "filePath";
+ setRoleNames(roles);
+
d = new QDeclarativeFolderListModelPrivate;
d->model.setFilter(QDir::AllDirs | QDir::Files | QDir::Drives | QDir::NoDotAndDotDot);
connect(&d->model, SIGNAL(rowsInserted(const QModelIndex&,int,int))
@@ -131,56 +134,35 @@ QDeclarativeFolderListModel::~QDeclarativeFolderListModel()
delete d;
}
-QHash<int,QVariant> QDeclarativeFolderListModel::data(int index, const QList<int> &roles) const
-{
- Q_UNUSED(roles);
- QHash<int,QVariant> folderData;
- QModelIndex modelIndex = d->model.index(index, 0, d->folderIndex);
- if (modelIndex.isValid()) {
- folderData[QDirModel::FileNameRole] = d->model.data(modelIndex, QDirModel::FileNameRole);
- folderData[QDirModel::FilePathRole] = QUrl::fromLocalFile(d->model.data(modelIndex, QDirModel::FilePathRole).toString());
- }
-
- return folderData;
-}
-
-QVariant QDeclarativeFolderListModel::data(int index, int role) const
+QVariant QDeclarativeFolderListModel::data(const QModelIndex &index, int role) const
{
QVariant rv;
- QModelIndex modelIndex = d->model.index(index, 0, d->folderIndex);
+ QModelIndex modelIndex = d->model.index(index.row(), 0, d->folderIndex);
if (modelIndex.isValid()) {
- if (role == QDirModel::FileNameRole)
- rv = d->model.data(modelIndex, QDirModel::FileNameRole);
- else if (role == QDirModel::FilePathRole)
+ if (role == FileNameRole)
+ rv = d->model.data(modelIndex, QDirModel::FileNameRole).toString();
+ else if (role == FilePathRole)
rv = QUrl::fromLocalFile(d->model.data(modelIndex, QDirModel::FilePathRole).toString());
}
-
return rv;
}
-int QDeclarativeFolderListModel::count() const
+QString QDeclarativeFolderListModel::fileName(int index) const
{
- return d->count;
+ QModelIndex modelIndex = d->model.index(index, 0, d->folderIndex);
+ return d->model.data(modelIndex, QDirModel::FileNameRole).toString();
}
-QList<int> QDeclarativeFolderListModel::roles() const
+QUrl QDeclarativeFolderListModel::filePath(int index) const
{
- QList<int> r;
- r << QDirModel::FileNameRole;
- r << QDirModel::FilePathRole;
- return r;
+ QModelIndex modelIndex = d->model.index(index, 0, d->folderIndex);
+ return QUrl::fromLocalFile(d->model.data(modelIndex, QDirModel::FilePathRole).toString());
}
-QString QDeclarativeFolderListModel::toString(int role) const
+int QDeclarativeFolderListModel::rowCount(const QModelIndex &parent) const
{
- switch (role) {
- case QDirModel::FileNameRole:
- return QLatin1String("fileName");
- case QDirModel::FilePathRole:
- return QLatin1String("filePath");
- }
-
- return QString();
+ Q_UNUSED(parent);
+ return d->count;
}
/*!
@@ -315,37 +297,41 @@ void QDeclarativeFolderListModel::refresh()
{
d->folderIndex = QModelIndex();
if (d->count) {
- int tmpCount = d->count;
+ emit beginRemoveRows(QModelIndex(), 0, d->count);
d->count = 0;
- emit itemsRemoved(0, tmpCount);
+ emit endRemoveRows();
}
d->folderIndex = d->model.index(d->folder.toLocalFile());
- d->count = d->model.rowCount(d->folderIndex);
- if (d->count) {
- emit itemsInserted(0, d->count);
+ int newcount = d->model.rowCount(d->folderIndex);
+ if (newcount) {
+ emit beginInsertRows(QModelIndex(), 0, newcount-1);
+ d->count = newcount;
+ emit endInsertRows();
}
}
void QDeclarativeFolderListModel::inserted(const QModelIndex &index, int start, int end)
{
if (index == d->folderIndex) {
+ emit beginInsertRows(QModelIndex(), start, end);
d->count = d->model.rowCount(d->folderIndex);
- emit itemsInserted(start, end - start + 1);
+ emit endInsertRows();
}
}
void QDeclarativeFolderListModel::removed(const QModelIndex &index, int start, int end)
{
if (index == d->folderIndex) {
+ emit beginRemoveRows(QModelIndex(), start, end);
d->count = d->model.rowCount(d->folderIndex);
- emit itemsRemoved(start, end - start + 1);
+ emit endRemoveRows();
}
}
void QDeclarativeFolderListModel::dataChanged(const QModelIndex &start, const QModelIndex &end)
{
if (start.parent() == d->folderIndex)
- emit itemsChanged(start.row(), end.row() - start.row() + 1, roles());
+ emit dataChanged(index(start.row(),0), index(end.row(),0));
}
/*!
@@ -413,11 +399,3 @@ void QDeclarativeFolderListModel::setShowOnlyReadable(bool on)
else
d->model.setFilter(d->model.filter() & ~QDir::Readable);
}
-
-void QDeclarativeFolderListModel::registerTypes()
-{
- qmlRegisterType<QDeclarativeFolderListModel>("Qt",4,7,"FolderListModel");
-}
-
-QT_END_NAMESPACE
-
diff --git a/tools/qml/qdeclarativefolderlistmodel.h b/src/imports/dirmodel/qdeclarativefolderlistmodel.h
index 1ecc784..86a7588 100644
--- a/tools/qml/qdeclarativefolderlistmodel.h
+++ b/src/imports/dirmodel/qdeclarativefolderlistmodel.h
@@ -45,15 +45,13 @@
#include <qdeclarative.h>
#include <QStringList>
#include <QUrl>
-#include "../../src/declarative/3rdparty/qlistmodelinterface_p.h"
-
-QT_BEGIN_NAMESPACE
+#include <QAbstractListModel>
class QDeclarativeContext;
class QModelIndex;
class QDeclarativeFolderListModelPrivate;
-class QDeclarativeFolderListModel : public QListModelInterface, public QDeclarativeParserStatus
+class QDeclarativeFolderListModel : public QAbstractListModel, public QDeclarativeParserStatus
{
Q_OBJECT
Q_INTERFACES(QDeclarativeParserStatus)
@@ -66,18 +64,21 @@ class QDeclarativeFolderListModel : public QListModelInterface, public QDeclarat
Q_PROPERTY(bool showDirs READ showDirs WRITE setShowDirs)
Q_PROPERTY(bool showDotAndDotDot READ showDotAndDotDot WRITE setShowDotAndDotDot)
Q_PROPERTY(bool showOnlyReadable READ showOnlyReadable WRITE setShowOnlyReadable)
+ Q_PROPERTY(int count READ count)
public:
QDeclarativeFolderListModel(QObject *parent = 0);
~QDeclarativeFolderListModel();
- static void registerTypes();
+ enum Roles { FileNameRole = Qt::UserRole+1, FilePathRole = Qt::UserRole+2 };
+
+ int rowCount(const QModelIndex &parent) const;
+ QVariant data(const QModelIndex &index, int role) const;
- virtual QHash<int,QVariant> data(int index, const QList<int> &roles = (QList<int>())) const;
- virtual QVariant data(int index, int role) const;
- virtual int count() const;
- virtual QList<int> roles() const;
- virtual QString toString(int role) const;
+ int count() const { return rowCount(QModelIndex()); }
+
+ Q_INVOKABLE QString fileName(int index) const;
+ Q_INVOKABLE QUrl filePath(int index) const;
QUrl folder() const;
void setFolder(const QUrl &folder);
@@ -121,8 +122,6 @@ private:
QDeclarativeFolderListModelPrivate *d;
};
-QT_END_NAMESPACE
-
QML_DECLARE_TYPE(QDeclarativeFolderListModel)
#endif // QDECLARATIVEFOLDERLISTMODEL_H
diff --git a/src/imports/dirmodel/qmldir b/src/imports/dirmodel/qmldir
new file mode 100644
index 0000000..0be644f
--- /dev/null
+++ b/src/imports/dirmodel/qmldir
@@ -0,0 +1 @@
+plugin qmlviewerplugin
diff --git a/src/imports/imports.pro b/src/imports/imports.pro
index a9d600e..df43b76 100644
--- a/src/imports/imports.pro
+++ b/src/imports/imports.pro
@@ -1,6 +1,6 @@
TEMPLATE = subdirs
-SUBDIRS += particles gestures
+SUBDIRS += dirmodel particles gestures
contains(QT_CONFIG, webkit): SUBDIRS += webkit
contains(QT_CONFIG, mediaservices): SUBDIRS += multimedia
diff --git a/tools/qml/content/Browser.qml b/tools/qml/content/Browser.qml
index c3a2cc0..fe7ad9c 100644
--- a/tools/qml/content/Browser.qml
+++ b/tools/qml/content/Browser.qml
@@ -1,4 +1,5 @@
import Qt 4.7
+import Qt.labs.folderlistmodel 1.0
Rectangle {
id: root
diff --git a/tools/qml/main.cpp b/tools/qml/main.cpp
index 0ddce72..18e2531 100644
--- a/tools/qml/main.cpp
+++ b/tools/qml/main.cpp
@@ -50,7 +50,6 @@
#include <QDebug>
#include <QMessageBox>
#include "qdeclarativetester.h"
-#include "qdeclarativefolderlistmodel.h"
QT_USE_NAMESPACE
@@ -203,7 +202,6 @@ int main(int argc, char ** argv)
QDeclarativeViewer::registerTypes();
QDeclarativeTester::registerTypes();
- QDeclarativeFolderListModel::registerTypes();
bool frameless = false;
QString fileName;
diff --git a/tools/qml/qml.pri b/tools/qml/qml.pri
index a2058c7..5e3e74b 100644
--- a/tools/qml/qml.pri
+++ b/tools/qml/qml.pri
@@ -10,12 +10,10 @@ HEADERS += $$PWD/qmlruntime.h \
$$PWD/proxysettings.h \
$$PWD/qdeclarativetester.h \
$$PWD/deviceorientation.h \
- $$PWD/qdeclarativefolderlistmodel.h \
$$PWD/loggerwidget.h
SOURCES += $$PWD/qmlruntime.cpp \
$$PWD/proxysettings.cpp \
$$PWD/qdeclarativetester.cpp \
- $$PWD/qdeclarativefolderlistmodel.cpp \
$$PWD/loggerwidget.cpp
RESOURCES = $$PWD/qmlruntime.qrc
diff --git a/tools/qml/qmlruntime.cpp b/tools/qml/qmlruntime.cpp
index 9700090..490fa34 100644
--- a/tools/qml/qmlruntime.cpp
+++ b/tools/qml/qmlruntime.cpp
@@ -906,6 +906,7 @@ bool QDeclarativeViewer::open(const QString& file_or_url)
QString fileName = url.toLocalFile();
if (!fileName.isEmpty()) {
+ fi.setFile(fileName);
if (fi.exists()) {
if (fi.suffix().toLower() != QLatin1String("qml")) {
qWarning() << "qml cannot open non-QML file" << fileName;