Browse Source

added qml implementation for view placeholder

pull/2/head
Skycoder42 7 years ago
parent
commit
dea3435e5e
No known key found for this signature in database GPG Key ID: 8E01AD9EF0578D2B
  1. 6
      examples/mvvmcore/SampleCore/SampleCore.pro
  2. 31
      examples/mvvmcore/SampleCore/containerviewmodel.cpp
  3. 34
      examples/mvvmcore/SampleCore/containerviewmodel.h
  4. 5
      examples/mvvmcore/SampleCore/drawerviewmodel.cpp
  5. 29
      examples/mvvmquick/SampleQuick/ChildView.qml
  6. 71
      examples/mvvmquick/SampleQuick/ContainerView.qml
  7. 3
      examples/mvvmquick/SampleQuick/main.cpp
  8. 2
      examples/mvvmquick/SampleQuick/qml.qrc
  9. 6
      src/imports/mvvmquick/mvvmquick.pro
  10. 55
      src/imports/mvvmquick/plugins.qmltypes
  11. 201
      src/imports/mvvmquick/qqmlviewplaceholder.cpp
  12. 77
      src/imports/mvvmquick/qqmlviewplaceholder.h
  13. 3
      src/imports/mvvmquick/qtmvvmquick_plugin.cpp
  14. 32
      src/mvvmcore/coreapp.cpp
  15. 15
      src/mvvmcore/coreapp.h
  16. 2
      src/mvvmcore/viewmodel.h

6
examples/mvvmcore/SampleCore/SampleCore.pro

@ -12,7 +12,8 @@ HEADERS += \
echoservice.h \
resultviewmodel.h \
drawerviewmodel.h \
tabviewmodel.h
tabviewmodel.h \
containerviewmodel.h
SOURCES += \
samplecoreapp.cpp \
@ -20,7 +21,8 @@ SOURCES += \
echoservice.cpp \
resultviewmodel.cpp \
drawerviewmodel.cpp \
tabviewmodel.cpp
tabviewmodel.cpp \
containerviewmodel.cpp
RESOURCES += \
sample_core.qrc

31
examples/mvvmcore/SampleCore/containerviewmodel.cpp

@ -0,0 +1,31 @@
#include "containerviewmodel.h"
ContainerViewModel::ContainerViewModel(QObject *parent) :
ViewModel(parent)
{}
ContainerViewModel::~ContainerViewModel()
{
qInfo(Q_FUNC_INFO);
}
QString ContainerViewModel::vmType() const
{
return QString::fromUtf8(QMetaType::typeName(qMetaTypeId<ChildViewModel*>()));
}
void ContainerViewModel::loadChild()
{
show<ChildViewModel>();
}
ChildViewModel::ChildViewModel(QObject *parent) :
ViewModel(parent)
{}
ChildViewModel::~ChildViewModel()
{
qInfo(Q_FUNC_INFO);
}

34
examples/mvvmcore/SampleCore/containerviewmodel.h

@ -0,0 +1,34 @@
#ifndef CONTAINERVIEWMODEL_H
#define CONTAINERVIEWMODEL_H
#include <QtMvvmCore/ViewModel>
class ChildViewModel : public QtMvvm::ViewModel
{
Q_OBJECT
public:
Q_INVOKABLE explicit ChildViewModel(QObject *parent = nullptr);
~ChildViewModel();
};
class ContainerViewModel : public QtMvvm::ViewModel
{
Q_OBJECT
Q_PROPERTY(QString vmType READ vmType CONSTANT)
public:
Q_INVOKABLE explicit ContainerViewModel(QObject *parent = nullptr);
~ContainerViewModel();
QString vmType() const;
public Q_SLOTS:
void loadChild();
};
Q_DECLARE_METATYPE(ChildViewModel*)
Q_DECLARE_METATYPE(ContainerViewModel*)
#endif // CONTAINERVIEWMODEL_H

5
examples/mvvmcore/SampleCore/drawerviewmodel.cpp

@ -3,6 +3,7 @@
#include "sampleviewmodel.h"
#include "tabviewmodel.h"
#include "containerviewmodel.h"
DrawerViewModel::DrawerViewModel(QObject *parent) :
ViewModel(parent),
@ -10,6 +11,7 @@ DrawerViewModel::DrawerViewModel(QObject *parent) :
{
_navModel->appendRow(new QStandardItem(tr("Main Sample")));
_navModel->appendRow(new QStandardItem(tr("Tab Sample")));
_navModel->appendRow(new QStandardItem(tr("View Container Sample")));
_navModel->appendRow(new QStandardItem(tr("Settings")));
}
@ -35,6 +37,9 @@ void DrawerViewModel::open(int index)
});
break;
case 2:
show<ContainerViewModel>();
break;
case 3:
show<QtMvvm::SettingsViewModel>();
break;
default:

