Optional arguments for QInputDialog static methods
authorKevin Krammer <kevin.krammer@kdab.com>
Sun, 28 Oct 2012 10:46:25 +0000 (11:46 +0100)
committerKevin Krammer <kevin.krammer@kdab.com>
Sun, 28 Oct 2012 10:47:02 +0000 (11:47 +0100)
declarativeobjects.cpp
declarativeobjects_p.h
dialogs.qml

index 6aa65e1..cc3e02e 100644 (file)
@@ -983,32 +983,274 @@ DeclarativeGroupBox::DeclarativeGroupBox(QObject *parent) : DeclarativeWidgetPro
 CUSTOM_METAOBJECT(DeclarativeGroupBox, QGroupBox)
 
 // DeclarativeInputDialog
-DeclarativeInputDialogAttached::DeclarativeInputDialogAttached(QObject *parent) : QObject(parent)
+class DeclarativeInputDialogAttached::Private
 {
+  public:
+    Private() : dialogAccepted(false),
+                value(0), min(-2147483647), max(2147483647), decimals(1), step(1),
+                currentItem(0), itemsEditable(true),
+                echoMode(QLineEdit::Normal)
+    {}
+
+  public:
+    QPointer<QObject> dialogParent;
+    QString title;
+    QString label;
+    bool dialogAccepted;
+
+    QVariant value;
+    QVariant min;
+    QVariant max;
+    int decimals;
+    int step;
+
+    int currentItem;
+    bool itemsEditable;
+
+    QLineEdit::EchoMode echoMode;
+    QString text;
+};
+
+DeclarativeInputDialogAttached::DeclarativeInputDialogAttached(QObject *parent)
+  : QObject(parent), d(new Private)
+{
+}
+
+DeclarativeInputDialogAttached::~DeclarativeInputDialogAttached()
+{
+  delete d;
+}
+
+void DeclarativeInputDialogAttached::setDialogParent(QObject *parent)
+{
+  if (parent == d->dialogParent)
+    return;
+
+  d->dialogParent = parent;
+  emit dialogParentChanged(parent);
+}
+
+QObject *DeclarativeInputDialogAttached::dialogParent() const
+{
+  return d->dialogParent;
+}
+
+void DeclarativeInputDialogAttached::setTitle(const QString &title)
+{
+  if (title == d->title)
+    return;
+
+  d->title = title;
+  emit titleChanged(title);
+}
+
+QString DeclarativeInputDialogAttached::title() const
+{
+  return d->title;
+}
+
+void DeclarativeInputDialogAttached::setLabel(const QString &label)
+{
+  if (label == d->label)
+    return;
+
+  d->label = label;
+  emit labelChanged(label);
+}
+
+QString DeclarativeInputDialogAttached::label() const
+{
+  return d->label;
+}
+
+bool DeclarativeInputDialogAttached::dialogAccepted() const
+{
+  return d->dialogAccepted;
+}
+
+void DeclarativeInputDialogAttached::setValue(const QVariant &value)
+{
+  if (value == d->value)
+    return;
+
+  d->value = value;
+  emit valueChanged(value);
+}
+
+QVariant DeclarativeInputDialogAttached::value() const
+{
+  return d->value;
+}
+
+void DeclarativeInputDialogAttached::setMin(const QVariant &min)
+{
+  if (min == d->min)
+    return;
+
+  d->min = min;
+  emit minChanged(min);
+}
+
+QVariant DeclarativeInputDialogAttached::min() const
+{
+  return d->min;
+}
+
+void DeclarativeInputDialogAttached::setMax(const QVariant &max)
+{
+  if (max == d->max)
+    return;
+
+  d->max = max;
+  emit maxChanged(max);
+}
+
+QVariant DeclarativeInputDialogAttached::max() const
+{
+  return d->max;
+}
+
+void DeclarativeInputDialogAttached::setDecimals(int decimals)
+{
+  if (decimals == d->decimals)
+    return;
+
+  d->decimals = decimals;
+  emit decimalsChanged(decimals);
+}
+
+int DeclarativeInputDialogAttached::decimals() const
+{
+  return d->decimals;
+}
+
+void DeclarativeInputDialogAttached::setStep(int step)
+{
+  if (step == d->step)
+    return;
+
+  d->step = step;
+  emit stepChanged(step);
 }
 
