diff options
Diffstat (limited to 'utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp')
-rw-r--r-- | utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp b/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp new file mode 100644 index 0000000000..f5eeb030dc --- /dev/null +++ b/utils/wpseditor/gui/src/QPropertyEditor/ColorCombo.cpp | |||
@@ -0,0 +1,73 @@ | |||
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. | ||
13 | // | ||
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. | ||
18 | // | ||
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 | ||
22 | // | ||
23 | // | ||
24 | // This class is based on the Color Editor Factory Example by Trolltech | ||
25 | // | ||
26 | // ************************************************************************************************* | ||
27 | |||
28 | #include "ColorCombo.h" | ||
29 | |||
30 | #include <Qt/qcolordialog.h> | ||
31 | |||
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); | ||
38 | } | ||
39 | addItem(tr("Custom"), QVariant((int)QVariant::UserType)); | ||
40 | connect(this, SIGNAL(currentIndexChanged(int)), this, SLOT(currentChanged(int))); | ||
41 | } | ||
42 | |||
43 | |||
44 | ColorCombo::~ColorCombo() {} | ||
45 | |||
46 | |||
47 | QColor ColorCombo::color() const { | ||
48 | return qVariantValue<QColor>(itemData(currentIndex(), Qt::DecorationRole)); | ||
49 | } | ||
50 | |||
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); | ||
58 | } | ||
59 | } | ||
60 | |||
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); | ||
68 | } | ||
69 | setCurrentIndex(findData(color, int(Qt::DecorationRole))); | ||
70 | } else | ||
71 | setCurrentIndex(findData(m_init)); | ||
72 | } | ||
73 | } | ||