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>
29 #include <kdeversion.h>
31 static QColor stringToColor(QString strColor)
34 QColor color("black");
37 if (strColor.startsWith("0x")) {
38 if (strColor.length() == 10) {
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
47 color.setNamedColor(strColor.replace(0, 2, "#"));
50 if (strColor.length() == 9) {
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
60 color.setNamedColor(strColor);
67 static QString colorToString(QColor color, bool alpha)
70 QTextStream stream(&colorStr);
72 stream.setIntegerBase(16);
73 stream.setFieldWidth(2);
74 stream.setFieldAlignment(QTextStream::AlignRight);
75 stream.setPadChar('0');
78 stream << color.alpha();
80 stream << color.red() << color.green() << color.blue();
85 ChooseColorWidget::ChooseColorWidget(QString text, QString color, QWidget *parent) :
88 QHBoxLayout *layout = new QHBoxLayout(this);
89 layout->setContentsMargins(0, 0, 0, 0);
90 layout->setSpacing(0);
92 QLabel *label = new QLabel(text, this);
94 QWidget *rightSide = new QWidget(this);
95 QHBoxLayout *rightSideLayout = new QHBoxLayout(rightSide);
96 rightSideLayout->setContentsMargins(0, 0, 0, 0);
97 rightSideLayout->setSpacing(0);
99 m_button = new KColorButton(stringToColor(color), rightSide);
100 // m_button->setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::Fixed);
101 ColorPickerWidget *picker = new ColorPickerWidget(rightSide);
103 layout->addWidget(label);
104 layout->addWidget(rightSide);
105 rightSideLayout->addWidget(m_button);
106 rightSideLayout->addWidget(picker, 0, Qt::AlignRight);
108 connect(picker, SIGNAL(colorPicked(QColor)), this, SLOT(setColor(QColor)));
109 connect(picker, SIGNAL(displayMessage(const QString&, int)), this, SIGNAL(displayMessage(const QString&, int)));
110 connect(m_button, SIGNAL(changed(QColor)), this, SIGNAL(modified()));
113 QString ChooseColorWidget::getColor()
115 bool alphaChannel = false;
116 #if KDE_IS_VERSION(4,5,0)
117 alphaChannel = m_button->isAlphaChannelEnabled();
119 return colorToString(m_button->color(), alphaChannel);
122 void ChooseColorWidget::setAlphaChannelEnabled(bool enabled)
124 #if KDE_IS_VERSION(4,5,0)
125 m_button->setAlphaChannelEnabled(enabled);
129 void ChooseColorWidget::setColor(QColor color)
131 m_button->setColor(color);
134 #include "choosecolorwidget.moc"