From: Kevin Krammer Date: Fri, 19 Oct 2012 16:33:05 +0000 (+0200) Subject: Implement QMessageBox X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=9d54eed53d4b5fd6108e87fd3c8070cb9f3524e2;p=konrad%2FDeclarativeWidgets.git Implement QMessageBox --- diff --git a/declarativeobjects.cpp b/declarativeobjects.cpp index ca53f6e..ca68a48 100644 --- a/declarativeobjects.cpp +++ b/declarativeobjects.cpp @@ -819,6 +819,74 @@ void DeclarativeMenuBar::addAction(QAction *action, AbstractDeclarativeObject *d CUSTOM_METAOBJECT(DeclarativeMenuBar, QMenuBar) +// DeclarativeMessageBox +DeclarativeMessageBoxAttached::DeclarativeMessageBoxAttached(QObject *parent) : QObject(parent) +{ +} + +void DeclarativeMessageBoxAttached::about(QObject *parent, const QString &title, const QString &text) +{ + QMessageBox::about(bestParentWindow(parent), title, text); +} + +void DeclarativeMessageBoxAttached::aboutQt(QObject *parent, const QString &title) +{ + QMessageBox::aboutQt(bestParentWindow(parent), title); +} + +int DeclarativeMessageBoxAttached::critical(QObject *parent, const QString &title, const QString &text, int buttons, int defaultButton) +{ + return QMessageBox::critical(bestParentWindow(parent), title, text, static_cast(buttons), static_cast(defaultButton)); +} + +int DeclarativeMessageBoxAttached::information(QObject *parent, const QString &title, const QString &text, int buttons, int defaultButton) +{ + return QMessageBox::information(bestParentWindow(parent), title, text, static_cast(buttons), static_cast(defaultButton)); +} + +int DeclarativeMessageBoxAttached::question(QObject *parent, const QString &title, const QString &text, int buttons, int defaultButton) +{ + return QMessageBox::question(bestParentWindow(parent), title, text, static_cast(buttons), static_cast(defaultButton)); +} + +int DeclarativeMessageBoxAttached::warning(QObject *parent, const QString &title, const QString &text, int buttons, int defaultButton) +{ + return QMessageBox::warning(bestParentWindow(parent), title, text, static_cast(buttons), static_cast(defaultButton)); +} + +QWidget *DeclarativeMessageBoxAttached::bestParentWindow(QObject *parent) const +{ + if (!parent) + parent = this->parent(); + + // if parent is a Declarative Object, search the proxied hierarchy + AbstractDeclarativeObject *declarativeObject = dynamic_cast(parent); + if (declarativeObject) + parent = declarativeObject->object(); + + while (parent) { + QWidget *widget = qobject_cast(parent); + if (widget) + return widget->topLevelWidget(); + + parent = parent->parent(); + } + + return 0; +} + +DeclarativeMessageBox::DeclarativeMessageBox(QObject *parent) : DeclarativeObjectProxy(parent) +{ + connectAllSignals(m_proxiedObject, this); +} + +DeclarativeMessageBoxAttached *DeclarativeMessageBox::qmlAttachedProperties(QObject *parent) +{ + return new DeclarativeMessageBoxAttached(parent); +} + +CUSTOM_METAOBJECT(DeclarativeMessageBox, QMessageBox) + // DeclarativePushButton DeclarativePushButton::DeclarativePushButton(QObject *parent) : DeclarativeWidgetProxy(parent) { diff --git a/declarativeobjects_p.h b/declarativeobjects_p.h index 4243f0c..0ff7507 100644 --- a/declarativeobjects_p.h +++ b/declarativeobjects_p.h @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -241,6 +242,8 @@ class DeclarativeBoxLayoutAttached : public QObject void setAlignment(Qt::Alignment alignment); Qt::Alignment alignment() const; + Q_INVOKABLE QString foo() const { qDebug() << Q_FUNC_INFO; return "foo"; } + Q_SIGNALS: void stretchChanged(int stretch); void alignmentChanged(Qt::Alignment alignment); @@ -491,6 +494,44 @@ class DeclarativeMenuBar : public DeclarativeWidgetProxy virtual void addAction(QAction *action, AbstractDeclarativeObject *declarativeObject); }; +class DeclarativeMessageBoxAttached : public QObject +{ + Q_OBJECT + Q_FLAGS(StandardButtons) + + public: + typedef QMessageBox::StandardButton StandardButton; + Q_DECLARE_FLAGS(StandardButtons, StandardButton); + + DeclarativeMessageBoxAttached(QObject *parent = 0); + + 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); + + private: + QWidget *bestParentWindow(QObject *parent) const; +}; + +class DeclarativeMessageBox : public DeclarativeObjectProxy +{ + DECLARATIVE_OBJECT + + public: + DeclarativeMessageBox(QObject *parent = 0); + + static DeclarativeMessageBoxAttached *qmlAttachedProperties(QObject *parent); +}; + +QML_DECLARE_TYPEINFO(DeclarativeMessageBox, QML_HAS_ATTACHED_PROPERTIES) + class DeclarativePushButton : public DeclarativeWidgetProxy { DECLARATIVE_OBJECT diff --git a/declarativewidgetdocument.cpp b/declarativewidgetdocument.cpp index 93f0b03..6471cd5 100644 --- a/declarativewidgetdocument.cpp +++ b/declarativewidgetdocument.cpp @@ -56,6 +56,8 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p qmlRegisterType("QtGui", 1, 0, "MainWindow"); qmlRegisterType("QtGui", 1, 0, "Menu"); qmlRegisterType("QtGui", 1, 0, "MenuBar"); + qmlRegisterType(); + qmlRegisterType("QtGui", 1, 0, "MessageBox"); qmlRegisterType("QtGui", 1, 0, "PushButton"); qmlRegisterType("QtGui", 1, 0, "Slider"); qmlRegisterType(); diff --git a/declarativewidgets.pro b/declarativewidgets.pro index 95c13ac..7cfc422 100644 --- a/declarativewidgets.pro +++ b/declarativewidgets.pro @@ -29,4 +29,5 @@ OTHER_FILES += \ editor.qml \ gallery.qml \ layouts.qml \ - test.qml + test.qml \ + messagebox.qml diff --git a/messagebox.qml b/messagebox.qml new file mode 100644 index 0000000..022f1da --- /dev/null +++ b/messagebox.qml @@ -0,0 +1,57 @@ +import QtGui 1.0 + +Widget { + VBoxLayout { + PushButton { + text: "MessageBox instance" + + onClicked: messageBox.show() + } + + PushButton { + id: aboutButton + text: "MessageBox::about" + onClicked: MessageBox.about(aboutButton, "title", "text") + } + PushButton { + text: "MessageBox::aboutQt" + onClicked: MessageBox.aboutQt(0, "text") + } + PushButton { + text: "MessageBox::critical" + onClicked: { + var button = MessageBox.critical(0, "title", "text") + console.log("MessageBox.critical returned " + button) + } + } + PushButton { + text: "MessageBox::information" + onClicked: { + var button = MessageBox.information(0, "title", "text") + console.log("MessageBox.information returned " + button) + } + } + PushButton { + text: "MessageBox::question" + onClicked: { + var button = MessageBox.question(0, "title", "text", MessageBox.Retry | MessageBox.Abort | MessageBox.Ignore, MessageBox.Abort) + console.log("MessageBox.question returned " + button) + } + } + PushButton { + text: "MessageBox::warning" + onClicked: { + var button = MessageBox.warning(0, "title", "text", MessageBox.Ok | MessageBox.Cancel) + console.log("MessageBox.warning returned " + button) + } + } + } + + MessageBox { + id: messageBox + + windowTitle: "MessageBox instance" + text: "text" + detailedText: "detailedText" + } +}