From 8b2ecc3c6efcb73b4435b8c83e076654382ce85f Mon Sep 17 00:00:00 2001
From: Thomas Hartmann <Thomas.Hartmann@nokia.com>
Date: Fri, 9 Apr 2010 17:30:58 +0200
Subject: [PATCH] QmlDesigner.propertyEditor: add editing of alpha channels for
 colors

---
 .../propertyeditor/Qt/ColorGroupBox.qml       | 23 +++++-
 .../components/propertyeditor/colorwidget.cpp |  4 +
 .../components/propertyeditor/colorwidget.h   | 73 +++++++++++++++++--
 3 files changed, 93 insertions(+), 7 deletions(-)

diff --git a/share/qtcreator/qmldesigner/propertyeditor/Qt/ColorGroupBox.qml b/share/qtcreator/qmldesigner/propertyeditor/Qt/ColorGroupBox.qml
index 0cd9c53fec5..7dd9cd0cfe3 100644
--- a/share/qtcreator/qmldesigner/propertyeditor/Qt/ColorGroupBox.qml
+++ b/share/qtcreator/qmldesigner/propertyeditor/Qt/ColorGroupBox.qml
@@ -65,20 +65,24 @@ QExtGroupBox {
                     valueSpinBox.setStyleSheet("color: "+scheme.changedBaseColor);
                     hueSpinBox.setStyleSheet("color: "+scheme.changedBaseColor);
                     saturationSpinBox.setStyleSheet("color: "+scheme.changedBaseColor);
+                    alphaSpinBox.setStyleSheet("color: "+scheme.changedBaseColor);
                 } else {
                     valueSpinBox.setStyleSheet("color: "+scheme.defaultColor);
                     hueSpinBox.setStyleSheet("color: "+scheme.defaultColor);
                     saturationSpinBox.setStyleSheet("color: "+scheme.defaultColor);
+                    alphaSpinBox.setStyleSheet("color: "+scheme.defaultColor);
                 }
             } else {
                 if (backendColor.isInSubState) {
                     valueSpinBox.setStyleSheet("color: "+scheme.changedStateColor);
                     hueSpinBox.setStyleSheet("color: "+scheme.changedStateColor);
                     saturationSpinBox.setStyleSheet("color: "+scheme.changedStateColor);
+                    alphaSpinBox.setStyleSheet("color: "+scheme.changedStateColor);
                 } else {
                     valueSpinBox.setStyleSheet("color: "+scheme.defaultColor);
                     hueSpinBox.setStyleSheet("color: "+scheme.defaultColor);
                     saturationSpinBox.setStyleSheet("color: "+scheme.defaultColor);
+                    alphaSpinBox.setStyleSheet("color: "+scheme.defaultColor);
                 }
             }
         }
@@ -133,7 +137,6 @@ QExtGroupBox {
     }
 
 
-
     layout: VerticalLayout {
         topMargin: 36
 
@@ -199,6 +202,7 @@ QExtGroupBox {
                             }
 
                         }
+                        
                         QWidget {
                             layout: HorizontalLayout {
                                 Label {
@@ -214,6 +218,23 @@ QExtGroupBox {
                                 }
                             }
                         }
+                        
+                        QWidget {
+                            layout: HorizontalLayout {
+                                topMargin: 12
+                                Label {
+                                    text: "A"
+                                    fixedWidth: 15
+                                }
+                                QSpinBox {
+                                    id: alphaSpinBox
+                                    maximum: 255
+                                    value: colorControl.alpha;
+                                    onValueChanged: if (Math.floor(colorControl.alpha)!=value)
+                                    colorControl.alpha=value;
+                                }
+                            }
+                        }
 
                     }
                 }
diff --git a/src/plugins/qmldesigner/components/propertyeditor/colorwidget.cpp b/src/plugins/qmldesigner/components/propertyeditor/colorwidget.cpp
index 26fa81f04d8..8c716350dbd 100644
--- a/src/plugins/qmldesigner/components/propertyeditor/colorwidget.cpp
+++ b/src/plugins/qmldesigner/components/propertyeditor/colorwidget.cpp
@@ -87,7 +87,9 @@ namespace QmlDesigner {
     {
         if (y<0) y=0;
         if (y>120) y=120;
+        int oldAlpha = m_color.alpha();
         m_color.setHsv((y * 359)/120, m_color.hsvSaturation(), m_color.value());
+        m_color.setAlpha(oldAlpha);
         update(); // redraw pointer
         emit hueChanged();
     }
@@ -139,7 +141,9 @@ namespace QmlDesigner {
         if (x>120) x=120;
         if (y<0) y=0;
         if (y>120) y=120;
+        int oldAlpha = m_color.alpha();
         newColor.setHsv(hue(), (x*255) / 120, 255 - (y*255) / 120);
+        newColor.setAlpha(oldAlpha);
         setColor(newColor);
     }
 
diff --git a/src/plugins/qmldesigner/components/propertyeditor/colorwidget.h b/src/plugins/qmldesigner/components/propertyeditor/colorwidget.h
index 81c8900ec24..baba3e3ab39 100644
--- a/src/plugins/qmldesigner/components/propertyeditor/colorwidget.h
+++ b/src/plugins/qmldesigner/components/propertyeditor/colorwidget.h
@@ -82,14 +82,46 @@ private:
     QString m_colorString;
 };
 
