QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
viewmodel.h
Go to the documentation of this file.
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 class CoreAppPrivate;
18 
19 class ViewModelPrivate;
21 class Q_MVVMCORE_EXPORT ViewModel : public QObject
22 {
23  Q_OBJECT
24 
25 public:
27  explicit ViewModel(QObject *parent = nullptr);
28  ~ViewModel() override;
29 
30 public Q_SLOTS:
32  virtual void onInit(const QVariantHash &params);
34  virtual void onResult(quint32 requestCode, const QVariant &result);
35 
36 Q_SIGNALS:
38  void resultReady(const QVariant &result);
39 
41  QTMVVM_REVISION_1 void instanceInvoked(const QVariantHash &params, QPrivateSignal);
42 
43 protected:
45  template <typename TViewModel>
46  inline void show(const QVariantHash &params = {}) const;
48  void show(const char *viewModelName, const QVariantHash &params = {}) const;
50  void show(const QMetaObject *viewMetaObject, const QVariantHash &params = {}) const;
52  template <typename TViewModel>
53  inline void showForResult(quint32 requestCode, const QVariantHash &params = {}) const;
55  void showForResult(quint32 requestCode, const char *viewModelName, const QVariantHash &params = {}) const;
57  void showForResult(quint32 requestCode, const QMetaObject *viewMetaObject, const QVariantHash &params = {}) const;
58 
59 private:
60  friend class QtMvvm::CoreApp;
61  friend class QtMvvm::CoreAppPrivate;
62 
64 
65  static void showImp(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parent, quint32 requestCode = 0);
66 };
67 
69 #define QTMVVM_SINGLETON Q_CLASSINFO("qtmvvm_singleton", "true")
70 
72 #define QTMVVM_CONTAINER_VM(x) Q_CLASSINFO("qtmvvm_container_viewmodel", #x)
73 
74 // ------------- Generic Implementation -------------
75 
76 template<typename TViewModel>
77 inline void ViewModel::show(const QVariantHash &params) const
78 {
79  static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
80  showImp(&TViewModel::staticMetaObject, params, const_cast<ViewModel*>(this));
81 }
82 
83 template<typename TViewModel>
84 void ViewModel::showForResult(quint32 requestCode, const QVariantHash &params) const
85 {
86  static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
87  showImp(&TViewModel::staticMetaObject, params, const_cast<ViewModel*>(this), requestCode);
88 }
89 
90 }
91 
92 Q_DECLARE_METATYPE(QtMvvm::ViewModel*)
93 
94 #endif // QTMVVM_VIEWMODEL_H
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:21
void showForResult(quint32 requestCode, const QVariantHash &params={}) const
Show another viewmodel as a child of this one and expect its result.
Definition: viewmodel.h:84
void show(const QVariantHash &params={}) const
Show another viewmodel as a child of this one.
Definition: viewmodel.h:77
The primary namespace of the QtMvvm library.