QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
coreapp.h
Go to the documentation of this file.
1 #ifndef QTMVVM_COREAPP_H
2 #define QTMVVM_COREAPP_H
3 
4 #include <type_traits>
5 
6 #include <QtCore/qobject.h>
7 #include <QtCore/qscopedpointer.h>
8 #include <QtCore/qcoreapplication.h>
10 
11 #include "QtMvvmCore/qtmvvmcore_global.h"
12 #include "QtMvvmCore/viewmodel.h"
13 #include "QtMvvmCore/ipresenter.h"
14 #include "QtMvvmCore/message.h"
15 
16 namespace QtMvvm {
17 
18 class CoreAppPrivate;
20 class Q_MVVMCORE_EXPORT CoreApp : public QObject
21 {
22  Q_OBJECT
23 
24 public:
26  explicit CoreApp(QObject *parent = nullptr);
27  ~CoreApp() override;
28 
30  static CoreApp *instance();
32  static void disableAutoBoot();
33 
35  void registerApp();
36 
38  template <typename TViewModel>
39  static inline void show(const QVariantHash &params = {}, QPointer<ViewModel> parentViewModel = nullptr);
41  static void show(const char *viewModelName, const QVariantHash &params = {}); //MAJOR merge methods
43  static void show(const char *viewModelName, const QVariantHash &params, QPointer<ViewModel> parentViewModel);
45  static void show(const QMetaObject *viewModelMetaObject, const QVariantHash &params = {}); //MAJOR merge methods
47  static void show(const QMetaObject *viewModelMetaObject, const QVariantHash &params, QPointer<ViewModel> parentViewModel);
48 
50  static MessageResult *showDialog(const MessageConfig &config);
51 
53  static QVariant safeCastInputType(const QByteArray &type, const QVariant &value);
55  template <typename T>
56  static void registerInputTypeMapping(const QByteArray &type);
58  static void registerInputTypeMapping(const QByteArray &type, int targetType);
59 
61  static IPresenter *presenter();
62 
63 public Q_SLOTS:
65  void bootApp();
66 
67 Q_SIGNALS:
69  void appStarted(QPrivateSignal);
70 
71 protected:
73  virtual void performRegistrations();
75  virtual int startApp(const QStringList &arguments) = 0;
77  virtual void closeApp();
78 
80  bool autoParse(QCommandLineParser &parser, const QStringList &arguments);
81 
82 private:
83  friend class QtMvvm::CoreAppPrivate;
85 
86  Q_DECL_DEPRECATED static void showImp(const QMetaObject *metaObject, const QVariantHash &params);
87  static void showImp(const QMetaObject *metaObject, const QVariantHash &params, QPointer<ViewModel> parentViewModel);
88 };
89 
90 template<typename TViewModel>
91 inline void CoreApp::show(const QVariantHash &params, QPointer<ViewModel> parentViewModel)
92 {
93  static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
94  showImp(&TViewModel::staticMetaObject, params, std::move(parentViewModel));
95 }
96 
97 template<typename T>
99 {
100  registerInputTypeMapping(type, qMetaTypeId<T>());
101 }
102 
103 }
104 
106 #define QTMVVM_REGISTER_CORE_APP(T) namespace {\
107  void __setup_ ## T ## _hook() { \
108  static_assert(std::is_base_of<QtMvvm::CoreApp, T>::value, "QTMVVM_REGISTER_CORE_APP must be used with a class that extends QtMvvm::CoreApp"); \
109  auto app = new T(nullptr); \
110  app->registerApp(); \
111  } \
112  } \
113  Q_COREAPP_STARTUP_FUNCTION(__setup_ ## T ## _hook)
114 
116 #define coreApp QtMvvm::CoreApp::instance()
117 
119 #endif // QTMVVM_COREAPP_H
static void show(const QVariantHash &params={}, QPointer< ViewModel > parentViewModel=nullptr)
Show a new ViewModel by its type.
Definition: coreapp.h:91
A configuration for a simple dialog to be shown from the core code.
Definition: message.h:20
A logicaly application object to drive the mvvm application from the core part.
Definition: coreapp.h:20
The interface for a GUI view presenter.
Definition: ipresenter.h:36
static void registerInputTypeMapping(const QByteArray &type)
Register a type to be used as variant type for the given edit type.
Definition: coreapp.h:98
A result watcher to get the result once a dialog has finished.
Definition: message.h:204
The primary namespace of the QtMvvm library.