29
examples/mvvmquick/SampleQuick/ChildView.qml

@ -0,0 +1,29 @@
import QtQuick 2.10
import QtQuick.Controls 2.3
import de.skycoder42.QtMvvm.Core 1.1
import de.skycoder42.QtMvvm.Quick 1.1
import de.skycoder42.QtMvvm.Sample 1.1
Rectangle {
property ChildViewModel viewModel: null
color: "red"
property bool dummyClosed: false
function closeAction() {
if(dummyClosed)
return false;
else {
dummyClosed = true;
label.text = qsTr("I was closed!")
return true;
}
}
Label {
id: label
anchors.centerIn: parent
text: qsTr("I am the child!")
}
}

71
examples/mvvmquick/SampleQuick/ContainerView.qml

@ -0,0 +1,71 @@
import QtQuick 2.10
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.3
import de.skycoder42.QtMvvm.Core 1.1
import de.skycoder42.QtMvvm.Quick 1.1
import de.skycoder42.QtMvvm.Sample 1.1
Page {
id: containerView
property ContainerViewModel viewModel: null
function presentItem(item) {
return viewPlaceholder.presentItem(item);
}
function closeAction() {
return viewPlaceholder.closeAction();
}
header: ContrastToolBar {
ToolBarLabel {
text: qsTr("View Container")
anchors.fill: parent
}
}
PresenterProgress {}
Pane {
anchors.fill: parent
ColumnLayout {
anchors.fill: parent
ViewPlaceholder {
id: viewPlaceholder
viewModelType: containerView.viewModel.vmType
parentViewModel: containerView.viewModel
autoPresent: false
Layout.fillWidth: true
Layout.fillHeight: true
BusyIndicator {
anchors.centerIn: parent
anchors.verticalCenterOffset: -(hookButton.height/2)
running: !viewPlaceholder.loadedView
}
Button {
id: hookButton
Layout.fillWidth: true
text: qsTr("Load from QML")
onClicked: viewPlaceholder.presentView();
enabled: !viewPlaceholder.loadedView
anchors.left: parent.left
anchors.right: parent.right
anchors.bottom: parent.bottom
}
}
Button {
Layout.fillWidth: true
text: viewPlaceholder.loadedView ? qsTr("Discard child") : qsTr("Show Child")
onClicked: viewPlaceholder.loadedView ? viewPlaceholder.discardView() : viewModel.loadChild();
}
}
}
}

3
examples/mvvmquick/SampleQuick/main.cpp