+inline QString properName(const QColor &color)
+{
+    QString s;
+    if (color.alpha() == 255)
+        s.sprintf("#%02x%02x%02x", color.red(), color.green(), color.blue());
+    else
+        s.sprintf("#%02x%02x%02x%02x", color.alpha(), color.red(), color.green(), color.blue());
+    return s;
+}
+
+inline QColor properColor(const QString &str)
+{
+    int lalpha = 255;
+    QString lcolorStr = str;
+    if (lcolorStr.at(0) == '#' && lcolorStr.length() == 9) {
+        QString alphaStr = lcolorStr;
+        alphaStr.truncate(3);
+        lcolorStr.remove(0, 3);
+        lcolorStr = "#" + lcolorStr;
+        alphaStr.remove(0,1);
+        bool v;
+        lalpha = alphaStr.toInt(&v, 16);
+        if (!v)
+            lalpha = 255;
+    }
+    QColor lcolor(lcolorStr);
+    lcolor.setAlpha(lalpha);
+    return lcolor;
+}
+
 class ColorBox : public QWidget
 {
 Q_OBJECT
 
-Q_PROPERTY(QString color READ color WRITE setColor NOTIFY colorChanged)
+Q_PROPERTY(QString strColor READ strColor WRITE setStrColor NOTIFY colorChanged)
+Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
 Q_PROPERTY(int hue READ hue WRITE setHue NOTIFY hueChanged)
 Q_PROPERTY(int saturation READ saturation WRITE setSaturation NOTIFY saturationChanged)
 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
+Q_PROPERTY(int alpha READ alpha WRITE setAlpha NOTIFY alphaChanged)
 
 public:
 
@@ -104,7 +136,9 @@ void setHue(int newHue)
     if (m_color.hsvHue() == newHue)
         return;
 
+    int oldAlpha = m_color.alpha();
     m_color.setHsv(newHue,m_color.hsvSaturation(),m_color.value());
+    m_color.setAlpha(oldAlpha);
     update();
     emit hueChanged();
     emit colorChanged();
@@ -118,12 +152,28 @@ int hue() const
     return retval;
 }
 
-void setColor(const QString &colorStr)
+void setAlpha(int newAlpha)
+{
+    if (m_color.alpha() == newAlpha)
+        return;
+
+    m_color.setAlpha(newAlpha);
+    update();
+    emit alphaChanged();
+    emit colorChanged();
+}
+
+int alpha() const
 {
-    if (m_color.name() == colorStr)
+    return m_color.alpha();
+}
+
+void setStrColor(const QString &colorStr)
+{
+    if (properName(m_color) == colorStr)
         return;
 
-    setColor(QColor(colorStr));
+    setColor(properColor(colorStr));
 }
 
 void setColor(const QColor &color)
@@ -134,19 +184,25 @@ void setColor(const QColor &color)
     int oldsaturation = m_color.hsvSaturation();
     int oldvalue = m_color.value();
     int oldhue = m_color.hsvHue();
+    int oldAlpha = m_color.alpha();
     m_color=color;
     update();
     if (oldhue != m_color.hsvHue()) emit hueChanged();
     if (oldsaturation != saturation()) emit saturationChanged();
     if (oldvalue != value()) emit valueChanged();
+    if (oldAlpha != alpha()) emit alphaChanged();
     emit colorChanged();
 }
 
-QString color() const
+QString strColor() const
 {
-    return m_color.name();
+    return properName(m_color);
 }
 
+QColor color() const
+{
+    return m_color;
+}
 
 int saturation() const
 {
@@ -156,7 +212,9 @@ int saturation() const
 void setSaturation(int newsaturation)
 {
     if (m_color.hsvSaturation()==newsaturation) return;
+    int oldAlpha = m_color.alpha();
     m_color.setHsv(m_color.hsvHue(),newsaturation,m_color.value());
+    m_color.setAlpha(oldAlpha);
     update();
     emit saturationChanged();
     emit colorChanged();
@@ -170,7 +228,9 @@ int value() const
 void setValue(int newvalue)
 {
     if (m_color.value()==newvalue) return;
+    int oldAlpha = m_color.alpha();
     m_color.setHsv(m_color.hsvHue(),m_color.hsvSaturation(),newvalue);
+    m_color.setAlpha(oldAlpha);
     update();
     emit valueChanged();
     emit colorChanged();
@@ -181,6 +241,7 @@ signals:
     void hueChanged();
     void saturationChanged();
     void valueChanged();
+    void alphaChanged();
 
 protected:
     void paintEvent(QPaintEvent *event);
-- 
GitLab