Add wpseditor, the Google Summer of Code 2008 project of Rostislav Chekan. Closes...
[kugel-rb.git] / utils / wpseditor / gui / src / QPropertyEditor / ColorCombo.cpp
blobf5eeb030dc0162ea169dba22d94fc9306597964b
1 // *************************************************************************************************
2 //
3 // QPropertyEditor v 0.1
4 //
5 // --------------------------------------
6 // Copyright (C) 2007 Volker Wiendl
7 //
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 // This class is based on the Color Editor Factory Example by Trolltech
26 // *************************************************************************************************
28 #include "ColorCombo.h"
30 #include <Qt/qcolordialog.h>
32 ColorCombo::ColorCombo(QWidget* parent /*= 0*/) : QComboBox(parent) {
33 QStringList colorNames = QColor::colorNames();
34 for (int i = 0; i < colorNames.size(); ++i) {
35 QColor color(colorNames[i]);
36 insertItem(i, colorNames[i]);
37 setItemData(i, color, Qt::DecorationRole);
39 addItem(tr("Custom"), QVariant((int)QVariant::UserType));
40 connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(currentChanged(int)));
44 ColorCombo::~ColorCombo() {}
47 QColor ColorCombo::color() const {
48 return qVariantValue<QColor>(itemData(currentIndex(), Qt::DecorationRole));
51 void ColorCombo::setColor(QColor color) {
52 m_init = color;
53 setCurrentIndex(findData(color, int(Qt::DecorationRole)));
54 if (currentIndex() == -1) {
55 addItem(color.name());
56 setItemData(count()-1, color, Qt::DecorationRole);
57 setCurrentIndex(count()-1);
61 void ColorCombo::currentChanged(int index) {
62 if (itemData(index).isValid() && itemData(index) == QVariant((int)QVariant::UserType)) {
63 QColor color = QColorDialog::getColor(m_init, this);
64 if (color.isValid()) {
65 if (findData(color, int(Qt::DecorationRole)) == -1) {
66 addItem(color.name());
67 setItemData(count()-1, color, Qt::DecorationRole);
69 setCurrentIndex(findData(color, int(Qt::DecorationRole)));
70 } else
71 setCurrentIndex(findData(m_init));