1
/****************************************************************************
3
** This file is part of the LibreCAD project, a 2D CAD program
5
** Copyright (C) 2010-2011 R. van Twisk (librecad@rvt.dds.nl)
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
9
** This file may be distributed and/or modified under the terms of the
10
** GNU General Public License version 2 as published by the Free Software
11
** Foundation and appearing in the file gpl-2.0.txt included in the
12
** packaging of this file.
14
** This program 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
17
** GNU General Public License for more details.
19
** You should have received a copy of the GNU General Public License
20
** along with this program; if not, write to the Free Software
21
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23
** This copyright notice MUST APPEAR in all copies of the script!
25
**********************************************************************/
27
#include "qg_colorbox.h"
29
#include <qcolordialog.h>
35
* Default Constructor. You must call init manually if you choose
36
* to use this constructor.
38
QG_ColorBox::QG_ColorBox(QWidget* parent, const char* name)
42
setEditable ( false );
44
showUnchanged = false;
49
* Constructor that calls init and provides a fully functional
50
* combobox for choosing colors.
52
* @param showByLayer true: Show attribute ByLayer, ByBlock.
54
QG_ColorBox::QG_ColorBox(bool showByLayer, bool showUnchanged,
55
QWidget* parent, const char* name)
59
setEditable ( false );
61
init(showByLayer, showUnchanged);
68
QG_ColorBox::~QG_ColorBox() {}
72
* Initialisation (called from constructor or manually but only
75
* @param showByLayer true: Show attribute ByLayer, ByBlock.
77
void QG_ColorBox::init(bool showByLayer, bool showUnchanged) {
78
this->showByLayer = showByLayer;
79
this->showUnchanged = showUnchanged;
82
addItem(QIcon(":/ui/color00.png"), tr("Unchanged"));
85
addItem(QIcon(":/ui/color00.png"), tr("By Layer"));
86
addItem(QIcon(":/ui/color00.png"), tr("By Block"));
88
//static colors starts here
89
//addColor(QIcon(":/ui/color01.png"), red,Qt::red);
90
QString red(tr("Red"));
91
addColor(Qt::red,red);
92
colorIndexStart=findText(red); // keep the starting point of static colors
93
addColor(Qt::darkRed,tr("Dark Red"));
94
addColor(Qt::yellow,tr("Yellow"));
95
addColor(Qt::darkYellow,tr("Dark Yellow"));
96
addColor(Qt::green,tr("Green"));
97
addColor(Qt::darkGreen,tr("Dark Green"));
98
addColor(Qt::cyan,tr("Cyan"));
99
addColor(Qt::darkCyan,tr("Dark Cyan"));
100
addColor(Qt::blue,tr("Blue"));
101
addColor(Qt::darkBlue,tr("Dark Blue"));
102
addColor(Qt::magenta,tr("Magenta"));
103
addColor(Qt::darkMagenta,tr("Dark Magenta"));
104
addItem(QIcon(":/ui/color07.png"), tr("Black / White"),QColor(Qt::black));
105
//colorIndexBlack=findData(QColor(Qt::black)); //record the number for special color black/white
106
//std::cout<<"colorIndexBlack="<<colorIndexBlack<<std::endl;
107
addColor(Qt::gray,tr("Gray"));
108
addColor(Qt::darkGray,tr("Dark Gray"));
109
addColor(Qt::lightGray,tr("Light Gray"));
110
//static colors ends here
111
// last item is reserved for "Others.." to trigger color picker
112
QString others(tr("Others.."));
113
addItem(QIcon(":/ui/colorxx.png"), others);
115
connect(this, SIGNAL(activated(int)),
116
this, SLOT(slotColorChanged(int)));
118
if (showUnchanged || showByLayer ) {
121
setCurrentIndex(findData(QColor(Qt::black))); //default to Qt::black
124
slotColorChanged(currentIndex());
128
* Sets the color shown in the combobox to the given color.
130
void QG_ColorBox::setColor(const RS_Color& color) {
131
currentColor = color;
132
//std::cout<<"initial color: "<<color<<std::endl;
135
if (color.isByLayer() && showByLayer) {
137
} else if (color.isByBlock() && showByLayer) {
140
for(cIndex=colorIndexStart;cIndex< count() - 1;cIndex++) {
141
//searching for the color, allowing difference up to 2
142
QColor q(itemData(cIndex).value<QColor>());
143
if( abs(q.red() - color.red())
144
+abs(q.green() - color.green())
145
+abs(q.blue() - color.blue()) <= 3
150
/*color not found, default to "others...*/
151
/* if(cIndex == count() - 1) {
152
cIndex=findData(QColor(Qt::black)); //default to Qt::black
155
setCurrentIndex(cIndex);
156
//std::cout<<"Default color for choosing: cIndex="<<cIndex<<" "<<RS_Color(itemData(cIndex).value<QColor>())<<std::endl;
158
if (currentIndex()!= count() -1 ) {
159
slotColorChanged(currentIndex());
164
* generate icon from color, then add to color box
166
void QG_ColorBox::addColor(Qt::GlobalColor color, QString text)
168
QPixmap pixmap(":/ui/color00.png");
169
int w = pixmap.width();
170
int h = pixmap.height();
171
QPainter painter(&pixmap);
172
painter.fillRect(1, 1, w-2, h-2, color);
173
addItem(QIcon(pixmap),text,QColor(color));
177
* Sets the color of the pixmap next to the "By Layer" item
178
* to the given color.
180
void QG_ColorBox::setLayerColor(const RS_Color& color) {
181
if (! showByLayer ) return;
183
pixmap = QPixmap(":/ui/color00.png");
184
int w = pixmap.width();
185
int h = pixmap.height();
186
QPainter painter(&pixmap);
187
painter.fillRect(1, 1, w-2, h-2, color);
190
setItemIcon(0, QIcon(pixmap));
191
setItemText(0, tr("By Layer"));
193
// needed for the first time a layer is added:
194
if (currentIndex()!= count() -1 ) {
195
slotColorChanged(currentIndex());
202
* Called when the color has changed. This method
203
* sets the current color to the value chosen or even
204
* offers a dialog to the user that allows him/ her to
205
* choose an individual color.
207
void QG_ColorBox::slotColorChanged(int index) {
208
currentColor.resetFlags();
219
switch (index-(int)showUnchanged) {
221
currentColor = RS_Color(RS2::FlagByLayer);
224
currentColor = RS_Color(RS2::FlagByBlock);
230
if ( index >= colorIndexStart ) {
231
if(index < count() -1 ) {
232
QVariant q0=itemData(index);
233
if(q0 != QVariant::Invalid ) {
234
currentColor=itemData(index).value<QColor>();
236
currentColor=Qt::black; //default to black color
238
} else { // color picker for "Others.."
239
RS_Color selectedColor = QColorDialog::getColor(currentColor, this);
240
//verify if user as clicked Ok or Cancel
241
if (selectedColor.isValid())
242
currentColor = selectedColor;
246
//printf("Current color is (%d): %s\n",
247
// index, currentColor.name().latin1());
249
emit colorChanged(currentColor);