4
0
mirror of https://github.com/QuasarApp/SimpleQmlNotify.git synced 2025-05-05 17:59:37 +00:00

fix questions

This commit is contained in:
Andrei Yankovich 2021-10-21 18:46:18 +03:00
parent 6080f7b9c8
commit ac9e1cc988
7 changed files with 108 additions and 54 deletions

@ -12,10 +12,6 @@ import QtQuick.Layouts 1.3
Dialog {
id : basePopup
width: 200
height: 100
x: 0
y: 0
transformOrigin: Item.Center

@ -45,26 +45,26 @@ BasePopUp {
backgroundColor: Material.background
contentItem:
Item {
Control {
id: control;
implicitHeight: rowLayout.implicitHeight
RowLayout {
id: rowLayout
spacing: 5
clip: true
Rectangle {
color: "#00000000"
Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
Layout.preferredWidth: rowLayout.height;
Layout.preferredHeight: rowLayout.height;
width: control.width
Image {
id: image
fillMode: Image.PreserveAspectCrop
clip: true
anchors.fill: parent;
source: img
}
Image {
id: image
Layout.preferredWidth: Math.max(message.height, 50);
Layout.preferredHeight: Math.max(message.height, 50);
Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
fillMode: Image.PreserveAspectCrop
clip: true
source: img
}
@ -72,15 +72,13 @@ BasePopUp {
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
horizontalAlignment: Text.AlignLeft
}
anchors.fill: parent
}
MouseArea {

@ -11,6 +11,7 @@ import QtQuick.Controls 2.15
import QtQuick.Layouts 1.3
Item {
id : root;
readonly property var model: notificationService;
readonly property var msg: model.notify
readonly property var qst: model.question
@ -45,8 +46,7 @@ Item {
x: parent.width / 2 - width / 2;
y: parent.height / 2 - height / 2;
width: 6 * metrix.pt;
height: width * 0.45
width: Math.min(6 * metrix.pt, root.width);
onAccepted: {
if (model) {

@ -10,6 +10,7 @@ NotificationForm {
autoClose: false
clickClose: false
modal: true
footer: DialogButtonBox {
standardButtons: Dialog.Yes | Dialog.No;

@ -76,14 +76,12 @@ Simple Qml notification service for qml applications.
int main() {
QmlNotificationService::init();
auto service = QmlNotificationService::NotificationService::getService();
int questionCode = service->setQuestion("title", "some text");
QObject::connect(service, QmlNotificationService::NotificationService::questionCompleted,
[questionCode](bool accepted, int questionCode) {
if (accepted && code === questionCode) {
// your action here.
}
})
QmlNotificationService::Listner listner = [] (bool accepted) {
// your action here.
};
service->setQuestion(listner, "title", "some text");
}
@ -100,19 +98,15 @@ Simple Qml notification service for qml applications.
anchors.fill: parent;
}
readonly property int questionCode: 0;
questionCode = notificationService.setQuestion(qsTr("Remove %0 user").arg(userModel.userId),
qsTr("All saved data and records will be delete, Do you want continuee?"))
Connections {
target: notificationService
function onQuestionCompleted(accepted, code) {
if (accepted && code === privateRoot.questionCode) {
if (userModel)
backEnd.removeUser(userModel.userId)
}
}
}
Item {
notificationService.setQuestion(this, "onQuestionCompleted", qsTr("Remove %0 user").arg(userModel.userId),
qsTr("All saved data and records will be delete, Do you want continuee?"));
function onQuestionCompleted(accepted) {
// your action here.
}
}
```
### Include translations

@ -6,6 +6,8 @@
*/
#include "notificationservice.h"
#include <QSharedPointer>
namespace QmlNotificationService {
NotificationService::NotificationService(QObject * ptr): QObject (ptr) {
@ -31,16 +33,26 @@ void NotificationService::setNotify(const NotificationData& notify) {
emit notifyChanged();
}
int NotificationService::setQuestion(const NotificationData &question) {
int NotificationService::setQuestion(const Listner& listner, const NotificationData &question) {
_question = question;
_question.setCode(rand() % std::numeric_limits<int>().max());
int questionCode = rand();
_question.setCode(questionCode);
emit questionChanged();
_listners[questionCode] = listner;
return _question.type();
}
void NotificationService::questionComplete(bool accepted, int code) {
if (_listners.contains(code)) {
auto listner = _listners.value(code);
listner(accepted);
_listners.remove(code);
}
emit questionCompleted(accepted, code);
}
@ -53,8 +65,34 @@ void NotificationService::setNotify(const QString &title,
static_cast<NotificationData::Type>(type)));
}
int NotificationService::setQuestion(const QString &title, const QString &text, const QString &img, int code) {
return setQuestion(NotificationData(title, text, img, code));
int NotificationService::setQuestion(QObject *listnerObject,
const QString &listnerMethod,
const QString &title,
const QString &text,
const QString &img,
int code) {
Listner listner = [listnerObject, listnerMethod](bool accept) {
const QByteArray stringData = listnerMethod.toLatin1();
char method[100];
method[qMin(99, stringData.size())] = '\0';
std::copy(stringData.constBegin(), stringData.constBegin() + qMin(99, stringData.size()), method);
QMetaObject::invokeMethod(listnerObject, method,
Qt::QueuedConnection, Q_ARG(bool, accept));
};
return setQuestion(listner, NotificationData(title, text, img, code));
}
int NotificationService::setQuestion(const Listner& listner,
const QString &title,
const QString &text,
const QString &img,
int code) {
return setQuestion(listner, NotificationData(title, text, img, code));
}
NotificationService *NotificationService::getService() {

@ -3,10 +3,16 @@
#include "notificationdata.h"
#include <QHash>
#include <QObject>
namespace QmlNotificationService {
/**
* @brief Listner This is listner lyambda function of user ansver. This function has next signature: void(bool accept)
*/
using Listner = std::function<void(bool accept)>;
/**
* @brief The NotificationService class. This class used for working with notify.
*/
@ -40,10 +46,11 @@ public:
/**
* @brief setQuestion This method sets information of the users question.
* @param listner This is listner lyambda function.
* @param question are data of the question.
* @return return question code. You must be save this code for check ansver of your qestion.
*/
int setQuestion(const NotificationData& question);
int setQuestion(const Listner &listner, const NotificationData& question);
/**
* @brief questionComplete This method is main responce method of the users questions.
@ -60,21 +67,39 @@ public:
* @param type are code of a notification type. For more information about notification type see the NotificationData enum.
*/
Q_INVOKABLE void setNotify(const QString& title = "",
const QString& text = "",
const QString& img = "",
int type = NotificationData::Normal);
const QString& text = "",
const QString& img = "",
int type = NotificationData::Normal);
/**
* @brief setQuestion This method set new question for user. When question changed on main application windows shows question.
* @param listnerObject This is qml object with listner function.
* @param listnerMethod This is listner method name. Method signature : void(bool)
* @param title are title of a question window.
* @param text are text value of a question window.
* @param img are url to image of a queston window.
* @param code are code of question. This code must be sendet to the questionComplete method after buttons clik.
*/
Q_INVOKABLE int setQuestion(const QString& title = "",
const QString& text = "",
const QString& img = "",
int code = 0);
Q_INVOKABLE int setQuestion(QObject *listnerObject,
const QString &listnerMethod,
const QString& title = "",
const QString& text = "",
const QString& img = "",
int code = 0);
/**
* @brief setQuestion This method set new question for user. When question changed on main application windows shows question.
* @param listner This is listner lyambda function.
* @param title are title of a question window.
* @param text are text value of a question window.
* @param img are url to image of a queston window.
* @param code are code of question. This code must be sendet to the questionComplete method after buttons clik.
*/
int setQuestion(const Listner &listner,
const QString& title = "",
const QString& text = "",
const QString& img = "",
int code = 0);
/**
@ -109,8 +134,10 @@ signals:
void questionCompleted(bool accepted, int questionCode);
private:
explicit NotificationService(QObject *ptr = nullptr);
QHash<int, Listner> _listners;
NotificationData _question;
NotificationData _notify;
QList<NotificationData> _history;