From 5732bf283683f0444dbec740cc26c995d9464de6 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Tue, 30 Jun 2009 16:26:44 +1000 Subject: Simple QML browser/loader. --- examples/declarative/loader/loader.pro | 9 ++++++ examples/declarative/loader/loader.qml | 41 ++++++++++++++++++++++++++++ examples/declarative/loader/loader.qrc | 5 ++++ examples/declarative/loader/main.cpp | 18 ++++++++++++ src/declarative/extra/qmlfolderlistmodel.cpp | 18 +++++++++--- src/declarative/extra/qmlfolderlistmodel.h | 5 +++- 6 files changed, 91 insertions(+), 5 deletions(-) create mode 100644 examples/declarative/loader/loader.pro create mode 100644 examples/declarative/loader/loader.qml create mode 100644 examples/declarative/loader/loader.qrc create mode 100644 examples/declarative/loader/main.cpp diff --git a/examples/declarative/loader/loader.pro b/examples/declarative/loader/loader.pro new file mode 100644 index 0000000..baa5b8c --- /dev/null +++ b/examples/declarative/loader/loader.pro @@ -0,0 +1,9 @@ +SOURCES = main.cpp +RESOURCES = loader.qrc + +QT += script declarative + +target.path = $$[QT_INSTALL_EXAMPLES]/declarative/loader +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS loader.pro +sources.path = $$[QT_INSTALL_EXAMPLES]/declarative/loader +INSTALLS += target sources diff --git a/examples/declarative/loader/loader.qml b/examples/declarative/loader/loader.qml new file mode 100644 index 0000000..4dd7a03 --- /dev/null +++ b/examples/declarative/loader/loader.qml @@ -0,0 +1,41 @@ +Rect { + id: Root + width: 300 + height: 400 + FolderListModel { + id: folders + nameFilters: [ "*.qml" ] + } + + Component { + id: FolderDelegate + Text { + id: Wrapper + width: Root.width + text: fileName + font.bold: true + font.size: 14 + MouseRegion { + anchors.fill: parent + onClicked: { + if (folders.isFolder(index)) { + folders.folder = filePath; + } else { + Root.qml = filePath; + } + } + } + } + } + + Text { id: DirText; text: folders.folder } + + ListView { + anchors.top: DirText.bottom + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + model: folders + delegate: FolderDelegate + } +} diff --git a/examples/declarative/loader/loader.qrc b/examples/declarative/loader/loader.qrc new file mode 100644 index 0000000..8c80052 --- /dev/null +++ b/examples/declarative/loader/loader.qrc @@ -0,0 +1,5 @@ + + + loader.qml + + diff --git a/examples/declarative/loader/main.cpp b/examples/declarative/loader/main.cpp new file mode 100644 index 0000000..31bff07 --- /dev/null +++ b/examples/declarative/loader/main.cpp @@ -0,0 +1,18 @@ +#include +#include +#include + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + + QFxView *canvas = new QFxView; + canvas->setUrl(QUrl("qrc:/loader.qml")); + canvas->execute(); + canvas->resize(210,240); + canvas->show(); + + return app.exec(); +} + + diff --git a/src/declarative/extra/qmlfolderlistmodel.cpp b/src/declarative/extra/qmlfolderlistmodel.cpp index 43ffd5c..abec01e 100644 --- a/src/declarative/extra/qmlfolderlistmodel.cpp +++ b/src/declarative/extra/qmlfolderlistmodel.cpp @@ -52,7 +52,8 @@ class QmlFolderListModelPrivate : public QObjectPrivate { public: QmlFolderListModelPrivate() { - folderIndex = model.index(0,0); + folder = QDir::currentPath(); + nameFilters << "*"; } QDirModel model; @@ -124,10 +125,13 @@ QString QmlFolderListModel::folder() const void QmlFolderListModel::setFolder(const QString &folder) { Q_D(QmlFolderListModel); + if (folder == d->folder) + return; QModelIndex index = d->model.index(folder); if (index.isValid() && d->model.isDir(index)) { d->folder = folder; QMetaObject::invokeMethod(this, "refresh", Qt::QueuedConnection); + emit folderChanged(); } } @@ -141,11 +145,14 @@ void QmlFolderListModel::setNameFilters(const QStringList &filters) { Q_D(QmlFolderListModel); d->nameFilters = filters; - QMetaObject::invokeMethod(this, "refresh", Qt::QueuedConnection); + d->model.setNameFilters(d->nameFilters); } void QmlFolderListModel::classComplete() { + Q_D(QmlFolderListModel); + if (!d->folderIndex.isValid()) + QMetaObject::invokeMethod(this, "refresh", Qt::QueuedConnection); } bool QmlFolderListModel::isFolder(int index) const @@ -159,9 +166,12 @@ void QmlFolderListModel::refresh() Q_D(QmlFolderListModel); int prevCount = count(); d->folderIndex = QModelIndex(); - emit itemsRemoved(0, prevCount); + if (prevCount) + emit itemsRemoved(0, prevCount); d->folderIndex = d->model.index(d->folder); - emit itemsInserted(0, count()); + qDebug() << "count" << count(); + if (count()) + emit itemsInserted(0, count()); } void QmlFolderListModel::inserted(const QModelIndex &index, int start, int end) diff --git a/src/declarative/extra/qmlfolderlistmodel.h b/src/declarative/extra/qmlfolderlistmodel.h index c8b605a..a6e8526 100644 --- a/src/declarative/extra/qmlfolderlistmodel.h +++ b/src/declarative/extra/qmlfolderlistmodel.h @@ -60,7 +60,7 @@ class Q_DECLARATIVE_EXPORT QmlFolderListModel : public QListModelInterface, publ Q_OBJECT Q_INTERFACES(QmlParserStatus) - Q_PROPERTY(QString folder READ folder WRITE setFolder) + Q_PROPERTY(QString folder READ folder WRITE setFolder NOTIFY folderChanged) Q_PROPERTY(QStringList nameFilters READ nameFilters WRITE setNameFilters) public: @@ -82,6 +82,9 @@ public: Q_INVOKABLE bool isFolder(int index) const; +Q_SIGNALS: + void folderChanged(); + private Q_SLOTS: void refresh(); void inserted(const QModelIndex &index, int start, int end); -- cgit v0.12