Implement QMessageBox
authorKevin Krammer <kevin.krammer@kdab.com>
Fri, 19 Oct 2012 16:33:05 +0000 (18:33 +0200)
committerKevin Krammer <kevin.krammer@kdab.com>
Sat, 20 Oct 2012 15:48:52 +0000 (17:48 +0200)
declarativeobjects.cpp
declarativeobjects_p.h
declarativewidgetdocument.cpp
declarativewidgets.pro
messagebox.qml [new file with mode: 0644]

index ca53f6e..ca68a48 100644 (file)
@@ -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<QMessageBox::StandardButtons>(buttons), static_cast<QMessageBox::StandardButton>(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<QMessageBox::StandardButtons>(buttons), static_cast<QMessageBox::StandardButton>(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<QMessageBox::StandardButtons>(buttons), static_cast<QMessageBox::StandardButton>(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<QMessageBox::StandardButtons>(buttons), static_cast<QMessageBox::StandardButton>(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<AbstractDeclarativeObject*>(parent);
+  if (declarativeObject)
+    parent = declarativeObject->object();
+
+  while (parent) {
+    QWidget *widget = qobject_cast<QWidget*>(parent);
+    if (widget)
+      return widget->topLevelWidget();
+
+    parent = parent->parent();
+  }
+
+  return 0;
+}
+
+DeclarativeMessageBox::DeclarativeMessageBox(QObject *parent) : DeclarativeObjectProxy<QMessageBox>(parent)
+{
+  connectAllSignals(m_proxiedObject, this);
+}
+
+DeclarativeMessageBoxAttached *DeclarativeMessageBox::qmlAttachedProperties(QObject *parent)
+{
+  return new DeclarativeMessageBoxAttached(parent);
+}
+
+CUSTOM_METAOBJECT(DeclarativeMessageBox, QMessageBox)
+
 // DeclarativePushButton
 DeclarativePushButton::DeclarativePushButton(QObject *parent) : DeclarativeWidgetProxy<QPushButton>(parent)
 {
index 4243f0c..0ff7507 100644 (file)
@@ -18,6 +18,7 @@
 #include <QtGui/QMainWindow>
 #include <QtGui/QMenu>
 #include <QtGui/QMenuBar>
+#include <QtGui/QMessageBox>
 #include <QtGui/QPushButton>
 #include <QtGui/QSlider>
 #include <QtGui/QStatusBar>
@@ -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<QMenuBar>
     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<QMessageBox>
+{
+  DECLARATIVE_OBJECT
+
+  public:
+    DeclarativeMessageBox(QObject *parent = 0);
+
+    static DeclarativeMessageBoxAttached *qmlAttachedProperties(QObject *parent);
+};
+
+QML_DECLARE_TYPEINFO(DeclarativeMessageBox, QML_HAS_ATTACHED_PROPERTIES)
+
 class DeclarativePushButton : public DeclarativeWidgetProxy<QPushButton>
 {
   DECLARATIVE_OBJECT
index 93f0b03..6471cd5 100644 (file)
@@ -56,6 +56,8 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p
   qmlRegisterType<DeclarativeMainWindow>("QtGui", 1, 0, "MainWindow");
   qmlRegisterType<DeclarativeMenu>("QtGui", 1, 0, "Menu");
   qmlRegisterType<DeclarativeMenuBar>("QtGui", 1, 0, "MenuBar");
+  qmlRegisterType<DeclarativeMessageBoxAttached>();
+  qmlRegisterType<DeclarativeMessageBox>("QtGui", 1, 0, "MessageBox");
   qmlRegisterType<DeclarativePushButton>("QtGui", 1, 0, "PushButton");
   qmlRegisterType<DeclarativeSlider>("QtGui", 1, 0, "Slider");
   qmlRegisterType<DeclarativeStatusBarAttached>();
index 95c13ac..7cfc422 100644 (file)
@@ -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 (file)
index 0000000..022f1da
--- /dev/null
@@ -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"
+  }
+}