Browse Source

added core imports

pull/2/head
Skycoder42 7 years ago
parent
commit
2ab998420b
  1. 15
      examples/mvvmquick/SampleQuick/SampleQuick.pro
  2. 13
      examples/mvvmquick/SampleQuick/main.cpp
  3. 5
      examples/mvvmquick/SampleQuick/main.qml
  4. 48
      examples/mvvmquick/SampleQuick/quickeventservice.cpp
  5. 38
      examples/mvvmquick/SampleQuick/quickeventservice.h
  6. 3
      src/imports/imports.pro
  7. 33
      src/imports/mvvmcore/mvvmcore.pro
  8. 57
      src/imports/mvvmcore/plugins.qmltypes
  9. 4
      src/imports/mvvmcore/qmldir
  10. 67
      src/imports/mvvmcore/qqmlmvvmbinding.cpp
  11. 67
      src/imports/mvvmcore/qqmlmvvmbinding.h
  12. 16
      src/imports/mvvmcore/qtmvvmcore_plugin.cpp
  13. 16
      src/imports/mvvmcore/qtmvvmcore_plugin.h
  14. 5
      src/imports/mvvmquick/mvvmquick.pro
  15. 8
      src/mvvmcore/binding.cpp
  16. 1
      src/mvvmcore/binding.h
  17. 10
      src/mvvmquick/quickpresenter.cpp
  18. 26
      src/mvvmquick/quickpresenter.h

15
examples/mvvmquick/SampleQuick/SampleQuick.pro

@ -4,7 +4,8 @@ QT += core gui qml quick mvvmquick
TARGET = SampleQuick
SOURCES += main.cpp
SOURCES += main.cpp \
quickeventservice.cpp
RESOURCES += qml.qrc
@ -44,11 +45,12 @@ samples_in_build {
QMLDEPPATH = $$PWD/../../../src/imports/mvvmquick
system($$QMAKE_MKDIR $$shell_quote($$shell_path($$FAKEPATH)))
# next, symlink all "compiled" files
# next, symlink all "compiled" files (whole dir for core, as it has no qml files
build_symlink_target.target = create_qml_build_symlinks
build_symlink_target.commands += $$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/libdeclarative_mvvmquick.so) $$shell_quote($$shell_path($$FAKEPATH/libdeclarative_mvvmquick.so)) \
$$escape_expand(\n\t)$$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/plugins.qmltypes) $$shell_quote($$shell_path($$FAKEPATH/plugins.qmltypes)) \
$$escape_expand(\n\t)$$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/qmldir) $$shell_quote($$shell_path($$FAKEPATH/qmldir))
build_symlink_target.commands += $$QMAKE_SYMBOLIC_LINK $$shell_path(../../../../../../../qml/de/skycoder42/qtmvvm/core) $$shell_path(qml/de/skycoder42/qtmvvm/core) \
$$escape_expand(\n\t)$$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/libdeclarative_mvvmquick.so) $$shell_path($$FAKEPATH/libdeclarative_mvvmquick.so) \
$$escape_expand(\n\t)$$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/plugins.qmltypes) $$shell_path($$FAKEPATH/plugins.qmltypes) \
$$escape_expand(\n\t)$$QMAKE_SYMBOLIC_LINK $$shell_path($$ORIGPATH/qmldir) $$shell_path($$FAKEPATH/qmldir)
QMAKE_EXTRA_TARGETS += build_symlink_target
# next, prepare compiler to symlink all the qml files
@ -68,3 +70,6 @@ samples_in_build {
QML_IMPORT_PATH = $$OUT_PWD/qml/
DEFINES += QML_PATH=\\\"$$QML_IMPORT_PATH\\\"
}
HEADERS += \
quickeventservice.h

13
examples/mvvmquick/SampleQuick/main.cpp

