added service

This commit is contained in:
Andrei Yankovich 2019-11-19 15:20:38 +03:00
parent a9bc85e6f7
commit 7dbe411c66
15 changed files with 537 additions and 0 deletions

1
.gitignore vendored
View File

@ -28,6 +28,7 @@ ui_*.h
*.jsc
Makefile*
*build-*
build/
# Qt unit tests
target_wrapper.*

71
BasePopUp.qml Normal file
View File

@ -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()
}

84
NotificationForm.qml Normal file
View File

@ -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
}
}
}
}

View File

@ -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();
}
}

7
QML.qrc Normal file
View File

@ -0,0 +1,7 @@
<RCC>
<qresource prefix="/QmlNotyfy">
<file>NotificationServiceView.qml</file>
<file>NotificationForm.qml</file>
<file>BasePopUp.qml</file>
</qresource>
</RCC>

23
QmlNotify.pri Normal file
View File

@ -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/"

46
QmlNotify.pro Normal file
View File

@ -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

View File

@ -1,2 +1,27 @@
# SimpleQmlNotify
Simple Qml notification service for qml applications.
## Use
### CPP
``` cpp
#include <qmlnotifyservice.h>
int main() {
QmlNotificationService::init();
auto service = QmlNotificationService::NotificationService::getService();
service->setNotify("title", "text", "UrlOfImage", NotificationData::Normal);
}
```
### QML
``` qml
NotificationServiceView {
anchors.fill: parent;
}
```

40
notificationdata.cpp Normal file
View File

@ -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;
}
}

46
notificationdata.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef NOTIFICATIONDATA_H
#define NOTIFICATIONDATA_H
#include <QObject>
#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

41
notificationservice.cpp Normal file
View File

@ -0,0 +1,41 @@
#include "notificationservice.h"
namespace QmlNotificationService {
NotificationService::NotificationService(QObject * ptr): QObject (ptr) {
qRegisterMetaType<NotificationData>("NotificationData");
qRegisterMetaType<QList<NotificationData>> ("QList<NotificationData>");
}
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<NotificationData::Type>(type)));
}
NotificationService *NotificationService::getService() {
static auto service = new NotificationService;
return service;
}
const QList<NotificationData> &NotificationService::history() const {
return _history;
}
}

62
notificationservice.h Normal file
View File

@ -0,0 +1,62 @@
#ifndef NOTIFICATIONSERVICE_H
#define NOTIFICATIONSERVICE_H
#include "notificationdata.h"
#include <QObject>
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<NotificationData> history READ history NOTIFY notifyChanged)
private:
explicit NotificationService(QObject *ptr = nullptr);
NotificationData _notify;
QList<NotificationData> _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 &notify);
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<NotificationData> & history() const;
signals:
void notifyChanged();
};
}
#endif // NOTIFICATIONSERVICE_H

13
notifyservice_global.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef NOTIFYSERVICE_GLOBAL_H
#define NOTIFYSERVICE_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(QMLNOTYFYSERICE_LIBRARY)
# define NOTIFYSERVICESHARED_EXPORT Q_DECL_EXPORT
#else
# define NOTIFYSERVICESHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // NOTIFYSERVICE_GLOBAL_H

18
qmlnotifyservice.cpp Normal file
View File

@ -0,0 +1,18 @@
#include "notificationservice.h"
#include "qmlnotifyservice.h"
#include <QQmlApplicationEngine>
#include <QQmlContext>
namespace QmlNotificationService {
bool init(QQmlApplicationEngine *engine) {
auto root = engine->rootContext();
if (!root)
return false;
root->setContextProperty("notificationService", NotificationService::getService());
return true;
}
}

30
qmlnotifyservice.h Normal file
View File

@ -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 <qmlnotifyservice.h>
*
* 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