Browse Source

prepare first presenter code

pull/2/head
Skycoder42 7 years ago
parent
commit
cd4b7b23fa
  1. 7
      src/mvvmquick/mvvmquick.pro
  2. 78
      src/mvvmquick/quickpresenter.cpp
  3. 8
      src/mvvmquick/quickpresenter.h
  4. 24
      src/mvvmquick/quickpresenter_p.h
  5. 18
      src/mvvmwidgets/widgetspresenter.cpp
  6. 2
      src/mvvmwidgets/widgetspresenter_p.h

7
src/mvvmquick/mvvmquick.pro

@ -1,13 +1,14 @@
TARGET = QtMvvmQuick TARGET = QtMvvmQuick
QT = core gui widgets mvvmcore QT = core gui widgets mvvmcore mvvmcore-private
HEADERS += \ HEADERS += \
qtmvvmquick_global.h \ qtmvvmquick_global.h \
quickpresenter.h quickpresenter.h \
quickpresenter_p.h
SOURCES += \ SOURCES += \
quickpresenter.cpp quickpresenter.cpp
TRANSLATIONS += \ TRANSLATIONS += \
translations/qtmvvmquick_de.ts \ translations/qtmvvmquick_de.ts \

78
src/mvvmquick/quickpresenter.cpp

