Browse Source

added show for result sample

added few more methods, try mingw fix by skipping translations
pull/2/head
Skycoder42 7 years ago
parent
commit
5664c9e982
  1. 6
      examples/mvvmcore/SampleCore/SampleCore.pro
  2. 30
      examples/mvvmcore/SampleCore/resultviewmodel.cpp
  3. 32
      examples/mvvmcore/SampleCore/resultviewmodel.h
  4. 14
      examples/mvvmcore/SampleCore/sampleviewmodel.cpp
  5. 5
      examples/mvvmcore/SampleCore/sampleviewmodel.h
  6. 9
      examples/mvvmwidgets/SampleWidgets/SampleWidgets.pro
  7. 2
      examples/mvvmwidgets/SampleWidgets/main.cpp
  8. 24
      examples/mvvmwidgets/SampleWidgets/resultdialog.cpp
  9. 24
      examples/mvvmwidgets/SampleWidgets/resultdialog.h
  10. 74
      examples/mvvmwidgets/SampleWidgets/resultdialog.ui
  11. 2
      examples/mvvmwidgets/SampleWidgets/sampleview.cpp
  12. 7
      examples/mvvmwidgets/SampleWidgets/sampleview.ui
  13. 59
      src/mvvmcore/coreapp.cpp
  14. 3
      src/mvvmcore/coreapp.h
  15. 14
      src/mvvmcore/coreapp_p.h
  16. 11
      src/mvvmcore/mvvmcore.pro
  17. 44
      src/mvvmcore/viewmodel.cpp
  18. 6
      src/mvvmcore/viewmodel.h
  19. 8
      src/mvvmquick/mvvmquick.pro
  20. 2
      src/mvvmwidgets/mvvmwidgets.pro

6
examples/mvvmcore/SampleCore/SampleCore.pro

@ -9,12 +9,14 @@ HEADERS += \
samplecoreapp.h \
sampleviewmodel.h \
ieventservice.h \
echoservice.h
echoservice.h \
resultviewmodel.h
SOURCES += \
samplecoreapp.cpp \
sampleviewmodel.cpp \
echoservice.cpp
echoservice.cpp \
resultviewmodel.cpp
target.path = $$[QT_INSTALL_EXAMPLES]/mvvmcore/$$TARGET
INSTALLS += target

30
examples/mvvmcore/SampleCore/resultviewmodel.cpp

@ -0,0 +1,30 @@
#include "resultviewmodel.h"
ResultViewModel::ResultViewModel(QObject *parent) :
ViewModel(parent),
_result()
{}
QString ResultViewModel::result() const
{
return _result;
}
void ResultViewModel::done()
{
emit resultReady(_result);
}
void ResultViewModel::setResult(QString result)
{
if (_result == result)
return;
_result = result;
emit resultChanged(_result);
}
void ResultViewModel::onInit(const QVariantHash &params)
{
setResult(params.value(QStringLiteral("default")).toString());
}

32
examples/mvvmcore/SampleCore/resultviewmodel.h

@ -0,0 +1,32 @@
#ifndef RESULTVIEWMODEL_H
#define RESULTVIEWMODEL_H
#include <QtMvvmCore/ViewModel>
class ResultViewModel : public QtMvvm::ViewModel
{
Q_OBJECT
Q_PROPERTY(QString result READ result WRITE setResult NOTIFY resultChanged)
public:
Q_INVOKABLE explicit ResultViewModel(QObject *parent = nullptr);
QString result() const;
public Q_SLOTS:
void done();
void setResult(QString result);
Q_SIGNALS:
void resultChanged(QString result);
protected:
void onInit(const QVariantHash &params) override;
private:
QString _result;
};
#endif // RESULTVIEWMODEL_H

14
examples/mvvmcore/SampleCore/sampleviewmodel.cpp

