QtMvvm  1.0.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
widgetspresenter.h
1 #ifndef QTMVVM_WIDGETSPRESENTER_H
2 #define QTMVVM_WIDGETSPRESENTER_H
3 
4 #include <QtCore/qobject.h>
5 #include <QtCore/qscopedpointer.h>
6 
7 #include <QtMvvmCore/ipresenter.h>
8 #include <QtMvvmCore/serviceregistry.h>
9 
10 #include <QtWidgets/qwidget.h>
11 
12 #include "QtMvvmWidgets/qtmvvmwidgets_global.h"
13 #include "QtMvvmWidgets/inputwidgetfactory.h"
14 
15 namespace QtMvvm {
16 
17 class WidgetsPresenterPrivate;
19 class Q_MVVMWIDGETS_EXPORT WidgetsPresenter : public QObject, public IPresenter
20 {
21  Q_OBJECT
22  Q_INTERFACES(QtMvvm::IPresenter)
23 
24 
25  Q_PROPERTY(InputWidgetFactory* inputWidgetFactory READ inputWidgetFactory WRITE setInputWidgetFactory NOTIFY inputWidgetFactoryChanged)
26  QTMVVM_INJECT(InputWidgetFactory*, inputWidgetFactory)
27 
28 public:
30  Q_INVOKABLE explicit WidgetsPresenter(QObject *parent = nullptr);
32 
34  template <typename TPresenter>
35  static void registerAsPresenter();
36 
38  template <typename TView>
39  static void registerView();
41  static void registerView(const QMetaObject *viewType);
42 
44  template <typename TViewModel, typename TView>
45  static void registerViewExplicitly();
47  static void registerViewExplicitly(const QMetaObject *viewModelType, const QMetaObject *viewType);
48 
49  void present(ViewModel *viewModel, const QVariantHash &params, QPointer<ViewModel> parent) override;
50  void showDialog(const MessageConfig &config, MessageResult *result) override;
51 
53  InputWidgetFactory* inputWidgetFactory() const;
54 
55 public Q_SLOTS:
57  void setInputWidgetFactory(InputWidgetFactory* inputWidgetFactory);
58 
59 Q_SIGNALS:
61  void inputWidgetFactoryChanged(InputWidgetFactory* inputWidgetFactory, QPrivateSignal);
62 
63 protected:
65  virtual const QMetaObject *findWidgetMetaObject(const QMetaObject *viewModelMetaObject);
67  virtual bool tryPresent(QWidget *view, QWidget *parentView);
68 
70  virtual void showForeground(QWidget *view) const;
71 
73  virtual void presentMessageBox(const MessageConfig &config, QPointer<MessageResult> result);
75  virtual void presentInputDialog(const MessageConfig &config, QPointer<MessageResult> result);
77  virtual void presentFileDialog(const MessageConfig &config, QPointer<MessageResult> result);
79  virtual void presentOtherDialog(const MessageConfig &config, QPointer<MessageResult> result);
80 
81 private:
82  QScopedPointer<WidgetsPresenterPrivate> d;
83 };
84 
85 // ------------- Generic Implementation -------------
86 
87 template<typename TPresenter>
88 void WidgetsPresenter::registerAsPresenter()
89 {
90  static_assert(std::is_base_of<WidgetsPresenter, TPresenter>::value, "TPresenter must inherit QtMvvm::WidgetsPresenter!");
92 }
93 
94 template<typename TView>
96 {
97  static_assert(std::is_base_of<QWidget, TView>::value, "TWidget must inherit QWidget!");
98  if(false) { //compile time check for the constructor
99  Q_UNREACHABLE();
100  Q_UNUSED(new TView(static_cast<ViewModel*>(nullptr), static_cast<QWidget*>(nullptr)))
101  }
102  registerView(&TView::staticMetaObject);
103 }
104 
105 template<typename TViewModel, typename TView>
107 {
108  static_assert(std::is_base_of<QWidget, TView>::value, "TWidget must inherit QWidget!");
109  static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must inherit ViewModel!");
110  if(false) { //compile time check for the constructor
111  Q_UNREACHABLE();
112  Q_UNUSED(new TView(static_cast<ViewModel*>(nullptr), static_cast<QWidget*>(nullptr)))
113  }
114  registerViewExplicitly(&TViewModel::staticMetaObject, &TView::staticMetaObject);
115 }
116 
117 }
118 
119 #endif // QTMVVM_WIDGETSPRESENTER_H
A configuration for a simple dialog to be shown from the core code.
Definition: message.h:18
The IPresenter implementation for the widgets module.
The interface for a GUI view presenter.
Definition: ipresenter.h:36
#define QTMVVM_INJECT(classType, name)
Mark a property for injection.
The base class for all viewmodels.
Definition: viewmodel.h:20
static void registerView()
Register a view to be found by the presenter.
A result watcher to get the result once a dialog has finished.
Definition: message.h:172
A factory class to generate input edit widgets by their type names.
The primary namespace of the QtMvvm library.
Definition: binding.h:10
void registerInterface(bool weak=false)
Register a service for its interface via the type.
static void registerViewExplicitly()
Register a view for a viewmodel to be found by the presenter.
static ServiceRegistry * instance()
Returns the ServiceRegistry singleton instance.