Add currentColor property to ColorDialog.
authorMitch Curtis <mitch.curtis@digia.com>
Fri, 23 Aug 2013 13:14:33 +0000 (15:14 +0200)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Tue, 17 Sep 2013 05:36:09 +0000 (07:36 +0200)
QColorDialog has this property. This patch effectively means that
the color property will be set when the dialog closes, instead of
whenever the current color in the dialog changes, so pressing cancel
will revert the color to what it was before the dialog was opened.

[ChangeLog][QtDeclarative][ColorDialog] Added currentColor property.

Change-Id: I2ef6b32954342cd2469cf1552d53f9e2fbf3420b
Reviewed-by: Shawn Rutledge <shawn.rutledge@digia.com>
src/imports/dialogs/DefaultColorDialog.qml
src/imports/dialogs/qquickabstractcolordialog.cpp
src/imports/dialogs/qquickabstractcolordialog_p.h
src/imports/dialogs/qquickplatformcolordialog.cpp
src/imports/widgets/qquickqcolordialog.cpp

index 4068c85..1d4c4a7 100644 (file)
@@ -48,15 +48,16 @@ AbstractColorDialog {
     property bool _valueSet: true // guard to prevent binding loops
     function _setControlsFromColor() {
         _valueSet = false
-        hueSlider.value = root.hue
-        saturationSlider.value = root.saturation
-        lightnessSlider.value = root.lightness
-        alphaSlider.value = root.alpha
-        crosshairs.x = root.lightness * paletteMap.width
-        crosshairs.y = (1.0 - root.saturation) * paletteMap.height
+        hueSlider.value = root.currentHue
+        saturationSlider.value = root.currentSaturation
+        lightnessSlider.value = root.currentLightness
+        alphaSlider.value = root.currentAlpha
+        crosshairs.x = root.currentLightness * paletteMap.width
+        crosshairs.y = (1.0 - root.currentSaturation) * paletteMap.height
         _valueSet = true
     }
-    onColorChanged: _setControlsFromColor()
+    onCurrentColorChanged: _setControlsFromColor()
+    onSelectionAccepted: root.color = root.currentColor
 
     Rectangle {
         id: content
@@ -204,7 +205,7 @@ AbstractColorDialog {
             ColorSlider {
                 id: hueSlider
                 value: 0.5
-                onValueChanged: if (_valueSet) root.color = Qt.hsla(hueSlider.value, saturationSlider.value, lightnessSlider.value, alphaSlider.value)
+                onValueChanged: if (_valueSet) root.currentColor = Qt.hsla(hueSlider.value, saturationSlider.value, lightnessSlider.value, alphaSlider.value)
                 text: qsTr("Hue")
                 trackDelegate: Rectangle {
                     rotation: -90
@@ -225,7 +226,7 @@ AbstractColorDialog {
                 id: saturationSlider
                 visible: !content.usePaletteMap
                 value: 0.5
-                onValueChanged: if (_valueSet) root.color = Qt.hsla(hueSlider.value, saturationSlider.value, lightnessSlider.value, alphaSlider.value)
+                onValueChanged: if (_valueSet) root.currentColor = Qt.hsla(hueSlider.value, saturationSlider.value, lightnessSlider.value, alphaSlider.value)
                 text: qsTr("Saturation")
                 trackDelegate: Rectangle {
                     rotation: -90
@@ -241,7 +242,7 @@ AbstractColorDialog {
                 id: lightnessSlider
                 visible: !content.usePaletteMap
                 value: 0.5
-                onValueChanged: if (_valueSet) root.color = Qt.hsla(hueSlider.value, saturationSlider.value, lightnessSlider.value, alphaSlider.value)
+                onValueChanged: if (_valueSet) root.currentColor = Qt.hsla(hueSlider.value, saturationSlider.value, lightnessSlider.value, alphaSlider.value)
                 text: qsTr("Luminosity")
                 trackDelegate: Rectangle {
                     rotation: -90
@@ -259,7 +260,7 @@ AbstractColorDialog {
                 minimum: 0.0
                 maximum: 1.0
                 value: 1.0
-                onValueChanged: if (_valueSet) root.color = Qt.hsla(hueSlider.value, saturationSlider.value, lightnessSlider.value, alphaSlider.value)
+                onValueChanged: if (_valueSet) root.currentColor = Qt.hsla(hueSlider.value, saturationSlider.value, lightnessSlider.value, alphaSlider.value)
                 text: qsTr("Alpha")
                 visible: root.showAlphaChannel
                 trackDelegate: Item {
@@ -296,9 +297,9 @@ AbstractColorDialog {
                 spacing: content.spacing
                 TextField {
                     id: colorField
-                    text: root.color.toString()
+                    text: root.currentColor.toString()
                     anchors.verticalCenter: parent.verticalCenter
-                    onAccepted:  root.color = text
+                    onAccepted:  root.currentColor = text
                     Component.onCompleted: width = implicitWidth + 10
                 }
                 Image {
index d565352..abac997 100644 (file)
@@ -109,6 +109,15 @@ void QQuickAbstractColorDialog::setColor(QColor arg)
         m_color = arg;
         emit colorChanged();
     }
+    setCurrentColor(arg);
+}
+
+void QQuickAbstractColorDialog::setCurrentColor(QColor currentColor)
+{
+    if (m_currentColor != currentColor) {
+        m_currentColor = currentColor;
+        emit currentColorChanged();
+    }
 }
 
 void QQuickAbstractColorDialog::setShowAlphaChannel(bool arg)
index bd23e0d..ad2c7ce 100644 (file)
@@ -66,10 +66,11 @@ class QQuickAbstractColorDialog : public QQuickAbstractDialog
     Q_OBJECT
     Q_PROPERTY(bool showAlphaChannel READ showAlphaChannel WRITE setShowAlphaChannel NOTIFY showAlphaChannelChanged)
     Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
-    Q_PROPERTY(qreal hue READ hue NOTIFY colorChanged)
-    Q_PROPERTY(qreal saturation READ saturation NOTIFY colorChanged)
-    Q_PROPERTY(qreal lightness READ lightness NOTIFY colorChanged)
-    Q_PROPERTY(qreal alpha READ alpha NOTIFY colorChanged)
+    Q_PROPERTY(QColor currentColor READ currentColor WRITE setCurrentColor NOTIFY currentColorChanged)
+    Q_PROPERTY(qreal currentHue READ currentHue NOTIFY currentColorChanged)
+    Q_PROPERTY(qreal currentSaturation READ currentSaturation NOTIFY currentColorChanged)
+    Q_PROPERTY(qreal currentLightness READ currentLightness NOTIFY currentColorChanged)
+    Q_PROPERTY(qreal currentAlpha READ currentAlpha NOTIFY currentColorChanged)
 
 public:
     QQuickAbstractColorDialog(QObject *parent = 0);
@@ -78,27 +79,31 @@ public:
     virtual QString title() const;
     bool showAlphaChannel() const;
     QColor color() const { return m_color; }
-    qreal hue() const { return m_color.hslHueF(); }
-    qreal saturation() const { return m_color.hslSaturationF(); }
-    qreal lightness() const { return m_color.lightnessF(); }
-    qreal alpha() const { return m_color.alphaF(); }
+    QColor currentColor() const { return m_currentColor; }
+    qreal currentHue() const { return m_currentColor.hslHueF(); }
+    qreal currentSaturation() const { return m_currentColor.hslSaturationF(); }
+    qreal currentLightness() const { return m_currentColor.lightnessF(); }
+    qreal currentAlpha() const { return m_currentColor.alphaF(); }
 
 public Q_SLOTS:
     void setVisible(bool v);
     void setModality(Qt::WindowModality m);
     void setTitle(const QString &t);
     void setColor(QColor arg);
+    void setCurrentColor(QColor currentColor);
     void setShowAlphaChannel(bool arg);
 
 Q_SIGNALS:
     void showAlphaChannelChanged();
     void colorChanged();
+    void currentColorChanged();
     void selectionAccepted();
 
 protected:
     QPlatformColorDialogHelper *m_dlgHelper;
     QSharedPointer<QColorDialogOptions> m_options;
     QColor m_color;
+    QColor m_currentColor;
 
     Q_DISABLE_COPY(QQuickAbstractColorDialog)
 };
index 8dc6d09..9de9b7a 100644 (file)
@@ -166,7 +166,7 @@ QPlatformColorDialogHelper *QQuickPlatformColorDialog::helper()
             return m_dlgHelper;
         connect(m_dlgHelper, SIGNAL(accept()), this, SLOT(accept()));
         connect(m_dlgHelper, SIGNAL(reject()), this, SLOT(reject()));
-        connect(m_dlgHelper, SIGNAL(currentColorChanged(QColor)), this, SLOT(setColor(QColor)));
+        connect(m_dlgHelper, SIGNAL(currentColorChanged(QColor)), this, SLOT(setCurrentColor(QColor)));
         connect(m_dlgHelper, SIGNAL(colorSelected(QColor)), this, SLOT(setColor(QColor)));
     }
 
@@ -232,6 +232,23 @@ QPlatformColorDialogHelper *QQuickPlatformColorDialog::helper()
     \qmlproperty color ColorDialog::color
 
     The color which the user selected.
+
+    \note This color is not always the same as the color held by the
+    currentColor property since the user can choose different colors before
+    finally selecting the one to use.
+
+    \sa currentColor
+*/
+
+/*!
+    \qmlproperty color ColorDialog::currentColor
+
+    The color which the user has currently selected.
+
+    For the color that is set when the dialog is accepted, use the \l color
+    property.
+
+    \sa color
 */
 
 QT_END_NAMESPACE
index d10eace..ee27d14 100644 (file)
@@ -163,7 +163,7 @@ QPlatformColorDialogHelper *QQuickQColorDialog::helper()
 
     if (!m_dlgHelper) {
         m_dlgHelper = new QColorDialogHelper();
-        connect(m_dlgHelper, SIGNAL(currentColorChanged(const QColor&)), this, SLOT(setColor(QColor)));
+        connect(m_dlgHelper, SIGNAL(currentColorChanged(const QColor&)), this, SLOT(setCurrentColor(QColor)));
         connect(m_dlgHelper, SIGNAL(colorSelected(const QColor&)), this, SLOT(setColor(QColor)));
         connect(m_dlgHelper, SIGNAL(accept()), this, SLOT(accept()));
         connect(m_dlgHelper, SIGNAL(reject()), this, SLOT(reject()));