-double DeclarativeInputDialogAttached::getDouble(QObject *parent, const QString &title, const QString &label,
-                                                 double value, double min, double max, int decimals)
+int DeclarativeInputDialogAttached::step() const
 {
-  return QInputDialog::getDouble(bestParentWindow(parent), title, label, value, min, max, decimals);
+  return d->step;
 }
 
-int DeclarativeInputDialogAttached::getInt(QObject *parent, const QString &title, const QString &label,
-                                           int value, int min, int max, int step)
+void DeclarativeInputDialogAttached::setCurrentItem(int current)
 {
-  return QInputDialog::getInt(bestParentWindow(parent), title, label, value, min, max, step);
+  if (current == d->currentItem)
+    return;
+
+  d->currentItem = current;
+  emit currentItemChanged(current);
+}
+
+int DeclarativeInputDialogAttached::currentItem() const
+{
+  return d->currentItem;
+}
+
+void DeclarativeInputDialogAttached::setItemsEditable(bool editable)
+{
+  if (editable == d->itemsEditable)
+    return;
+
+  d->itemsEditable = editable;
+  emit itemsEditableChanged(editable);
 }
 
-QString DeclarativeInputDialogAttached::getItem(QObject *parent, const QString &title, const QString &label,
-                                                const QStringList &items, int current, bool editable)
+bool DeclarativeInputDialogAttached::itemsEditable() const
 {
-  return QInputDialog::getItem(bestParentWindow(parent), title, label, items, current, editable);
+  return d->itemsEditable;
 }
 
-QString DeclarativeInputDialogAttached::getText(QObject *parent, const QString &title, const QString &label,
-                                                int echoMode, const QString &text)
+void DeclarativeInputDialogAttached::setEchoMode(QLineEdit::EchoMode echoMode)
 {
-  return QInputDialog::getText(bestParentWindow(parent), title, label, static_cast<QLineEdit::EchoMode>(echoMode), text);
+  if (echoMode == d->echoMode)
+    return;
+
+  d->echoMode = echoMode;
+  emit echoModeChanged(echoMode);
+}
+
+QLineEdit::EchoMode DeclarativeInputDialogAttached::echoMode() const
+{
+  return d->echoMode;
+}
+
+void DeclarativeInputDialogAttached::setText(const QString &text)
+{
+  if (text == d->text)
+    return;
+
+  d->text = text;
+  emit textChanged(text);
+}
+
+QString DeclarativeInputDialogAttached::text() const
+{
+  return d->text;
+}
+
+double DeclarativeInputDialogAttached::getDouble()
+{
+  QWidget *parent = bestParentWindow(d->dialogParent);
+  bool ok = false;
+  const double value = d->value.canConvert<double>() ? d->value.value<double>() : 0.0;
+  const double min = d->min.canConvert<double>() ? d->min.value<double>() : -2147483647;
+  const double max = d->max.canConvert<double>() ? d->max.value<double>() : 2147483647;
+
+  const double retVal = QInputDialog::getDouble(parent, d->title, d->label, value, min, max, d->decimals, &ok);
+
+  setDialogAccepted(ok);
+  return retVal;
+}
+
+int DeclarativeInputDialogAttached::getInt()
+{
+  QWidget *parent = bestParentWindow(d->dialogParent);
+  bool ok = false;
+  const int value = d->value.canConvert<int>() ? d->value.value<int>() : 0;
+  const int min = d->min.canConvert<int>() ? d->min.value<int>() : -2147483647;
+  const int max = d->max.canConvert<int>() ? d->max.value<int>() : 2147483647;
+
+  const int retVal = QInputDialog::getInt(parent, d->title, d->label, value, min, max, d->step, &ok);
+
+  setDialogAccepted(ok);
+  return retVal;
+}
+
+QString DeclarativeInputDialogAttached::getItem(const QStringList &items)
+{
+  QWidget *parent = bestParentWindow(d->dialogParent);
+  bool ok = false;
+
+  const QString retVal = QInputDialog::getItem(parent, d->title, d->label, items, d->currentItem, d->itemsEditable, &ok);
+
+  setDialogAccepted(ok);
+  return retVal;
+}
+
+QString DeclarativeInputDialogAttached::getText()
+{
+  QWidget *parent = bestParentWindow(d->dialogParent);
+  bool ok = false;
+
+  const QString retVal = QInputDialog::getText(parent, d->title, d->label, d->echoMode, d->text, &ok);
+
+  setDialogAccepted(ok);
+  return retVal;
+}
+
+void DeclarativeInputDialogAttached::setDialogAccepted(bool accepted)
+{
+  if (accepted == d->dialogAccepted)
+    return;
+
+  d->dialogAccepted = accepted;
+  emit dialogAcceptedChanged(accepted);
 }
 
 QWidget *DeclarativeInputDialogAttached::bestParentWindow(QObject *parent) const
