1 /***************************************************************************
2 * Copyright (C) 2010 by Till Theato (root@ttill.de) *
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. *
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. *
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 ***************************************************************************/
21 #include "choosecolorwidget.h"
22 #include "colorpickerwidget.h"
25 #include <QHBoxLayout>
27 #include <KColorButton>
28 #include <KLocalizedString>
30 static QColor stringToColor(QString strColor)
33 QColor color("black");
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
64 static QString colorToString(QColor color, bool alpha)
67 QTextStream stream(&colorStr);
69 stream.setIntegerBase(16);
70 stream.setFieldWidth(2);
71 stream.setFieldAlignment(QTextStream::AlignRight);
72 stream.setPadChar('0');
75 stream << color.alpha();
77 stream << color.red() << color.green() << color.blue();
82 ChooseColorWidget::ChooseColorWidget(QString text, QString color, QWidget *parent) :
85 QHBoxLayout *layout = new QHBoxLayout(this);
86 layout->setContentsMargins(0, 0, 0, 0);
87 layout->setSpacing(0);
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);
94 layout->addWidget(label);
95 layout->addWidget(m_button);
96 layout->addWidget(picker, 0, Qt::AlignRight);
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()));
103 QString ChooseColorWidget::getColor()
105 return colorToString(m_button->color(), m_button->isAlphaChannelEnabled());
108 void ChooseColorWidget::setAlphaChannelEnabled(bool enabled)
110 m_button->setAlphaChannelEnabled(enabled);
113 void ChooseColorWidget::setColor(QColor color)
115 m_button->setColor(color);
118 #include "choosecolorwidget.moc"