Support for QInputDialog static get methods
authorKevin Krammer <kevin.krammer@kdab.com>
Mon, 22 Oct 2012 14:14:46 +0000 (16:14 +0200)
committerKevin Krammer <kevin.krammer@kdab.com>
Mon, 22 Oct 2012 14:14:46 +0000 (16:14 +0200)
declarativeobjects.cpp
declarativeobjects_p.h
declarativewidgetdocument.cpp
inputdialog.qml [new file with mode: 0644]

index fc174f4..378a874 100644 (file)
@@ -774,11 +774,65 @@ DeclarativeFontDialog::DeclarativeFontDialog(QObject *parent) : DeclarativeWidge
 CUSTOM_METAOBJECT(DeclarativeFontDialog, QFontDialog)
 
 // DeclarativeInputDialog
+DeclarativeInputDialogAttached::DeclarativeInputDialogAttached(QObject *parent) : QObject(parent)
+{
+}
+
+double DeclarativeInputDialogAttached::getDouble(QObject *parent, const QString &title, const QString &label,
+                                                 double value, double min, double max, int decimals)
+{
+  return QInputDialog::getDouble(bestParentWindow(parent), title, label, value, min, max, decimals);
+}
+
+int DeclarativeInputDialogAttached::getInt(QObject *parent, const QString &title, const QString &label,
+                                           int value, int min, int max, int step)
+{
+  return QInputDialog::getInt(bestParentWindow(parent), title, label, value, min, max, step);
+}
+
+QString DeclarativeInputDialogAttached::getItem(QObject *parent, const QString &title, const QString &label,
+                                                const QStringList &items, int current, bool editable)
+{
+  return QInputDialog::getItem(bestParentWindow(parent), title, label, items, current, editable);
+}
+
+QString DeclarativeInputDialogAttached::getText(QObject *parent, const QString &title, const QString &label,
+                                                int echoMode, const QString &text)
+{
+  return QInputDialog::getText(bestParentWindow(parent), title, label, static_cast<QLineEdit::EchoMode>(echoMode), text);
+}
+
+QWidget *DeclarativeInputDialogAttached::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;
+}
+
 DeclarativeInputDialog::DeclarativeInputDialog(QObject *parent) : DeclarativeWidgetProxy<InputDialog>(parent)
 {
   connectAllSignals(m_proxiedObject, this);
 }
 
+DeclarativeInputDialogAttached *DeclarativeInputDialog::qmlAttachedProperties(QObject *parent)
+{
+  return new DeclarativeInputDialogAttached(parent);
+}
+
 CUSTOM_METAOBJECT(DeclarativeInputDialog, InputDialog)
 
 // DeclarativeLabel
index 3cb552b..078212d 100644 (file)
@@ -487,14 +487,41 @@ class DeclarativeFontDialog : public DeclarativeWidgetProxy<QFontDialog>
     DeclarativeFontDialog(QObject *parent = 0);
 };
 
+class DeclarativeInputDialogAttached : public QObject
+{
+  Q_OBJECT
+
+  public:
+    DeclarativeInputDialogAttached(QObject *parent = 0);
+
+    Q_INVOKABLE double getDouble(QObject *parent, const QString &title, const QString &label,
+                                 double value = 0, double min = -2147483647, double max = 2147483647, int decimals = 1);
+
+    Q_INVOKABLE int getInt(QObject *parent, const QString &title, const QString &label,
+                           int value = 0, int min = -2147483647, int max = 2147483647, int step = 1);
+
+    Q_INVOKABLE QString getItem(QObject *parent, const QString &title, const QString &label,
+                                const QStringList &items, int current = 0, bool editable = true);
+
+    Q_INVOKABLE QString getText(QObject *parent, const QString &title, const QString &label,
+                                int echoMode = QLineEdit::Normal, const QString &text = QString());
+
+  private:
+    QWidget *bestParentWindow(QObject *parent) const;
+};
+
 class DeclarativeInputDialog : public DeclarativeWidgetProxy<InputDialog>
 {
   DECLARATIVE_OBJECT
 
   public:
     DeclarativeInputDialog(QObject *parent = 0);
+
+    static DeclarativeInputDialogAttached *qmlAttachedProperties(QObject *parent);
 };
 
+QML_DECLARE_TYPEINFO(DeclarativeInputDialog, QML_HAS_ATTACHED_PROPERTIES)
+
 class DeclarativeLabel : public DeclarativeWidgetProxy<QLabel>
 {
   DECLARATIVE_OBJECT
index 05d22ad..1955afb 100644 (file)
@@ -53,6 +53,7 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p
   qmlRegisterType<DeclarativeFileDialogAttached>();
   qmlRegisterType<DeclarativeFileDialog>("QtGui", 1, 0, "FileDialog");
   qmlRegisterType<DeclarativeFontDialog>("QtGui", 1, 0, "FontDialog");
+  qmlRegisterType<DeclarativeInputDialogAttached>();
   qmlRegisterType<DeclarativeInputDialog>("QtGui", 1, 0, "InputDialog");
   qmlRegisterType<DeclarativeLabel>("QtGui", 1, 0, "Label");
   qmlRegisterType<DeclarativeMainWindow>("QtGui", 1, 0, "MainWindow");
diff --git a/inputdialog.qml b/inputdialog.qml
new file mode 100644 (file)
index 0000000..7f4569c
--- /dev/null
@@ -0,0 +1,38 @@
+import QtGui 1.0
+
+Widget {
+  VBoxLayout {
+    PushButton {
+      text: "InputDialog::getDouble"
+      onClicked: {
+        var value = InputDialog.getDouble(0, "Double Input", "A Double number")
+        console.log("InputDialog.getDouble returned " + value)
+      }
+    }
+
+    PushButton {
+      text: "InputDialog::getInt"
+      onClicked: {
+        var value = InputDialog.getInt(0, "Integer Input", "An Integer number")
+        console.log("InputDialog.getInt returned " + value)
+      }
+    }
+
+    PushButton {
+      text: "InputDialog::getItem"
+      onClicked: {
+        var items = [ "Item 1", "Item 2", "Item 3"]
+        var item = InputDialog.getItem(0, "Item Selection", "Select an item", items)
+        console.log("InputDialog.getItem returned " + item)
+      }
+    }
+
+    PushButton {
+      text: "InputDialog::getText"
+      onClicked: {
+        var text = InputDialog.getText(0, "Text Input", "Any text")
+        console.log("InputDialog.getText returned " + text)
+      }
+    }
+  }
+}