]> git.sesse.net Git - kdenlive/blobdiff - src/choosecolorwidget.cpp
Add GUI for MLT filter dynamic text.
[kdenlive] / src / choosecolorwidget.cpp
index 1b81ca3123536d11f667a43adeaa75d088df1299..aa154b6ed9f935f4906ac6afdca2c3ad01d85455 100644 (file)
 #include <KColorButton>
 #include <KLocalizedString>
 
+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
+    }
+
+    return color;
+}
+
+static QString colorToString(QColor color, bool alpha)
+{
+    QString colorStr;
+    QTextStream stream(&colorStr);
+    stream << "#";
+    stream.setIntegerBase(16);
+    stream.setFieldWidth(2);
+    stream.setFieldAlignment(QTextStream::AlignRight);
+    stream.setPadChar('0');
+    if(alpha)
+    {
+        stream << color.alpha();
+    }
+    stream <<  color.red() << color.green() << color.blue();
 
-ChooseColorWidget::ChooseColorWidget(QString text, QColor color, QWidget *parent) :
+    return colorStr;
+}
+
+ChooseColorWidget::ChooseColorWidget(QString text, QString color, QWidget *parent) :
         QWidget(parent)
 {
     QHBoxLayout *layout = new QHBoxLayout(this);
@@ -36,7 +87,7 @@ ChooseColorWidget::ChooseColorWidget(QString text, QColor color, QWidget *parent
     layout->setSpacing(0);
 
     QLabel *label = new QLabel(text, this);
-    m_button = new KColorButton(color, this);
+    m_button = new KColorButton(stringToColor(color), this);
     m_button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
     ColorPickerWidget *picker = new ColorPickerWidget(this);
 
@@ -49,9 +100,14 @@ ChooseColorWidget::ChooseColorWidget(QString text, QColor color, QWidget *parent
     connect(m_button, SIGNAL(changed(QColor)), this, SIGNAL(modified()));
 }
 
-QColor ChooseColorWidget::getColor()
+QString ChooseColorWidget::getColor()
+{
+    return colorToString(m_button->color(), m_button->isAlphaChannelEnabled());
+}
+
+void ChooseColorWidget::setAlphaChannelEnabled(bool enabled)
 {
-    return m_button->color();
+    m_button->setAlphaChannelEnabled(enabled);
 }
 
 void ChooseColorWidget::setColor(QColor color)