From a4674ff625f6a6b4ffd4421f0ddd4227180d31fd Mon Sep 17 00:00:00 2001 From: Kevin Krammer Date: Wed, 13 Mar 2013 14:35:25 +0100 Subject: [PATCH] Export file system model as a type --- lib/declarativefilesystemmodelextension.cpp | 54 +++++++++++++++++++++++++++ lib/declarativefilesystemmodelextension.h | 51 +++++++++++++++++++++++++ lib/declarativewidgetsdocument.cpp | 3 + lib/lib.pro | 6 ++- 4 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 lib/declarativefilesystemmodelextension.cpp create mode 100644 lib/declarativefilesystemmodelextension.h diff --git a/lib/declarativefilesystemmodelextension.cpp b/lib/declarativefilesystemmodelextension.cpp new file mode 100644 index 0000000..9e110af --- /dev/null +++ b/lib/declarativefilesystemmodelextension.cpp @@ -0,0 +1,54 @@ +/* + Copyright (C) 2012-2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Kevin Krammer, kevin.krammer@kdab.com + Author: Tobias Koenig, tobias.koenig@kdab.com + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#include "declarativefilesystemmodelextension.h" + +#include +#include + +DeclarativeFileSystemModelExtension::DeclarativeFileSystemModelExtension(QObject *parent) + : DeclarativeObjectExtension(parent) +{ +} + +QFileSystemModel *DeclarativeFileSystemModelExtension::extendedModel() const +{ + QFileSystemModel *model = qobject_cast(extendedObject()); + Q_ASSERT(model); + + return model; +} + +void DeclarativeFileSystemModelExtension::setRootPath(const QString &path) +{ + // TODO: for whatever reason QFileSystemModel does not emit its rootPathChanged() signal + QFileSystemModel *model = extendedModel(); + if (model->rootPath() == path) + return; + + model->setRootPath(path); + + emit rootPathChanged(path); +} + +QString DeclarativeFileSystemModelExtension::rootPath() const +{ + return extendedModel()->rootPath(); +} diff --git a/lib/declarativefilesystemmodelextension.h b/lib/declarativefilesystemmodelextension.h new file mode 100644 index 0000000..4353965 --- /dev/null +++ b/lib/declarativefilesystemmodelextension.h @@ -0,0 +1,51 @@ +/* + Copyright (C) 2012-2013 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com + Author: Kevin Krammer, kevin.krammer@kdab.com + Author: Tobias Koenig, tobias.koenig@kdab.com + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +#ifndef DECLARATIVEFILESYSTEMMODELEXTENSION_H +#define DECLARATIVEFILESYSTEMMODELEXTENSION_H + +#include "declarativeobjectextension.h" + +class QFileSystemModel; + +class DeclarativeFileSystemModelExtension : public DeclarativeObjectExtension +{ + Q_OBJECT + + // repeat property declarations, qmlRegisterExtendedType doesn't see the ones from base class + Q_PROPERTY(QDeclarativeListProperty data READ data DESIGNABLE false) + Q_PROPERTY(QString rootPath READ rootPath WRITE setRootPath NOTIFY rootPathChanged) + + Q_CLASSINFO("DefaultProperty", "data") + + public: + explicit DeclarativeFileSystemModelExtension(QObject *parent = 0); + + QFileSystemModel *extendedModel() const; + + void setRootPath(const QString &path); + + QString rootPath() const; + + Q_SIGNALS: + void rootPathChanged(const QString &rootPath); +}; + +#endif // DECLARATIVEFILESYSTEMMODELEXTENSION_H diff --git a/lib/declarativewidgetsdocument.cpp b/lib/declarativewidgetsdocument.cpp index 6368e6b..f0d4ae7 100644 --- a/lib/declarativewidgetsdocument.cpp +++ b/lib/declarativewidgetsdocument.cpp @@ -28,6 +28,7 @@ #include "declarativedeclarativecontext_p.h" #include "declarativedeclarativeviewextension_p.h" #include "declarativefiledialog_p.h" +#include "declarativefilesystemmodelextension.h" #include "declarativefontdialog_p.h" #include "declarativeformlayout_p.h" #include "declarativegridlayout_p.h" @@ -64,6 +65,7 @@ #include #include #include +#include #include #include #include @@ -111,6 +113,7 @@ DeclarativeWidgetsDocument::DeclarativeWidgetsDocument(const QUrl &url, QObject qmlRegisterExtendedType("QtGui", 1, 0, "ButtonGroup"); qmlRegisterType("QtGui", 1, 0, "DeclarativeContextProperty"); qmlRegisterType("QtGui", 1, 0, "DeclarativeContext"); + qmlRegisterExtendedType("QtGui", 1, 0, "FileSystemModel"); qmlRegisterType(); qmlRegisterExtendedType("QtGui", 1, 0, "Separator"); qmlRegisterType(); diff --git a/lib/lib.pro b/lib/lib.pro index c11d435..2978878 100644 --- a/lib/lib.pro +++ b/lib/lib.pro @@ -40,7 +40,8 @@ HEADERS = \ declarativewidgetsdocument.h \ objectadaptors_p.h \ qmetaobjectbuilder_p.h \ - staticdialogmethodattached_p.h + staticdialogmethodattached_p.h \ + declarativefilesystemmodelextension.h SOURCES = \ abstractdeclarativeobject.cpp \ @@ -77,4 +78,5 @@ SOURCES = \ declarativewidgetsdocument.cpp \ objectadaptors.cpp \ qmetaobjectbuilder.cpp \ - staticdialogmethodattached.cpp + staticdialogmethodattached.cpp \ + declarativefilesystemmodelextension.cpp -- 1.7.2.5