QtMvvm  1.1.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);
31  ~WidgetsPresenter() override;
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 
50  static InputWidgetFactory* getInputWidgetFactory();
51 
52  void present(ViewModel *viewModel, const QVariantHash &params, QPointer<ViewModel> parent) override;
53  void showDialog(const MessageConfig &config, MessageResult *result) override;
54 
56  InputWidgetFactory* inputWidgetFactory() const;
57 
58 public Q_SLOTS:
60  void setInputWidgetFactory(InputWidgetFactory* inputWidgetFactory);
61 
62 Q_SIGNALS:
64  void inputWidgetFactoryChanged(InputWidgetFactory* inputWidgetFactory, QPrivateSignal);
65 
66 protected:
68  virtual const QMetaObject *findWidgetMetaObject(const QMetaObject *viewModelMetaObject);
70  virtual bool tryPresent(QWidget *view, QWidget *parentView);
71 
73  virtual void showForeground(QWidget *view) const;
74 
76  virtual void presentMessageBox(const MessageConfig &config, QPointer<MessageResult> result);
78  virtual void presentInputDialog(const MessageConfig &config, QPointer<MessageResult> result);
80  virtual void presentFileDialog(const MessageConfig &config, QPointer<MessageResult> result);
82  void presentColorDialog(const MessageConfig &config, const QPointer<MessageResult> &result); //MAJOR make virtual
84  void presentProgressDialog(const MessageConfig &config, const QPointer<MessageResult> &result); //MAJOR make virtual
86  virtual void presentOtherDialog(const MessageConfig &config, QPointer<MessageResult> result);
87 
88 private:
89  QScopedPointer<WidgetsPresenterPrivate> d;
90 };
91 
92 // ------------- Generic Implementation -------------
93 
94 template<typename TPresenter>
95 void WidgetsPresenter::registerAsPresenter()
96 {
97  static_assert(std::is_base_of<WidgetsPresenter, TPresenter>::value, "TPresenter must inherit QtMvvm::WidgetsPresenter!");
99 }
100 
101 template<typename TView>
103 {
104  static_assert(std::is_base_of<QWidget, TView>::value, "TWidget must inherit QWidget!");
105  if(false) { //compile time check for the constructor
106  Q_UNREACHABLE();
107  Q_UNUSED(new TView(static_cast<ViewModel*>(nullptr), static_cast<QWidget*>(nullptr)))
108  }
109  registerView(&TView::staticMetaObject);
110 }
111 
112 template<typename TViewModel, typename TView>
114 {
115  static_assert(std::is_base_of<QWidget, TView>::value, "TWidget must inherit QWidget!");
116  static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must inherit ViewModel!");
117  if(false) { //compile time check for the constructor
118  Q_UNREACHABLE();
119  Q_UNUSED(new TView(static_cast<ViewModel*>(nullptr), static_cast<QWidget*>(nullptr)))
120  }
121  registerViewExplicitly(&TViewModel::staticMetaObject, &TView::staticMetaObject);
122 }
123 
124 }
125 
126 #endif // QTMVVM_WIDGETSPRESENTER_H
A configuration for a simple dialog to be shown from the core code.
Definition: message.h:20
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:21
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:204
A factory class to generate input edit widgets by their type names.
The primary namespace of the QtMvvm library.
void registerInterface(DestructionScope scope=DestroyOnAppDestroy, 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.