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

View File

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

View File

@ -45,42 +45,40 @@ BasePopUp {
backgroundColor: Material.background backgroundColor: Material.background
contentItem: contentItem:
Item { Control {
id: control;
implicitHeight: rowLayout.implicitHeight
RowLayout { RowLayout {
id: rowLayout id: rowLayout
spacing: 5 spacing: 5
clip: true clip: true
Rectangle { width: control.width
color: "#00000000"
Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
Layout.preferredWidth: rowLayout.height;
Layout.preferredHeight: rowLayout.height;
Image { Image {
id: 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 fillMode: Image.PreserveAspectCrop
clip: true clip: true
anchors.fill: parent;
source: img source: img
} }
}
Label { Label {
id: message id: message
text: popup.text text: popup.text
Layout.alignment: Qt.AlignRight | Qt.AlignVCenter Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
Layout.fillHeight: true;
Layout.fillWidth: true; Layout.fillWidth: true;
clip: true clip: true
wrapMode: Text.WordWrap wrapMode: Text.WordWrap
verticalAlignment: Text.AlignVCenter verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignLeft
} }
anchors.fill: parent
} }
MouseArea { MouseArea {

View File

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

View File

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

View File

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

View File

@ -6,6 +6,8 @@
*/ */
#include "notificationservice.h" #include "notificationservice.h"
#include <QSharedPointer>
namespace QmlNotificationService { namespace QmlNotificationService {
NotificationService::NotificationService(QObject * ptr): QObject (ptr) { NotificationService::NotificationService(QObject * ptr): QObject (ptr) {
@ -31,16 +33,26 @@ void NotificationService::setNotify(const NotificationData& notify) {
emit notifyChanged(); emit notifyChanged();
} }
int NotificationService::setQuestion(const NotificationData &question) { int NotificationService::setQuestion(const Listner& listner, const NotificationData &question) {
_question = question; _question = question;
_question.setCode(rand() % std::numeric_limits<int>().max()); int questionCode = rand();
_question.setCode(questionCode);
emit questionChanged(); emit questionChanged();
_listners[questionCode] = listner;
return _question.type(); return _question.type();
} }
void NotificationService::questionComplete(bool accepted, int code) { 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); emit questionCompleted(accepted, code);
} }
@ -53,8 +65,34 @@ void NotificationService::setNotify(const QString &title,
static_cast<NotificationData::Type>(type))); static_cast<NotificationData::Type>(type)));
} }
int NotificationService::setQuestion(const QString &title, const QString &text, const QString &img, int code) { int NotificationService::setQuestion(QObject *listnerObject,
return setQuestion(NotificationData(title, text, img, code)); 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() { NotificationService *NotificationService::getService() {

View File

@ -3,10 +3,16 @@
#include "notificationdata.h" #include "notificationdata.h"
#include <QHash>
#include <QObject> #include <QObject>
namespace QmlNotificationService { 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. * @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. * @brief setQuestion This method sets information of the users question.
* @param listner This is listner lyambda function.
* @param question are data of the question. * @param question are data of the question.
* @return return question code. You must be save this code for check ansver of your qestion. * @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. * @brief questionComplete This method is main responce method of the users questions.
@ -66,12 +73,30 @@ public:
/** /**
* @brief setQuestion This method set new question for user. When question changed on main application windows shows question. * @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 title are title of a question window.
* @param text are text value of a question window. * @param text are text value of a question window.
* @param img are url to image of a queston 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. * @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 = "", 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& text = "",
const QString& img = "", const QString& img = "",
int code = 0); int code = 0);
@ -109,8 +134,10 @@ signals:
void questionCompleted(bool accepted, int questionCode); void questionCompleted(bool accepted, int questionCode);
private: private:
explicit NotificationService(QObject *ptr = nullptr); explicit NotificationService(QObject *ptr = nullptr);
QHash<int, Listner> _listners;
NotificationData _question; NotificationData _question;
NotificationData _notify; NotificationData _notify;
QList<NotificationData> _history; QList<NotificationData> _history;