From 88bbdd401e7aff26123f73cf7c9da45dc1d1cc1f Mon Sep 17 00:00:00 2001 From: Kevin Krammer Date: Sun, 4 Nov 2012 11:03:53 +0100 Subject: [PATCH] Refactor message box attached to common base class --- examples/messagebox.qml | 32 +++++++++++++--- lib/declarativemessagebox.cpp | 81 +++++++++++++++++++++++++++-------------- lib/declarativemessagebox_p.h | 37 +++++++++++++------ 3 files changed, 105 insertions(+), 45 deletions(-) diff --git a/examples/messagebox.qml b/examples/messagebox.qml index f2a6f7f..3004334 100644 --- a/examples/messagebox.qml +++ b/examples/messagebox.qml @@ -11,18 +11,26 @@ Widget { PushButton { id: aboutButton text: qsTr("MessageBox::about") - onClicked: MessageBox.about(aboutButton, qsTr("About DeclarativeWidgets"), qsTr("This is a text about declarative widgets.")) + onClicked: { + MessageBox.parent = aboutButton + MessageBox.about(qsTr("About DeclarativeWidgets"), qsTr("This is a text about declarative widgets.")) + } } PushButton { text: qsTr("MessageBox::aboutQt") - onClicked: MessageBox.aboutQt(0, qsTr("About Qt")) + onClicked: MessageBox.aboutQt() + } + + PushButton { + text: qsTr("MessageBox::aboutQt with title") + onClicked: MessageBox.aboutQt(qsTr("About Qt message box")) } PushButton { text: qsTr("MessageBox::critical") onClicked: { - var button = MessageBox.critical(0, qsTr("Error"), qsTr("An error occurred!")) + var button = MessageBox.critical(qsTr("Error"), qsTr("An error occurred!")) console.log("MessageBox.critical returned " + button) } } @@ -30,7 +38,7 @@ Widget { PushButton { text: qsTr("MessageBox::information") onClicked: { - var button = MessageBox.information(0, qsTr("Information"), qsTr("The taxi is here")) + var button = MessageBox.information(qsTr("Information"), qsTr("The taxi is here")) console.log("MessageBox.information returned " + button) } } @@ -38,7 +46,18 @@ Widget { PushButton { text: qsTr("MessageBox::question") onClicked: { - var button = MessageBox.question(0, qsTr("Network error"), qsTr("Can not connect to host"), MessageBox.Retry | MessageBox.Abort | MessageBox.Ignore, MessageBox.Abort) + MessageBox.buttons = MessageBox.Retry | MessageBox.Abort | MessageBox.Ignore + var button = MessageBox.question(qsTr("Network error"), qsTr("Can not connect to host")) + console.log("MessageBox.question returned " + button) + } + } + + PushButton { + text: qsTr("MessageBox::question with default abort") + onClicked: { + MessageBox.buttons = MessageBox.Retry | MessageBox.Abort | MessageBox.Ignore + MessageBox.defaultButton = MessageBox.Abort + var button = MessageBox.question(qsTr("Network error"), qsTr("Can not connect to host")) console.log("MessageBox.question returned " + button) } } @@ -46,7 +65,8 @@ Widget { PushButton { text: qsTr("MessageBox::warning") onClicked: { - var button = MessageBox.warning(0, qsTr("Warning"), qsTr("Do you really want to quit?"), MessageBox.Ok | MessageBox.Cancel, MessageBox.Ok) + MessageBox.buttons = MessageBox.Ok | MessageBox.Cancel + var button = MessageBox.warning(0, qsTr("Warning"), qsTr("Do you really want to quit?")) console.log("MessageBox.warning returned " + button) } } diff --git a/lib/declarativemessagebox.cpp b/lib/declarativemessagebox.cpp index 4eca6b2..0e2559d 100644 --- a/lib/declarativemessagebox.cpp +++ b/lib/declarativemessagebox.cpp @@ -20,60 +20,87 @@ #include "declarativemessagebox_p.h" +class DeclarativeMessageBoxAttached::Private +{ + public: + Private() : buttons(QMessageBox::Ok), defaultButton(QMessageBox::NoButton) {} + + public: + QMessageBox::StandardButtons buttons; + QMessageBox::StandardButton defaultButton; +}; + DeclarativeMessageBoxAttached::DeclarativeMessageBoxAttached(QObject *parent) - : QObject(parent) + : StaticDialogMethodAttached(parent), d(new Private) +{ +} + +DeclarativeMessageBoxAttached::~DeclarativeMessageBoxAttached() { + delete d; } -void DeclarativeMessageBoxAttached::about(QObject *parent, const QString &title, const QString &text) +void DeclarativeMessageBoxAttached::setButtons(int buttons) { - QMessageBox::about(bestParentWindow(parent), title, text); + if (d->buttons == buttons) + return; + + d->buttons = static_cast(buttons); + emit buttonsChanged(buttons); } -void DeclarativeMessageBoxAttached::aboutQt(QObject *parent, const QString &title) +int DeclarativeMessageBoxAttached::buttons() const { - QMessageBox::aboutQt(bestParentWindow(parent), title); + return d->buttons; } -int DeclarativeMessageBoxAttached::critical(QObject *parent, const QString &title, const QString &text, int buttons, int defaultButton) +void DeclarativeMessageBoxAttached::setDefaultButton(int defaultButton) { - return QMessageBox::critical(bestParentWindow(parent), title, text, static_cast(buttons), static_cast(defaultButton)); + if (d->defaultButton == defaultButton) + return; + + d->defaultButton = static_cast(defaultButton); + emit defaultButtonChanged(defaultButton); } -int DeclarativeMessageBoxAttached::information(QObject *parent, const QString &title, const QString &text, int buttons, int defaultButton) +int DeclarativeMessageBoxAttached::defaultButton() const { - return QMessageBox::information(bestParentWindow(parent), title, text, static_cast(buttons), static_cast(defaultButton)); + return d->defaultButton; } -int DeclarativeMessageBoxAttached::question(QObject *parent, const QString &title, const QString &text, int buttons, int defaultButton) +void DeclarativeMessageBoxAttached::about(const QString &title, const QString &text) { - return QMessageBox::question(bestParentWindow(parent), title, text, static_cast(buttons), static_cast(defaultButton)); + QMessageBox::about(bestParentWindow(), title, text); } -int DeclarativeMessageBoxAttached::warning(QObject *parent, const QString &title, const QString &text, int buttons, int defaultButton) +void DeclarativeMessageBoxAttached::aboutQt() { - return QMessageBox::warning(bestParentWindow(parent), title, text, static_cast(buttons), static_cast(defaultButton)); + QMessageBox::aboutQt(bestParentWindow()); } -QWidget *DeclarativeMessageBoxAttached::bestParentWindow(QObject *parent) const +void DeclarativeMessageBoxAttached::aboutQt(const QString &title) { - if (!parent) - parent = this->parent(); + QMessageBox::aboutQt(bestParentWindow(), title); +} - // if parent is a Declarative Object, search the proxied hierarchy - AbstractDeclarativeObject *declarativeObject = dynamic_cast(parent); - if (declarativeObject) - parent = declarativeObject->object(); +int DeclarativeMessageBoxAttached::critical(const QString &title, const QString &text) +{ + return QMessageBox::critical(bestParentWindow(), title, text, d->buttons, d->defaultButton); +} - while (parent) { - QWidget *widget = qobject_cast(parent); - if (widget) - return widget->topLevelWidget(); +int DeclarativeMessageBoxAttached::information(const QString &title, const QString &text) +{ + return QMessageBox::information(bestParentWindow(), title, text, d->buttons, d->defaultButton); +} - parent = parent->parent(); - } +int DeclarativeMessageBoxAttached::question(const QString &title, const QString &text) +{ + return QMessageBox::question(bestParentWindow(), title, text, d->buttons, d->defaultButton); +} - return 0; +int DeclarativeMessageBoxAttached::warning(const QString &title, const QString &text) +{ + return QMessageBox::warning(bestParentWindow(), title, text, d->buttons, d->defaultButton); } DeclarativeMessageBox::DeclarativeMessageBox(QObject *parent) : DeclarativeObjectProxy(parent) diff --git a/lib/declarativemessagebox_p.h b/lib/declarativemessagebox_p.h index b04bb20..8d75893 100644 --- a/lib/declarativemessagebox_p.h +++ b/lib/declarativemessagebox_p.h @@ -23,28 +23,41 @@ #include "declarativeobjectproxy_p.h" +#include "staticdialogmethodattached_p.h" + #include -class DeclarativeMessageBoxAttached : public QObject +class DeclarativeMessageBoxAttached : public StaticDialogMethodAttached { Q_OBJECT + Q_PROPERTY(int buttons READ buttons WRITE setButtons NOTIFY buttonsChanged) + Q_PROPERTY(int defaultButton READ defaultButton WRITE setDefaultButton NOTIFY defaultButtonChanged) public: DeclarativeMessageBoxAttached(QObject *parent = 0); + ~DeclarativeMessageBoxAttached(); + + void setButtons(int buttons); + int buttons() const; + + void setDefaultButton(int defaultButton); + int defaultButton() const; + + Q_INVOKABLE void about(const QString &title, const QString &text); + Q_INVOKABLE void aboutQt(); + Q_INVOKABLE void aboutQt(const QString &title); + Q_INVOKABLE int critical(const QString &title, const QString &text); + Q_INVOKABLE int information(const QString &title, const QString &text); + Q_INVOKABLE int question(const QString &title, const QString &text); + Q_INVOKABLE int warning(const QString &title, const QString &text); - Q_INVOKABLE void about(QObject *parent, const QString &title, const QString &text); - Q_INVOKABLE void aboutQt(QObject *parent, const QString &title); - Q_INVOKABLE int critical(QObject *parent, const QString &title, const QString &text, - int buttons = QMessageBox::Ok, int defaultButton = QMessageBox::NoButton); - Q_INVOKABLE int information(QObject *parent, const QString &title, const QString &text, - int buttons = QMessageBox::Ok, int defaultButton = QMessageBox::NoButton); - Q_INVOKABLE int question(QObject *parent, const QString &title, const QString &text, - int buttons = QMessageBox::Ok, int defaultButton = QMessageBox::NoButton); - Q_INVOKABLE int warning(QObject *parent, const QString &title, const QString &text, - int buttons = QMessageBox::Ok, int defaultButton = QMessageBox::NoButton); + Q_SIGNALS: + void buttonsChanged(int buttons); + void defaultButtonChanged(int defaultButton); private: - QWidget *bestParentWindow(QObject *parent) const; + class Private; + Private *const d; }; class DeclarativeMessageBox : public DeclarativeObjectProxy -- 1.7.2.5