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