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