index 63e65c1..727da1c 100644 (file)
@@ -628,24 +628,97 @@ class DeclarativeGroupBox : public DeclarativeWidgetProxy<QGroupBox>
 class DeclarativeInputDialogAttached : public QObject
 {
   Q_OBJECT
+  Q_PROPERTY(QObject* parent READ dialogParent WRITE setDialogParent NOTIFY dialogParentChanged)
+  Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
+  Q_PROPERTY(QString label READ label WRITE setLabel NOTIFY labelChanged)
+  Q_PROPERTY(bool ok READ dialogAccepted NOTIFY dialogAcceptedChanged)
+
+  Q_PROPERTY(QVariant value READ value WRITE setValue NOTIFY valueChanged)
+  Q_PROPERTY(QVariant min READ min WRITE setMin NOTIFY minChanged)
+  Q_PROPERTY(QVariant max READ max WRITE setMax NOTIFY maxChanged)
+  Q_PROPERTY(int decimals READ decimals WRITE setDecimals NOTIFY decimalsChanged)
+  Q_PROPERTY(int step READ step WRITE setStep NOTIFY stepChanged)
+
+  Q_PROPERTY(int current READ currentItem WRITE setCurrentItem NOTIFY currentItemChanged)
+  Q_PROPERTY(bool editable READ itemsEditable WRITE setItemsEditable NOTIFY itemsEditableChanged)
+
+  Q_PROPERTY(QLineEdit::EchoMode echoMode READ echoMode WRITE setEchoMode NOTIFY echoModeChanged)
+  Q_PROPERTY(QString text READ text WRITE setText NOTIFY textChanged)
 
   public:
     DeclarativeInputDialogAttached(QObject *parent = 0);
+    ~DeclarativeInputDialogAttached();
+
+    void setDialogParent(QObject *parent);
+    QObject *dialogParent() const;
+
+    void setTitle(const QString &title);
+    QString title() const;
+
+    void setLabel(const QString &label);
+    QString label() const;
+
+    bool dialogAccepted() const;
+
+    void setValue(const QVariant &value);
+    QVariant value() const;
+
+    void setMin(const QVariant &min);
+    QVariant min() const;
 
-    Q_INVOKABLE double getDouble(QObject *parent, const QString &title, const QString &label,
-                                 double value = 0, double min = -2147483647, double max = 2147483647, int decimals = 1);
+    void setMax(const QVariant &max);
+    QVariant max() const;
 
-    Q_INVOKABLE int getInt(QObject *parent, const QString &title, const QString &label,
-                           int value = 0, int min = -2147483647, int max = 2147483647, int step = 1);
+    void setDecimals(int decimals);
+    int decimals() const;
 
-    Q_INVOKABLE QString getItem(QObject *parent, const QString &title, const QString &label,
-                                const QStringList &items, int current = 0, bool editable = true);
+    void setStep(int step);
+    int step() const;
 
-    Q_INVOKABLE QString getText(QObject *parent, const QString &title, const QString &label,
-                                int echoMode = QLineEdit::Normal, const QString &text = QString());
+    void setCurrentItem(int current);
+    int currentItem() const;
+
+    void setItemsEditable(bool editable);
+    bool itemsEditable() const;
+
+    void setEchoMode(QLineEdit::EchoMode echoMode);
+    QLineEdit::EchoMode echoMode() const;
+
+    void setText(const QString &text);
+    QString text() const;
+
+    Q_INVOKABLE double getDouble();
+
+    Q_INVOKABLE int getInt();
+
+    Q_INVOKABLE QString getItem(const QStringList &items);
+
+    Q_INVOKABLE QString getText();
+
+  Q_SIGNALS:
+    void dialogParentChanged(QObject *parent);
+    void titleChanged(const QString &title);
+    void labelChanged(const QString &label);
+    void dialogAcceptedChanged(bool accepted);
+
+    void valueChanged(const QVariant &value);
+    void minChanged(const QVariant &min);
+    void maxChanged(const QVariant &max);
+    void decimalsChanged(int decimals);
+    void stepChanged(int step);
+
+    void currentItemChanged(int current);
+    void itemsEditableChanged(bool editable);
+
+    void echoModeChanged(QLineEdit::EchoMode echoMode);
+    void textChanged(const QString &text);
 
   private:
+    void setDialogAccepted(bool accepted);
     QWidget *bestParentWindow(QObject *parent) const;
+
+    class Private;
+    Private *const d;
 };
 
 class DeclarativeInputDialog : public DeclarativeWidgetProxy<InputDialog>
