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