Browse Source

refactoring

pull/2/head
Skycoder42 7 years ago
parent
commit
665a37620f
  1. 25
      examples/mvvmcore/SampleCore/SampleCore.pro
  2. 12
      examples/mvvmcore/SampleCore/samplecore_global.h
  3. 7
      examples/mvvmcore/SampleCore/samplecoreapp.cpp
  4. 4
      examples/mvvmcore/SampleCore/samplecoreapp.h
  5. 11
      examples/mvvmwidgets/SampleWidgets/SampleWidgets.pro
  6. 8
      examples/mvvmwidgets/SampleWidgets/main.cpp
  7. 3
      src/mvvmcore/coreapp.cpp
  8. 11
      src/mvvmcore/coreapp.h
  9. 4
      src/mvvmcore/serviceregistry.cpp
  10. 8
      src/mvvmcore/serviceregistry.h
  11. 2
      src/mvvmcore/viewmodel.cpp
  12. 19
      src/mvvmcore/viewmodel.h
  13. 5
      src/mvvmquick/builddummy.h
  14. 5
      src/mvvmwidgets/builddummy.h

25
examples/mvvmcore/SampleCore/SampleCore.pro

@ -1,33 +1,20 @@
TEMPLATE = lib
QT = core gui mvvmcore
CONFIG += static
TARGET = SampleCore
DEFINES += SAMPLECORE_LIBRARY
HEADERS += \
samplecore_global.h \
samplecoreapp.h \
sampleviewmodel.h \
ieventservice.h \
echoservice.h
sampleviewmodel.h \
ieventservice.h \
echoservice.h
SOURCES += \
samplecoreapp.cpp \
sampleviewmodel.cpp \
echoservice.cpp
sampleviewmodel.cpp \
echoservice.cpp
target.path = $$[QT_INSTALL_EXAMPLES]/mvvmcore/$$TARGET
INSTALLS += target
#not found by linker?
unix:!mac {
LIBS += -L$$OUT_PWD/../../../lib #required to make this the first place to search
LIBS += -L$$[QT_INSTALL_LIBS] -licudata
LIBS += -L$$[QT_INSTALL_LIBS] -licui18n
LIBS += -L$$[QT_INSTALL_LIBS] -licuuc
}
#add lib dir to rpath
mac: QMAKE_LFLAGS += '-Wl,-rpath,\'$$OUT_PWD/../../../lib\''

12
examples/mvvmcore/SampleCore/samplecore_global.h

@ -1,12 +0,0 @@
#ifndef SAMPLECORE_GLOBAL_H
#define SAMPLECORE_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(SAMPLECORE_LIBRARY)
# define SAMPLECORESHARED_EXPORT Q_DECL_EXPORT
#else
# define SAMPLECORESHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // SAMPLECORE_GLOBAL_H

7
examples/mvvmcore/SampleCore/samplecoreapp.cpp

@ -3,8 +3,6 @@
#include <QtCore/QCommandLineParser>
QTMVVM_REGISTER_CORE_APP(SampleCoreApp)
SampleCoreApp::SampleCoreApp(QObject *parent) :
CoreApp(parent)
{}
@ -23,7 +21,6 @@ int SampleCoreApp::startApp(const QStringList &arguments)
if(!autoParse(parser, arguments))
return EXIT_SUCCESS;
//TODO implement app code
QtMvvm::ViewModel::show<SampleViewModel>();
return EXIT_FAILURE;
show<SampleViewModel>();
return EXIT_SUCCESS;
}

4
examples/mvvmcore/SampleCore/samplecoreapp.h

