QtMvvm  1.0.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
viewmodel.h
1 #ifndef QTMVVM_VIEWMODEL_H
2 #define QTMVVM_VIEWMODEL_H
3 
4 #include <type_traits>
5 
6 #include <QtCore/qobject.h>
7 #include <QtCore/qscopedpointer.h>
8 #include <QtCore/qvariant.h>
9 #include <QtCore/qpointer.h>
10 
11 #include "QtMvvmCore/qtmvvmcore_global.h"
12 #include "QtMvvmCore/injection.h"
13 
14 namespace QtMvvm {
15 
16 class CoreApp;
17 
18 class ViewModelPrivate;
20 class Q_MVVMCORE_EXPORT ViewModel : public QObject
21 {
22  Q_OBJECT
23 
24 public:
26  explicit ViewModel(QObject *parent = nullptr);
27  ~ViewModel();
28 
29 public Q_SLOTS:
31  virtual void onInit(const QVariantHash &params);
33  virtual void onResult(quint32 requestCode, const QVariant &result);
34 
35 Q_SIGNALS:
37  void resultReady(const QVariant &result);
38 
39 protected:
41  template <typename TViewModel>
42  inline void show(const QVariantHash &params = {}) const;
44  void show(const char *viewModelName, const QVariantHash &params = {}) const;
46  void show(const QMetaObject *viewMetaObject, const QVariantHash &params = {}) const;
48  template <typename TViewModel>
49  inline void showForResult(quint32 requestCode, const QVariantHash &params = {}) const;
51  void showForResult(quint32 requestCode, const char *viewModelName, const QVariantHash &params = {}) const;
53  void showForResult(quint32 requestCode, const QMetaObject *viewMetaObject, const QVariantHash &params = {}) const;
54 
55 private:
56  friend class QtMvvm::CoreApp;
57 
59 
60  static void showImp(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parent, quint32 requestCode = 0);
61 };
62 
63 // ------------- Generic Implementation -------------
64 
65 template<typename TViewModel>
66 inline void ViewModel::show(const QVariantHash &params) const
67 {
68  static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
69  showImp(&TViewModel::staticMetaObject, params, const_cast<ViewModel*>(this));
70 }
71 
72 template<typename TViewModel>
73 void ViewModel::showForResult(quint32 requestCode, const QVariantHash &params) const
74 {
75  static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
76  showImp(&TViewModel::staticMetaObject, params, const_cast<ViewModel*>(this), requestCode);
77 }
78 
79 }
80 
81 #endif // QTMVVM_VIEWMODEL_H
virtual const QMetaObject * metaObject() const
A logicaly application object to drive the mvvm application from the core part.
Definition: coreapp.h:20
The base class for all viewmodels.
Definition: viewmodel.h:20
void showForResult(quint32 requestCode, const QVariantHash &params={}) const
Show another viewmodel as a child of this one and expect its result.
Definition: viewmodel.h:73
void show(const QVariantHash &params={}) const
Show another viewmodel as a child of this one.
Definition: viewmodel.h:66
The primary namespace of the QtMvvm library.
Definition: binding.h:10
QObject * parent() const