From: Kevin Krammer Date: Sun, 28 Oct 2012 09:24:09 +0000 (+0100) Subject: Optional arguments for QColorDialog static methods X-Git-Url: http://git.silmor.de/gitweb/?a=commitdiff_plain;h=a3a4428a5944e1fab477afb44da3b982e359e293;p=konrad%2FDeclarativeWidgets.git Optional arguments for QColorDialog static methods --- diff --git a/declarativeobjects.cpp b/declarativeobjects.cpp index 981a364..6aa65e1 100644 --- a/declarativeobjects.cpp +++ b/declarativeobjects.cpp @@ -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 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(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 diff --git a/declarativeobjects_p.h b/declarativeobjects_p.h index f2ca887..63e65c1 100644 --- a/declarativeobjects_p.h +++ b/declarativeobjects_p.h @@ -443,15 +443,31 @@ class DeclarativeCheckBox : public DeclarativeWidgetProxy 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 diff --git a/dialogs.qml b/dialogs.qml index b686c5e..e44614e 100644 --- a/dialogs.qml +++ b/dialogs.qml @@ -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")) } }