@ -3,9 +3,7 @@
#include <QtMvvmCore/CoreApp>
#include "samplecore_global.h"
class SAMPLECORESHARED_EXPORT SampleCoreApp : public QtMvvm::CoreApp
class SampleCoreApp : public QtMvvm::CoreApp
{
Q_OBJECT

11
examples/mvvmwidgets/SampleWidgets/SampleWidgets.pro

@ -6,12 +6,12 @@ TARGET = SampleWidgets
HEADERS += \
mainwindow.h \
widgetseventservice.h
widgetseventservice.h
SOURCES += \
main.cpp \
mainwindow.cpp \
widgetseventservice.cpp
widgetseventservice.cpp
FORMS += \
mainwindow.ui
@ -30,9 +30,16 @@ unix:!mac {
#add lib dir to rpath
mac: QMAKE_LFLAGS += '-Wl,-rpath,\'$$OUT_PWD/../../../lib\''
#link to core lib
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../../mvvmcore/SampleCore/release/ -lSampleCore
else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../../mvvmcore/SampleCore/debug/ -lSampleCore
else:unix: LIBS += -L$$OUT_PWD/../../mvvmcore/SampleCore/ -lSampleCore
INCLUDEPATH += $$PWD/../../mvvmcore/SampleCore
DEPENDPATH += $$PWD/../../mvvmcore/SampleCore
win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../mvvmcore/SampleCore/release/libSampleCore.a
else:win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../mvvmcore/SampleCore/debug/libSampleCore.a
else:win32:!win32-g++:CONFIG(release, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../mvvmcore/SampleCore/release/SampleCore.lib
else:win32:!win32-g++:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$OUT_PWD/../../mvvmcore/SampleCore/debug/SampleCore.lib
else:unix: PRE_TARGETDEPS += $$OUT_PWD/../../mvvmcore/SampleCore/libSampleCore.a

8
examples/mvvmwidgets/SampleWidgets/main.cpp

@ -1,7 +1,7 @@
#include "mainwindow.h"
#include <QApplication>
#include <QtMvvmCore/ServiceRegistry>
#include <samplecoreapp.h>
#include "widgetseventservice.h"
@ -10,6 +10,8 @@
#define TEST_INST 2
#define TEST_CURRENT TEST_DIRECT
QTMVVM_REGISTER_CORE_APP(SampleCoreApp)
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
@ -30,10 +32,10 @@ int main(int argc, char *argv[])
return new WidgetsEventService(echo, nullptr);
});
if(TEST_CURRENT == TEST_INST)
QtMvvm::ServiceRegistry::instance()->registerInterface<IEventService>(new WidgetsEventService(QtMvvm::ServiceRegistry::instance()->acquireInstance<EchoService>()));
QtMvvm::ServiceRegistry::instance()->registerInterface<IEventService>(new WidgetsEventService(QtMvvm::ServiceRegistry::instance()->service<EchoService>()));
//debug test
auto event = QtMvvm::ServiceRegistry::instance()->acquireInstance<IEventService>();
auto event = QtMvvm::ServiceRegistry::instance()->service<IEventService>();
return a.exec();
}

3
src/mvvmcore/coreapp.cpp

@ -29,6 +29,7 @@ void CoreApp::registerApp()
//register metatypes
setParent(qApp);
CoreAppPrivate::instance = this;
performRegistrations();
if(CoreAppPrivate::bootEnabled)
QMetaObject::invokeMethod(this, "bootApp", Qt::QueuedConnection);
}
@ -43,6 +44,8 @@ void CoreApp::bootApp()
qApp->exit(res);
}
void CoreApp::performRegistrations() {}
void CoreApp::closeApp() {}
bool CoreApp::autoParse(QCommandLineParser &parser, const QStringList &arguments)

11
src/mvvmcore/coreapp.h

@ -3,9 +3,11 @@
#include <QtCore/qobject.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/qcoreapplication.h>
class QCommandLineParser;
#include "QtMvvmCore/qtmvvmcore_global.h"
#include "QtMvvmCore/viewmodel.h"
namespace QtMvvm {
@ -27,15 +29,24 @@ public Q_SLOTS:
void bootApp();
protected:
virtual void performRegistrations();
virtual int startApp(const QStringList &arguments) = 0;
virtual void closeApp();
bool autoParse(QCommandLineParser &parser, const QStringList &arguments);
template <typename TViewModel>
inline void show(const QVariantHash &params = {}) const;
private:
QScopedPointer<CoreAppPrivate> d;
};
template<typename TViewModel>
inline void CoreApp::show(const QVariantHash &params) const
{
ViewModel::show<TViewModel>(params);
}
}
#define QTMVVM_REGISTER_CORE_APP(T) \

4
src/mvvmcore/serviceregistry.cpp

