Skycoder42
7 years ago
19 changed files with 423 additions and 11 deletions
@ -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 |
|||
} |
|||
} |
|||
|
@ -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); |
|||
} |
@ -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
|
@ -1,4 +1,5 @@ |
|||
TEMPLATE = subdirs |
|||
|
|||
SUBDIRS += \ |
|||
mvvmquick |
|||
mvvmquick \ |
|||
mvvmcore |
|||
|
@ -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 |
|||
} |
@ -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" } |
|||
} |
|||
} |
@ -0,0 +1,4 @@ |
|||
module de.skycoder42.qtmvvm.core |
|||
plugin declarative_mvvmcore |
|||
classname QtMvvmCoreDeclarativeModule |
|||
typeinfo plugins.qmltypes |
@ -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))); |
|||
} |
@ -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
|
@ -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"); |
|||
} |
@ -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
|
Loading…
Reference in new issue