QtMvvm  1.1.0
A mvvm oriented library for Qt, to create Projects for Widgets and Quick in parallel
Public Slots | Signals | Public Member Functions | Static Public Member Functions | Protected Member Functions | Properties | List of all members
QtMvvm::QuickPresenter Class Reference

The IPresenter implementation for the quick module. More...

#include <quickpresenter.h>

+ Inheritance diagram for QtMvvm::QuickPresenter:

Public Slots

void setInputViewFactory (InputViewFactory *inputViewFactory)
 WRITE accessor for QuickPresenter::inputViewFactory.
 

Signals

void inputViewFactoryChanged (InputViewFactory *inputViewFactory, QPrivateSignal)
 NOTIFY accessor for QuickPresenter::inputViewFactory.
 

Public Member Functions

Q_INVOKABLE QuickPresenter (QObject *parent=nullptr)
 Invokable constructor.
 
void present (ViewModel *viewModel, const QVariantHash &params, QPointer< ViewModel > parent) override
 Called to present a viewmodel. More...
 
void showDialog (const MessageConfig &config, MessageResult *result) override
 Called to present a dialog via a message config. More...
 
virtual bool presentToQml (QObject *qmlPresenter, QObject *viewObject)
 Is called to present a view to presenter living in QML. More...
 
InputViewFactoryinputViewFactory () const
 READ accessor for QuickPresenter::inputViewFactory.
 
- Public Member Functions inherited from QObject
virtual const QMetaObjectmetaObject () const const
 
virtual void * qt_metacast (const char *)
 
virtual int qt_metacall (QMetaObject::Call, int, void **)
 
 QObject (QObject *parent)
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
QString objectName () const const
 
void setObjectName (const QString &name)
 
bool isWidgetType () const const
 
bool isWindowType () const const
 
bool signalsBlocked () const const
 
bool blockSignals (bool block)
 
QThreadthread () const const
 
void moveToThread (QThread *targetThread)
 
int startTimer (int interval, Qt::TimerType timerType)
 
int startTimer (std::chrono::milliseconds time, Qt::TimerType timerType)
 
void killTimer (int id)
 
findChild (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QString &name, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegExp &regExp, Qt::FindChildOptions options) const const
 
QList< T > findChildren (const QRegularExpression &re, Qt::FindChildOptions options) const const
 
const QObjectList & children () const const
 
void setParent (QObject *parent)
 
void installEventFilter (QObject *filterObj)
 
void removeEventFilter (QObject *obj)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const const
 
bool disconnect (const char *signal, const QObject *receiver, const char *method) const const
 
bool disconnect (const QObject *receiver, const char *method) const const
 
void dumpObjectTree ()
 
void dumpObjectInfo ()
 
void dumpObjectTree () const const
 
void dumpObjectInfo () const const
 
bool setProperty (const char *name, const QVariant &value)
 
QVariant property (const char *name) const const
 
QList< QByteArraydynamicPropertyNames () const const
 
void destroyed (QObject *obj)
 
void objectNameChanged (const QString &objectName)
 
QObjectparent () const const
 
bool inherits (const char *className) const const
 
void deleteLater ()
 

Static Public Member Functions

template<typename TPresenter >
static void registerAsPresenter ()
 Register a subclass of the QuickPresenter as the active presenter for the CoreApp.
 
static void addViewSearchDir (const QString &dirPath)
 Adds a directory to search for input views. More...
 
template<typename TViewModel >
static void registerViewExplicitly (const QUrl &viewUrl)
 Register a view for a viewmodel to be found by the presenter. More...
 
static void registerViewExplicitly (const QMetaObject *viewModelType, const QUrl &viewUrl)
 Register a view for a viewmodel to be found by the presenter. More...
 
static InputViewFactorygetInputViewFactory ()
 Returns the internally used input view factory.
 
- Static Public Member Functions inherited from QObject
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
QMetaObject::Connection connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method, Qt::ConnectionType type)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, Functor functor)
 