@ -12,6 +12,7 @@
#include <resultviewmodel.h>
#include <drawerviewmodel.h>
#include <tabviewmodel.h>
#include <containerviewmodel.h>
#include "quickeventservice.h"
@ -34,6 +35,8 @@ int main(int argc, char *argv[])
qmlRegisterUncreatableType<DrawerViewModel>("de.skycoder42.QtMvvm.Sample", 1, 1, "DrawerViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<TabViewModel>("de.skycoder42.QtMvvm.Sample", 1, 1, "TabViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<TabItemViewModel>("de.skycoder42.QtMvvm.Sample", 1, 1, "TabItemViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<ContainerViewModel>("de.skycoder42.QtMvvm.Sample", 1, 1, "ContainerViewModel", QStringLiteral("ViewModels cannot be created"));
qmlRegisterUncreatableType<ChildViewModel>("de.skycoder42.QtMvvm.Sample", 1, 1, "ChildViewModel", QStringLiteral("ViewModels cannot be created"));
QtMvvm::ServiceRegistry::instance()->registerObject<EchoService>();
QtMvvm::ServiceRegistry::instance()->registerInterface<IEventService, QuickEventService>();

2
examples/mvvmquick/SampleQuick/qml.qrc

@ -8,5 +8,7 @@
<file>DrawerView.qml</file>
<file>TabView.qml</file>
<file>TabItemView.qml</file>
<file>ContainerView.qml</file>
<file>ChildView.qml</file>
</qresource>
</RCC>

6
src/imports/mvvmquick/mvvmquick.pro

@ -12,7 +12,8 @@ HEADERS += \
settingsuibuilder.h \
settingssectionmodel.h \
multifilterproxymodel.h \
settingsentrymodel.h
settingsentrymodel.h \
qqmlviewplaceholder.h
SOURCES += \
qtmvvmquick_plugin.cpp \
@ -20,7 +21,8 @@ SOURCES += \
settingsuibuilder.cpp \
settingssectionmodel.cpp \
multifilterproxymodel.cpp \
settingsentrymodel.cpp
settingsentrymodel.cpp \
qqmlviewplaceholder.cpp
QML_FILES += \
QtMvvmApp.qml \

55
src/imports/mvvmquick/plugins.qmltypes

@ -102,6 +102,61 @@ Module {
Parameter { name: "mimeTypes"; type: "QStringList" }
}
}
Component {
name: "QtMvvm::QQmlViewPlaceholder"
defaultProperty: "data"
prototype: "QQuickItem"
exports: ["de.skycoder42.QtMvvm.Quick/ViewPlaceholder 1.1"]
exportMetaObjectRevisions: [0]
Property { name: "viewModelType"; type: "string" }
Property { name: "showParams"; type: "QVariantHash" }
Property { name: "parentViewModel"; type: "QtMvvm::ViewModel"; isPointer: true }
Property { name: "autoPresent"; type: "bool" }
Property { name: "autoResizeView"; type: "bool" }
Property { name: "replaceViews"; type: "bool" }
Property { name: "closeViewOnAction"; type: "bool" }
Property { name: "loadedView"; type: "QQuickItem"; isReadonly: true; isPointer: true }
Signal {
name: "viewModelTypeChanged"
Parameter { name: "viewModelType"; type: "string" }
}
Signal {
name: "showParamsChanged"
Parameter { name: "showParams"; type: "QVariantHash" }
}
Signal {
name: "parentViewModelChanged"
Parameter { name: "parentViewModel"; type: "QtMvvm::ViewModel"; isPointer: true }
}
Signal {
name: "autoPresentChanged"
Parameter { name: "autoPresent"; type: "bool" }
}
Signal {
name: "autoResizeViewChanged"
Parameter { name: "autoResizeView"; type: "bool" }
}
Signal {
name: "replaceViewsChanged"
Parameter { name: "replaceViews"; type: "bool" }
}
Signal {
name: "closeViewOnActionChanged"
Parameter { name: "closeViewOnAction"; type: "bool" }
}
Signal {
name: "loadedViewChanged"
Parameter { name: "loadedView"; type: "QQuickItem"; isPointer: true }
}
Method { name: "presentView" }
Method { name: "discardView" }
Method {
name: "presentItem"
type: "bool"
Parameter { name: "item"; type: "QVariant" }
}
Method { name: "closeAction"; type: "bool" }
}
Component {
name: "QtMvvm::SettingsUiBuilder"
prototype: "QObject"

201
src/imports/mvvmquick/qqmlviewplaceholder.cpp

@ -0,0 +1,201 @@
#include "qqmlviewplaceholder.h"
#include <QtCore/QMetaMethod>
#include <QtQml/QQmlInfo>
#include <QtQml/QQmlProperty>
#include <QtMvvmCore/CoreApp>
using namespace QtMvvm;
QQmlViewPlaceholder::QQmlViewPlaceholder(QQuickItem *parent) :
QQuickItem{parent}
{
// auto presenting
connect(this, &QQmlViewPlaceholder::viewModelTypeChanged,
this, &QQmlViewPlaceholder::presentIfReady);
connect(this, &QQmlViewPlaceholder::autoPresentChanged,
this, &QQmlViewPlaceholder::presentIfReady);
// size changes
connect(this, &QQmlViewPlaceholder::widthChanged,
this, &QQmlViewPlaceholder::resizeView);
connect(this, &QQmlViewPlaceholder::heightChanged,
this, &QQmlViewPlaceholder::resizeView);
connect(this, &QQmlViewPlaceholder::autoResizeViewChanged,
this, &QQmlViewPlaceholder::resizeView);
}
bool QQmlViewPlaceholder::presentItem(const QVariant &item)
{
// check if the parameter is valid
auto quickItem = item.value<QQuickItem*>();
if(!quickItem) {
qmlWarning(this) << "presentItem called with invalid item of type " << item.typeName();
return false;
}
// handle already existing view case
if(_loadedView) {
if(_replaceViews) { // quick discard without reenableing all children
_loadedView->setVisible(false);
_loadedView->deleteLater();
_loadedView = nullptr;
} else {
qmlWarning(this) << R"(A view has already been presented. Discard it first via "discardView" or set "replaceViews" to true)";
return false;
}
}
// add
_loadedView = quickItem;
quickItem->setParent(this);
quickItem->setParentItem(this);
resizeView();
quickItem->setVisible(true);
// hide all children
for(auto child : childItems()) {
if(child != _loadedView)
child->setVisible(false);
}
emit loadedViewChanged(_loadedView);
return true;
}
bool QQmlViewPlaceholder::closeAction()
{
if(!_loadedView)
return false;
// call close on the view
auto vMetaObject = _loadedView->metaObject();
auto cMethodIndex = vMetaObject->indexOfMethod("closeAction()");
if(cMethodIndex != -1) {
auto cMethod = vMetaObject->method(cMethodIndex);
QVariant ok = false;
cMethod.invoke(_loadedView, Q_RETURN_ARG(QVariant, ok));
if(ok.toBool())
return true;
}
// close the view itself (if wished)
if(_closeViewOnAction) {
discardView();
return true;
}
// otherwise: nothing closed
return false;
}
void QQmlViewPlaceholder::setParentViewModel(ViewModel *parentViewModel)
{
// first: clear the auto-connected viewmodel, if required
if(_clearParentVmCon && _parentVmCon)
disconnect(_parentVmCon);
// then: set property as usual
if(_parentViewModel == parentViewModel)
return;
_parentViewModel = parentViewModel;
emit parentViewModelChanged(_parentViewModel);
// check the vm parent for a presenter method
auto view = qobject_cast<QQuickItem*>(_parentViewModel->parent());
if(view) {
if(view->metaObject()->indexOfMethod("presentItem(QVariant)") == -1)
qmlWarning(this) << R"(Parent item of "parentViewModel" does not have a "presentItem" method. Check the ViewPlaceholder documentation!)";
} else
qmlWarning(this) << R"(Parent item of "parentViewModel" is not an Item)";
presentIfReady();
}
QQuickItem *QQmlViewPlaceholder::loadedView() const
{
return _loadedView;
}
void QQmlViewPlaceholder::componentComplete()
{
// auto-set the vm if not already set
if(!_parentViewModel)
getParentViewModel();
// last step: call base implementation, then present
QQuickItem::componentComplete();
_isReady = true;
presentIfReady();
}
void QQmlViewPlaceholder::presentView()
{
CoreApp::show(qUtf8Printable(_viewModelType), _showParams, _parentViewModel);
}
void QQmlViewPlaceholder::discardView()
{
// hide view
_loadedView->setVisible(false);
// show all children again
for(auto child : childItems()) {
if(child != _loadedView)
child->setVisible(true);
}
// now delete it
_loadedView->deleteLater();
_loadedView = nullptr;
emit loadedViewChanged(nullptr);
}
void QQmlViewPlaceholder::parentItemVmChanged(ViewModel *viewModel)
{
// set vm without clearing the connection
_clearParentVmCon = false;
setParentViewModel(viewModel);
_clearParentVmCon = true;
}
void QQmlViewPlaceholder::presentIfReady()
{
if(_isReady &&
_autoPresent &&
!_loadedView &&
_parentViewModel &&
!_viewModelType.isEmpty())
presentView();
}
void QQmlViewPlaceholder::resizeView()
{
if(_loadedView && _autoResizeView) {
_loadedView->setWidth(width());
_loadedView->setHeight(height());
}
}
void QQmlViewPlaceholder::getParentViewModel()
{
auto pItem = parentItem();
if(!pItem)
return;
QQmlProperty vmProp{pItem, QStringLiteral("viewModel")};
if(!vmProp.isValid())
return;
// set the vm from the property
auto vm = vmProp.read().value<ViewModel*>();
if(vm)
setParentViewModel(vm); // no warning here - might be ok for lazy presented vms
// connect to further changes, via helper slot
auto cSlotIndex = metaObject()->indexOfSlot("parentItemVmChanged(QtMvvm::ViewModel*)");
Q_ASSERT(cSlotIndex != -1);
if(_parentVmCon)
disconnect(_parentVmCon);
_parentVmCon = connect(pItem, vmProp.property().notifySignal(),
this, metaObject()->method(cSlotIndex));
}

77
src/imports/mvvmquick/qqmlviewplaceholder.h

@ -0,0 +1,77 @@
#ifndef QTMVVM_QQMLVIEWPLACEHOLDER_H
#define QTMVVM_QQMLVIEWPLACEHOLDER_H
#include <QtQuick/QQuickItem>
#include <QtMvvmCore/ViewModel>
namespace QtMvvm {
class QQmlViewPlaceholder : public QQuickItem
{
Q_OBJECT
Q_PROPERTY(QString viewModelType MEMBER _viewModelType NOTIFY viewModelTypeChanged)
Q_PROPERTY(QVariantHash showParams MEMBER _showParams NOTIFY showParamsChanged)
Q_PROPERTY(QtMvvm::ViewModel* parentViewModel MEMBER _parentViewModel WRITE setParentViewModel NOTIFY parentViewModelChanged)
Q_PROPERTY(bool autoPresent MEMBER _autoPresent NOTIFY autoPresentChanged)
Q_PROPERTY(bool autoResizeView MEMBER _autoResizeView NOTIFY autoResizeViewChanged)
Q_PROPERTY(bool replaceViews MEMBER _replaceViews NOTIFY replaceViewsChanged)
Q_PROPERTY(bool closeViewOnAction MEMBER _closeViewOnAction NOTIFY closeViewOnActionChanged)
Q_PROPERTY(QQuickItem* loadedView READ loadedView NOTIFY loadedViewChanged)
public:
explicit QQmlViewPlaceholder(QQuickItem *parent = nullptr);
Q_INVOKABLE bool presentItem(const QVariant &item);
Q_INVOKABLE bool closeAction();
void setParentViewModel(ViewModel *parentViewModel);
QQuickItem* loadedView() const;
void componentComplete() override;
public Q_SLOTS:
void presentView();
void discardView();
Q_SIGNALS:
void viewModelTypeChanged(const QString &viewModelType);
void showParamsChanged(const QVariantHash &showParams);
void parentViewModelChanged(QtMvvm::ViewModel* parentViewModel);
void autoPresentChanged(bool autoPresent);
void autoResizeViewChanged(bool autoResizeView);
void replaceViewsChanged(bool replaceViews);
void closeViewOnActionChanged(bool closeViewOnAction);
void loadedViewChanged(QQuickItem* loadedView);
private Q_SLOTS:
void parentItemVmChanged(QtMvvm::ViewModel *viewModel);
void presentIfReady();
void resizeView();
private:
QString _viewModelType;
QVariantHash _showParams;
ViewModel *_parentViewModel = nullptr;
bool _autoPresent = true;
bool _autoResizeView = true;
bool _replaceViews = false;
bool _closeViewOnAction = false;
QPointer<QQuickItem> _loadedView = nullptr;
bool _isReady = false;
bool _clearParentVmCon = true;
QMetaObject::Connection _parentVmCon;
void getParentViewModel();
void connectSizeChanges();
void disconnectSizeChanges();
};
}
#endif // QTMVVM_QQMLVIEWPLACEHOLDER_H

3
src/imports/mvvmquick/qtmvvmquick_plugin.cpp

@ -6,6 +6,7 @@
#include "qqmlquickpresenter.h"
#include "settingsuibuilder.h"
#include "qqmlviewplaceholder.h"
#ifdef Q_OS_ANDROID
#include "androidfilechooser.h"
#endif
@ -49,7 +50,7 @@ void QtMvvmQuickDeclarativeModule::registerTypes(const char *uri)
#endif
//Version 1.1
qmlRegisterModule(uri, 1, 1);
qmlRegisterType<QtMvvm::QQmlViewPlaceholder>(uri, 1, 1, "ViewPlaceholder");
// Check to make shure no module update is forgotten
static_assert(VERSION_MAJOR == 1 && VERSION_MINOR == 1, "QML module version needs to be updated");

32
src/mvvmcore/coreapp.cpp

@ -35,11 +35,12 @@ void CoreApp::registerApp()
qRegisterMetaType<const QMetaObject*>("const QMetaObject*");
qRegisterMetaType<MessageConfig::StandardButton>();
qRegisterMetaType<QtMvvm::SettingsElements::Entry>();
qRegisterMetaType<QtMvvm::SettingsElements::Group>();
qRegisterMetaType<QtMvvm::SettingsElements::Section>();
qRegisterMetaType<QtMvvm::SettingsElements::Category>();
qRegisterMetaType<QtMvvm::SettingsElements::Setup>();
qRegisterMetaType<ViewModel*>();
qRegisterMetaType<SettingsElements::Entry>();
qRegisterMetaType<SettingsElements::Group>();
qRegisterMetaType<SettingsElements::Section>();
qRegisterMetaType<SettingsElements::Category>();
qRegisterMetaType<SettingsElements::Setup>();
registerInterfaceConverter<IPresenter>();
@ -56,6 +57,11 @@ void CoreApp::registerApp()
}
void CoreApp::show(const char *viewModelName, const QVariantHash &params)
{
show(viewModelName, params, nullptr);
}
void CoreApp::show(const char *viewModelName, const QVariantHash &params, QPointer<ViewModel> parentViewModel)
{
auto metaId = QMetaType::type(viewModelName);
auto metaObject = QMetaType::metaObjectForType(metaId);
@ -64,17 +70,22 @@ void CoreApp::show(const char *viewModelName, const QVariantHash &params)
viewModelName +
QByteArrayLiteral(") does not name a type with meta data"));
}
show(metaObject, params);
show(metaObject, params, std::move(parentViewModel));
}
void CoreApp::show(const QMetaObject *viewModelMetaObject, const QVariantHash &params)
{
show(viewModelMetaObject, params, nullptr);
}
void CoreApp::show(const QMetaObject *viewModelMetaObject, const QVariantHash &params, QPointer<ViewModel> parentViewModel)
{
if(!viewModelMetaObject->inherits(&ViewModel::staticMetaObject)) {
throw PresenterException(QByteArrayLiteral("Given type (") +
viewModelMetaObject->className() +
QByteArrayLiteral(") is not a class that extends QtMvvm::ViewModel"));
}
showImp(viewModelMetaObject, params);
showImp(viewModelMetaObject, params, std::move(parentViewModel));
}
MessageResult *CoreApp::showDialog(const MessageConfig &config)
@ -152,11 +163,16 @@ bool CoreApp::autoParse(QCommandLineParser &parser, const QStringList &arguments
}
void CoreApp::showImp(const QMetaObject *metaObject, const QVariantHash &params)
{
showImp(metaObject, params, nullptr);
}
void CoreApp::showImp(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parentViewModel)
{
QMetaObject::invokeMethod(CoreAppPrivate::dInstance().data(), "showViewModel", Qt::QueuedConnection,
Q_ARG(const QMetaObject*, metaObject),
Q_ARG(QVariantHash, params),
Q_ARG(QPointer<ViewModel>, nullptr),
Q_ARG(QPointer<ViewModel>, std::move(parentViewModel)),
Q_ARG(quint32, 0));
}

