Migration of QtMvvm from github
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

96 lines
3.0 KiB

#ifndef QTMVVM_COREAPP_H
#define QTMVVM_COREAPP_H
#include <type_traits>
#include <QtCore/qobject.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/qcoreapplication.h>
class QCommandLineParser;
#include "QtMvvmCore/qtmvvmcore_global.h"
#include "QtMvvmCore/viewmodel.h"
#include "QtMvvmCore/ipresenter.h"
#include "QtMvvmCore/message.h"
namespace QtMvvm {
class CoreAppPrivate;
//! A logicaly application object to drive the mvvm application from the core part
class Q_MVVMCORE_EXPORT CoreApp : public QObject
{
Q_OBJECT
public:
//! Default Constructor
explicit CoreApp(QObject *parent = nullptr);
~CoreApp() override;
//! Returns the current CoreApp instance
static CoreApp *instance();
//! Disables the automatic startup of the CoreApp
static void disableAutoBoot();
//! Registers this instance as the CoreApp instance
void registerApp();
//! Show a new ViewModel by its type
template <typename TViewModel>
static inline void show(const QVariantHash &params = {});
//! Show a new ViewModel by its name
static void show(const char *viewModelName, const QVariantHash &params = {});
//! Show a new ViewModel by its metaobject
static void show(const QMetaObject *viewModelMetaObject, const QVariantHash &params = {});
//! Show a basic dialog
static MessageResult *showDialog(const MessageConfig &config);
public Q_SLOTS:
//! Boots up the app and starts the mvvm presenting
void bootApp();
Q_SIGNALS:
//! Is emitted once the app has successfully started
void appStarted(QPrivateSignal);
protected:
//! Is called as part of the app registration, before any view setups
virtual void performRegistrations();
//! Is called as part of the app start/boot, after all the view setups to start the app
virtual int startApp(const QStringList &arguments) = 0;
//! Is called when the app is about to quit
virtual void closeApp();
//! Can be used to automatically handle help and version commandline arguments
bool autoParse(QCommandLineParser &parser, const QStringList &arguments);
private:
friend class QtMvvm::CoreAppPrivate;
QScopedPointer<CoreAppPrivate> d;
static void showImp(const QMetaObject *metaObject, const QVariantHash &params);
};
template<typename TViewModel>
inline void CoreApp::show(const QVariantHash &params)
{
static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
ViewModel::showImp(&TViewModel::staticMetaObject, params, nullptr);
}
}
//! Registers you custom CoreApp class as CoreApp to be used
#define QTMVVM_REGISTER_CORE_APP(T) \
static void _setup_ ## T ## _hook() { \
static_assert(std::is_base_of<QtMvvm::CoreApp, T>::value, "QTMVVM_REGISTER_CORE_APP must be used with a class that extends QtMvvm::CoreApp"); \
auto app = new T(nullptr); \
app->registerApp(); \
} \
Q_COREAPP_STARTUP_FUNCTION(_setup_ ## T ## _hook)
//! A define as shortcut to the CoreApp. Can be redefined to cast to your custom class type
#define coreApp QtMvvm::CoreApp::instance()
//!@file coreapp.h The Header that defines the QtMvvm::CoreApp class
#endif // QTMVVM_COREAPP_H