]> git.sesse.net Git - kdenlive/blobdiff - src/choosecolorwidget.cpp
Const'ref
[kdenlive] / src / choosecolorwidget.cpp
index aa154b6ed9f935f4906ac6afdca2c3ad01d85455..f8b8d3024b7050e30d4883316808a8948ae64a9b 100644 (file)
 
 #include <KColorButton>
 #include <KLocalizedString>
+#include <kdeversion.h>
+#include <KDebug>
 
 static QColor stringToColor(QString strColor)
 {
     bool ok = false;
     QColor color("black");
     int intval = 0;
-
     if (strColor.startsWith("0x")) {
-        // Format must be 0xRRGGBBAA
-        intval = strColor.toUInt(&ok, 16);
-        color.setRgb( ( intval >> 24 ) & 0xff,   // r
-                      ( intval >> 16 ) & 0xff,   // g
-                      ( intval >>  8 ) & 0xff,   // b
-                      ( intval )       & 0xff ); // a
-    } else if (strColor.startsWith("#") && strColor.length() == 9) {
-        // Format must be #AARRGGBB
-        strColor = strColor.replace('#', "0x");
-        intval = strColor.toUInt(&ok, 16);
-        color.setRgb( ( intval >> 16 ) & 0xff,   // r
-                      ( intval >>  8 ) & 0xff,   // g
-                      ( intval       ) & 0xff,   // b
-                      ( intval >> 24 ) & 0xff ); // a
-    } else if (strColor.startsWith("#") && strColor.length() == 7) {
-        // Format must be #RRGGBB
-        strColor = strColor.replace('#', "0x");
-        intval = strColor.toUInt(&ok, 16);
-        color.setRgb( ( intval >> 16 ) & 0xff,   // r
-                      ( intval >>  8 ) & 0xff,   // g
-                      ( intval       ) & 0xff,   // b
-                      0xff );                    // a
+        if (strColor.length() == 10) {
+            // 0xRRGGBBAA
+            intval = strColor.toUInt(&ok, 16);
+            color.setRgb( ( intval >> 24 ) & 0xff,   // r
+                          ( intval >> 16 ) & 0xff,   // g
+                          ( intval >>  8 ) & 0xff,   // b
+                          ( intval       ) & 0xff ); // a
+        } else {
+            // 0xRRGGBB, 0xRGB
+            color.setNamedColor(strColor.replace(0, 2, "#"));
+        }
+    } else {
+        if (strColor.length() == 9) {
+            // #AARRGGBB
+            strColor = strColor.replace('#', "0x");
+            intval = strColor.toUInt(&ok, 16);
+            color.setRgb( ( intval >> 16 ) & 0xff,   // r
+                          ( intval >>  8 ) & 0xff,   // g
+                          ( intval       ) & 0xff,   // b
+                          ( intval >> 24 ) & 0xff ); // a
+       } else if (strColor.length() == 8) {
+           // 0xRRGGBB
+           strColor = strColor.replace('#', "0x");
+           color.setNamedColor(strColor);
+        } else {
+            // #RRGGBB, #RGB
+            color.setNamedColor(strColor);
+        }
     }
 
     return color;
 }
 
-static QString colorToString(QColor color, bool alpha)
+static QString colorToString(const QColor &color, bool alpha)
 {
     QString colorStr;
     QTextStream stream(&colorStr);
-    stream << "#";
+    stream << "0x";
     stream.setIntegerBase(16);
     stream.setFieldWidth(2);
     stream.setFieldAlignment(QTextStream::AlignRight);
     stream.setPadChar('0');
-    if(alpha)
-    {
+    stream <<  color.red() << color.green() << color.blue();
+    if (alpha) {
         stream << color.alpha();
+    } else {
+       // MLT always wants 0xRRGGBBAA format
+       stream << "ff";
     }
-    stream <<  color.red() << color.green() << color.blue();
-
     return colorStr;
 }
 
-ChooseColorWidget::ChooseColorWidget(QString text, QString color, QWidget *parent) :
+ChooseColorWidget::ChooseColorWidget(const QString &text, const QString &color, bool alphaEnabled, QWidget *parent) :
         QWidget(parent)
 {
     QHBoxLayout *layout = new QHBoxLayout(this);
@@ -87,30 +95,40 @@ ChooseColorWidget::ChooseColorWidget(QString text, QString color, QWidget *paren
     layout->setSpacing(0);
 
     QLabel *label = new QLabel(text, this);
-    m_button = new KColorButton(stringToColor(color), this);
-    m_button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
-    ColorPickerWidget *picker = new ColorPickerWidget(this);
+
+    QWidget *rightSide = new QWidget(this);
+    QHBoxLayout *rightSideLayout = new QHBoxLayout(rightSide);
+    rightSideLayout->setContentsMargins(0, 0, 0, 0);
+    rightSideLayout->setSpacing(0);
+
+    m_button = new KColorButton(stringToColor(color), rightSide);
+#if KDE_IS_VERSION(4,5,0)
+    if (alphaEnabled) m_button->setAlphaChannelEnabled(alphaEnabled);
+#endif
+//     m_button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
+    ColorPickerWidget *picker = new ColorPickerWidget(rightSide);
 
     layout->addWidget(label);
-    layout->addWidget(m_button);
-    layout->addWidget(picker, 0, Qt::AlignRight);
+    layout->addWidget(rightSide);
+    rightSideLayout->addWidget(m_button);
+    rightSideLayout->addWidget(picker, 0, Qt::AlignRight);
 
     connect(picker, SIGNAL(colorPicked(QColor)), this, SLOT(setColor(QColor)));
-    connect(picker, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
+    connect(picker, SIGNAL(displayMessage(QString,int)), this, SIGNAL(displayMessage(QString,int)));
+    connect(picker, SIGNAL(disableCurrentFilter(bool)), this, SIGNAL(disableCurrentFilter(bool)));
     connect(m_button, SIGNAL(changed(QColor)), this, SIGNAL(modified()));
 }
 
-QString ChooseColorWidget::getColor()
-{
-    return colorToString(m_button->color(), m_button->isAlphaChannelEnabled());
-}
-
-void ChooseColorWidget::setAlphaChannelEnabled(bool enabled)
+QString ChooseColorWidget::getColor() const
 {
-    m_button->setAlphaChannelEnabled(enabled);
+    bool alphaChannel = false;
+#if KDE_IS_VERSION(4,5,0)
+    alphaChannel = m_button->isAlphaChannelEnabled();
+#endif
+    return colorToString(m_button->color(), alphaChannel);
 }
 
-void ChooseColorWidget::setColor(QColor color)
+void ChooseColorWidget::setColor(const QColor& color)
 {
     m_button->setColor(color);
 }