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