~librecad-dev/librecad/librecad

« back to all changes in this revision

Viewing changes to librecad/src/ui/qg_colorbox.cpp

  • Committer: Scott Howard
  • Date: 2014-02-21 19:07:55 UTC
  • Revision ID: showard@debian.org-20140221190755-csjax9wb146hgdq4
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** This file is part of the LibreCAD project, a 2D CAD program
 
4
**
 
5
** Copyright (C) 2010-2011 R. van Twisk (librecad@rvt.dds.nl)
 
6
** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
 
7
**
 
8
**
 
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.
 
13
**
 
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.
 
18
**
 
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
 
22
**
 
23
** This copyright notice MUST APPEAR in all copies of the script!
 
24
**
 
25
**********************************************************************/
 
26
 
 
27
#include "qg_colorbox.h"
 
28
 
 
29
#include <qcolordialog.h>
 
30
#include <qpainter.h>
 
31
#include <qpixmap.h>
 
32
#include <iostream>
 
33
 
 
34
/**
 
35
 * Default Constructor. You must call init manually if you choose
 
36
 * to use this constructor.
 
37
 */
 
38
QG_ColorBox::QG_ColorBox(QWidget* parent, const char* name)
 
39
    : QComboBox(parent) {
 
40
 
 
41
    setObjectName(name);
 
42
    setEditable ( false );
 
43
    showByLayer = false;
 
44
    showUnchanged = false;
 
45
    unchanged = false;
 
46
}
 
47
 
 
48
/**
 
49
 * Constructor that calls init and provides a fully functional
 
50
 * combobox for choosing colors.
 
51
 *
 
52
 * @param showByLayer true: Show attribute ByLayer, ByBlock.
 
53
 */
 
54
QG_ColorBox::QG_ColorBox(bool showByLayer, bool showUnchanged,
 
55
                         QWidget* parent, const char* name)
 
56
    : QComboBox(parent) {
 
57
 
 
58
    setObjectName(name);
 
59
    setEditable ( false );
 
60
    unchanged = false;
 
61
    init(showByLayer, showUnchanged);
 
62
}
 
63
 
 
64
 
 
65
/**
 
66
 * Destructor
 
67
 */
 
68
QG_ColorBox::~QG_ColorBox() {}
 
69
 
 
70
 
 
71
/**
 
72
 * Initialisation (called from constructor or manually but only
 
73
 * once).
 
74
 *
 
75
 * @param showByLayer true: Show attribute ByLayer, ByBlock.
 
76
 */
 
77
void QG_ColorBox::init(bool showByLayer, bool showUnchanged) {
 
78
    this->showByLayer = showByLayer;
 
79
    this->showUnchanged = showUnchanged;
 
80
 
 
81
    if (showUnchanged) {
 
82
        addItem(QIcon(":/ui/color00.png"), tr("Unchanged"));
 
83
    }
 
84
    if (showByLayer) {
 
85
        addItem(QIcon(":/ui/color00.png"), tr("By Layer"));
 
86
        addItem(QIcon(":/ui/color00.png"), tr("By Block"));
 
87
    }
 
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);
 
114
 
 
115
    connect(this, SIGNAL(activated(int)),
 
116
            this, SLOT(slotColorChanged(int)));
 
117
 
 
118
    if (showUnchanged || showByLayer ) {
 
119
        setCurrentIndex(0);
 
120
    } else {
 
121
        setCurrentIndex(findData(QColor(Qt::black))); //default to Qt::black
 
122
    }
 
123
 
 
124
    slotColorChanged(currentIndex());
 
125
}
 
126
 
 
127
/**
 
128
 * Sets the color shown in the combobox to the given color.
 
129
 */
 
130
void QG_ColorBox::setColor(const RS_Color& color) {
 
131
    currentColor = color;
 
132
//std::cout<<"initial color: "<<color<<std::endl;
 
133
    int cIndex;
 
134
 
 
135
    if (color.isByLayer() && showByLayer) {
 
136
        cIndex=0;
 
137
    } else if (color.isByBlock() && showByLayer) {
 
138
        cIndex=1;
 
139
    } else {
 
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
 
146
                ) {
 
147
                        break;
 
148
          }
 
149
        }
 
150
        /*color not found, default to "others...*/
 
151
/*        if(cIndex == count() - 1) {
 
152
            cIndex=findData(QColor(Qt::black)); //default to Qt::black
 
153
        }*/
 
154
    }
 
155
    setCurrentIndex(cIndex);
 
156
//std::cout<<"Default color for choosing: cIndex="<<cIndex<<" "<<RS_Color(itemData(cIndex).value<QColor>())<<std::endl;
 
157
 
 
158
    if (currentIndex()!= count() -1 ) {
 
159
        slotColorChanged(currentIndex());
 
160
    }
 
161
}
 
162
 
 
163
/**
 
164
 * generate icon from color, then add to color box
 
165
 */
 
166
void QG_ColorBox::addColor(Qt::GlobalColor color, QString text)
 
167
{
 
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));
 
174
}
 
175
 
 
176
/**
 
177
 * Sets the color of the pixmap next to the "By Layer" item
 
178
 * to the given color.
 
179
 */
 
180
void QG_ColorBox::setLayerColor(const RS_Color& color) {
 
181
    if (! showByLayer ) return;
 
182
    QPixmap pixmap;
 
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);
 
188
    painter.end();
 
189
 
 
190
    setItemIcon(0, QIcon(pixmap));
 
191
    setItemText(0, tr("By Layer"));
 
192
 
 
193
    // needed for the first time a layer is added:
 
194
    if (currentIndex()!= count() -1 ) {
 
195
        slotColorChanged(currentIndex());
 
196
    }
 
197
}
 
198
 
 
199
 
 
200
 
 
201
/**
 
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.
 
206
 */
 
207
void QG_ColorBox::slotColorChanged(int index) {
 
208
    currentColor.resetFlags();
 
209
    if (showUnchanged) {
 
210
        if (index==0) {
 
211
            unchanged = true;
 
212
        }
 
213
        else {
 
214
            unchanged = false;
 
215
        }
 
216
    }
 
217
 
 
218
    if (showByLayer) {
 
219
        switch (index-(int)showUnchanged) {
 
220
        case 0:
 
221
            currentColor = RS_Color(RS2::FlagByLayer);
 
222
            break;
 
223
        case 1:
 
224
            currentColor = RS_Color(RS2::FlagByBlock);
 
225
            break;
 
226
        default:
 
227
            break;
 
228
        }
 
229
    }
 
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>();
 
235
                } else {
 
236
            currentColor=Qt::black; //default to black color
 
237
                }
 
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;
 
243
        }
 
244
    }
 
245
 
 
246
    //printf("Current color is (%d): %s\n",
 
247
    //       index, currentColor.name().latin1());
 
248
 
 
249
    emit colorChanged(currentColor);
 
250
}
 
251
 
 
252