@ -1,14 +1,30 @@
#include "quickpresenter.h" #include "quickpresenter.h"
#include "quickpresenter_p.h"
#include <QtCore/QDir>
#include <QtCore/QDirIterator>
#include <QtMvvmCore/private/coreapp_p.h>
#include <QtMvvmCore/private/qtmvvm_logging_p.h>
using namespace QtMvvm; using namespace QtMvvm;
QuickPresenter::QuickPresenter(QObject *parent) : QuickPresenter::QuickPresenter(QObject *parent) :
QObject(parent), QObject(parent),
IPresenter() IPresenter(),
d(new QuickPresenterPrivate())
{} {}
void QuickPresenter::registerViewExplicitly(const QMetaObject *viewModelType, const QUrl &viewUrl) QuickPresenter::~QuickPresenter() {}
void QuickPresenter::addViewSearchDir(const QString &dirUrl)
{ {
QuickPresenterPrivate::currentPresenter()->d->searchDirs.prepend(dirUrl);
}
void QuickPresenter::registerViewExplicitly(const QMetaObject *viewModelType, const QUrl &viewUrl)
{
QuickPresenterPrivate::currentPresenter()->d->explicitMappings.insert(viewModelType, viewUrl);
} }
void QuickPresenter::present(QtMvvm::ViewModel *viewModel, const QVariantHash &params, QPointer<QtMvvm::ViewModel> parent) void QuickPresenter::present(QtMvvm::ViewModel *viewModel, const QVariantHash &params, QPointer<QtMvvm::ViewModel> parent)
@ -23,5 +39,63 @@ void QuickPresenter::showDialog(const QtMvvm::MessageConfig &config, QtMvvm::Mes
QUrl QuickPresenter::findViewUrl(const QMetaObject *viewModelType) QUrl QuickPresenter::findViewUrl(const QMetaObject *viewModelType)
{ {
auto currentMeta = viewModelType;
while(currentMeta && currentMeta->inherits(&ViewModel::staticMetaObject)) {
if(d->explicitMappings.contains(currentMeta))
return d->explicitMappings.value(currentMeta);
else {
QByteArray cName = currentMeta->className();
auto lIndex = cName.lastIndexOf("ViewModel");
if(lIndex > 0)
cName.truncate(lIndex);
for(auto dir : qAsConst(d->searchDirs)) {
QDir searchDir(dir,
QStringLiteral("%1*.qml").arg(QString::fromLatin1(cName)),
QDir::NoSort,
QDir::Files | QDir::NoDotAndDotDot | QDir::Readable);
QDirIterator iterator(searchDir, QDirIterator::Subdirectories);
if(iterator.hasNext()) {
iterator.next();
QUrl resUrl;
if(dir.startsWith(QStringLiteral(":"))) {
resUrl.setScheme(QStringLiteral("qrc"));
resUrl.setPath(iterator.filePath().mid(1)); //skip the beginning colon
logDebug() << "TEST:" << QUrl::fromLocalFile(iterator.filePath());
} else
resUrl = QUrl::fromLocalFile(iterator.filePath());
logDebug() << "Found URL for viewmodel"
<< viewModelType->className()
<< "as:" << resUrl;
return resUrl;
}
}
}
currentMeta = currentMeta->superClass();
}
return QUrl();
}
// ------------- Private Implementation -------------
QuickPresenterPrivate::QuickPresenterPrivate() :
explicitMappings(),
searchDirs({QStringLiteral(":/qtmvvm/views")})
{}
QuickPresenter *QuickPresenterPrivate::currentPresenter()
{
auto presenter = static_cast<QuickPresenter*>(CoreAppPrivate::dInstance()->currentPresenter());
if(!presenter) {
presenter = new QuickPresenter();
CoreApp::setMainPresenter(presenter);
}
#ifndef Q_NO_DEBUG
Q_ASSERT_X(dynamic_cast<QuickPresenter*>(CoreAppPrivate::dInstance()->currentPresenter()),
Q_FUNC_INFO,
"Cannot register views if the current presenter does not extend QtMvvm::QuickPresenter");
#endif
return presenter;
} }

8
src/mvvmquick/quickpresenter.h

@ -2,6 +2,8 @@
#define QTMVVM_QUICKPRESENTER_H #define QTMVVM_QUICKPRESENTER_H
#include <QtCore/qobject.h> #include <QtCore/qobject.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/qurl.h>
#include <QtMvvmCore/ipresenter.h> #include <QtMvvmCore/ipresenter.h>
#include <QtMvvmCore/coreapp.h> #include <QtMvvmCore/coreapp.h>
@ -10,6 +12,7 @@
namespace QtMvvm { namespace QtMvvm {
class QuickPresenterPrivate;
class Q_MVVMQUICK_EXPORT QuickPresenter : public QObject, public IPresenter class Q_MVVMQUICK_EXPORT QuickPresenter : public QObject, public IPresenter
{ {
Q_OBJECT Q_OBJECT
@ -17,10 +20,12 @@ class Q_MVVMQUICK_EXPORT QuickPresenter : public QObject, public IPresenter
public: public:
explicit QuickPresenter(QObject *parent = nullptr); explicit QuickPresenter(QObject *parent = nullptr);
~QuickPresenter();
template <typename TPresenter = QuickPresenter> template <typename TPresenter = QuickPresenter>
static void registerAsPresenter(); static void registerAsPresenter();
static void addViewSearchDir(const QString &dirUrl);
template <typename TViewModel> template <typename TViewModel>
static void registerViewExplicitly(const QUrl &viewUrl); static void registerViewExplicitly(const QUrl &viewUrl);
static void registerViewExplicitly(const QMetaObject *viewModelType, const QUrl &viewUrl); static void registerViewExplicitly(const QMetaObject *viewModelType, const QUrl &viewUrl);
@ -30,6 +35,9 @@ public:
protected: protected:
virtual QUrl findViewUrl(const QMetaObject *viewModelType); virtual QUrl findViewUrl(const QMetaObject *viewModelType);
private:
QScopedPointer<QuickPresenterPrivate> d;
}; };
template<typename TPresenter> template<typename TPresenter>

24
src/mvvmquick/quickpresenter_p.h

@ -0,0 +1,24 @@
#ifndef QTMVVM_QUICKPRESENTER_P_H
#define QTMVVM_QUICKPRESENTER_P_H
#include <QtCore/QHash>
#include "qtmvvmquick_global.h"
#include "quickpresenter.h"
namespace QtMvvm {
class QuickPresenterPrivate
{
public:
QuickPresenterPrivate();
QHash<const QMetaObject *, QUrl> explicitMappings;
QStringList searchDirs;
static QuickPresenter *currentPresenter();
};
}
#endif // QTMVVM_QUICKPRESENTER_P_H

18
src/mvvmwidgets/widgetspresenter.cpp

@ -41,7 +41,7 @@ void WidgetsPresenter::registerViewExplicitly(const QMetaObject *viewModelType,
{ {
Q_ASSERT_X(viewModelType->inherits(&ViewModel::staticMetaObject), Q_FUNC_INFO, "viewModelType must be a QtMvvm::ViewModel class"); Q_ASSERT_X(viewModelType->inherits(&ViewModel::staticMetaObject), Q_FUNC_INFO, "viewModelType must be a QtMvvm::ViewModel class");
Q_ASSERT_X(viewType->inherits(&QWidget::staticMetaObject), Q_FUNC_INFO, "viewType must be a QWidget class"); Q_ASSERT_X(viewType->inherits(&QWidget::staticMetaObject), Q_FUNC_INFO, "viewType must be a QWidget class");
WidgetsPresenterPrivate::currentPresenter()->d->explicitMappings.insert(viewModelType->className(), viewType); WidgetsPresenterPrivate::currentPresenter()->d->explicitMappings.insert(viewModelType, viewType);
} }
void WidgetsPresenter::present(ViewModel *viewModel, const QVariantHash &params, QPointer<ViewModel> parent) void WidgetsPresenter::present(ViewModel *viewModel, const QVariantHash &params, QPointer<ViewModel> parent)
@ -112,10 +112,10 @@ const QMetaObject *WidgetsPresenter::findWidgetMetaObject(const QMetaObject *vie
{ {
auto currentMeta = viewModelMetaObject; auto currentMeta = viewModelMetaObject;
while(currentMeta && currentMeta->inherits(&ViewModel::staticMetaObject)) { while(currentMeta && currentMeta->inherits(&ViewModel::staticMetaObject)) {
QByteArray cName = currentMeta->className(); if(d->explicitMappings.contains(currentMeta))
if(d->explicitMappings.contains(cName)) return d->explicitMappings.value(currentMeta);
return d->explicitMappings.value(cName);
else { else {
QByteArray cName = currentMeta->className();
auto lIndex = cName.lastIndexOf("ViewModel"); auto lIndex = cName.lastIndexOf("ViewModel");
if(lIndex > 0) if(lIndex > 0)
cName.truncate(lIndex); cName.truncate(lIndex);
@ -401,15 +401,15 @@ WidgetsPresenterPrivate::WidgetsPresenterPrivate() :
WidgetsPresenter *WidgetsPresenterPrivate::currentPresenter() WidgetsPresenter *WidgetsPresenterPrivate::currentPresenter()
{ {
#ifndef Q_NO_DEBUG
Q_ASSERT_X(dynamic_cast<WidgetsPresenter*>(CoreAppPrivate::dInstance()->currentPresenter()),
Q_FUNC_INFO,
"Cannot register widgets if the current presenter does not extend QtMvvm::WidgetsPresenter");
#endif
auto presenter = static_cast<WidgetsPresenter*>(CoreAppPrivate::dInstance()->currentPresenter()); auto presenter = static_cast<WidgetsPresenter*>(CoreAppPrivate::dInstance()->currentPresenter());
if(!presenter) { if(!presenter) {
presenter = new WidgetsPresenter(); presenter = new WidgetsPresenter();
CoreApp::setMainPresenter(presenter); CoreApp::setMainPresenter(presenter);
} }
#ifndef Q_NO_DEBUG
Q_ASSERT_X(dynamic_cast<WidgetsPresenter*>(CoreAppPrivate::dInstance()->currentPresenter()),
Q_FUNC_INFO,
"Cannot register widgets if the current presenter does not extend QtMvvm::WidgetsPresenter");
#endif
return presenter; return presenter;
} }

2
src/mvvmwidgets/widgetspresenter_p.h

@ -18,7 +18,7 @@ public:
QScopedPointer<InputViewFactory> inputViewFactory; QScopedPointer<InputViewFactory> inputViewFactory;
QSet<const QMetaObject*> implicitMappings; QSet<const QMetaObject*> implicitMappings;
QHash<QByteArray, const QMetaObject*> explicitMappings; QHash<const QMetaObject*, const QMetaObject*> explicitMappings;
}; };
} }

Loading…
Cancel
Save