@ -1,5 +1,6 @@
#include "sampleviewmodel.h"
#include <QDebug>
#include "resultviewmodel.h"
const QString SampleViewModel::KeyActive = QStringLiteral("active");
const QString SampleViewModel::KeyNames = QStringLiteral("names");
@ -62,6 +63,13 @@ void SampleViewModel::setActive(bool active)
emit activeChanged(_active);
}
void SampleViewModel::getResult()
{
showForResult<ResultViewModel>(ResCode, {
{QStringLiteral("default"), _name}
});
}
void SampleViewModel::clearEvents()
{
_eventsModel->setStringList({});
@ -80,6 +88,12 @@ void SampleViewModel::onInit(const QVariantHash &params)
setActive(params.value(KeyActive, false).toBool());
}
void SampleViewModel::onResult(quint32 requestCode, const QVariant &result)
{
if(requestCode == ResCode && result.isValid())
addEvent(result.toString());
}
void SampleViewModel::addEvent(const QString &event)
{
qDebug() << event;

5
examples/mvvmcore/SampleCore/sampleviewmodel.h

@ -31,6 +31,8 @@ public:
public Q_SLOTS:
void setName(QString name);
void setActive(bool active);
void getResult();
void clearEvents();
Q_SIGNALS:
@ -39,11 +41,14 @@ Q_SIGNALS:
protected:
void onInit(const QVariantHash &params) override;
void onResult(quint32 requestCode, const QVariant &result) override;
private Q_SLOTS:
void addEvent(const QString &event);
private:
static const quint32 ResCode = 42u;
QString _name;
bool _active;
QStringListModel *_eventsModel;

9
examples/mvvmwidgets/SampleWidgets/SampleWidgets.pro

@ -6,15 +6,18 @@ TARGET = SampleWidgets
HEADERS += \
widgetseventservice.h \
sampleview.h
sampleview.h \
resultdialog.h
SOURCES += \
main.cpp \
widgetseventservice.cpp \
sampleview.cpp
sampleview.cpp \
resultdialog.cpp
FORMS += \
sampleview.ui
sampleview.ui \
resultdialog.ui
target.path = $$[QT_INSTALL_EXAMPLES]/mvvmwidgets/$$TARGET
INSTALLS += target

2
examples/mvvmwidgets/SampleWidgets/main.cpp

@ -5,6 +5,7 @@
#include "widgetseventservice.h"
#include "sampleview.h"
#include "resultdialog.h"
#define TEST_DIRECT 0
#define TEST_FN 1
@ -19,6 +20,7 @@ int main(int argc, char *argv[])
QtMvvm::WidgetsPresenter::registerAsPresenter();
QtMvvm::WidgetsPresenter::registerView<SampleView>();
QtMvvm::WidgetsPresenter::registerView<ResultDialog>();
if(TEST_CURRENT == TEST_DIRECT)
QtMvvm::ServiceRegistry::instance()->registerObject<EchoService>();

24
examples/mvvmwidgets/SampleWidgets/resultdialog.cpp

@ -0,0 +1,24 @@
#include "resultdialog.h"
#include "ui_resultdialog.h"
#include <QtMvvmCore/Binding>
ResultDialog::ResultDialog(QtMvvm::ViewModel *viewModel, QWidget *parent) :
QDialog(parent),
_viewModel(static_cast<ResultViewModel*>(viewModel)),
ui(new Ui::ResultDialog)
{
ui->setupUi(this);
QtMvvm::bind(_viewModel, "result",
ui->lineEdit, "text",
QtMvvm::Binding::TwoWay,
nullptr,
"editingFinished()");
connect(this, &ResultDialog::accepted,
_viewModel, &ResultViewModel::done);
}
ResultDialog::~ResultDialog()
{
delete ui;
}

24
examples/mvvmwidgets/SampleWidgets/resultdialog.h

@ -0,0 +1,24 @@
#ifndef RESULTDIALOG_H
#define RESULTDIALOG_H
#include <QDialog>
#include <resultviewmodel.h>
namespace Ui {
class ResultDialog;
}
class ResultDialog : public QDialog
{
Q_OBJECT
public:
Q_INVOKABLE explicit ResultDialog(QtMvvm::ViewModel *viewModel, QWidget *parent = nullptr);
~ResultDialog();
private:
ResultViewModel *_viewModel;
Ui::ResultDialog *ui;
};
#endif // RESULTDIALOG_H

74
examples/mvvmwidgets/SampleWidgets/resultdialog.ui

@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ResultDialog</class>
<widget class="QDialog" name="ResultDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>354</width>
<height>100</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>Enter a result to be reported as event to the main view:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineEdit"/>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ResultDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ResultDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

2
examples/mvvmwidgets/SampleWidgets/sampleview.cpp

@ -20,6 +20,8 @@ SampleView::SampleView(QtMvvm::ViewModel *viewModel, QWidget *parent) :
ui->eventsListView->setModel(_viewModel->eventsModel());
connect(ui->clearButton, &QPushButton::clicked,
_viewModel, &SampleViewModel::clearEvents);
connect(ui->resultButton, &QPushButton::clicked,
_viewModel, &SampleViewModel::getResult);
}
SampleView::~SampleView()