index e44614e..85c372f 100644 (file)
@@ -20,37 +20,87 @@ Widget {
     PushButton {
       text: qsTr("InputDialog::getInt()")
       onClicked: {
-        var value = InputDialog.getInt(0, qsTr("Integer Input"), qsTr("Percentage..."), 25)
+        var value = InputDialog.getInt()
         console.log("InputDialog.getInt returned " + value)
       }
     }
 
     PushButton {
+      text: qsTr("InputDialog::getInt() with options")
+      onClicked: {
+        InputDialog.title = qsTr("Integer Input")
+        InputDialog.label = qsTr("Percentage...")
+        InputDialog.value = 25
+        InputDialog.min = 0
+        InputDialog.max = 100
+        InputDialog.step = 10
+        var value = InputDialog.getInt()
+        console.log("InputDialog.getInt " + (InputDialog.ok ? "returned " + value : "was cancelled"))
+      }
+    }
+
+    PushButton {
       text: qsTr("InputDialog::getDouble()")
       onClicked: {
-        var value = InputDialog.getDouble(0, qsTr("Double Input"), qsTr("Amount:"), 37.56)
+        var value = InputDialog.getDouble()
         console.log("InputDialog.getDouble returned " + value)
       }
     }
 
     PushButton {
+      text: qsTr("InputDialog::getDouble() with options")
+      onClicked: {
+        InputDialog.title = qsTr("Double Input")
+        InputDialog.label = qsTr("Amount:")
+        InputDialog.value = 37.56
+        InputDialog.decimals = 2
+        var value = InputDialog.getDouble()
+        console.log("InputDialog.getDouble " + (InputDialog.ok ? "returned " + value : "was cancelled"))
+      }
+    }
+
+    PushButton {
       text: qsTr("InputDialog::getItem()")
       onClicked: {
-        var items = [ qsTr("Spring"), qsTr("Summer"), qsTr("Autem"), qsTr("Winter") ]
-        var item = InputDialog.getItem(0, qsTr("Item Selection"), qsTr("Season:"), items)
+        var items = [ qsTr("Spring"), qsTr("Summer"), qsTr("Autumn"), qsTr("Winter") ]
+        var item = InputDialog.getItem(items)
         console.log("InputDialog.getItem returned " + item)
       }
     }
 
     PushButton {
+      text: qsTr("InputDialog::getItem() with options")
+      onClicked: {
+         InputDialog.title = qsTr("Item Selection")
+        InputDialog.label = qsTr("Season:")
+        InputDialog.current = 2
+        var items = [ qsTr("Spring"), qsTr("Summer"), qsTr("Autumn"), qsTr("Winter") ]
+        var item = InputDialog.getItem(items)
+        console.log("InputDialog.getItem " + (InputDialog.ok ? "returned " + item : "was cancelled"))
+      }
+    }
+
+    PushButton {
       text: qsTr("InputDialog::getText()")
       onClicked: {
-        var text = InputDialog.getText(0, qsTr("Text Input"), qsTr("User name:"))
+        var text = InputDialog.getText()
         console.log("InputDialog.getText returned " + text)
       }
     }
 
     PushButton {
+      text: qsTr("InputDialog::getText() with options")
+      onClicked: {
+        InputDialog.title = qsTr("Text Input")
+        InputDialog.label = qsTr("Password:")
+        InputDialog.echoMode = LineEdit.Password
+        InputDialog.text = "secret"
+        var text = InputDialog.getText()
+        console.log("InputDialog.getText " + (InputDialog.ok ? "returned " + text : "was cancelled"))
+      }
+    }
+
+    PushButton {
       text: qsTr("Color Dialog...")
       onClicked: {
         if (colorDialog.exec())