QMetaObject::Connection connect (const QObject *sender, PointerToMemberFunction signal, const QObject *context, Functor functor, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
bool disconnect (const QObject *sender, const QMetaMethod &signal, const QObject *receiver, const QMetaMethod &method)
 
bool disconnect (const QMetaObject::Connection &connection)
 
bool disconnect (const QObject *sender, PointerToMemberFunction signal, const QObject *receiver, PointerToMemberFunction method)
 

Protected Member Functions

virtual QUrl findViewUrl (const QMetaObject *viewModelType)
 Is called to find the URL of the view to be used for the given viewmodel. More...
 
virtual int presentMethodIndex (const QMetaObject *presenterMetaObject, QObject *viewObject)
 Tries to find the index of a method to present the view object on the presenter. More...
 
bool nameOrClassContains (const QObject *obj, const QString &contained, Qt::CaseSensitivity caseSensitive=Qt::CaseInsensitive) const
 Checks if the object name or class name contains the given string. More...
 
- Protected Member Functions inherited from QObject
QObjectsender () const const
 
int senderSignalIndex () const const
 
int receivers (const char *signal) const const
 
bool isSignalConnected (const QMetaMethod &signal) const const
 
virtual void timerEvent (QTimerEvent *event)
 
virtual void childEvent (QChildEvent *event)
 
virtual void customEvent (QEvent *event)
 
virtual void connectNotify (const QMetaMethod &signal)
 
virtual void disconnectNotify (const QMetaMethod &signal)
 

Properties

InputViewFactory inputViewFactory
 The factory to create input views with, as injected property. More...
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The IPresenter implementation for the quick module.

This presenter is automatically registered as the default presenter for the IPresenter interface with the ServiceRegistry, but as weak service, in order to make it possible to overwrite it.

The class handles all the logic required for presenting Quick Controsl 2 based views. You can extend this class and reimplement it's virtual methods if you need to adjust how certain views or dialogs are presented, or if you want to support custom stuff.

By default, the presenter will use the ":/qtmvvm/views" directory for finding views. When presenting a viewmodel, the presenter tries to find a view url with a matching name. If the viewmodel is for example named MyViewModel, then the view must start with My too. For example it can be named MyView.qml or MyDialog.qml

Note
Implicit detection of views for viewmodels can sometimes lead to ambiguities and thus a wrong view beeing found. In such cases, use registerViewExplicitly() instead.

Definition at line 19 of file quickpresenter.h.

Member Function Documentation

◆ addViewSearchDir()

QtMvvm::QuickPresenter::addViewSearchDir ( const QString dirPath)
static

Adds a directory to search for input views.

Parameters
dirPathA path to directory to be searched for input views

Simply adds the directory to the ones beeing searched for views. The new directoy will be prepended to the search list and thus be searched before any other directory.

See also
QuickPresenter::registerViewExplicitly

◆ findViewUrl()

QtMvvm::QuickPresenter::findViewUrl ( const QMetaObject viewModelType)
protectedvirtual

Is called to find the URL of the view to be used for the given viewmodel.

Parameters
viewModelTypeThe metobject of the viewmodel to find a view for
Returns
The URL of the found file
Exceptions
PresenterExceptionIf no view url could be found

The default implementation simply check all explicitly registered views and then tries to match the name with all qml files available in the search dirs. If no match if found, the same is tried for the parent viewmodel type recursively, until the ViewModel base is reached.

See also
QuickPresenter::addViewSearchDir

◆ nameOrClassContains()

QtMvvm::QuickPresenter::nameOrClassContains ( const QObject obj,
const QString contained,
Qt::CaseSensitivity  caseSensitive = Qt::CaseInsensitive 
) const
protected

Checks if the object name or class name contains the given string.

Parameters
objThe object to search for the string
containedThe string to be searched
caseSensitiveSpecifies if the check should be case sensitive
Returns
true if the object or class name contain the string, false if not

◆ present()

void QtMvvm::QuickPresenter::present ( ViewModel viewModel,
const QVariantHash &  params,
QPointer< ViewModel parent 
)
overridevirtual

Called to present a viewmodel.

Parameters
viewModelThe ViewModel to be presenter
paramsAdditional parameters for the viemodel initialization
parentAn optional parent for the viewmodels view
Exceptions
PresenterExceptionWhen presenting fails for whatever reason

This method should:

  1. Find and create a view for the viewmodel
  2. Use the view of the parent viewModel to make the new view a child of that parent view
  3. Make the viewmodel a child of the view (via QObject::setParent)
  4. Call the viewmodels ViewModel::onInit method with the given parameters
  5. Find a presentation method for the view (based on the view/viewmodel and/or parent)
  6. Show the view to the user

If this method returns it is assumed the presentation was successful. If you throw the exception, presenting has failed, and the app will automatically destroy the viewmodel and handle cleanups and results.

Note
If you need to present asynchronous, then you need to perform this step yourself in case the asynchronous presentation failed.
See also
IPresenter::showDialog

Implements QtMvvm::IPresenter.

◆ presentMethodIndex()

QtMvvm::QuickPresenter::presentMethodIndex ( const QMetaObject presenterMetaObject,
QObject viewObject 
)
protectedvirtual

Tries to find the index of a method to present the view object on the presenter.

Parameters
presenterMetaObjectThe metobject of the presenter to search a method on
viewObjectThe QML item to be presented on that presenter
Returns
The index of the method from the meta object to be called, or -1 if no method could be found.

This method is called to find the method. The method must take a single argument, the view to be presented, and return a bool to report whether presenting actually worked or not. The default implementation first checks if the view extends QQuickPopup, and if yes looks for the presentPopup method. If the view is a normal QQuickItem, the name is analyzed. If the name contains Drawer, the presentDrawerContent method is checkd. If the name contains Tab, the presentTab method is checkd. If none applies or none was found, the method looks for the presentItem method. If that one is not found as well, `-1 is returned.

See also
QuickPresenter::presentToQml

◆ presentToQml()

QtMvvm::QuickPresenter::presentToQml ( QObject qmlPresenter,
QObject viewObject 
)
virtual

Is called to present a view to presenter living in QML.

Parameters
qmlPresenterThe presenter to present the view to
viewObjectThe view QML object to be presented
Returns
true if successfully presented, false if not

Is called from the qml presentation part to actually present a view to another view that takes the role of a presenter, or the actual main presenter.

The default implementation uses presentMethodIndex() to find a method to call, then calls it and returns the result of that method call. If no method was found, false is returned.

See also
QuickPresenter::presentMethodIndex

◆ registerViewExplicitly() [1/2]

template<typename TViewModel >
QtMvvm::QuickPresenter::registerViewExplicitly ( const QUrl viewUrl)
static

Register a view for a viewmodel to be found by the presenter.

Template Parameters
TViewModelThe viewmodel to to register the view for
Parameters
viewUrlThe QML url register within the presenter. Must be a valid qml component

The url is registered with the current presenter. It is registered explicitly, which means that whenever the given viewmodel is beeing presented, this exact url will be used. Explicit registration have precedence over implicit ones.

See also
QuickPresenter::addViewSearchDir

Definition at line 87 of file quickpresenter.h.

◆ registerViewExplicitly() [2/2]

QtMvvm::QuickPresenter::registerViewExplicitly ( const QMetaObject viewModelType,
const QUrl viewUrl 
)
static

Register a view for a viewmodel to be found by the presenter.

Parameters
viewModelTypeThe viewmodel to to register the view for
viewUrlThe QML url register within the presenter. Must be a valid qml component

The url is registered with the current presenter. It is registered explicitly, which means that whenever the given viewmodel is beeing presented, this exact url will be used. Explicit registration have precedence over implicit ones.

See also
QuickPresenter::addViewSearchDir

◆ showDialog()

void QtMvvm::QuickPresenter::showDialog ( const MessageConfig config,
MessageResult result 
)
overridevirtual

Called to present a dialog via a message config.

Parameters
configThe configuration for the dialog to be shown
resultA reference to the object to report results to
Exceptions
PresenterExceptionWhen presenting fails for whatever reason

This method should create a simple dialog based of the configuration passed to it. Read the MessageConfig for more details on the parameters. The MessageConfig::type and MessageConfig::subType properties are used to determine the kind of dialog to be shown. The other properties are used to configure the dialog.

The result is a reference to a result object to report the result value and the pressed button to. The result is owned by the caller, you must never delete it. Use the special "GUI methods" to report the result and configure a close target.

If this method returns it is assumed the presentation was successful. If you throw the exception, presenting has failed, and the app will automatically complete the message result with the MessageConfig::NoButton constant.

Note
If you need to present asynchronous, then you need to perform this step yourself in case the asynchronous presentation failed.
See also
IPresenter::present, MessageConfig, MessageResult, MessageResult::complete, MessageResult::setCloseTarget

Implements QtMvvm::IPresenter.

Property Documentation

◆ inputViewFactory

QtMvvm::QuickPresenter::inputViewFactory
readwrite

The factory to create input views with, as injected property.

Default: Injected

Do not set this property yourself. It is automatically injected when showing the viewmodel. You can use the ServiceRegistry::registerInterface if you need to use a factory different from the default one.

Accessors
READinputViewFactory()
WRITEsetInputViewFactory()
NOTIFYinputViewFactoryChanged()
See also
QTMVVM_INJECT

Definition at line 25 of file quickpresenter.h.


The documentation for this class was generated from the following files: