Browse Source

added basic popup and view presenting with sample

pull/2/head
Skycoder42 7 years ago
parent
commit
4ca2207cd7
  1. 42
      examples/mvvmquick/SampleQuick/ResultView.qml
  2. 3
      examples/mvvmquick/SampleQuick/SampleQuick.pro
  3. 113
      examples/mvvmquick/SampleQuick/SampleView.qml
  4. 9
      examples/mvvmquick/SampleQuick/main.cpp
  5. 1
      examples/mvvmquick/SampleQuick/qml.qrc
  6. 20
      examples/mvvmquick/SampleQuick/qpmx.json
  7. 22
      src/imports/mvvmcore/plugins.qmltypes
  8. 5
      src/imports/mvvmcore/qtmvvmcore_plugin.cpp
  9. 30
      src/imports/mvvmquick/PopupPresenter.qml
  10. 8
      src/imports/mvvmquick/PresentingStackView.qml
  11. 30
      src/imports/mvvmquick/QtMvvmApp.qml
  12. 3
      src/imports/mvvmquick/mvvmquick.pro
  13. 1
      src/imports/mvvmquick/plugins.qmltypes
  14. 4
      src/imports/mvvmquick/qmldir
  15. 7
      src/imports/mvvmquick/qqmlquickpresenter.cpp
  16. 4
      src/imports/mvvmquick/qqmlquickpresenter.h

42
examples/mvvmquick/SampleQuick/ResultView.qml

@ -0,0 +1,42 @@
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import de.skycoder42.QtMvvm.Core 1.0
import de.skycoder42.QtMvvm.Quick 1.0
import de.skycoder42.quickextras 2.0
import de.skycoder42.QtMvvm.Sample 1.0
AlertDialog {
id: resultDialog
property ResultViewModel viewModel: null
title: qsTr("Enter something")
ColumnLayout {
anchors.fill: parent
Label {
text: qsTr("Enter a result to be reported as event to the main view:")
wrapMode: Text.WordWrap
Layout.fillWidth: true
}
TextField {
id: resultEdit
Layout.fillWidth: true
selectByMouse: true
MvvmBinding {
viewModel: resultDialog.viewModel
viewModelProperty: "result"
view: resultEdit
viewProperty: "text"
}
}
}
standardButtons: Dialog.Ok | Dialog.Cancel
onAccepted: viewModel.done()
}

3
examples/mvvmquick/SampleQuick/SampleQuick.pro

@ -73,3 +73,6 @@ samples_in_build {
HEADERS += \
quickeventservice.h
!ReleaseBuild:!DebugBuild:!system(qpmx -d $$shell_quote($$_PRO_FILE_PWD_) --qmake-run init $$QPMX_EXTRA_OPTIONS $$shell_quote($$QMAKE_QMAKE) $$shell_quote($$OUT_PWD)): error(qpmx initialization failed. Check the compilation log for details.)
else: include($$OUT_PWD/qpmx_generated.pri)

113
examples/mvvmquick/SampleQuick/SampleView.qml

@ -1,11 +1,114 @@
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import de.skycoder42.QtMvvm.Core 1.0
import de.skycoder42.QtMvvm.Quick 1.0
import de.skycoder42.quickextras 2.0
import de.skycoder42.QtMvvm.Sample 1.0
Rectangle {
property QtObject viewModel: null
Page {
id: sampleView
property SampleViewModel viewModel: null
anchors.fill: parent
header: ActionBar {
showMenuButton: false
title: qsTr("Sample")
color: "red"
moreMenu: Menu {
Action {
text: qsTr("Another &Input")
onTriggered: viewModel.getInput()
}
Action {
text: qsTr("Add &Files")
onTriggered: viewModel.getFiles()
}
Component.onCompleted: console.log(viewModel)
MenuSeparator {}
Action {
text: qsTr("&About")
onTriggered: viewModel.about()
}
}
}
PresenterProgress {}
Pane {
anchors.fill: parent
GridLayout {
columns: 2
anchors.fill: parent
Label {
text: qsTr("Name:")
}
TextField {
id: nameEdit
Layout.fillWidth: true
selectByMouse: true
MvvmBinding {
viewModel: sampleView.viewModel
view: nameEdit
viewModelProperty: "name"
viewProperty: "text"
}
}
Label {
text: qsTr("Active:")
}
Switch {
id: activeSwitch
Layout.alignment: Qt.AlignLeft
MvvmBinding {
viewModel: sampleView.viewModel
view: activeSwitch
viewModelProperty: "active"
viewProperty: "checked"
}
}
Label {
text: qsTr("Events:")
}
RowLayout {
Layout.fillWidth: true
Button {
text: qsTr("&Clear")
onClicked: viewModel.clearEvents()
}
Button {
text: qsTr("Get &Result")
onClicked: viewModel.getResult()
}
}
ListView {
id: lView
Layout.columnSpan: 2
Layout.fillWidth: true
Layout.fillHeight: true
clip: true
ScrollBar.vertical: ScrollBar {}
model: viewModel.eventsModel
delegate: ItemDelegate {
width: parent.width
text: viewModel.eventsModel.data(viewModel.eventsModel.index(index, 0)) //because "display" is not accessible
}
}
}
}
}

9
examples/mvvmquick/SampleQuick/main.cpp

@ -3,9 +3,14 @@
#include <QtMvvmCore/ServiceRegistry>
#include <QtMvvmQuick/QuickPresenter>
#include <quickextras.h>
#include <samplecoreapp.h>
#include <echoservice.h>
#include <sampleviewmodel.h>
#include <resultviewmodel.h>
#include "quickeventservice.h"
QTMVVM_REGISTER_CORE_APP(SampleCoreApp)
@ -19,12 +24,16 @@ int main(int argc, char *argv[])
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
qmlRegisterUncreatableType<SampleViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "SampleViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<ResultViewModel>("de.skycoder42.QtMvvm.Sample", 1, 0, "ResultViewModel", QStringLiteral("ViewModels cannot be created"));
QtMvvm::QuickPresenter::registerAsPresenter();
QtMvvm::ServiceRegistry::instance()->registerObject<EchoService>();
QtMvvm::ServiceRegistry::instance()->registerInterface<IEventService, QuickEventService>();
QQmlApplicationEngine engine;
QuickExtras::setupEngine(&engine);
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())
return -1;

1
examples/mvvmquick/SampleQuick/qml.qrc

@ -4,5 +4,6 @@
</qresource>
<qresource prefix="/qtmvvm/views">
<file>SampleView.qml</file>
<file>ResultView.qml</file>
</qresource>
</RCC>

20
examples/mvvmquick/SampleQuick/qpmx.json

@ -0,0 +1,20 @@
{
"dependencies": [
{
"package": "de.skycoder42.quickextras",
"provider": "qpm",
"version": "2.1.0"
}
],
"license": {
"file": "",
"name": ""
},
"prcFile": "",
"priFile": "",
"priIncludes": [
],
"publishers": {
},
"source": false
}

22
src/imports/mvvmcore/plugins.qmltypes

@ -11,7 +11,7 @@ Module {
Component {
name: "QtMvvm::QQmlMvvmBinding"
prototype: "QObject"
exports: ["de.skycoder42.QtMvvm.Core/Binding 1.0"]
exports: ["de.skycoder42.QtMvvm.Core/MvvmBinding 1.0"]
exportMetaObjectRevisions: [0]
Enum {
name: "BindingDirection"
@ -336,4 +336,24 @@ Module {
Parameter { name: "onResult"; type: "QJSValue" }
}
}
Component {
name: "QtMvvm::ViewModel"
prototype: "QObject"
exports: ["de.skycoder42.QtMvvm.Core/ViewModel 1.0"]
isCreatable: false
exportMetaObjectRevisions: [0]
Signal {
name: "resultReady"
Parameter { name: "result"; type: "QVariant" }
}
Method {
name: "onInit"
Parameter { name: "params"; type: "QVariantHash" }
}
Method {
name: "onResult"
Parameter { name: "requestCode"; type: "uint" }
Parameter { name: "result"; type: "QVariant" }
}
}
}

5
src/imports/mvvmcore/qtmvvmcore_plugin.cpp

@ -2,6 +2,8 @@
#include <QtQml>
#include <QtMvvmCore/ViewModel>
#include "qqmlmvvmbinding.h"
#include "qqmlmvvmmessage.h"
@ -19,7 +21,8 @@ void QtMvvmCoreDeclarativeModule::registerTypes(const char *uri)
Q_ASSERT(qstrcmp(uri, "de.skycoder42.QtMvvm.Core") == 0);
//Version 1.0
qmlRegisterType<QtMvvm::QQmlMvvmBinding>(uri, 1, 0, "Binding");
qmlRegisterUncreatableType<QtMvvm::ViewModel>(uri, 1, 0, "ViewModel", tr("ViewModels cannot be created from QML"));
qmlRegisterType<QtMvvm::QQmlMvvmBinding>(uri, 1, 0, "MvvmBinding");
qmlRegisterSingletonType<QtMvvm::QQmlMvvmMessage>(uri, 1, 0, "Message", createMessageSingleton);
// Check to make shure no module update is forgotten

30
src/imports/mvvmquick/PopupPresenter.qml

@ -0,0 +1,30 @@
import QtQuick 2.10
import QtQuick.Controls 2.3
QtObject {
id: _popPresenter
property var popups: []
function presentPopup(root, popup) {
popup.parent = root;
popup.closed.connect(function() {
var index = popups.indexOf(popup);
if(index > -1) {
popup.destroy();
popups.splice(index, 1);
}
});
popup.open();
popups.push(popup);
return true;
}
function closeAction() {
if(popups.length > 0) {
popups[popups.length - 1].close();
return true;
} else
return false;
}
}

8
src/imports/mvvmquick/PresentingStackView.qml

@ -14,6 +14,12 @@ StackView {
return false;
}
function safePop(item, operation) {
var resItem = pop(item, operation)
if(resItem)
resItem.destroy();
}
function closeAction() {
if(typeof _presenterStack.currentItem.closeAction == "function") {
if(_presenterStack.currentItem.closeAction())
@ -23,7 +29,7 @@ StackView {
if(_presenterStack.depth <= 1)
return false;
else {
if(_presenterStack.pop())
if(_presenterStack.safePop())
return true;
else
return false;

30
src/imports/mvvmquick/QtMvvmApp.qml

@ -9,20 +9,29 @@ ApplicationWindow {
height: 520
PresenterProgress {
z: -10 //keep it low so its hidden after the first view was shown
id: _rootProgress
z: _rootStack.empty ? 10 : -10
}
PopupPresenter {
id: _rootPopup
}
PresentingStackView {
id: mainStack
id: _rootStack
anchors.fill: parent
}
function presentDrawerContent(item) {
return false
}
function presentItem(item) {
return mainStack.presentItem(item);
return _rootStack.presentItem(item);
}
function presentPopup(item) {
return true;
function presentPopup(popup) {
return _rootPopup.presentPopup(contentItem, popup);
}
Component.onCompleted: QuickPresenter.qmlPresenter = _root
@ -30,15 +39,10 @@ ApplicationWindow {
onClosing: {
var closed = false;//messageBox.closeAction();
// if(!closed) {
// if(popups.length > 0) {
// popups[popups.length - 1].close();
// closed = true;
// }
// }
if(!closed)
closed = mainStack.closeAction();
closed = _rootPopup.closeAction();
if(!closed)
closed = _rootStack.closeAction();
close.accepted = !closed;
}

3
src/imports/mvvmquick/mvvmquick.pro

@ -17,7 +17,8 @@ SOURCES += \
QML_FILES += \
QtMvvmApp.qml \
PresentingStackView.qml \
PresenterProgress.qml
PresenterProgress.qml \
PopupPresenter.qml
OTHER_FILES += qmldir

1
src/imports/mvvmquick/plugins.qmltypes

@ -15,6 +15,7 @@ Module {
isCreatable: false
isSingleton: true
exportMetaObjectRevisions: [0]
Property { name: "currentStyle"; type: "string"; isReadonly: true }
Property { name: "qmlPresenter"; type: "QObject"; isPointer: true }
Property { name: "viewLoading"; type: "bool"; isReadonly: true }
Property { name: "loadingProgress"; type: "double"; isReadonly: true }

4
src/imports/mvvmquick/qmldir

@ -3,6 +3,8 @@ plugin declarative_mvvmquick
classname QtMvvmQuickDeclarativeModule
typeinfo plugins.qmltypes
QtMvvmApp 1.0 QtMvvmApp.qml
PresenterProgress 1.0 PresenterProgress.qml
PresentingStackView 1.0 PresentingStackView.qml
PopupPresenter 1.0 PopupPresenter.qml
QtMvvmApp 1.0 QtMvvmApp.qml

7
src/imports/mvvmquick/qqmlquickpresenter.cpp

@ -4,6 +4,8 @@
#include <QtQuick/QQuickItem>
#include <QtQuickControls2/QQuickStyle>
#include <QtMvvmQuick/private/quickpresenter_p.h>
using namespace QtMvvm;
@ -18,6 +20,11 @@ QQmlQuickPresenter::QQmlQuickPresenter(QQmlEngine *engine) :
QuickPresenterPrivate::setQmlPresenter(this);
}
QString QQmlQuickPresenter::currentStyle() const
{
return QQuickStyle::name();
}
bool QQmlQuickPresenter::isViewLoading() const
{
return _latestComponent;

4
src/imports/mvvmquick/qqmlquickpresenter.h

@ -19,6 +19,8 @@ class QQmlQuickPresenter : public QObject
{
Q_OBJECT
Q_PROPERTY(QString currentStyle READ currentStyle CONSTANT)
Q_PROPERTY(QObject* qmlPresenter MEMBER _qmlPresenter NOTIFY qmlPresenterChanged)
Q_PROPERTY(bool viewLoading READ isViewLoading NOTIFY viewLoadingChanged)
Q_PROPERTY(qreal loadingProgress READ loadingProgress NOTIFY loadingProgressChanged)
@ -26,6 +28,8 @@ class QQmlQuickPresenter : public QObject
public:
explicit QQmlQuickPresenter(QQmlEngine *engine);
QString currentStyle() const;
bool isViewLoading() const;
qreal loadingProgress() const;

Loading…
Cancel
Save