From 7dbe411c66e32a004e6f0c4ccb1307c63b5ee616 Mon Sep 17 00:00:00 2001 From: "a.yankovich" Date: Tue, 19 Nov 2019 15:20:38 +0300 Subject: [PATCH] added service --- .gitignore | 1 + BasePopUp.qml | 71 +++++++++++++++++++++++++++++++ NotificationForm.qml | 84 +++++++++++++++++++++++++++++++++++++ NotificationServiceView.qml | 30 +++++++++++++ QML.qrc | 7 ++++ QmlNotify.pri | 23 ++++++++++ QmlNotify.pro | 46 ++++++++++++++++++++ README.md | 25 +++++++++++ notificationdata.cpp | 40 ++++++++++++++++++ notificationdata.h | 46 ++++++++++++++++++++ notificationservice.cpp | 41 ++++++++++++++++++ notificationservice.h | 62 +++++++++++++++++++++++++++ notifyservice_global.h | 13 ++++++ qmlnotifyservice.cpp | 18 ++++++++ qmlnotifyservice.h | 30 +++++++++++++ 15 files changed, 537 insertions(+) create mode 100644 BasePopUp.qml create mode 100644 NotificationForm.qml create mode 100644 NotificationServiceView.qml create mode 100644 QML.qrc create mode 100644 QmlNotify.pri create mode 100644 QmlNotify.pro create mode 100644 notificationdata.cpp create mode 100644 notificationdata.h create mode 100644 notificationservice.cpp create mode 100644 notificationservice.h create mode 100644 notifyservice_global.h create mode 100644 qmlnotifyservice.cpp create mode 100644 qmlnotifyservice.h diff --git a/.gitignore b/.gitignore index 5291a38..341bc81 100644 --- a/.gitignore +++ b/.gitignore @@ -28,6 +28,7 @@ ui_*.h *.jsc Makefile* *build-* +build/ # Qt unit tests target_wrapper.* diff --git a/BasePopUp.qml b/BasePopUp.qml new file mode 100644 index 0000000..62af5a8 --- /dev/null +++ b/BasePopUp.qml @@ -0,0 +1,71 @@ +import QtQuick 2.11 +import QtQuick.Controls 2.3 +import QtQuick.Controls.Material 2.0 +import QtQuick.Layouts 1.3 +import QtGraphicalEffects 1.12 + +Dialog { + id : basePopup + width: 200 + height: 100 + x: 0 + y: 0 + + transformOrigin: Item.Center + + property bool autoClose: true + property bool clickClose: true + + property int closeInterval: 15000; + property int margin : 0 + property color backgroundColor: Material.background + + background: Item { + id: bacground + + Rectangle{ + anchors.fill: parent + id: backCorner + color: backgroundColor + radius: metrix.mm + } + + DropShadow { + anchors.fill: parent + source: backCorner + color: "#80000000" + verticalOffset: 10 + samples: 30 + } + } + + function _show() { + + if (autoClose) { + timerAutoClose.start(); + } + + open(); + } + + Timer { + id: timerAutoClose; + running: false; + repeat: false; + interval: closeInterval; + + onTriggered: { + close(); + } + } + + onClosed: { + if (autoClose) + opacity = 0; + } + + closePolicy: (!clickClose || autoClose)? Popup.NoAutoClose: Popup.CloseOnReleaseOutside + + onRejected: close() + +} diff --git a/NotificationForm.qml b/NotificationForm.qml new file mode 100644 index 0000000..72445bc --- /dev/null +++ b/NotificationForm.qml @@ -0,0 +1,84 @@ +import QtQuick 2.11 +import QtQuick.Controls 2.3 +import QtQuick.Controls.Material 2.0 +import QtQuick.Layouts 1.3 + +BasePopUp { + id : popup + + property string text: qsTr("Message") + property string img: "" + property string titleText: qsTr("Message") + property int type: 0 + + function _getBackGraundColor(type) { + switch(type) { + + case 1: return "#FFC107" + case 2: return "#FF5722" + + } + + return Material.background + } + + autoClose: true; + closeInterval: 5000 + + margins: 0 + margin: 0 + spacing: 0 + + + backgroundColor: _getBackGraundColor(type); + + Page { + id: page + anchors.fill: parent + spacing: 0 + + background: Rectangle { + color: "#00000000" + } + + header: Label { + text: titleText + horizontalAlignment: Text.AlignHCenter + } + + contentItem: + RowLayout { + id: rowLayout + spacing: 5 + clip: true + + Rectangle { + color: "#00000000" + Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter + Layout.preferredWidth: rowLayout.height; + Layout.preferredHeight: rowLayout.height; + + Image { + id: image + fillMode: Image.PreserveAspectCrop + clip: true + anchors.fill: parent; + source: img + } + } + + + Label { + id: message + text: popup.text + Layout.alignment: Qt.AlignRight | Qt.AlignVCenter + Layout.fillHeight: true; + Layout.fillWidth: true; + clip: true + wrapMode: Text.WordWrap + verticalAlignment: Text.AlignVCenter + horizontalAlignment: Text.AlignHCenter + } + } + } +} diff --git a/NotificationServiceView.qml b/NotificationServiceView.qml new file mode 100644 index 0000000..fb6d18e --- /dev/null +++ b/NotificationServiceView.qml @@ -0,0 +1,30 @@ +import QtQuick 2.12 +import QtQuick.Controls.Material 2.12 +import QtQuick.Controls 2.12 +import QtQuick.Layouts 1.3 +import QtGraphicalEffects 1.12 + +Item { + readonly property var model: notificationService; + readonly property var msg: model.notify + readonly property var history: model.history + + + NotificationForm { + id: notyfyView + titleText : msg.title; + text: (msg)? msg.text: ""; + img: (msg)? msg.img: ""; + type: (msg)? msg.type: 0; + + x: parent.width - width - margin; + y: margin; + + width: 6 * metrix.controlPtMaterial; + height: width * 0.5 + } + + onMsgChanged: { + notyfyView._show(); + } +} diff --git a/QML.qrc b/QML.qrc new file mode 100644 index 0000000..d760c9a --- /dev/null +++ b/QML.qrc @@ -0,0 +1,7 @@ + + + NotificationServiceView.qml + NotificationForm.qml + BasePopUp.qml + + diff --git a/QmlNotify.pri b/QmlNotify.pri new file mode 100644 index 0000000..866300e --- /dev/null +++ b/QmlNotify.pri @@ -0,0 +1,23 @@ +# +# Copyright (C) 2018 - 2019 QuasarApp. +# Distributed under the lgplv3 software license, see the accompanying +# Everyone is permitted to copy and distribute verbatim copies +# of this license document, but changing it is not allowed. +# + +!isEmpty(QMLNOTYFYSERICE_LIB):error("QmlNotify.pri already included") +QMLNOTYFYSERICE_LIB = 1 + +#DEPENDS +CONFIG(release, debug|release): { + QMLNOTYFYSERICE_LIB_OUTPUT_DIR="$$PWD/build/release" +} else { + QMLNOTYFYSERICE_LIB_OUTPUT_DIR="$$PWD/build/debug" +} + +LIBS += -L$$QMLNOTYFYSERICE_LIB_OUTPUT_DIR -lQmlNotyfyService + +INCLUDEPATH += "$$PWD/" + + + diff --git a/QmlNotify.pro b/QmlNotify.pro new file mode 100644 index 0000000..62c458d --- /dev/null +++ b/QmlNotify.pro @@ -0,0 +1,46 @@ +# +# Copyright (C) 2018 - 2019 QuasarApp. +# Distributed under the lgplv3 software license, see the accompanying +# Everyone is permitted to copy and distribute verbatim copies +# of this license document, but changing it is not allowed. +# + + +CONFIG += c++14 +TARGET = QmlNotyfyService +TEMPLATE = lib +QT += quick + +DEFINES += QMLNOTYFYSERICE_LIBRARY + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +CONFIG(release, debug|release): { + DESTDIR = $$PWD/build/release + +} else { + DESTDIR = $$PWD/build/debug +} + +RESOURCES += \ + QML.qrc + +HEADERS += \ + notificationdata.h \ + notificationservice.h \ + notifyservice_global.h \ + qmlnotifyservice.h + +SOURCES += \ + notificationdata.cpp \ + notificationservice.cpp \ + qmlnotifyservice.cpp diff --git a/README.md b/README.md index f68a8c0..b61dde6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,27 @@ # SimpleQmlNotify Simple Qml notification service for qml applications. + +## Use + +### CPP +``` cpp + #include + + int main() { + QmlNotificationService::init(); + auto service = QmlNotificationService::NotificationService::getService(); + service->setNotify("title", "text", "UrlOfImage", NotificationData::Normal); + } + + +``` + +### QML + +``` qml + + NotificationServiceView { + anchors.fill: parent; + } + +``` diff --git a/notificationdata.cpp b/notificationdata.cpp new file mode 100644 index 0000000..b9aeb01 --- /dev/null +++ b/notificationdata.cpp @@ -0,0 +1,40 @@ +#include "notificationdata.h" +namespace QmlNotificationService { + +NotificationData::NotificationData(const QString &title, + const QString &text, + const QString &img, Type type) { + + _text = text; + _title = title; + _img = img; + _type = type; +} + +QString NotificationData::text() const { + return _text; +} + +QString NotificationData::img() const { + return _img; +} + +QString NotificationData::title() const { + return _title; +} + +bool NotificationData::operator ==(const NotificationData &righ) { + return _title == righ._title && + _text == righ._text && + _img == righ._img && + _type == righ._type; +} + +bool NotificationData::operator !=(const NotificationData &righ) { + return !operator==(righ); +} + +int NotificationData::type() const { + return _type; +} +} diff --git a/notificationdata.h b/notificationdata.h new file mode 100644 index 0000000..32797d6 --- /dev/null +++ b/notificationdata.h @@ -0,0 +1,46 @@ +#ifndef NOTIFICATIONDATA_H +#define NOTIFICATIONDATA_H +#include +#include "notifyservice_global.h" +namespace QmlNotificationService { + +/** + * @brief The NotificationData class view data for NotificationServiceView + */ +class NOTIFYSERVICESHARED_EXPORT NotificationData +{ + Q_GADGET + Q_PROPERTY(QString text READ text) + Q_PROPERTY(QString img READ img) + Q_PROPERTY(QString title READ title) + Q_PROPERTY(int type READ type) + + QString _text; + QString _img; + QString _title; + int _type; + +public: + + enum Type { + Normal, + Warning = 1, + Error = 2, + }; + + explicit NotificationData(const QString& title = "", + const QString& text = "", + const QString& img = "", + Type type = Type::Normal); + + Q_INVOKABLE QString text() const; + Q_INVOKABLE QString img() const; + Q_INVOKABLE QString title() const; + Q_INVOKABLE int type() const; + + bool operator ==(const NotificationData &righ); + bool operator !=(const NotificationData &righ); + +}; +} +#endif // NOTIFICATIONDATA_H diff --git a/notificationservice.cpp b/notificationservice.cpp new file mode 100644 index 0000000..e723972 --- /dev/null +++ b/notificationservice.cpp @@ -0,0 +1,41 @@ +#include "notificationservice.h" +namespace QmlNotificationService { + +NotificationService::NotificationService(QObject * ptr): QObject (ptr) { + qRegisterMetaType("NotificationData"); + qRegisterMetaType> ("QList"); + +} + +NotificationData NotificationService::notify() const { + return _notify; +} + +void NotificationService::setNotify(const NotificationData& notify) { + if (_notify != notify) + _history.push_back(_notify); + + _notify = notify; + + emit notifyChanged(); +} + +void NotificationService::setNotify(const QString &title, + const QString &text, + const QString &img, + int type) { + + setNotify(NotificationData(title, text, img, + static_cast(type))); +} + +NotificationService *NotificationService::getService() { + static auto service = new NotificationService; + return service; +} + +const QList &NotificationService::history() const { + return _history; +} + +} diff --git a/notificationservice.h b/notificationservice.h new file mode 100644 index 0000000..b3e6bc1 --- /dev/null +++ b/notificationservice.h @@ -0,0 +1,62 @@ +#ifndef NOTIFICATIONSERVICE_H +#define NOTIFICATIONSERVICE_H + +#include "notificationdata.h" + +#include + +namespace QmlNotificationService { + +/** + * @brief The NotificationService class + */ +class NOTIFYSERVICESHARED_EXPORT NotificationService: public QObject +{ + Q_OBJECT + + Q_PROPERTY(NotificationData notify READ notify NOTIFY notifyChanged) + Q_PROPERTY(QList history READ history NOTIFY notifyChanged) + +private: + explicit NotificationService(QObject *ptr = nullptr); + + NotificationData _notify; + QList _history; + +public: + /** + * @brief notify + * @return notyfyData for qml + */ + NotificationData notify() const; + + /** + * @brief setNotify - add new message for application + * @param notify - message data + */ + void setNotify(const NotificationData ¬ify); + + Q_INVOKABLE void setNotify(const QString& title = "", + const QString& text = "", + const QString& img = "", + int type = NotificationData::Normal); + + /** + * @brief getService + * @return pointer t oservice + */ + static NotificationService* getService(); + + /** + * @brief history + * @return list of all notify + */ + const QList & history() const; + +signals: + void notifyChanged(); +}; + +} + +#endif // NOTIFICATIONSERVICE_H diff --git a/notifyservice_global.h b/notifyservice_global.h new file mode 100644 index 0000000..ca3ff7b --- /dev/null +++ b/notifyservice_global.h @@ -0,0 +1,13 @@ +#ifndef NOTIFYSERVICE_GLOBAL_H +#define NOTIFYSERVICE_GLOBAL_H + +#include + +#if defined(QMLNOTYFYSERICE_LIBRARY) +# define NOTIFYSERVICESHARED_EXPORT Q_DECL_EXPORT +#else +# define NOTIFYSERVICESHARED_EXPORT Q_DECL_IMPORT +#endif + + +#endif // NOTIFYSERVICE_GLOBAL_H diff --git a/qmlnotifyservice.cpp b/qmlnotifyservice.cpp new file mode 100644 index 0000000..c82697e --- /dev/null +++ b/qmlnotifyservice.cpp @@ -0,0 +1,18 @@ +#include "notificationservice.h" +#include "qmlnotifyservice.h" + +#include +#include + +namespace QmlNotificationService { + +bool init(QQmlApplicationEngine *engine) { + auto root = engine->rootContext(); + if (!root) + return false; + + root->setContextProperty("notificationService", NotificationService::getService()); + return true; +} +} + diff --git a/qmlnotifyservice.h b/qmlnotifyservice.h new file mode 100644 index 0000000..a7cbcce --- /dev/null +++ b/qmlnotifyservice.h @@ -0,0 +1,30 @@ +#ifndef QMLNOTIFYSERVICE_H +#define QMLNOTIFYSERVICE_H +#include "notifyservice_global.h" + +#include "notificationservice.h" + +class QQmlApplicationEngine; + +/** + * Simple notify service for qml. + * Use : + * #include + * + * QmlNotificationService::init(); + * auto service = QmlNotificationService::NotificationService::getService() + * service->setNotify("title", "text", "UrlOfImage", NotificationData::Normal) + * + * + * in qml : + * + * NotificationServiceView { + anchors.fill: parent; + } + */ +namespace QmlNotificationService { + bool NOTIFYSERVICESHARED_EXPORT init(QQmlApplicationEngine *engine); +} + + +#endif // QMLNOTIFYSERVICE_H