From 26bfe7cb1a5f068b5287174f113335c8731dac82 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 17 May 2017 14:34:51 +0200 Subject: Incorporate QQuickDialog::result from QQuickPlatformDialog The experimental QQuickPlatformDialog in Qt.labs.platform had a bit more elaborate accepted vs. rejected dialog result handling. Copy the same logic to QQuickDialog in QtQuick.Controls. [ChangeLog][Controls][Dialog] Added "result" property that holds whether the dialog was previously accepted or rejected. Change-Id: I2c55848b5eeaad2130d32a337c2590212736f22c Reviewed-by: Mitch Curtis --- src/quicktemplates2/qquickdialog.cpp | 57 ++++++++++++++++++++++++++++++--- src/quicktemplates2/qquickdialog_p.h | 13 ++++++-- src/quicktemplates2/qquickdialog_p_p.h | 3 +- tests/auto/controls/data/tst_dialog.qml | 2 ++ 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/quicktemplates2/qquickdialog.cpp b/src/quicktemplates2/qquickdialog.cpp index 3ac99caa..00719ae4 100644 --- a/src/quicktemplates2/qquickdialog.cpp +++ b/src/quicktemplates2/qquickdialog.cpp @@ -300,17 +300,44 @@ void QQuickDialog::setStandardButtons(QPlatformDialogHelper::StandardButtons but emit standardButtonsChanged(); } +/*! + \since QtQuick.Controls 2.3 + \qmlproperty int QtQuick.Controls::Dialog::result + + This property holds the result code. + + Standard result codes: + \value Dialog.Accepted The dialog was accepted. + \value Dialog.Rejected The dialog was rejected. + + \sa accept(), reject(), done() +*/ +int QQuickDialog::result() const +{ + Q_D(const QQuickDialog); + return d->result; +} + +void QQuickDialog::setResult(int result) +{ + Q_D(QQuickDialog); + if (d->result == result) + return; + + d->result = result; + emit resultChanged(); +} + /*! \qmlmethod void QtQuick.Controls::Dialog::accept() Closes the dialog and emits the \l accepted() signal. - \sa reject() + \sa reject(), done() */ void QQuickDialog::accept() { - close(); - emit accepted(); + done(Accepted); } /*! @@ -318,12 +345,32 @@ void QQuickDialog::accept() Closes the dialog and emits the \l rejected() signal. - \sa accept() + \sa accept(), done() */ void QQuickDialog::reject() +{ + done(Rejected); +} + +/*! + \since QtQuick.Controls 2.3 + \qmlmethod void Qt.labs.platform::Dialog::done(int result) + + Closes the dialog, sets the \a result, and emits \l accepted() or + \l rejected() depending on whether the result is \c Dialog.Accepted + or \c Dialog.Rejected, respectively. + + \sa accept(), reject(), result +*/ +void QQuickDialog::done(int result) { close(); - emit rejected(); + setResult(result); + + if (result == Accepted) + emit accepted(); + else if (result == Rejected) + emit rejected(); } void QQuickDialog::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) diff --git a/src/quicktemplates2/qquickdialog_p.h b/src/quicktemplates2/qquickdialog_p.h index 97c90a59..0743ee0a 100644 --- a/src/quicktemplates2/qquickdialog_p.h +++ b/src/quicktemplates2/qquickdialog_p.h @@ -62,6 +62,7 @@ class Q_QUICKTEMPLATES2_PRIVATE_EXPORT QQuickDialog : public QQuickPopup Q_PROPERTY(QQuickItem *header READ header WRITE setHeader NOTIFY headerChanged FINAL) Q_PROPERTY(QQuickItem *footer READ footer WRITE setFooter NOTIFY footerChanged FINAL) Q_PROPERTY(QPlatformDialogHelper::StandardButtons standardButtons READ standardButtons WRITE setStandardButtons NOTIFY standardButtonsChanged FINAL) + Q_PROPERTY(int result READ result WRITE setResult NOTIFY resultChanged FINAL REVISION 3) Q_FLAGS(QPlatformDialogHelper::StandardButtons) public: @@ -79,9 +80,16 @@ public: QPlatformDialogHelper::StandardButtons standardButtons() const; void setStandardButtons(QPlatformDialogHelper::StandardButtons buttons); + enum StandardCode { Rejected, Accepted }; + Q_ENUM(StandardCode) + + int result() const; + void setResult(int result); + public Q_SLOTS: - void accept(); - void reject(); + virtual void accept(); + virtual void reject(); + virtual void done(int result); Q_SIGNALS: void accepted(); @@ -91,6 +99,7 @@ Q_SIGNALS: void headerChanged(); void footerChanged(); void standardButtonsChanged(); + Q_REVISION(3) void resultChanged(); protected: void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override; diff --git a/src/quicktemplates2/qquickdialog_p_p.h b/src/quicktemplates2/qquickdialog_p_p.h index 13885ca9..2a0f4b53 100644 --- a/src/quicktemplates2/qquickdialog_p_p.h +++ b/src/quicktemplates2/qquickdialog_p_p.h @@ -60,13 +60,14 @@ class QQuickDialogPrivate : public QQuickPopupPrivate Q_DECLARE_PUBLIC(QQuickDialog) public: - QQuickDialogPrivate() : buttonBox(nullptr) { } + QQuickDialogPrivate() : result(0), buttonBox(nullptr) { } static QQuickDialogPrivate *get(QQuickDialog *dialog) { return dialog->d_func(); } + int result; QString title; QQuickDialogButtonBox *buttonBox; QScopedPointer layout; diff --git a/tests/auto/controls/data/tst_dialog.qml b/tests/auto/controls/data/tst_dialog.qml index 2f3d2a6b..4d75b910 100644 --- a/tests/auto/controls/data/tst_dialog.qml +++ b/tests/auto/controls/data/tst_dialog.qml @@ -99,6 +99,7 @@ TestCase { verify(acceptedSpy.valid) control.accept() compare(acceptedSpy.count, 1) + compare(control.result, Dialog.Accepted) tryCompare(control, "visible", false) } @@ -118,6 +119,7 @@ TestCase { verify(rejectedSpy.valid) control.reject() compare(rejectedSpy.count, 1) + compare(control.result, Dialog.Rejected) tryCompare(control, "visible", false) -- cgit v1.2.3