@ -1,18 +1,29 @@
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtMvvmCore/ServiceRegistry>
#include <QtMvvmQuick/QuickPresenter>
#include <samplecoreapp.h>
#include <echoservice.h>
#include "quickeventservice.h"
QTMVVM_REGISTER_CORE_APP(SampleCoreApp)
int main(int argc, char *argv[])
{
#ifdef QML_PATH
qputenv("QML2_IMPORT_PATH", QML_PATH);
#endif
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QtMvvm::QuickPresenter::registerAsPresenter();
QtMvvm::ServiceRegistry::instance()->registerObject<EchoService>();
QtMvvm::ServiceRegistry::instance()->registerInterface<IEventService, QuickEventService>();
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
if (engine.rootObjects().isEmpty())

5
examples/mvvmquick/SampleQuick/main.qml

@ -1,6 +1,11 @@
import QtQuick 2.10
import de.skycoder42.qtmvvm.core 1.0
import de.skycoder42.qtmvvm.quick 1.0
App {
title: qsTr("Hello World")
QtMvvmBinding {
id: binding
}
}

48
examples/mvvmquick/SampleQuick/quickeventservice.cpp

@ -0,0 +1,48 @@
#include "quickeventservice.h"
QuickEventService::QuickEventService(QObject *parent) :
QObject(parent),
IEventService(),
_cnt(0),
_events(),
_echoService(nullptr)
{}
QuickEventService::QuickEventService(EchoService *svc, QObject *parent) :
QObject(parent),
IEventService(),
_cnt(0),
_events(),
_echoService(svc)
{
qtmvvm_init();
}
int QuickEventService::addEvent(const QString &name)
{
QSharedPointer<QTimer> timer {
new QTimer(this),
&QTimer::deleteLater
};
_events.insert(_cnt, timer);
connect(timer.data(), &QTimer::timeout, this, [this, name]() {
_echoService->ping(name);
});
timer->start(1000);
return _cnt++;
}
void QuickEventService::removeEvent(int eventId)
{
_events.remove(eventId);
}
void QuickEventService::qtmvvm_init()
{
qDebug(Q_FUNC_INFO);
Q_ASSERT(_echoService);
connect(_echoService, &EchoService::pong,
this, &QuickEventService::eventTriggered);
}

38
examples/mvvmquick/SampleQuick/quickeventservice.h

@ -0,0 +1,38 @@
#ifndef QUICKEVENTSERVICE_H
#define QUICKEVENTSERVICE_H
#include <QtCore/QObject>
#include <QtCore/QHash>
#include <QtCore/QTimer>
#include <QtCore/QSharedPointer>
#include <ieventservice.h>
#include <echoservice.h>
#include <QtMvvmCore/ViewModel>
class QuickEventService : public QObject, public IEventService
{
Q_OBJECT
Q_INTERFACES(IEventService)
QTMVVM_INJECT_PROP(EchoService*, echoService, _echoService)
public:
Q_INVOKABLE explicit QuickEventService(QObject *parent = nullptr);
explicit QuickEventService(EchoService* svc, QObject *parent = nullptr);
int addEvent(const QString &name) override;
void removeEvent(int eventId) override;
Q_SIGNALS:
void eventTriggered(const QString &event) final;
private:
int _cnt;
QHash<int, QSharedPointer<QTimer>> _events;
EchoService* _echoService;
Q_INVOKABLE void qtmvvm_init();
};
#endif // QUICKEVENTSERVICE_H

3
src/imports/imports.pro

@ -1,4 +1,5 @@
TEMPLATE = subdirs
SUBDIRS += \
mvvmquick
mvvmquick \
mvvmcore

33
src/imports/mvvmcore/mvvmcore.pro

@ -0,0 +1,33 @@
QT += core qml quick mvvmcore
CXX_MODULE = mvvmcore
TARGETPATH = de/skycoder42/qtmvvm/core
TARGET = declarative_mvvmcore
IMPORT_VERSION = 1.0
HEADERS += \
qtmvvmcore_plugin.h \
qqmlmvvmbinding.h
SOURCES += \
qtmvvmcore_plugin.cpp \
qqmlmvvmbinding.cpp
OTHER_FILES += qmldir
generate_qmltypes {
typeextra1.target = qmltypes
typeextra1.depends += export LD_LIBRARY_PATH := "$$shadowed($$dirname(_QMAKE_CONF_))/lib/:$(LD_LIBRARY_PATH)"
typeextra2.target = qmltypes
typeextra2.depends += export QML2_IMPORT_PATH := "$$shadowed($$dirname(_QMAKE_CONF_))/qml/"
QMAKE_EXTRA_TARGETS += typeextra1 typeextra2
}
load(qml_plugin)
generate_qmltypes {
qmltypes.depends = ../../../qml/$$TARGETPATH/$(TARGET) #overwrite the target deps
mfirst.target = all
mfirst.depends += qmltypes
QMAKE_EXTRA_TARGETS += mfirst
}

57
src/imports/mvvmcore/plugins.qmltypes

@ -0,0 +1,57 @@
import QtQuick.tooling 1.2
// This file describes the plugin-supplied types contained in the library.
// It is used for QML tooling purposes only.
//
// This file was auto-generated by:
// 'qmlplugindump -nonrelocatable de.skycoder42.qtmvvm.core 1.0'
Module {
dependencies: ["QtQuick 2.8"]
Component {
name: "QtMvvm::QQmlMvvmBinding"
prototype: "QObject"
exports: ["de.skycoder42.qtmvvm.core/QtMvvmBinding 1.0"]
exportMetaObjectRevisions: [0]
Enum {
name: "BindingDirection"
values: {
"SingleInit": 1,
"OneWayToView": 3,
"OneWayToViewModel": 4,
"TwoWay": 7
}
}
Property { name: "viewModel"; type: "QObject"; isPointer: true }
Property { name: "viewModelProperty"; type: "string" }
Property { name: "view"; type: "QObject"; isPointer: true }
Property { name: "viewProperty"; type: "string" }
Property { name: "type"; type: "BindingDirection" }
Signal {
name: "viewModelChanged"
Parameter { name: "viewModel"; type: "QObject"; isPointer: true }
}
Signal {
name: "viewModelPropertyChanged"
Parameter { name: "viewModelProperty"; type: "string" }
}
Signal {
name: "viewChanged"
Parameter { name: "view"; type: "QObject"; isPointer: true }
}
Signal {
name: "viewPropertyChanged"
Parameter { name: "viewProperty"; type: "string" }
}
Signal {
name: "typeChanged"
Parameter { name: "type"; type: "BindingDirection" }
}
Method {
name: "setType"
Parameter { name: "type"; type: "BindingDirection" }
}
Method { name: "unbind" }
Method { name: "isValid"; type: "bool" }
}
}

4
src/imports/mvvmcore/qmldir

@ -0,0 +1,4 @@
module de.skycoder42.qtmvvm.core
plugin declarative_mvvmcore
classname QtMvvmCoreDeclarativeModule
typeinfo plugins.qmltypes

67
src/imports/mvvmcore/qqmlmvvmbinding.cpp

@ -0,0 +1,67 @@
#include "qqmlmvvmbinding.h"
using namespace QtMvvm;
QQmlMvvmBinding::QQmlMvvmBinding(QObject *parent) :
QObject(parent),
QQmlParserStatus(),
_binding(),
_completed(false),
_viewModel(nullptr),
_viewModelProperty(),
_view(nullptr),
_viewProperty(),
_type(TwoWay)
{
connect(this, &QQmlMvvmBinding::viewModelChanged,
this, &QQmlMvvmBinding::resetBinding);
connect(this, &QQmlMvvmBinding::viewModelPropertyChanged,
this, &QQmlMvvmBinding::resetBinding);
connect(this, &QQmlMvvmBinding::viewChanged,
this, &QQmlMvvmBinding::resetBinding);
connect(this, &QQmlMvvmBinding::viewPropertyChanged,
this, &QQmlMvvmBinding::resetBinding);
connect(this, &QQmlMvvmBinding::typeChanged,
this, &QQmlMvvmBinding::resetBinding);
}
void QQmlMvvmBinding::classBegin() {}
void QQmlMvvmBinding::componentComplete()
{
_completed = true;
resetBinding();
}
QQmlMvvmBinding::BindingDirection QQmlMvvmBinding::type() const
{
return _type;
}
bool QQmlMvvmBinding::isValid() const
{
return _binding.isValid();
}
void QQmlMvvmBinding::setType(BindingDirection type)
{
if (_type == type)
return;
_type = type;
emit typeChanged(_type);
}
void QQmlMvvmBinding::unbind()
{
_binding.unbind();
}
void QQmlMvvmBinding::resetBinding()
{
if(!_completed || !_viewModel || !_view)
return;
_binding.unbind();
_binding = QtMvvm::bind(_viewModel, qUtf8Printable(_viewModelProperty),
_view, qUtf8Printable(_viewProperty),
static_cast<Binding::BindingDirection>(static_cast<int>(_type)));
}

67
src/imports/mvvmcore/qqmlmvvmbinding.h

@ -0,0 +1,67 @@
#ifndef QTMVVM_QQMLMVVMBINDING_H
#define QTMVVM_QQMLMVVMBINDING_H
#include <QtCore/QObject>
#include <QtQml/QQmlParserStatus>
#include <QtMvvmCore/Binding>
namespace QtMvvm {
class QQmlMvvmBinding : public QObject, public QQmlParserStatus
{
Q_OBJECT
Q_INTERFACES(QQmlParserStatus)
Q_PROPERTY(QObject* viewModel MEMBER _viewModel NOTIFY viewModelChanged)
Q_PROPERTY(QString viewModelProperty MEMBER _viewModelProperty NOTIFY viewModelPropertyChanged)
Q_PROPERTY(QObject* view MEMBER _view NOTIFY viewChanged)
Q_PROPERTY(QString viewProperty MEMBER _viewProperty NOTIFY viewPropertyChanged)
Q_PROPERTY(BindingDirection type READ type WRITE setType NOTIFY typeChanged) //MEMBER is broken for flags
public:
enum BindingDirectionFlag { //copy flags from binding
SingleInit = Binding::SingleInit,
OneWayToView = Binding::OneWayToView,
OneWayToViewModel = Binding::OneWayToViewModel,
TwoWay = Binding::TwoWay
};
Q_DECLARE_FLAGS(BindingDirection, BindingDirectionFlag)
Q_FLAG(BindingDirection)
explicit QQmlMvvmBinding(QObject *parent = nullptr);
void classBegin() override;
void componentComplete() override;
BindingDirection type() const;
Q_INVOKABLE bool isValid() const;
public Q_SLOTS:
void setType(BindingDirection type);
void unbind();
Q_SIGNALS:
void viewModelChanged(QObject* viewModel);
void viewModelPropertyChanged(QString viewModelProperty);
void viewChanged(QObject* view);
void viewPropertyChanged(QString viewProperty);
void typeChanged(BindingDirection type);
private Q_SLOTS:
void resetBinding();
private:
Binding _binding;
bool _completed;
QObject* _viewModel;
QString _viewModelProperty;
QObject* _view;
QString _viewProperty;
BindingDirection _type;
};
}
#endif // QTMVVM_QQMLMVVMBINDING_H

16
src/imports/mvvmcore/qtmvvmcore_plugin.cpp

@ -0,0 +1,16 @@
#include "qtmvvmcore_plugin.h"
#include <QtQml>
#include "qqmlmvvmbinding.h"
QtMvvmCoreDeclarativeModule::QtMvvmCoreDeclarativeModule(QObject *parent) :
QQmlExtensionPlugin(parent)
{}
void QtMvvmCoreDeclarativeModule::registerTypes(const char *uri)
{
Q_ASSERT(qstrcmp(uri, "de.skycoder42.qtmvvm.core") == 0);
qmlRegisterType<QtMvvm::QQmlMvvmBinding>(uri, 1, 0, "QtMvvmBinding");
}

16
src/imports/mvvmcore/qtmvvmcore_plugin.h

@ -0,0 +1,16 @@
#ifndef QTMVVMCORE_PLUGIN_H
#define QTMVVMCORE_PLUGIN_H
#include <QQmlExtensionPlugin>
class QtMvvmCoreDeclarativeModule : public QQmlExtensionPlugin
{
Q_OBJECT
Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
public:
QtMvvmCoreDeclarativeModule(QObject *parent = nullptr);
void registerTypes(const char *uri) override;
};
#endif // QTMVVMCORE_PLUGIN_H

5
src/imports/mvvmquick/mvvmquick.pro

@ -22,17 +22,16 @@ OTHER_FILES += qmldir
generate_qmltypes {
typeextra1.target = qmltypes
typeextra1.depends += export LD_LIBRARY_PATH := "$$shadowed($$dirname(_QMAKE_CONF_))/lib/:$(LD_LIBRARY_PATH)"
qmltypes.depends += typeextra1
typeextra2.target = qmltypes
typeextra2.depends += export QML2_IMPORT_PATH := "$$shadowed($$dirname(_QMAKE_CONF_))/qml/"
qmltypes.depends += typeextra2
QMAKE_EXTRA_TARGETS += typeextra1 typeextra2
}
load(qml_plugin)
generate_qmltypes {
qmltypes.depends = ../../../qml/$$TARGETPATH/$(TARGET) #overwrite the target deps
mfirst.target = all
mfirst.depends += qmltypes
QMAKE_EXTRA_TARGETS += mfirst

8
src/mvvmcore/binding.cpp

@ -100,6 +100,14 @@ bool Binding::isValid() const
return d;
}
void Binding::unbind()
{
if(d) {
d->deleteLater();
d.clear();
}
}
// ------------- Private Implementation -------------
Binding BindingPrivate::bind(QObject *viewModel, const QMetaProperty &viewModelProperty, QObject *view, const QMetaProperty &viewProperty, Binding::BindingDirection type, const QMetaMethod &viewModelChangeSignal, const QMetaMethod &viewChangeSignal)

1
src/mvvmcore/binding.h

@ -31,6 +31,7 @@ public:
~Binding();
bool isValid() const;
void unbind();
private:
QPointer<QObject> d;

10
src/mvvmquick/quickpresenter.cpp

@ -6,6 +6,11 @@ QuickPresenter::QuickPresenter(QObject *parent) :
IPresenter()
{}
void QuickPresenter::registerViewExplicitly(const QMetaObject *viewModelType, const QUrl &viewUrl)
{
}
void QuickPresenter::present(QtMvvm::ViewModel *viewModel, const QVariantHash &params, QPointer<QtMvvm::ViewModel> parent)
{
@ -15,3 +20,8 @@ void QuickPresenter::showDialog(const QtMvvm::MessageConfig &config, QtMvvm::Mes
{
}
QUrl QuickPresenter::findViewUrl(const QMetaObject *viewModelType)
{
}

26
src/mvvmquick/quickpresenter.h

@ -4,6 +4,7 @@
#include <QtCore/qobject.h>
#include <QtMvvmCore/ipresenter.h>
#include <QtMvvmCore/coreapp.h>
#include "QtMvvmQuick/qtmvvmquick_global.h"
@ -17,10 +18,35 @@ class Q_MVVMQUICK_EXPORT QuickPresenter : public QObject, public IPresenter
public:
explicit QuickPresenter(QObject *parent = nullptr);
template <typename TPresenter = QuickPresenter>
static void registerAsPresenter();
template <typename TViewModel>
static void registerViewExplicitly(const QUrl &viewUrl);
static void registerViewExplicitly(const QMetaObject *viewModelType, const QUrl &viewUrl);
void present(ViewModel *viewModel, const QVariantHash &params, QPointer<ViewModel> parent) override;
void showDialog(const MessageConfig &config, MessageResult *result) override;
protected:
virtual QUrl findViewUrl(const QMetaObject *viewModelType);
};
template<typename TPresenter>
void QuickPresenter::registerAsPresenter()
{
static_assert(std::is_base_of<QuickPresenter, TPresenter>::value, "TPresenter must inherit QtMvvm::QuickPresenter!");
CoreApp::setMainPresenter(new TPresenter());
}
template<typename TViewModel>
void QuickPresenter::registerViewExplicitly(const QUrl &viewUrl)
{
static_assert(std::is_base_of<ViewModel, TViewModel>::value, "TViewModel must inherit ViewModel!");
registerViewExplicitly(&TViewModel::staticMetaObject, viewUrl);
}
}
#endif // QTMVVM_QUICKPRESENTER_H

Loading…
Cancel
Save