]> git.sesse.net Git - kdenlive/blob - src/choosecolorwidget.cpp
if(foo) delete foo; -> delete foo
[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 #include <kdeversion.h>
30 #include <KDebug>
31
32 static QColor stringToColor(QString strColor)
33 {
34     bool ok = false;
35     QColor color("black");
36     int intval = 0;
37     if (strColor.startsWith("0x")) {
38         if (strColor.length() == 10) {
39             // 0xRRGGBBAA
40             intval = strColor.toUInt(&ok, 16);
41             color.setRgb( ( intval >> 24 ) & 0xff,   // r
42                           ( intval >> 16 ) & 0xff,   // g
43                           ( intval >>  8 ) & 0xff,   // b
44                           ( intval       ) & 0xff ); // a
45         } else {
46             // 0xRRGGBB, 0xRGB
47             color.setNamedColor(strColor.replace(0, 2, "#"));
48         }
49     } else {
50         if (strColor.length() == 9) {
51             // #AARRGGBB
52             strColor = strColor.replace('#', "0x");
53             intval = strColor.toUInt(&ok, 16);
54             color.setRgb( ( intval >> 16 ) & 0xff,   // r
55                           ( intval >>  8 ) & 0xff,   // g
56                           ( intval       ) & 0xff,   // b
57                           ( intval >> 24 ) & 0xff ); // a
58         } else if (strColor.length() == 8) {
59             // 0xRRGGBB
60             strColor = strColor.replace('#', "0x");
61             color.setNamedColor(strColor);
62         } else {
63             // #RRGGBB, #RGB
64             color.setNamedColor(strColor);
65         }
66     }
67
68     return color;
69 }
70
71 static QString colorToString(const QColor &color, bool alpha)
72 {
73     QString colorStr;
74     QTextStream stream(&colorStr);
75     stream << "0x";
76     stream.setIntegerBase(16);
77     stream.setFieldWidth(2);
78     stream.setFieldAlignment(QTextStream::AlignRight);
79     stream.setPadChar('0');
80     stream <<  color.red() << color.green() << color.blue();
81     if(alpha)
82     {
83         stream << color.alpha();
84     }
85     else {
86         // MLT always wants 0xRRGGBBAA format
87         stream << "ff";
88     }
89     return colorStr;
90 }
91
92 ChooseColorWidget::ChooseColorWidget(const QString &text, const QString &color, bool alphaEnabled, QWidget *parent) :
93         QWidget(parent)
94 {
95     QHBoxLayout *layout = new QHBoxLayout(this);
96     layout->setContentsMargins(0, 0, 0, 0);
97     layout->setSpacing(0);
98
99     QLabel *label = new QLabel(text, this);
100
101     QWidget *rightSide = new QWidget(this);
102     QHBoxLayout *rightSideLayout = new QHBoxLayout(rightSide);
103     rightSideLayout->setContentsMargins(0, 0, 0, 0);
104     rightSideLayout->setSpacing(0);
105
106     m_button = new KColorButton(stringToColor(color), rightSide);
107 #if KDE_IS_VERSION(4,5,0)
108     if (alphaEnabled) m_button->setAlphaChannelEnabled(alphaEnabled);
109 #endif
110 //     m_button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
111     ColorPickerWidget *picker = new ColorPickerWidget(rightSide);
112
113     layout->addWidget(label);
114     layout->addWidget(rightSide);
115     rightSideLayout->addWidget(m_button);
116     rightSideLayout->addWidget(picker, 0, Qt::AlignRight);
117
118     connect(picker, SIGNAL(colorPicked(QColor)), this, SLOT(setColor(QColor)));
119     connect(picker, SIGNAL(displayMessage(QString,int)), this, SIGNAL(displayMessage(QString,int)));
120     connect(picker, SIGNAL(disableCurrentFilter(bool)), this, SIGNAL(disableCurrentFilter(bool)));
121     connect(m_button, SIGNAL(changed(QColor)), this, SIGNAL(modified()));
122 }
123
124 QString ChooseColorWidget::getColor() const
125 {
126     bool alphaChannel = false;
127 #if KDE_IS_VERSION(4,5,0)
128     alphaChannel = m_button->isAlphaChannelEnabled();
129 #endif
130     return colorToString(m_button->color(), alphaChannel);
131 }
132
133 void ChooseColorWidget::setColor(const QColor& color)
134 {
135     m_button->setColor(color);
136 }
137
138 #include "choosecolorwidget.moc"