Support for QColorDialog static getColor methods
authorKevin Krammer <kevin.krammer@kdab.com>
Mon, 22 Oct 2012 13:12:51 +0000 (15:12 +0200)
committerKevin Krammer <kevin.krammer@kdab.com>
Mon, 22 Oct 2012 13:12:51 +0000 (15:12 +0200)
declarativeobjects.cpp
declarativeobjects_p.h
declarativewidgetdocument.cpp
dialogs.qml

index ca68a48..b34c07c 100644 (file)
@@ -639,11 +639,51 @@ DeclarativeCheckBox::DeclarativeCheckBox(QObject *parent) : DeclarativeWidgetPro
 CUSTOM_METAOBJECT(DeclarativeCheckBox, QCheckBox)
 
 // DeclarativeColorDialog
+DeclarativeColorDialogAttached::DeclarativeColorDialogAttached(QObject *parent) : QObject(parent)
+{
+}
+
+QColor DeclarativeColorDialogAttached::getColor(const QColor &initialColor, QObject *parent, const QString &title, int options)
+{
+  return QColorDialog::getColor(initialColor, bestParentWindow(parent), title, static_cast<QColorDialog::ColorDialogOptions>(options));
+}
+
+QColor DeclarativeColorDialogAttached::getColor(const QColor &initialColor, QObject *parent)
+{
+  return QColorDialog::getColor(initialColor, bestParentWindow(parent));
+}
+
+QWidget *DeclarativeColorDialogAttached::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;
+}
+
 DeclarativeColorDialog::DeclarativeColorDialog(QObject *parent) : DeclarativeWidgetProxy<QColorDialog>(parent)
 {
   connectAllSignals(m_proxiedObject, this);
 }
 
+DeclarativeColorDialogAttached *DeclarativeColorDialog::qmlAttachedProperties(QObject *parent)
+{
+  return new DeclarativeColorDialogAttached(parent);
+}
+
 CUSTOM_METAOBJECT(DeclarativeColorDialog, QColorDialog)
 
 // DeclarativeDialog
index 066cee3..fd170e5 100644 (file)
@@ -400,14 +400,32 @@ class DeclarativeCheckBox : public DeclarativeWidgetProxy<QCheckBox>
     DeclarativeCheckBox(QObject *parent = 0);
 };
 
+class DeclarativeColorDialogAttached : public QObject
+{
+  Q_OBJECT
+
+  public:
+    DeclarativeColorDialogAttached(QObject *parent = 0);
+
+    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);
+
+  private:
+    QWidget *bestParentWindow(QObject *parent) const;
+};
+
 class DeclarativeColorDialog : public DeclarativeWidgetProxy<QColorDialog>
 {
   DECLARATIVE_OBJECT
 
   public:
     DeclarativeColorDialog(QObject *parent = 0);
+
+    static DeclarativeColorDialogAttached *qmlAttachedProperties(QObject *parent);
 };
 
+QML_DECLARE_TYPEINFO(DeclarativeColorDialog, QML_HAS_ATTACHED_PROPERTIES)
+
 class DeclarativeDialog : public DeclarativeWidgetProxy<QDialog>
 {
   DECLARATIVE_OBJECT
index 6471cd5..31e0d13 100644 (file)
@@ -46,6 +46,7 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p
   // widgets
   qmlRegisterType<DeclarativeCalendarWidget>("QtGui", 1, 0, "CalendarWidget");
   qmlRegisterType<DeclarativeCheckBox>("QtGui", 1, 0, "CheckBox");
+  qmlRegisterType<DeclarativeColorDialogAttached>();
   qmlRegisterType<DeclarativeColorDialog>("QtGui", 1, 0, "ColorDialog");
   qmlRegisterType<DeclarativeDialog>("QtGui", 1, 0, "Dialog");
   qmlRegisterType<DeclarativeDialogButtonBox>("QtGui", 1, 0, "DialogButtonBox");
index a87cf4c..7de2616 100644 (file)
@@ -32,6 +32,20 @@ Widget {
     }
 
     PushButton {
+      text: qsTr("Color Dialog::getColor")
+      onClicked: {
+          console.log("Selected color: " + ColorDialog.getColor("#ff0000"))
+      }
+    }
+
+    PushButton {
+      text: qsTr("Color Dialog::getColor with title")
+      onClicked: {
+          console.log("Selected color: " + ColorDialog.getColor("#ff0000", 0, "Pick a color"))
+      }
+    }
+
+    PushButton {
       text: qsTr("File Dialog...")
       onClicked: {
         if (fileDialog.exec())