15
src/mvvmcore/coreapp.h

@ -36,11 +36,13 @@ public:
//! Show a new ViewModel by its type
template <typename TViewModel>
static inline void show(const QVariantHash &params = {});
static inline void show(const QVariantHash &params = {}, QPointer<ViewModel> parentViewModel = nullptr);
//! Show a new ViewModel by its name
static void show(const char *viewModelName, const QVariantHash &params = {});
static void show(const char *viewModelName, const QVariantHash &params = {}); //MAJOR merge methods
static void show(const char *viewModelName, const QVariantHash &params, QPointer<ViewModel> parentViewModel);
//! Show a new ViewModel by its metaobject
static void show(const QMetaObject *viewModelMetaObject, const QVariantHash &params = {});
static void show(const QMetaObject *viewModelMetaObject, const QVariantHash &params = {}); //MAJOR merge methods
static void show(const QMetaObject *viewModelMetaObject, const QVariantHash &params, QPointer<ViewModel> parentViewModel);
//! Show a basic dialog
static MessageResult *showDialog(const MessageConfig &config);
@ -68,14 +70,15 @@ private:
friend class QtMvvm::CoreAppPrivate;
QScopedPointer<CoreAppPrivate> d;
static void showImp(const QMetaObject *metaObject, const QVariantHash &params);
Q_DECL_DEPRECATED static void showImp(const QMetaObject *metaObject, const QVariantHash &params);
static void showImp(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parentViewModel);
};
template<typename TViewModel>
inline void CoreApp::show(const QVariantHash &params)
inline void CoreApp::show(const QVariantHash &params, QPointer<ViewModel> parentViewModel)
{
static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
ViewModel::showImp(&TViewModel::staticMetaObject, params, nullptr);
showImp(&TViewModel::staticMetaObject, params, std::move(parentViewModel));
}
}

2
src/mvvmcore/viewmodel.h

@ -87,4 +87,6 @@ void ViewModel::showForResult(quint32 requestCode, QVariantHash params) const
}
Q_DECLARE_METATYPE(QtMvvm::ViewModel*)
#endif // QTMVVM_VIEWMODEL_H

Loading…
Cancel
Save