Optional arguments for QColorDialog static methods
authorKevin Krammer <kevin.krammer@kdab.com>
Sun, 28 Oct 2012 09:24:09 +0000 (10:24 +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 981a364..6aa65e1 100644 (file)
@@ -655,18 +655,62 @@ DeclarativeCheckBox::DeclarativeCheckBox(QObject *parent) : DeclarativeWidgetPro
 CUSTOM_METAOBJECT(DeclarativeCheckBox, QCheckBox)
 
 // DeclarativeColorDialog
-DeclarativeColorDialogAttached::DeclarativeColorDialogAttached(QObject *parent) : QObject(parent)
+class DeclarativeColorDialogAttached::Private
 {
+  public:
+    Private() : options(0) {}
+
+  public:
+    QPointer<QObject> dialogParent;
+    QString title;
+    QColorDialog::ColorDialogOptions options;
+};
+
+DeclarativeColorDialogAttached::DeclarativeColorDialogAttached(QObject *parent)
+  : QObject(parent), d(new Private)
+{
+}
+
+DeclarativeColorDialogAttached::~DeclarativeColorDialogAttached()
+{
+  delete d;
+}
+
+void DeclarativeColorDialogAttached::setDialogParent(QObject *parent)
+{
+  if (parent == d->dialogParent)
+    return;
+
+  d->dialogParent = parent;
+  emit dialogParentChanged(parent);
 }
 
-QColor DeclarativeColorDialogAttached::getColor(const QColor &initialColor, QObject *parent, const QString &title, int options)
+QObject *DeclarativeColorDialogAttached::dialogParent() const
 {
-  return QColorDialog::getColor(initialColor, bestParentWindow(parent), title, static_cast<QColorDialog::ColorDialogOptions>(options));
+  return d->dialogParent;
 }
 
-QColor DeclarativeColorDialogAttached::getColor(const QColor &initialColor, QObject *parent)
+void DeclarativeColorDialogAttached::setTitle(const QString &title)
 {
-  return QColorDialog::getColor(initialColor, bestParentWindow(parent));
+  if (title == d->title)
+    return;
+
+  d->title = title;
+  emit titleChanged(title);
+}
+
+QString DeclarativeColorDialogAttached::title() const
+{
+  return d->title;
+}
+
+QColor DeclarativeColorDialogAttached::getColor(const QColor &initialColor)
+{
+  QWidget *parent = bestParentWindow(d->dialogParent);
+  if (!d->title.isEmpty() || d->options != 0)
+    return QColorDialog::getColor(initialColor, parent, d->title, d->options);
+  else
+    return QColorDialog::getColor(initialColor, parent);
 }
 
 QWidget *DeclarativeColorDialogAttached::bestParentWindow(QObject *parent) const
index f2ca887..63e65c1 100644 (file)
@@ -443,15 +443,31 @@ class DeclarativeCheckBox : public DeclarativeWidgetProxy<QCheckBox>
 class DeclarativeColorDialogAttached : public QObject
 {
   Q_OBJECT
+  Q_PROPERTY(QObject* parent READ dialogParent WRITE setDialogParent NOTIFY dialogParentChanged)
+  Q_PROPERTY(QString title READ title WRITE setTitle NOTIFY titleChanged)
+  // TODO option
 
   public:
     DeclarativeColorDialogAttached(QObject *parent = 0);
+    ~DeclarativeColorDialogAttached();
+
+    void setDialogParent(QObject *parent);
+    QObject *dialogParent() const;
+
+    void setTitle(const QString &title);
+    QString title() const;
 
-    Q_INVOKABLE QColor getColor(const QColor &initialColor, QObject *parent, const QString &title, int options = 0);
-    Q_INVOKABLE QColor getColor(const QColor &initialColor, QObject *parent = 0);
+    Q_INVOKABLE QColor getColor(const QColor &initialColor);
+
+  Q_SIGNALS:
+    void dialogParentChanged(QObject *parent);
+    void titleChanged(const QString &title);
 
   private:
     QWidget *bestParentWindow(QObject *parent) const;
+
+    class Private;
+    Private *const d;
 };
 
 class DeclarativeColorDialog : public DeclarativeWidgetProxy<QColorDialog>
index b686c5e..e44614e 100644 (file)
@@ -68,7 +68,8 @@ Widget {
     PushButton {
       text: qsTr("Color Dialog::getColor with title")
       onClicked: {
-          console.log("Selected color: " + ColorDialog.getColor("#ff0000", 0, "Pick a color"))
+          ColorDialog.title = "Pick a color"
+          console.log("Selected color: " + ColorDialog.getColor("#ff0000"))
       }
     }