1 /***************************************************************************
2 File : ColorBox.cpp
3 Project : QtiPlot
4 --------------------------------------------------------------------
5 Copyright : (C) 2006-2010 by Ion Vasilief, Alex Kargovsky
6 Email (use @ for *) : ion_vasilief*yahoo.fr, kargovsky*yumr.phys.msu.su
7 Description : A combo box to select a standard color
8
9 ***************************************************************************/
10
11 /***************************************************************************
12 * *
13 * This program is free software; you can redistribute it and/or modify *
14 * it under the terms of the GNU General Public License as published by *
15 * the Free Software Foundation; either version 2 of the License, or *
16 * (at your option) any later version. *
17 * *
18 * This program is distributed in the hope that it will be useful, *
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21 * GNU General Public License for more details. *
22 * *
23 * You should have received a copy of the GNU General Public License *
24 * along with this program; if not, write to the Free Software *
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26 * Boston, MA 02110-1301 USA *
27 * *
28 ***************************************************************************/
29 #include "ColorBox.h"
30
31 #include <QSettings>
32 #include <QPixmap>
33 #include <QPainter>
34 #include <algorithm>
35
36 const QColor ColorBox::colors[] = {
37 QColor(Qt::black),
38 QColor(Qt::red),
39 QColor(Qt::green),
40 QColor(Qt::blue),
41 QColor(Qt::cyan),
42 QColor(Qt::magenta),
43 QColor(Qt::yellow),
44 QColor(Qt::darkYellow),
45 QColor(Qt::darkBlue),
46 QColor(Qt::darkMagenta),
47 QColor(Qt::darkRed),
48 QColor(Qt::darkGreen),
49 QColor(Qt::darkCyan),
50 QColor("#0000A0"),
51 QColor("#FF8000"),
52 QColor("#8000FF"),
53 QColor("#FF0080"),
54 QColor(Qt::white),
55 QColor(Qt::lightGray),
56 QColor(Qt::gray),
57 QColor("#FFFF80"),
58 QColor("#80FFFF"),
59 QColor("#FF80FF"),
60 QColor(Qt::darkGray),
61 };
62
ColorBox(QWidget * parent)63 ColorBox::ColorBox(QWidget *parent) : QComboBox(parent)
64 {
65 setEditable(false);
66 init();
67 }
68
init()69 void ColorBox::init()
70 {
71 QList<QColor> indexedColors = colorList();
72 QStringList color_names = colorNames();
73
74 QPixmap icon = QPixmap(28, 16);
75 QRect r = QRect(0, 0, 27, 15);
76
77 QPainter p;
78 p.begin(&icon);
79
80 for (int i = 0; i < indexedColors.size(); i++){
81 p.setBrush(QBrush(indexedColors[i]));
82 p.drawRect(r);
83 this->addItem(icon, color_names[i]);
84 }
85 p.end();
86 }
87
setColor(const QColor & c)88 void ColorBox::setColor(const QColor& c)
89 {
90 setCurrentIndex(colorIndex(c));
91 }
92
color() const93 QColor ColorBox::color() const
94 {
95 return color(this->currentIndex());
96 }
97
colorIndex(const QColor & c)98 int ColorBox::colorIndex(const QColor& c)
99 {
100 if (!isValidColor(c))
101 return 0;
102
103 return colorList().indexOf(c);
104 }
105
color(int colorIndex)106 QColor ColorBox::color(int colorIndex)
107 {
108 QList<QColor> colorsList = colorList();
109 if (colorIndex >= 0 && colorIndex < colorsList.size())
110 return colorsList[colorIndex];
111
112 return Qt::black; // default color is black.
113 }
114
colorList()115 QList<QColor> ColorBox::colorList()
116 {
117 #ifdef Q_OS_MAC
118 QSettings settings(QSettings::IniFormat,QSettings::UserScope, "ProIndependent", "QtiPlot");
119 #else
120 QSettings settings(QSettings::NativeFormat,QSettings::UserScope, "ProIndependent", "QtiPlot");
121 #endif
122 settings.beginGroup("/General");
123
124 QList<QColor> indexedColors;
125 QStringList lst = settings.value("/IndexedColors").toStringList();
126 if (!lst.isEmpty()){
127 for (int i = 0; i < lst.size(); i++)
128 indexedColors << QColor(lst[i]);
129 } else {
130 for (int i = 0; i < colors_count; i++)
131 indexedColors << colors[i];
132 }
133 settings.endGroup();
134
135 return indexedColors;
136 }
137
colorNames()138 QStringList ColorBox::colorNames()
139 {
140 #ifdef Q_OS_MAC
141 QSettings settings(QSettings::IniFormat,QSettings::UserScope, "ProIndependent", "QtiPlot");
142 #else
143 QSettings settings(QSettings::NativeFormat,QSettings::UserScope, "ProIndependent", "QtiPlot");
144 #endif
145 settings.beginGroup("/General");
146 QStringList color_names = settings.value("/IndexedColorNames", defaultColorNames()).toStringList();
147 settings.endGroup();
148 return color_names;
149 }
150
defaultColor(int colorIndex)151 QColor ColorBox::defaultColor(int colorIndex)
152 {
153 if (colorIndex >= 0 && colorIndex < (int)sizeof(colors))
154 return colors[colorIndex];
155
156 return Qt::black; // default color is black.
157 }
158
isValidColor(const QColor & color)159 bool ColorBox::isValidColor(const QColor& color)
160 {
161 return colorList().contains(color);
162 }
163
numPredefinedColors()164 int ColorBox::numPredefinedColors()
165 {
166 return colors_count;
167 }
168
defaultColorNames()169 QStringList ColorBox::defaultColorNames()
170 {
171 QStringList color_names = QStringList() << tr( "black" );
172 color_names << tr( "red" );
173 color_names << tr( "green" );
174 color_names << tr( "blue" );
175 color_names << tr( "cyan" );
176 color_names << tr( "magenta" );
177 color_names << tr( "yellow" );
178 color_names << tr( "dark yellow" );
179 color_names << tr( "navy" );
180 color_names << tr( "purple" );
181 color_names << tr( "wine" );
182 color_names << tr( "olive" );
183 color_names << tr( "dark cyan" );
184 color_names << tr( "royal" );
185 color_names << tr( "orange" );
186 color_names << tr( "violet" );
187 color_names << tr( "pink" );
188 color_names << tr( "white" );
189 color_names << tr( "light gray" );
190 color_names << tr( "gray" );
191 color_names << tr( "light yellow" );
192 color_names << tr( "light cyan" );
193 color_names << tr( "light magenta" );
194 color_names << tr( "dark gray" );
195 return color_names;
196 }
197
defaultColors()198 QList<QColor> ColorBox::defaultColors()
199 {
200 QList<QColor> lst;
201 for (int i = 0; i < colors_count; i++)
202 lst << colors[i];
203
204 return lst;
205 }
206