@ -36,7 +36,7 @@ void ServiceRegistry::registerService(const QByteArray &iid, const std::function
d->services.insert(iid, QSharedPointer<ServiceRegistryPrivate::FnServiceInfo>::create(fn, injectables));
}
QObject *ServiceRegistry::acquireInstanceObject(const QByteArray &iid)
QObject *ServiceRegistry::serviceObj(const QByteArray &iid)
{
QMutexLocker _(&d->serviceMutex);
auto ref = d->services.value(iid);
@ -138,7 +138,7 @@ QObject *ServiceRegistryPrivate::MetaServiceInfo::construct(ServiceRegistryPriva
}
}
auto initMethod = metaObject->indexOfMethod("qtmvvm_init()");
auto initMethod = metaObject->indexOfMethod("qtmvvm_init()"); //TODO document
if(initMethod != -1) {
auto method = metaObject->method(initMethod);
method.invoke(instance);

8
src/mvvmcore/serviceregistry.h

@ -39,8 +39,8 @@ public:
QByteArrayList injectables);
template <typename TInterface>
TInterface *acquireInstance();
QObject *acquireInstanceObject(const QByteArray &iid);
TInterface *service();
QObject *serviceObj(const QByteArray &iid);
private:
QScopedPointer<ServiceRegistryPrivate> d;
@ -155,9 +155,9 @@ void ServiceRegistry::registerObject(TService *service)
#undef QTMVVM_SERVICE_ASSERT
template<typename TInterface>
TInterface *ServiceRegistry::acquireInstance()
TInterface *ServiceRegistry::service()
{
return qobject_cast<TInterface*>(acquireInstanceObject(__helpertypes::inject_iid<TInterface*>()));
return qobject_cast<TInterface*>(serviceObj(__helpertypes::inject_iid<TInterface*>()));
}
}

2
src/mvvmcore/viewmodel.cpp

@ -36,7 +36,7 @@ void ViewModel::onShow() {}
void ViewModel::onClose() {}
void ViewModel::showImp(const QMetaObject *mo, QObject *parent)
void ViewModel::showImp(const QMetaObject *mo, const QVariantHash &params, ViewModel *parent)
{
Q_UNIMPLEMENTED();
}

19
src/mvvmcore/viewmodel.h

@ -5,6 +5,7 @@
#include <QtCore/qobject.h>
#include <QtCore/qscopedpointer.h>
#include <QtCore/qvariant.h>
#include "QtMvvmCore/qtmvvmcore_global.h"
@ -26,7 +27,7 @@ public:
bool deleteOnClose() const;
template <typename TViewModel>
static void show(QObject *parent = nullptr);
inline static void show(const QVariantHash &params = {});
public Q_SLOTS:
void setDeleteOnClose(bool deleteOnClose);
@ -40,17 +41,27 @@ protected:
virtual void onShow();
virtual void onClose();
template <typename TViewModel>
inline void showChild(const QVariantHash &params = {}) const;
private:
QScopedPointer<ViewModelPrivate> d;
static void showImp(const QMetaObject *mo, QObject *parent);
static void showImp(const QMetaObject *mo, const QVariantHash &params, ViewModel *parent);
};
template<typename TViewModel>
void ViewModel::show(QObject *parent)
inline void ViewModel::show(const QVariantHash &params)
{
static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
showImp(&TViewModel::staticMetaObject, params, nullptr);
}
template<typename TViewModel>
inline void ViewModel::showChild(const QVariantHash &params) const
{
static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must extend QtMvvm::ViewModel");
showImp(&TViewModel::staticMetaObject, parent);
showImp(&TViewModel::staticMetaObject, params, const_cast<ViewModel*>(this));
}
}

5
src/mvvmquick/builddummy.h

@ -1,11 +1,12 @@
#ifndef BUILDDUMMY_H
#define BUILDDUMMY_H
#include "qtmvvmquick_global.h"
class builddummy
class Q_MVVMQUICK_EXPORT builddummy
{
public:
builddummy();
};
#endif // BUILDDUMMY_H
#endif // BUILDDUMMY_H

5
src/mvvmwidgets/builddummy.h

@ -1,11 +1,12 @@
#ifndef BUILDDUMMY_H
#define BUILDDUMMY_H
#include "qtmvvmwidgets_global.h"
class builddummy
class Q_MVVMWIDGETS_EXPORT builddummy
{
public:
builddummy();
};
#endif // BUILDDUMMY_H
#endif // BUILDDUMMY_H

Loading…
Cancel
Save