From 5cda49abed0a01eaf282f0a59ae61d086b221ecf Mon Sep 17 00:00:00 2001 From: Kevin Krammer Date: Mon, 22 Oct 2012 15:12:51 +0200 Subject: [PATCH] Support for QColorDialog static getColor methods --- declarativeobjects.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ declarativeobjects_p.h | 18 ++++++++++++++++++ declarativewidgetdocument.cpp | 1 + dialogs.qml | 14 ++++++++++++++ 4 files changed, 73 insertions(+), 0 deletions(-) diff --git a/declarativeobjects.cpp b/declarativeobjects.cpp index ca68a48..b34c07c 100644 --- a/declarativeobjects.cpp +++ b/declarativeobjects.cpp @@ -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(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(parent); + if (declarativeObject) + parent = declarativeObject->object(); + + while (parent) { + QWidget *widget = qobject_cast(parent); + if (widget) + return widget->topLevelWidget(); + + parent = parent->parent(); + } + + return 0; +} + DeclarativeColorDialog::DeclarativeColorDialog(QObject *parent) : DeclarativeWidgetProxy(parent) { connectAllSignals(m_proxiedObject, this); } +DeclarativeColorDialogAttached *DeclarativeColorDialog::qmlAttachedProperties(QObject *parent) +{ + return new DeclarativeColorDialogAttached(parent); +} + CUSTOM_METAOBJECT(DeclarativeColorDialog, QColorDialog) // DeclarativeDialog diff --git a/declarativeobjects_p.h b/declarativeobjects_p.h index 066cee3..fd170e5 100644 --- a/declarativeobjects_p.h +++ b/declarativeobjects_p.h @@ -400,14 +400,32 @@ class DeclarativeCheckBox : public DeclarativeWidgetProxy 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 { DECLARATIVE_OBJECT public: DeclarativeColorDialog(QObject *parent = 0); + + static DeclarativeColorDialogAttached *qmlAttachedProperties(QObject *parent); }; +QML_DECLARE_TYPEINFO(DeclarativeColorDialog, QML_HAS_ATTACHED_PROPERTIES) + class DeclarativeDialog : public DeclarativeWidgetProxy { DECLARATIVE_OBJECT diff --git a/declarativewidgetdocument.cpp b/declarativewidgetdocument.cpp index 6471cd5..31e0d13 100644 --- a/declarativewidgetdocument.cpp +++ b/declarativewidgetdocument.cpp @@ -46,6 +46,7 @@ DeclarativeWidgetDocument::DeclarativeWidgetDocument(const QUrl &url, QObject *p // widgets qmlRegisterType("QtGui", 1, 0, "CalendarWidget"); qmlRegisterType("QtGui", 1, 0, "CheckBox"); + qmlRegisterType(); qmlRegisterType("QtGui", 1, 0, "ColorDialog"); qmlRegisterType("QtGui", 1, 0, "Dialog"); qmlRegisterType("QtGui", 1, 0, "DialogButtonBox"); diff --git a/dialogs.qml b/dialogs.qml index a87cf4c..7de2616 100644 --- a/dialogs.qml +++ b/dialogs.qml @@ -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()) -- 1.7.2.5