From 081ce57f8dc084f636d2f09080a1bf2099080dd0 Mon Sep 17 00:00:00 2001 From: Skycoder42 Date: Wed, 14 Feb 2018 18:18:50 +0100 Subject: [PATCH] implemented core message qml bindings --- examples/mvvmquick/SampleQuick/main.qml | 6 +- src/imports/mvvmcore/mvvmcore.pro | 6 +- src/imports/mvvmcore/plugins.qmltypes | 282 +++++++++++++++++++++ src/imports/mvvmcore/qqmlmvvmmessage.cpp | 107 ++++++++ src/imports/mvvmcore/qqmlmvvmmessage.h | 81 ++++++ src/imports/mvvmcore/qtmvvmcore_plugin.cpp | 7 + src/imports/mvvmcore/qtmvvmcore_plugin.h | 2 +- src/imports/mvvmquick/qtmvvmquick_plugin.h | 2 +- src/mvvmquick/quickpresenter.cpp | 4 +- 9 files changed, 486 insertions(+), 11 deletions(-) create mode 100644 src/imports/mvvmcore/qqmlmvvmmessage.cpp create mode 100644 src/imports/mvvmcore/qqmlmvvmmessage.h diff --git a/examples/mvvmquick/SampleQuick/main.qml b/examples/mvvmquick/SampleQuick/main.qml index 84a4a20..0333aee 100644 --- a/examples/mvvmquick/SampleQuick/main.qml +++ b/examples/mvvmquick/SampleQuick/main.qml @@ -3,9 +3,5 @@ import de.skycoder42.qtmvvm.core 1.0 import de.skycoder42.qtmvvm.quick 1.0 App { - title: qsTr("Hello World") - - QtMvvmBinding { - id: binding - } + title: qsTr("QtMvvm Quick Sample") } diff --git a/src/imports/mvvmcore/mvvmcore.pro b/src/imports/mvvmcore/mvvmcore.pro index 10176d1..5749c46 100644 --- a/src/imports/mvvmcore/mvvmcore.pro +++ b/src/imports/mvvmcore/mvvmcore.pro @@ -6,11 +6,13 @@ IMPORT_VERSION = 1.0 HEADERS += \ qtmvvmcore_plugin.h \ - qqmlmvvmbinding.h + qqmlmvvmbinding.h \ + qqmlmvvmmessage.h SOURCES += \ qtmvvmcore_plugin.cpp \ - qqmlmvvmbinding.cpp + qqmlmvvmbinding.cpp \ + qqmlmvvmmessage.cpp OTHER_FILES += qmldir diff --git a/src/imports/mvvmcore/plugins.qmltypes b/src/imports/mvvmcore/plugins.qmltypes index 2a6176d..826a99d 100644 --- a/src/imports/mvvmcore/plugins.qmltypes +++ b/src/imports/mvvmcore/plugins.qmltypes @@ -54,4 +54,286 @@ Module { Method { name: "unbind" } Method { name: "isValid"; type: "bool" } } + Component { + name: "QtMvvm::QQmlMvvmMessage" + prototype: "QObject" + exports: ["de.skycoder42.qtmvvm.core/QtMvvmMessage 1.0"] + isCreatable: false + isSingleton: true + exportMetaObjectRevisions: [0] + Method { + name: "information" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "okText"; type: "string" } + } + Method { + name: "information" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + } + Method { + name: "information" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + } + Method { + name: "question" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "yesText"; type: "string" } + Parameter { name: "noText"; type: "string" } + } + Method { + name: "question" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "yesText"; type: "string" } + } + Method { + name: "question" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + } + Method { + name: "question" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + } + Method { + name: "warning" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "okText"; type: "string" } + } + Method { + name: "warning" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + } + Method { + name: "warning" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + } + Method { + name: "critical" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "okText"; type: "string" } + } + Method { + name: "critical" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "onResult"; type: "QJSValue" } + } + Method { + name: "critical" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + } + Method { + name: "about" + Parameter { name: "description"; type: "string" } + Parameter { name: "websiteUrl"; type: "QUrl" } + Parameter { name: "licenseName"; type: "string" } + Parameter { name: "licenseUrl"; type: "QUrl" } + Parameter { name: "companyName"; type: "string" } + Parameter { name: "addQtVersion"; type: "bool" } + Parameter { name: "extraTopInfos"; type: "QStringList" } + Parameter { name: "extraBottomInfos"; type: "string" } + } + Method { + name: "about" + Parameter { name: "description"; type: "string" } + Parameter { name: "websiteUrl"; type: "QUrl" } + Parameter { name: "licenseName"; type: "string" } + Parameter { name: "licenseUrl"; type: "QUrl" } + Parameter { name: "companyName"; type: "string" } + Parameter { name: "addQtVersion"; type: "bool" } + Parameter { name: "extraTopInfos"; type: "QStringList" } + } + Method { + name: "about" + Parameter { name: "description"; type: "string" } + Parameter { name: "websiteUrl"; type: "QUrl" } + Parameter { name: "licenseName"; type: "string" } + Parameter { name: "licenseUrl"; type: "QUrl" } + Parameter { name: "companyName"; type: "string" } + Parameter { name: "addQtVersion"; type: "bool" } + } + Method { + name: "about" + Parameter { name: "description"; type: "string" } + Parameter { name: "websiteUrl"; type: "QUrl" } + Parameter { name: "licenseName"; type: "string" } + Parameter { name: "licenseUrl"; type: "QUrl" } + Parameter { name: "companyName"; type: "string" } + } + Method { + name: "about" + Parameter { name: "description"; type: "string" } + Parameter { name: "websiteUrl"; type: "QUrl" } + Parameter { name: "licenseName"; type: "string" } + Parameter { name: "licenseUrl"; type: "QUrl" } + } + Method { + name: "about" + Parameter { name: "description"; type: "string" } + Parameter { name: "websiteUrl"; type: "QUrl" } + Parameter { name: "licenseName"; type: "string" } + } + Method { + name: "about" + Parameter { name: "description"; type: "string" } + Parameter { name: "websiteUrl"; type: "QUrl" } + } + Method { + name: "about" + Parameter { name: "description"; type: "string" } + } + Method { + name: "getInput" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "inputType"; type: "const char"; isPointer: true } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "defaultValue"; type: "QVariant" } + Parameter { name: "viewProperties"; type: "QVariantMap" } + Parameter { name: "okText"; type: "string" } + Parameter { name: "cancelText"; type: "string" } + } + Method { + name: "getInput" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "inputType"; type: "const char"; isPointer: true } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "defaultValue"; type: "QVariant" } + Parameter { name: "viewProperties"; type: "QVariantMap" } + Parameter { name: "okText"; type: "string" } + } + Method { + name: "getInput" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "inputType"; type: "const char"; isPointer: true } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "defaultValue"; type: "QVariant" } + Parameter { name: "viewProperties"; type: "QVariantMap" } + } + Method { + name: "getInput" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "inputType"; type: "const char"; isPointer: true } + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "defaultValue"; type: "QVariant" } + } + Method { + name: "getInput" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "inputType"; type: "const char"; isPointer: true } + Parameter { name: "onResult"; type: "QJSValue" } + } + Method { + name: "getInput" + Parameter { name: "title"; type: "string" } + Parameter { name: "text"; type: "string" } + Parameter { name: "inputType"; type: "const char"; isPointer: true } + } + Method { + name: "getExistingDirectory" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + Parameter { name: "dir"; type: "QUrl" } + } + Method { + name: "getExistingDirectory" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + } + Method { + name: "getExistingDirectory" + Parameter { name: "onResult"; type: "QJSValue" } + } + Method { name: "getExistingDirectory" } + Method { + name: "getOpenFile" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + Parameter { name: "supportedMimeTypes"; type: "QStringList" } + Parameter { name: "dir"; type: "QUrl" } + } + Method { + name: "getOpenFile" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + Parameter { name: "supportedMimeTypes"; type: "QStringList" } + } + Method { + name: "getOpenFile" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + } + Method { + name: "getOpenFile" + Parameter { name: "onResult"; type: "QJSValue" } + } + Method { name: "getOpenFile" } + Method { + name: "getOpenFiles" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + Parameter { name: "supportedMimeTypes"; type: "QStringList" } + Parameter { name: "dir"; type: "QUrl" } + } + Method { + name: "getOpenFiles" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + Parameter { name: "supportedMimeTypes"; type: "QStringList" } + } + Method { + name: "getOpenFiles" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + } + Method { + name: "getOpenFiles" + Parameter { name: "onResult"; type: "QJSValue" } + } + Method { + name: "getSaveFile" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + Parameter { name: "supportedMimeTypes"; type: "QStringList" } + Parameter { name: "dir"; type: "QUrl" } + } + Method { + name: "getSaveFile" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + Parameter { name: "supportedMimeTypes"; type: "QStringList" } + } + Method { + name: "getSaveFile" + Parameter { name: "onResult"; type: "QJSValue" } + Parameter { name: "title"; type: "string" } + } + Method { + name: "getSaveFile" + Parameter { name: "onResult"; type: "QJSValue" } + } + } } diff --git a/src/imports/mvvmcore/qqmlmvvmmessage.cpp b/src/imports/mvvmcore/qqmlmvvmmessage.cpp new file mode 100644 index 0000000..c72bc8e --- /dev/null +++ b/src/imports/mvvmcore/qqmlmvvmmessage.cpp @@ -0,0 +1,107 @@ +#include "qqmlmvvmmessage.h" +using namespace QtMvvm; + +QQmlMvvmMessage::QQmlMvvmMessage(QJSEngine *engine, QObject *parent) : + QObject(parent), + _engine(engine) +{} + +void QQmlMvvmMessage::information(const QString &title, const QString &text, const QJSValue &onResult, const QString &okText) +{ + if(onResult.isCallable()) { + QtMvvm::information(title, text, this, [onResult](){ + QJSValue(onResult).call(); + }, okText); + } else + QtMvvm::information(title, text, okText); +} + +void QQmlMvvmMessage::question(const QString &title, const QString &text, const QJSValue &onResult, const QString &yesText, const QString &noText) +{ + if(onResult.isCallable()) { + QtMvvm::question(title, text, this, [onResult](bool res){ + QJSValue(onResult).call({res}); + }, yesText, noText); + } else + QtMvvm::question(title, text, yesText, noText); +} + +void QQmlMvvmMessage::warning(const QString &title, const QString &text, const QJSValue &onResult, const QString &okText) +{ + if(onResult.isCallable()) { + QtMvvm::warning(title, text, this, [onResult](){ + QJSValue(onResult).call(); + }, okText); + } else + QtMvvm::warning(title, text, okText); +} + +void QQmlMvvmMessage::critical(const QString &title, const QString &text, const QJSValue &onResult, const QString &okText) +{ + if(onResult.isCallable()) { + QtMvvm::critical(title, text, this, [onResult](){ + QJSValue(onResult).call(); + }, okText); + } else + QtMvvm::critical(title, text, okText); +} + +void QQmlMvvmMessage::about(const QString &description, const QUrl &websiteUrl, const QString &licenseName, const QUrl &licenseUrl, const QString &companyName, bool addQtVersion, const QStringList &extraTopInfos, const QString &extraBottomInfos) +{ + QtMvvm::about(description, websiteUrl, licenseName, licenseUrl, companyName, addQtVersion, extraTopInfos, extraBottomInfos); +} + +void QQmlMvvmMessage::getInput(const QString &title, const QString &text, const char *inputType, const QJSValue &onResult, const QVariant &defaultValue, const QVariantMap &viewProperties, const QString &okText, const QString &cancelText) +{ + if(onResult.isCallable()) { + auto engine = _engine; + QtMvvm::getInput(title, text, inputType, this, [engine, onResult](QVariant result){ + QJSValue(onResult).call({engine->toScriptValue(result)}); + }, defaultValue, viewProperties, okText, cancelText); + } else + QtMvvm::getInput(title, text, inputType, defaultValue, viewProperties, okText, cancelText); +} + +void QQmlMvvmMessage::getExistingDirectory(const QJSValue &onResult, const QString &title, const QUrl &dir) +{ + if(onResult.isCallable()) { + auto engine = _engine; + QtMvvm::getExistingDirectory(this, [engine, onResult](QUrl url){ + QJSValue(onResult).call({engine->toScriptValue(url)}); + }, title, dir); + } else + QtMvvm::getExistingDirectory(title, dir); +} + +void QQmlMvvmMessage::getOpenFile(const QJSValue &onResult, const QString &title, const QStringList &supportedMimeTypes, const QUrl &dir) +{ + if(onResult.isCallable()) { + auto engine = _engine; + QtMvvm::getOpenFile(this, [engine, onResult](QUrl url){ + QJSValue(onResult).call({engine->toScriptValue(url)}); + }, title, supportedMimeTypes, dir); + } else + QtMvvm::getOpenFile(title, supportedMimeTypes, dir); +} + +void QQmlMvvmMessage::getOpenFiles(const QJSValue &onResult, const QString &title, const QStringList &supportedMimeTypes, const QUrl &dir) +{ + if(onResult.isCallable()) { + auto engine = _engine; + QtMvvm::getOpenFiles(this, [engine, onResult](QList url){ + QJSValue(onResult).call({engine->toScriptValue(url)}); + }, title, supportedMimeTypes, dir); + } else + QtMvvm::getOpenFiles(title, supportedMimeTypes, dir); +} + +void QQmlMvvmMessage::getSaveFile(const QJSValue &onResult, const QString &title, const QStringList &supportedMimeTypes, const QUrl &dir) +{ + if(onResult.isCallable()) { + auto engine = _engine; + QtMvvm::getSaveFile(this, [engine, onResult](QUrl url){ + QJSValue(onResult).call({engine->toScriptValue(url)}); + }, title, supportedMimeTypes, dir); + } else + QtMvvm::getSaveFile(title, supportedMimeTypes, dir); +} diff --git a/src/imports/mvvmcore/qqmlmvvmmessage.h b/src/imports/mvvmcore/qqmlmvvmmessage.h new file mode 100644 index 0000000..4baeeaa --- /dev/null +++ b/src/imports/mvvmcore/qqmlmvvmmessage.h @@ -0,0 +1,81 @@ +#ifndef QTMVVM_QQMLMVVMMESSAGE_H +#define QTMVVM_QQMLMVVMMESSAGE_H + +#include + +#include +#include +#include + +#include + +namespace QtMvvm { + +class QQmlMvvmMessage : public QObject +{ + Q_OBJECT + +public: + explicit QQmlMvvmMessage(QJSEngine *engine, QObject *parent = nullptr); + +public Q_SLOTS: + void information(const QString &title, + const QString &text, + const QJSValue &onResult = {}, + const QString &okText = {}); + void question(const QString &title, + const QString &text, + const QJSValue &onResult = {}, + const QString &yesText = {}, + const QString &noText = {}); + void warning(const QString &title, + const QString &text, + const QJSValue &onResult = {}, + const QString &okText = {}); + void critical(const QString &title, + const QString &text, + const QJSValue &onResult = {}, + const QString &okText = {}); + + void about(const QString &description, + const QUrl &websiteUrl = QUrl(), + const QString &licenseName = QString(), + const QUrl &licenseUrl = QUrl(), + const QString &companyName = QString(), + bool addQtVersion = true, + const QStringList &extraTopInfos = QStringList(), + const QString &extraBottomInfos = QString()); + + void getInput(const QString &title, + const QString &text, + const char *inputType, + const QJSValue &onResult = {}, + const QVariant &defaultValue = {}, + const QVariantMap &viewProperties = {}, + const QString &okText = {}, + const QString &cancelText = {}); + + void getExistingDirectory(const QJSValue &onResult = {}, + const QString &title = {}, + const QUrl &dir = {}); + void getOpenFile(const QJSValue &onResult = {}, + const QString &title = {}, + const QStringList &supportedMimeTypes = {}, + const QUrl &dir = {}); + + void getOpenFiles(const QJSValue &onResult, + const QString &title = {}, + const QStringList &supportedMimeTypes = {}, + const QUrl &dir = {}); + void getSaveFile(const QJSValue &onResult, + const QString &title = {}, + const QStringList &supportedMimeTypes = {}, + const QUrl &dir = {}); + +private: + QJSEngine *_engine; +}; + +} + +#endif // QTMVVM_QQMLMVVMMESSAGE_H diff --git a/src/imports/mvvmcore/qtmvvmcore_plugin.cpp b/src/imports/mvvmcore/qtmvvmcore_plugin.cpp index 1563389..9a37b47 100644 --- a/src/imports/mvvmcore/qtmvvmcore_plugin.cpp +++ b/src/imports/mvvmcore/qtmvvmcore_plugin.cpp @@ -3,6 +3,12 @@ #include #include "qqmlmvvmbinding.h" +#include "qqmlmvvmmessage.h" + +static QObject *createMessageSingleton(QQmlEngine *qmlEngine, QJSEngine *jsEngine) +{ + return new QtMvvm::QQmlMvvmMessage(jsEngine, qmlEngine); +} QtMvvmCoreDeclarativeModule::QtMvvmCoreDeclarativeModule(QObject *parent) : QQmlExtensionPlugin(parent) @@ -13,4 +19,5 @@ void QtMvvmCoreDeclarativeModule::registerTypes(const char *uri) Q_ASSERT(qstrcmp(uri, "de.skycoder42.qtmvvm.core") == 0); qmlRegisterType(uri, 1, 0, "QtMvvmBinding"); + qmlRegisterSingletonType(uri, 1, 0, "QtMvvmMessage", createMessageSingleton); } diff --git a/src/imports/mvvmcore/qtmvvmcore_plugin.h b/src/imports/mvvmcore/qtmvvmcore_plugin.h index 69c7379..ec49c51 100644 --- a/src/imports/mvvmcore/qtmvvmcore_plugin.h +++ b/src/imports/mvvmcore/qtmvvmcore_plugin.h @@ -1,7 +1,7 @@ #ifndef QTMVVMCORE_PLUGIN_H #define QTMVVMCORE_PLUGIN_H -#include +#include class QtMvvmCoreDeclarativeModule : public QQmlExtensionPlugin { diff --git a/src/imports/mvvmquick/qtmvvmquick_plugin.h b/src/imports/mvvmquick/qtmvvmquick_plugin.h index a0fde9a..cf044af 100644 --- a/src/imports/mvvmquick/qtmvvmquick_plugin.h +++ b/src/imports/mvvmquick/qtmvvmquick_plugin.h @@ -1,7 +1,7 @@ #ifndef QTMVVMQUICK_PLUGIN_H #define QTMVVMQUICK_PLUGIN_H -#include +#include class QtMvvmQuickDeclarativeModule : public QQmlExtensionPlugin { diff --git a/src/mvvmquick/quickpresenter.cpp b/src/mvvmquick/quickpresenter.cpp index 6594279..59e5bc9 100644 --- a/src/mvvmquick/quickpresenter.cpp +++ b/src/mvvmquick/quickpresenter.cpp @@ -13,12 +13,12 @@ void QuickPresenter::registerViewExplicitly(const QMetaObject *viewModelType, co void QuickPresenter::present(QtMvvm::ViewModel *viewModel, const QVariantHash ¶ms, QPointer parent) { - + qDebug(Q_FUNC_INFO); } void QuickPresenter::showDialog(const QtMvvm::MessageConfig &config, QtMvvm::MessageResult *result) { - + qDebug(Q_FUNC_INFO); } QUrl QuickPresenter::findViewUrl(const QMetaObject *viewModelType)