From aa81173335cf493055ab4143c970d99298b28822 Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Wed, 21 Feb 2018 19:56:37 +0100 Subject: [PATCH] added simple drawer logic --- examples/mvvmquick/SampleQuick/SampleView.qml | 4 +- src/imports/mvvmquick/DialogPresenter.qml | 98 +++++++++---------- src/imports/mvvmquick/PresentingDrawer.qml | 39 ++++++++ src/imports/mvvmquick/PresentingStackView.qml | 15 ++- src/imports/mvvmquick/QtMvvmApp.qml | 23 +++++ src/imports/mvvmquick/mvvmquick.pro | 9 +- src/imports/mvvmquick/plugins.qmltypes | 1 + src/imports/mvvmquick/qmldir | 1 + src/imports/mvvmquick/qqmlquickpresenter.cpp | 10 ++ src/imports/mvvmquick/qqmlquickpresenter.h | 3 + 10 files changed, 141 insertions(+), 62 deletions(-) create mode 100644 src/imports/mvvmquick/PresentingDrawer.qml diff --git a/examples/mvvmquick/SampleQuick/SampleView.qml b/examples/mvvmquick/SampleQuick/SampleView.qml index b73d697..02adee1 100644 --- a/examples/mvvmquick/SampleQuick/SampleView.qml +++ b/examples/mvvmquick/SampleQuick/SampleView.qml @@ -11,9 +11,11 @@ Page { property SampleViewModel viewModel: null header: ActionBar { - showMenuButton: false + showMenuButton: true title: qsTr("Sample") + onMenuButtonClicked: QuickPresenter.toggleDrawer() + moreMenu: Menu { MenuItem { text: qsTr("Another Input") diff --git a/src/imports/mvvmquick/DialogPresenter.qml b/src/imports/mvvmquick/DialogPresenter.qml index 0af0181..b28216c 100644 --- a/src/imports/mvvmquick/DialogPresenter.qml +++ b/src/imports/mvvmquick/DialogPresenter.qml @@ -28,79 +28,71 @@ QtObject { } property var _popups: [] - property Component _msgBoxComponent: Component { - MsgBox { - id: __msgBox - - onClosed: { - var index = _popups.indexOf(__msgBox); - if(index > -1) { - __msgBox.destroy(); - _dialogPresenter._popups.splice(index, 1); - } + property Component _msgBoxComponent: MsgBox { + id: __msgBox + + onClosed: { + var index = _popups.indexOf(__msgBox); + if(index > -1) { + __msgBox.destroy(); + _dialogPresenter._popups.splice(index, 1); } + } - Component.onCompleted: { - _popups.push(__msgBox) - __msgBox.open() - } + Component.onCompleted: { + _popups.push(__msgBox) + __msgBox.open() } } - property Component _inputComponent: Component { - InputDialog { - id: __input + property Component _inputComponent: InputDialog { + id: __input - onClosed: { - var index = _popups.indexOf(__input); - if(index > -1) { - __input.destroy(); - _dialogPresenter._popups.splice(index, 1); - } + onClosed: { + var index = _popups.indexOf(__input); + if(index > -1) { + __input.destroy(); + _dialogPresenter._popups.splice(index, 1); } + } - Component.onCompleted: { - _popups.push(__input) - __input.open() - } + Component.onCompleted: { + _popups.push(__input) + __input.open() } } - property Component _fileComponent: Component { - FileDialog { - id: __file + property Component _fileComponent: FileDialog { + id: __file - onClosed: { - var index = _popups.indexOf(__file); - if(index > -1) { - __file.destroy(); - _dialogPresenter._popups.splice(index, 1); - } + onClosed: { + var index = _popups.indexOf(__file); + if(index > -1) { + __file.destroy(); + _dialogPresenter._popups.splice(index, 1); } + } - Component.onCompleted: { - _popups.push(__file) - __file.open() - } + Component.onCompleted: { + _popups.push(__file) + __file.open() } } - property Component _folderComponent: Component { - FolderDialog { - id: __folder + property Component _folderComponent: FolderDialog { + id: __folder - onClosed: { - var index = _popups.indexOf(__folder); - if(index > -1) { - __folder.destroy(); - _dialogPresenter._popups.splice(index, 1); - } + onClosed: { + var index = _popups.indexOf(__folder); + if(index > -1) { + __folder.destroy(); + _dialogPresenter._popups.splice(index, 1); } + } - Component.onCompleted: { - _popups.push(__folder) - __folder.open() - } + Component.onCompleted: { + _popups.push(__folder) + __folder.open() } } diff --git a/src/imports/mvvmquick/PresentingDrawer.qml b/src/imports/mvvmquick/PresentingDrawer.qml new file mode 100644 index 0000000..45d1eb3 --- /dev/null +++ b/src/imports/mvvmquick/PresentingDrawer.qml @@ -0,0 +1,39 @@ +import QtQuick 2.10 +import QtQuick.Controls 2.3 + +Drawer { + id: _presentingDrawer + + width: Math.min(300, parent.width - 24); + height: parent.height - y + + property Item _mainChild: null + + function toggle() { + if(visible) + close(); + else + open(); + } + + function presentDrawerContent(item) { + if(_mainChild) + _mainChild.destroy(); + item.parent = _presentingDrawer; //TODO test + _mainChild = item; + return true; + } + + function closeAction() { + if(_mainChild && typeof _mainChild.closeAction == "function") { + if(_mainChild.closeAction()) + return true; + } + + if(visible) { + close(); + return true; + } else + return false; + } +} diff --git a/src/imports/mvvmquick/PresentingStackView.qml b/src/imports/mvvmquick/PresentingStackView.qml index 7e3e31b..1ad443d 100644 --- a/src/imports/mvvmquick/PresentingStackView.qml +++ b/src/imports/mvvmquick/PresentingStackView.qml @@ -8,10 +8,17 @@ StackView { property int opDuration: 75 function presentItem(item) { - if(push(item)) - return true; - else - return false; + if(item.presentAsRoot) { //TODO document + if(push(item)) + return true; + else + return false; + } else { + if(replace(null, item)) + return true; + else + return false; + } } function safePop(item, operation) { diff --git a/src/imports/mvvmquick/QtMvvmApp.qml b/src/imports/mvvmquick/QtMvvmApp.qml index d206b12..d84eddb 100644 --- a/src/imports/mvvmquick/QtMvvmApp.qml +++ b/src/imports/mvvmquick/QtMvvmApp.qml @@ -8,6 +8,8 @@ ApplicationWindow { width: 360 height: 520 + readonly property alias drawer: _drawerLoader.item + PresenterProgress { id: _rootProgress z: _rootStack.empty ? 10 : -10 @@ -18,6 +20,15 @@ ApplicationWindow { anchors.fill: parent } + Loader { + id: _drawerLoader + active: false + asynchronous: false + sourceComponent: PresentingDrawer { + id: _rootDrawer + } + } + PopupPresenter { id: _rootPopup rootItem: _root.contentItem @@ -29,6 +40,9 @@ ApplicationWindow { } function presentDrawerContent(item) { + if(!_drawerLoader.item) + _drawerLoader.active = true; + _drawerLoader.item.presentDrawerContent(item); return false } @@ -44,11 +58,20 @@ ApplicationWindow { return _rootDialogs.showDialog(config, result); } + function toggleDrawer() { + if(_drawerLoader.item) + _drawerLoader.item.toggle(); + else + console.warn("No drawer like view active. Cannot toggle drawer"); + } + Component.onCompleted: QuickPresenter.qmlPresenter = _root onClosing: { var closed = false; + if(!closed && _drawerLoader.item) + closed = _drawerLoader.item.closeAction(); if(!closed) closed = _rootDialogs.closeAction(); if(!closed) diff --git a/src/imports/mvvmquick/mvvmquick.pro b/src/imports/mvvmquick/mvvmquick.pro index 391b409..5bd6cc4 100644 --- a/src/imports/mvvmquick/mvvmquick.pro +++ b/src/imports/mvvmquick/mvvmquick.pro @@ -25,7 +25,11 @@ QML_FILES += \ TintIcon.qml \ MsgBoxBase.qml \ MsgBox.qml \ - InputDialog.qml + InputDialog.qml \ + PresentingDrawer.qml + +RESOURCES += \ + qtmvvmquick_plugin.qrc OTHER_FILES += qmldir @@ -53,6 +57,3 @@ generate_qmltypes { mfirst.depends += qmltypes QMAKE_EXTRA_TARGETS += mfirst } - -RESOURCES += \ - qtmvvmquick_plugin.qrc diff --git a/src/imports/mvvmquick/plugins.qmltypes b/src/imports/mvvmquick/plugins.qmltypes index 708c01d..308e9f4 100644 --- a/src/imports/mvvmquick/plugins.qmltypes +++ b/src/imports/mvvmquick/plugins.qmltypes @@ -70,6 +70,7 @@ Module { name: "inputViewFactoryChanged" Parameter { name: "inputViewFactory"; type: "InputViewFactory"; isPointer: true } } + Method { name: "toggleDrawer" } Method { name: "mimeTypeFilters" type: "QStringList" diff --git a/src/imports/mvvmquick/qmldir b/src/imports/mvvmquick/qmldir index 6238a4a..f502ced 100644 --- a/src/imports/mvvmquick/qmldir +++ b/src/imports/mvvmquick/qmldir @@ -12,5 +12,6 @@ PresenterProgress 1.0 PresenterProgress.qml PresentingStackView 1.0 PresentingStackView.qml PopupPresenter 1.0 PopupPresenter.qml DialogPresenter 1.0 DialogPresenter.qml +PresentingDrawer 1.0 PresentingDrawer.qml QtMvvmApp 1.0 QtMvvmApp.qml diff --git a/src/imports/mvvmquick/qqmlquickpresenter.cpp b/src/imports/mvvmquick/qqmlquickpresenter.cpp index 4f4267a..79f2b3f 100644 --- a/src/imports/mvvmquick/qqmlquickpresenter.cpp +++ b/src/imports/mvvmquick/qqmlquickpresenter.cpp @@ -54,6 +54,16 @@ QStringList QQmlQuickPresenter::mimeTypeFilters(const QStringList &mimeTypes) co return filters; } +void QQmlQuickPresenter::toggleDrawer() +{ + if(!_qmlPresenter) { + qmlWarning(this).space() << "No QML-Presenter registered! Unable to toggle drawer"; + return; + } + + QMetaObject::invokeMethod(_qmlPresenter, "toggleDrawer"); +} + void QQmlQuickPresenter::present(ViewModel *viewModel, const QVariantHash ¶ms, const QUrl &viewUrl, QPointer parent) { auto component = _componentCache.object(viewUrl); diff --git a/src/imports/mvvmquick/qqmlquickpresenter.h b/src/imports/mvvmquick/qqmlquickpresenter.h index 5ea8dcd..fdce547 100644 --- a/src/imports/mvvmquick/qqmlquickpresenter.h +++ b/src/imports/mvvmquick/qqmlquickpresenter.h @@ -40,6 +40,9 @@ public: Q_INVOKABLE QStringList mimeTypeFilters(const QStringList &mimeTypes) const; +public Q_SLOTS: + void toggleDrawer(); + Q_SIGNALS: void qmlPresenterChanged(QObject* qmlPresenter); void viewLoadingChanged(bool viewLoading);