]> git.sesse.net Git - kdenlive/blob - src/choosecolorwidget.cpp
Add GUI for MLT filter dynamic text.
[kdenlive] / src / choosecolorwidget.cpp
1 /***************************************************************************
2  *   Copyright (C) 2010 by Till Theato (root@ttill.de)                     *
3  *                                                                         *
4  *   This program is free software; you can redistribute it and/or modify  *
5  *   it under the terms of the GNU General Public License as published by  *
6  *   the Free Software Foundation; either version 2 of the License, or     *
7  *   (at your option) any later version.                                   *
8  *                                                                         *
9  *   This program is distributed in the hope that it will be useful,       *
10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12  *   GNU General Public License for more details.                          *
13  *                                                                         *
14  *   You should have received a copy of the GNU General Public License     *
15  *   along with this program; if not, write to the                         *
16  *   Free Software Foundation, Inc.,                                       *
17  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA          *
18  ***************************************************************************/
19
20
21 #include "choosecolorwidget.h"
22 #include "colorpickerwidget.h"
23
24 #include <QLabel>
25 #include <QHBoxLayout>
26
27 #include <KColorButton>
28 #include <KLocalizedString>
29
30 static QColor stringToColor(QString strColor)
31 {
32     bool ok = false;
33     QColor color("black");
34     int intval = 0;
35
36     if (strColor.startsWith("0x")) {
37         // Format must be 0xRRGGBBAA
38         intval = strColor.toUInt(&ok, 16);
39         color.setRgb( ( intval >> 24 ) & 0xff,   // r
40                       ( intval >> 16 ) & 0xff,   // g
41                       ( intval >>  8 ) & 0xff,   // b
42                       ( intval )       & 0xff ); // a
43     } else if (strColor.startsWith("#") && strColor.length() == 9) {
44         // Format must be #AARRGGBB
45         strColor = strColor.replace('#', "0x");
46         intval = strColor.toUInt(&ok, 16);
47         color.setRgb( ( intval >> 16 ) & 0xff,   // r
48                       ( intval >>  8 ) & 0xff,   // g
49                       ( intval       ) & 0xff,   // b
50                       ( intval >> 24 ) & 0xff ); // a
51     } else if (strColor.startsWith("#") && strColor.length() == 7) {
52         // Format must be #RRGGBB
53         strColor = strColor.replace('#', "0x");
54         intval = strColor.toUInt(&ok, 16);
55         color.setRgb( ( intval >> 16 ) & 0xff,   // r
56                       ( intval >>  8 ) & 0xff,   // g
57                       ( intval       ) & 0xff,   // b
58                       0xff );                    // a
59     }
60
61     return color;
62 }
63
64 static QString colorToString(QColor color, bool alpha)
65 {
66     QString colorStr;
67     QTextStream stream(&colorStr);
68     stream << "#";
69     stream.setIntegerBase(16);
70     stream.setFieldWidth(2);
71     stream.setFieldAlignment(QTextStream::AlignRight);
72     stream.setPadChar('0');
73     if(alpha)
74     {
75         stream << color.alpha();
76     }
77     stream <<  color.red() << color.green() << color.blue();
78
79     return colorStr;
80 }
81
82 ChooseColorWidget::ChooseColorWidget(QString text, QString color, QWidget *parent) :
83         QWidget(parent)
84 {
85     QHBoxLayout *layout = new QHBoxLayout(this);
86     layout->setContentsMargins(0, 0, 0, 0);
87     layout->setSpacing(0);
88
89     QLabel *label = new QLabel(text, this);
90     m_button = new KColorButton(stringToColor(color), this);
91     m_button->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Fixed);
92     ColorPickerWidget *picker = new ColorPickerWidget(this);
93
94     layout->addWidget(label);
95     layout->addWidget(m_button);
96     layout->addWidget(picker, 0, Qt::AlignRight);
97
98     connect(picker, SIGNAL(colorPicked(QColor)), this, SLOT(setColor(QColor)));
99     connect(picker, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
100     connect(m_button, SIGNAL(changed(QColor)), this, SIGNAL(modified()));
101 }
102
103 QString ChooseColorWidget::getColor()
104 {
105     return colorToString(m_button->color(), m_button->isAlphaChannelEnabled());
106 }
107
108 void ChooseColorWidget::setAlphaChannelEnabled(bool enabled)
109 {
110     m_button->setAlphaChannelEnabled(enabled);
111 }
112
113 void ChooseColorWidget::setColor(QColor color)
114 {
115     m_button->setColor(color);
116 }
117
118 #include "choosecolorwidget.moc"