7
examples/mvvmwidgets/SampleWidgets/sampleview.ui

@ -63,6 +63,13 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="resultButton">
<property name="text">
<string>Get &amp;Result</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">

59
src/mvvmcore/coreapp.cpp

@ -38,6 +38,9 @@ void CoreApp::disableAutoBoot()
void CoreApp::registerApp()
{
//register metatypes
qRegisterMetaType<const QMetaObject*>("const QMetaObject*");
//setup
setParent(qApp);
CoreAppPrivate::instance = this;
performRegistrations();
@ -45,6 +48,11 @@ void CoreApp::registerApp()
QMetaObject::invokeMethod(this, "bootApp", Qt::QueuedConnection);
}
IPresenter *CoreApp::presenter() const
{
return d->presenter.data();
}
void CoreApp::bootApp()
{
if(!d->presenter)
@ -81,6 +89,28 @@ bool CoreApp::autoParse(QCommandLineParser &parser, const QStringList &arguments
}
}
void CoreApp::show(const char *viewModelName, const QVariantHash &params) const
{
auto metaId = QMetaType::type(viewModelName);
auto metaObject = QMetaType::metaObjectForType(metaId);
if(!metaObject) {
throw PresenterException(QByteArrayLiteral("Given name (") +
viewModelName +
QByteArrayLiteral(") does not name a type with meta data"));
}
show(metaObject, params);
}
void CoreApp::show(const QMetaObject *viewMetaObject, const QVariantHash &params) const
{
if(!viewMetaObject->inherits(&ViewModel::staticMetaObject)) {
throw PresenterException(QByteArrayLiteral("Given type (") +
viewMetaObject->className() +
QByteArrayLiteral(") is not a class that extends QtMvvm::ViewModel"));
}
ViewModel::showImp(viewMetaObject, params, nullptr);
}
// ------------- Private Implementation -------------
bool CoreAppPrivate::bootEnabled = true;
@ -96,6 +126,20 @@ QScopedPointer<CoreAppPrivate> &CoreAppPrivate::dInstance()
}
void CoreAppPrivate::showViewModel(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parent, quint32 requestCode)
{
QMetaObject::invokeMethod(this, "showViewModelPrivate", Qt::QueuedConnection,
Q_ARG(const QMetaObject*, metaObject),
Q_ARG(const QVariantHash&, params),
Q_ARG(QPointer<ViewModel>, parent),
Q_ARG(quint32, requestCode));
}
IPresenter *CoreAppPrivate::currentPresenter() const
{
return presenter.data();
}
void CoreAppPrivate::showViewModelPrivate(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parent, quint32 requestCode)
{
if(presenter) {
QPointer<ViewModel> vm;
@ -106,9 +150,13 @@ void CoreAppPrivate::showViewModel(const QMetaObject *metaObject, const QVariant
throw ServiceConstructionException("Invalid types - not at QtMvvm::ViewModel");
presenter->present(vm, params, parent);
if(requestCode != 0) {
QObject::connect(vm, &ViewModel::resultReady,
parent, &ViewModel::onResult,
Qt::UniqueConnection);
QObject::connect(vm, &ViewModel::resultReady, parent, [vm, requestCode, parent](const QVariant &r){
vm->disconnect(parent);
parent->onResult(requestCode, r);
});
QObject::connect(vm, &ViewModel::destroyed, parent, [vm, requestCode, parent](){
parent->onResult(requestCode, QVariant());
});
}
} catch(QException &e) {
logCritical() << "Failed to present viewmodel of type"
@ -124,8 +172,3 @@ void CoreAppPrivate::showViewModel(const QMetaObject *metaObject, const QVariant
<< "- no presenter was set";
}
}
IPresenter *CoreAppPrivate::currentPresenter() const
{
return presenter.data();
}

3
src/mvvmcore/coreapp.h

@ -26,6 +26,7 @@ public:
static void disableAutoBoot();
void registerApp();
IPresenter *presenter() const;
public Q_SLOTS:
void bootApp();
@ -38,6 +39,8 @@ protected:
bool autoParse(QCommandLineParser &parser, const QStringList &arguments);
template <typename TViewModel>
inline void show(const QVariantHash &params = {}) const;
void show(const char *viewModelName, const QVariantHash &params = {}) const;
void show(const QMetaObject *viewMetaObject, const QVariantHash &params = {}) const;
private:
friend class QtMvvm::CoreAppPrivate;

14
src/mvvmcore/coreapp_p.h

@ -8,16 +8,26 @@
namespace QtMvvm {
class Q_MVVMCORE_EXPORT CoreAppPrivate
class Q_MVVMCORE_EXPORT CoreAppPrivate : public QObject
{
Q_OBJECT
friend class QtMvvm::CoreApp;
public:
static QScopedPointer<CoreAppPrivate> &dInstance();
void showViewModel(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parent, quint32 requestCode);
void showViewModel(const QMetaObject *metaObject,
const QVariantHash &params,
QPointer<ViewModel> parent,
quint32 requestCode);
IPresenter *currentPresenter() const;
private Q_SLOTS:
void showViewModelPrivate(const QMetaObject *metaObject,
const QVariantHash &params,
QPointer<ViewModel> parent,
quint32 requestCode);
private:
CoreAppPrivate();

11
src/mvvmcore/mvvmcore.pro

@ -15,8 +15,8 @@ HEADERS += \
qtmvvm_logging_p.h \
binding.h \
binding_p.h \
message.h \
message_p.h
message.h \
message_p.h
SOURCES += \
viewmodel.cpp \
@ -24,8 +24,8 @@ SOURCES += \
serviceregistry.cpp \
qtmvvmcore_global.cpp \
binding.cpp \
message.cpp \
ipresenter.cpp
message.cpp \
ipresenter.cpp
TRANSLATIONS += \
translations/qtmvvmcore_de.ts \
@ -35,7 +35,7 @@ DISTFILES += $$TRANSLATIONS
qpmx_ts_target.path = $$[QT_INSTALL_TRANSLATIONS]
qpmx_ts_target.depends += lrelease
INSTALLS += qpmx_ts_target
!mingw: INSTALLS += qpmx_ts_target
load(qt_module)
@ -52,3 +52,4 @@ else: include($$OUT_PWD/qpmx_generated.pri)
qpmx_ts_target.files -= $$OUT_PWD/$$QPMX_WORKINGDIR/qtmvvmcore_template.qm
qpmx_ts_target.files += translations/qtmvvmcore_template.ts

44
src/mvvmcore/viewmodel.cpp

@ -20,6 +20,50 @@ void ViewModel::onResult(quint32 requestCode, const QVariant &result)
<< "with request code" << requestCode;
}
void ViewModel::show(const char *viewModelName, const QVariantHash &params) const
{
auto metaId = QMetaType::type(viewModelName);
auto metaObject = QMetaType::metaObjectForType(metaId);
if(!metaObject) {
throw PresenterException(QByteArrayLiteral("Given name (") +
viewModelName +
QByteArrayLiteral(") does not name a type with meta data"));
}
show(metaObject, params);
}
void ViewModel::show(const QMetaObject *viewMetaObject, const QVariantHash &params) const
{
if(!viewMetaObject->inherits(&ViewModel::staticMetaObject)) {
throw PresenterException(QByteArrayLiteral("Given type (") +
viewMetaObject->className() +
QByteArrayLiteral(") is not a class that extends QtMvvm::ViewModel"));
}
showImp(viewMetaObject, params, const_cast<ViewModel*>(this));
}
void ViewModel::showForResult(quint32 requestCode, const char *viewModelName, const QVariantHash &params) const
{
auto metaId = QMetaType::type(viewModelName);
auto metaObject = QMetaType::metaObjectForType(metaId);
if(!metaObject) {
throw PresenterException(QByteArrayLiteral("Given name (") +
viewModelName +
QByteArrayLiteral(") does not name a type with meta data"));
}
showForResult(requestCode, metaObject, params);
}
void ViewModel::showForResult(quint32 requestCode, const QMetaObject *viewMetaObject, const QVariantHash &params) const
{
if(!viewMetaObject->inherits(&ViewModel::staticMetaObject)) {
throw PresenterException(QByteArrayLiteral("Given type (") +
viewMetaObject->className() +
QByteArrayLiteral(") is not a class that extends QtMvvm::ViewModel"));
}
showResultImp(requestCode, viewMetaObject, params);
}
void ViewModel::showImp(const QMetaObject *mo, const QVariantHash &params, QPointer<ViewModel> parent)
{
CoreAppPrivate::dInstance()->showViewModel(mo, params, parent, 0);

6
src/mvvmcore/viewmodel.h

@ -28,13 +28,17 @@ public Q_SLOTS:
virtual void onResult(quint32 requestCode, const QVariant &result);
Q_SIGNALS:
void resultReady(quint32 requestCode, const QVariant &result);
void resultReady(const QVariant &result);
protected:
template <typename TViewModel>
inline void show(const QVariantHash &params = {}) const;
void show(const char *viewModelName, const QVariantHash &params = {}) const;
void show(const QMetaObject *viewMetaObject, const QVariantHash &params = {}) const;
template <typename TViewModel>
inline void showForResult(quint32 requestCode, const QVariantHash &params = {}) const;
void showForResult(quint32 requestCode, const char *viewModelName, const QVariantHash &params = {}) const;
void showForResult(quint32 requestCode, const QMetaObject *viewMetaObject, const QVariantHash &params = {}) const;
private:
friend class QtMvvm::CoreApp;

8
src/mvvmquick/mvvmquick.pro

@ -3,11 +3,11 @@ TARGET = QtMvvmQuick
QT = core gui widgets mvvmcore
HEADERS += \
qtmvvmquick_global.h \
builddummy.h
qtmvvmquick_global.h \
builddummy.h
SOURCES += \
builddummy.cpp
builddummy.cpp
TRANSLATIONS += \
translations/qtmvvmquick_de.ts \
@ -17,7 +17,7 @@ DISTFILES += $$TRANSLATIONS
qpmx_ts_target.path = $$[QT_INSTALL_TRANSLATIONS]
qpmx_ts_target.depends += lrelease
INSTALLS += qpmx_ts_target
!mingw: INSTALLS += qpmx_ts_target
load(qt_module)

2
src/mvvmwidgets/mvvmwidgets.pro

@ -19,7 +19,7 @@ DISTFILES += $$TRANSLATIONS
qpmx_ts_target.path = $$[QT_INSTALL_TRANSLATIONS]
qpmx_ts_target.depends += lrelease
INSTALLS += qpmx_ts_target
!mingw: INSTALLS += qpmx_ts_target
load(qt_module)

Loading…
Cancel
Save