]> Shamusworld >> Repos - architektonas/blob - src/widgets/qg_colorbox.cpp
GPL compliance check...
[architektonas] / src / widgets / qg_colorbox.cpp
1 // qg_colorbox.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  05/22/2010  Added this text. :-)
15 //
16
17 #include "qg_colorbox.h"
18
19 /**
20  * Default Constructor. You must call init manually if you choose
21  * to use this constructor.
22  */
23 QG_ColorBox::QG_ColorBox(QWidget * parent/*= 0*/, const char */*name = 0*/):
24         QComboBox(parent)
25 {
26         showByLayer = false;
27         showUnchanged = false;
28         unchanged = false;
29 }
30
31 /**
32  * Constructor that calls init and provides a fully functional
33  * combobox for choosing colors.
34  *
35  * @param showByLayer true: Show attribute ByLayer, ByBlock.
36  */
37 QG_ColorBox::QG_ColorBox(bool showByLayer, bool showUnchanged, QWidget * parent/*= 0*/,
38         const char */*name = 0*/): QComboBox(parent)
39 {
40         unchanged = false;
41         init(showByLayer, showUnchanged);
42 }
43
44 /**
45  * Destructor
46  */
47 QG_ColorBox::~QG_ColorBox()
48 {
49 }
50
51 RS_Color QG_ColorBox::getColor()
52 {
53         return currentColor;
54 }
55
56 /**
57  * Initialisation (called from constructor or manually but only
58  * once).
59  *
60  * @param showByLayer true: Show attribute ByLayer, ByBlock.
61  */
62 void QG_ColorBox::init(bool showByLayer, bool showUnchanged)
63 {
64         this->showByLayer = showByLayer;
65         this->showUnchanged = showUnchanged;
66
67         if (showUnchanged)
68 //              addItem(QIcon(color00_xpm), tr("Unchanged"));
69                 addItem(QIcon(":/res/color00.xpm"), tr("Unchanged"));
70
71         if (showByLayer)
72         {
73 //              addItem(QIcon(color00_xpm), tr("By Layer"));
74 //              addItem(QIcon(color00_xpm), tr("By Block"));
75                 addItem(QIcon(":/res/color00.xpm"), tr("By Layer"));
76                 addItem(QIcon(":/res/color00.xpm"), tr("By Block"));
77         }
78
79 /*      addItem(QIcon(color01_xpm), tr("Red"));
80         addItem(QIcon(color02_xpm), tr("Yellow"));
81         addItem(QIcon(color03_xpm), tr("Green"));
82         addItem(QIcon(color04_xpm), tr("Cyan"));
83         addItem(QIcon(color05_xpm), tr("Blue"));
84         addItem(QIcon(color06_xpm), tr("Magenta"));
85         addItem(QIcon(color07_xpm), tr("Black / White"));
86         addItem(QIcon(color08_xpm), tr("Gray"));
87         addItem(QIcon(color09_xpm), tr("Light Gray"));
88         addItem(QIcon(colorxx_xpm), tr("Others.."));*/
89         addItem(QIcon(":/res/color01.xpm"), tr("Red"));
90         addItem(QIcon(":/res/color02.xpm"), tr("Yellow"));
91         addItem(QIcon(":/res/color03.xpm"), tr("Green"));
92         addItem(QIcon(":/res/color04.xpm"), tr("Cyan"));
93         addItem(QIcon(":/res/color05.xpm"), tr("Blue"));
94         addItem(QIcon(":/res/color06.xpm"), tr("Magenta"));
95         addItem(QIcon(":/res/color07.xpm"), tr("Black / White"));
96         addItem(QIcon(":/res/color08.xpm"), tr("Gray"));
97         addItem(QIcon(":/res/color09.xpm"), tr("Light Gray"));
98         addItem(QIcon(":/res/colorxx.xpm"), tr("Others.."));
99
100         connect(this, SIGNAL(activated(int)), this, SLOT(slotColorChanged(int)));
101
102         if (showUnchanged)
103                 setCurrentIndex(0);
104         else if (showByLayer)
105                 setCurrentIndex(0);
106         else
107                 setCurrentIndex(6);
108
109         slotColorChanged(currentIndex());
110 }
111
112 bool QG_ColorBox::isUnchanged()
113 {
114         return unchanged;
115 }
116
117 /**
118  * Sets the color shown in the combobox to the given color.
119  */
120 void QG_ColorBox::setColor(const RS_Color & color)
121 {
122 //#warning "!!! Color is being misreported here !!!"
123 /*
124 And fuck. Probably because the operator== in RS_Color doesn't know how to
125 convert between Qt:: style colors and RGB. Either way, have to fix this shit
126 Look at Red: Have to convert them all to RS_Color...
127 */
128         currentColor = color;
129
130         if (color.isByLayer() && showByLayer)
131                 setCurrentIndex(0);
132         else if (color.isByBlock() && showByLayer)
133                 setCurrentIndex(1);
134         else if (color == RS_Color(255, 0, 0))          //Qt::red)
135                 setCurrentIndex(0 + (int)showByLayer * 2 + (int)showUnchanged);
136         else if (color == RS_Color(255, 255, 0))        //Qt::yellow)
137                 setCurrentIndex(1 + (int)showByLayer * 2 + (int)showUnchanged);
138         else if (color == RS_Color(0, 255, 0))          //Qt::green)
139                 setCurrentIndex(2 + (int)showByLayer * 2 + (int)showUnchanged);
140         else if (color == RS_Color(0, 255, 255))        //Qt::cyan)
141                 setCurrentIndex(3 + (int)showByLayer * 2 + (int)showUnchanged);
142         else if (color == RS_Color(0, 0, 255))          //Qt::blue)
143                 setCurrentIndex(4 + (int)showByLayer * 2 + (int)showUnchanged);
144         else if (color == RS_Color(255, 0, 255))        //Qt::magenta)
145                 setCurrentIndex(5 + (int)showByLayer * 2 + (int)showUnchanged);
146 //      else if (color == Qt::white || color == Qt::black)
147         else if (color == RS_Color(255, 255, 255) || color == RS_Color(0, 0, 0))
148                 setCurrentIndex(6 + (int)showByLayer * 2 + (int)showUnchanged);
149         else if (color == RS_Color(127, 127, 127))      //Qt::gray)
150                 setCurrentIndex(7 + (int)showByLayer * 2 + (int)showUnchanged);
151         else if (color == RS_Color(191, 191, 191))
152                 setCurrentIndex(8 + (int)showByLayer * 2 + (int)showUnchanged);
153         else
154                 setCurrentIndex(9 + (int)showByLayer * 2 + (int)showUnchanged);
155
156         if (currentIndex() != 9 + (int)showByLayer * 2 + (int)showUnchanged)
157                 slotColorChanged(currentIndex());
158 }
159
160 /**
161  * Sets the color of the pixmap next to the "By Layer" item
162  * to the given color.
163  */
164 void QG_ColorBox::setLayerColor(const RS_Color & color)
165 {
166         if (showByLayer)
167         {
168                 QPixmap pixmap;
169
170                 if (color == Qt::black || color == Qt::white)
171                 {
172 //                      pixmap = color07_xpm;
173                         pixmap = QPixmap(":/res/color07.xpm");
174                 }
175                 else
176                 {
177 //                      pixmap = color00_xpm;
178                         pixmap = QPixmap(":/res/color00.xpm");
179                         int w = pixmap.width();
180                         int h = pixmap.height();
181                         QPainter painter(&pixmap);
182                         painter.fillRect(1, 1, w - 2, h - 2, color);
183                         painter.end();
184                 }
185
186 //              changeItem(pixmap, tr("By Layer"), 0);
187                 setItemIcon(0, QIcon(pixmap));
188
189                 // needed for the first time a layer is added:
190                 if (currentIndex() != 9)
191                         slotColorChanged(currentIndex());
192         }
193 }
194
195 /**
196  * Called when the color has changed. This method
197  * sets the current color to the value chosen or even
198  * offers a dialog to the user that allows him/ her to
199  * choose an individual color.
200  */
201 void QG_ColorBox::slotColorChanged(int index)
202 {
203         currentColor.resetFlags();
204
205         if (showUnchanged)
206         {
207                 if (index == 0)
208                         unchanged = true;
209                 else
210                         unchanged = false;
211         }
212
213         if (showByLayer)
214         {
215                 switch (index - (int)showUnchanged)
216                 {
217                 case 0:
218                         currentColor = RS_Color(RS2::FlagByLayer);
219                         break;
220                 case 1:
221                         currentColor = RS_Color(RS2::FlagByBlock);
222                         break;
223                 default:
224                         break;
225                 }
226         }
227
228         switch (index - (int)showByLayer * 2 - (int)showUnchanged)
229         {
230         case 0:
231                 currentColor.setRgb(255, 0, 0);
232                 break;
233         case 1:
234                 currentColor.setRgb(255, 255, 0);
235                 break;
236         case 2:
237                 currentColor.setRgb(0, 255, 0);
238                 break;
239         case 3:
240                 currentColor.setRgb(0, 255, 255);
241                 break;
242         case 4:
243                 currentColor.setRgb(0, 0, 255);
244                 break;
245         case 5:
246                 currentColor.setRgb(255, 0, 255);
247                 break;
248         case 6:
249                 currentColor.setRgb(0, 0, 0);
250                 break;
251         case 7:
252                 currentColor.setRgb(127, 127, 127);
253                 break;
254         case 8:
255                 currentColor.setRgb(191, 191, 191);
256                 break;
257         case 9:
258                 currentColor = QColorDialog::getColor(currentColor, this);
259                 break;
260         default:
261                 break;
262         }
263
264         //printf("Current color is (%d): %s\n",
265         //       index, currentColor.name().latin1());
266
267         emit colorChanged(currentColor);
268 }