]> Shamusworld >> Repos - architektonas/blob - src/widgets/widthbox.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / widgets / widthbox.cpp
1 // widthbox.cpp
2 //
3 // Originally part of QCad Community Edition by Andrew Mustun
4 // Extensively rewritten and refactored by James L. Hammons
5 // Portions copyright (C) 2001-2003 RibbonSoft
6 // Copyright (C) 2010 Underground Software
7 // See the README and GPLv2 files for licensing and warranty information
8 //
9 // JLH = James L. Hammons <jlhamm@acm.org>
10 //
11 // Who  When        What
12 // ---  ----------  -----------------------------------------------------------
13 // JLH  05/11/2010  Added this text. :-)
14 //
15
16 #include "widthbox.h"
17
18 #include "debug.h"
19
20 /**
21  * Default Constructor. You must call init manually if you choose
22  * to use this constructor.
23  */
24 WidthBox::WidthBox(QWidget * parent, const char */*name*/): 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 widths.
34  *
35  * @param showByLayer true: Show attributes ByLayer, ByBlock
36  */
37 WidthBox::WidthBox(bool showByLayer, bool showUnchanged, QWidget * parent, const char */*name*/):
38         QComboBox(parent)
39 {
40     init(showByLayer, showUnchanged);
41 }
42
43 /**
44  * Destructor
45  */
46 WidthBox::~WidthBox()
47 {
48 }
49
50 /**
51  * Initialisation (called from constructor or manually but only
52  * once).
53  *
54  * @param showByLayer true: Show attributes ByLayer, ByBlock
55  */
56 void WidthBox::init(bool showByLayer, bool showUnchanged)
57 {
58         this->showByLayer = showByLayer;
59         this->showUnchanged = showUnchanged;
60
61         if (showUnchanged)
62                 addItem(QIcon(":/res/width00.xpm"), tr("- Unchanged -"));
63
64         if (showByLayer)
65                 addItem(QIcon(":/res/width00.xpm"), tr("By Layer"));
66                 addItem(QIcon(":/res/width00.xpm"), tr("By Block"));
67
68         addItem(QIcon(":/res/width01.xpm"), tr("Default"));
69         addItem(QIcon(":/res/width01.xpm"), tr("0.00mm"));
70         addItem(QIcon(":/res/width01.xpm"), tr("0.05mm"));
71         addItem(QIcon(":/res/width01.xpm"), tr("0.09mm"));
72         addItem(QIcon(":/res/width01.xpm"), tr("0.13mm (ISO)"));
73         addItem(QIcon(":/res/width01.xpm"), tr("0.15mm"));
74         addItem(QIcon(":/res/width01.xpm"), tr("0.18mm (ISO)"));
75         addItem(QIcon(":/res/width01.xpm"), tr("0.20mm"));
76         addItem(QIcon(":/res/width01.xpm"), tr("0.25mm (ISO)"));
77         addItem(QIcon(":/res/width01.xpm"), tr("0.30mm"));
78         addItem(QIcon(":/res/width03.xpm"), tr("0.35mm (ISO)"));
79         addItem(QIcon(":/res/width03.xpm"), tr("0.40mm"));
80         addItem(QIcon(":/res/width04.xpm"), tr("0.50mm (ISO)"));
81         addItem(QIcon(":/res/width05.xpm"), tr("0.53mm"));
82         addItem(QIcon(":/res/width05.xpm"), tr("0.60mm"));
83         addItem(QIcon(":/res/width06.xpm"), tr("0.70mm (ISO)"));
84         addItem(QIcon(":/res/width07.xpm"), tr("0.80mm"));
85         addItem(QIcon(":/res/width08.xpm"), tr("0.90mm"));
86         addItem(QIcon(":/res/width09.xpm"), tr("1.00mm (ISO)"));
87         addItem(QIcon(":/res/width10.xpm"), tr("1.06mm"));
88         addItem(QIcon(":/res/width10.xpm"), tr("1.20mm"));
89         addItem(QIcon(":/res/width12.xpm"), tr("1.40mm (ISO)"));
90         addItem(QIcon(":/res/width12.xpm"), tr("1.58mm"));
91         addItem(QIcon(":/res/width12.xpm"), tr("2.00mm (ISO)"));
92         addItem(QIcon(":/res/width12.xpm"), tr("2.11mm"));
93
94         connect(this, SIGNAL(activated(int)), this, SLOT(slotWidthChanged(int)));
95
96         setCurrentIndex(0);
97         slotWidthChanged(currentIndex());
98 }
99
100 RS2::LineWidth WidthBox::getWidth()
101 {
102         return currentWidth;
103 }
104
105 /**
106  * Sets the currently selected width item to the given width.
107  */
108 void WidthBox::setWidth(RS2::LineWidth w)
109 {
110         DEBUG->print("WidthBox::setWidth %d\n", (int)w);
111         int offset = (int)showByLayer * 2 + (int)showUnchanged;
112
113         switch (w)
114         {
115         case RS2::WidthByLayer:
116                 if (showByLayer)
117                         setCurrentIndex(0 + (int)showUnchanged);
118                 else
119                         DEBUG->print(Debug::D_WARNING, "WidthBox::setWidth: Unsupported width.");
120                 break;
121         case RS2::WidthByBlock:
122                 if (showByLayer)
123                         setCurrentIndex(1 + (int)showUnchanged);
124                 else
125                         DEBUG->print(Debug::D_WARNING, "WidthBox::setWidth: Unsupported width.");
126                 break;
127         case RS2::WidthDefault:
128                 setCurrentIndex(0 + offset);
129                 break;
130         case RS2::Width00:
131                 setCurrentIndex(1 + offset);
132                 break;
133         case RS2::Width01:
134                 setCurrentIndex(2 + offset);
135                 break;
136         case RS2::Width02:
137                 setCurrentIndex(3 + offset);
138                 break;
139         case RS2::Width03:
140                 setCurrentIndex(4 + offset);
141                 break;
142         case RS2::Width04:
143                 setCurrentIndex(5 + offset);
144                 break;
145         case RS2::Width05:
146                 setCurrentIndex(6 + offset);
147                 break;
148         case RS2::Width06:
149                 setCurrentIndex(7 + offset);
150                 break;
151         case RS2::Width07:
152                 setCurrentIndex(8 + offset);
153                 break;
154         case RS2::Width08:
155                 setCurrentIndex(9 + offset);
156                 break;
157         case RS2::Width09:
158                 setCurrentIndex(10 + offset);
159                 break;
160         case RS2::Width10:
161                 setCurrentIndex(11 + offset);
162                 break;
163         case RS2::Width11:
164                 setCurrentIndex(12 + offset);
165                 break;
166         case RS2::Width12:
167                 setCurrentIndex(13 + offset);
168                 break;
169         case RS2::Width13:
170                 setCurrentIndex(14 + offset);
171                 break;
172         case RS2::Width14:
173                 setCurrentIndex(15 + offset);
174                 break;
175         case RS2::Width15:
176                 setCurrentIndex(16 + offset);
177                 break;
178         case RS2::Width16:
179                 setCurrentIndex(17 + offset);
180                 break;
181         case RS2::Width17:
182                 setCurrentIndex(18 + offset);
183                 break;
184         case RS2::Width18:
185                 setCurrentIndex(19 + offset);
186                 break;
187         case RS2::Width19:
188                 setCurrentIndex(20 + offset);
189                 break;
190         case RS2::Width20:
191                 setCurrentIndex(21 + offset);
192                 break;
193         case RS2::Width21:
194                 setCurrentIndex(22 + offset);
195                 break;
196         case RS2::Width22:
197                 setCurrentIndex(23 + offset);
198                 break;
199         case RS2::Width23:
200                 setCurrentIndex(24 + offset);
201                 break;
202         default:
203                 break;
204         }
205
206         slotWidthChanged(currentIndex());
207 }
208
209 /**
210  * Sets the pixmap showing the width of the "By Layer" item.
211  */
212 void WidthBox::setLayerWidth(RS2::LineWidth w)
213 {
214         if (showByLayer)
215         {
216                 QPixmap pixmap;
217
218                 switch(w)
219                 {
220                 default:
221                 case RS2::Width00:
222                         pixmap = QPixmap(":/res/width00.xpm");
223                         break;
224                 case RS2::Width01:
225                 case RS2::Width02:
226                         pixmap = QPixmap(":/res/width01.xpm");
227                         break;
228                 case RS2::Width03:
229                 case RS2::Width04:
230                         pixmap = QPixmap(":/res/width02.xpm");
231                         break;
232                 case RS2::Width05:
233                 case RS2::Width06:
234                         pixmap = QPixmap(":/res/width03.xpm");
235                         break;
236                 case RS2::Width07:
237                 case RS2::Width08:
238                         pixmap = QPixmap(":/res/width04.xpm");
239                         break;
240                 case RS2::Width09:
241                 case RS2::Width10:
242                         pixmap = QPixmap(":/res/width05.xpm");
243                         break;
244                 case RS2::Width11:
245                 case RS2::Width12:
246                         pixmap = QPixmap(":/res/width06.xpm");
247                         break;
248                 case RS2::Width13:
249                 case RS2::Width14:
250                         pixmap = QPixmap(":/res/width07.xpm");
251                         break;
252                 case RS2::Width15:
253                 case RS2::Width16:
254                         pixmap = QPixmap(":/res/width08.xpm");
255                         break;
256                 case RS2::Width17:
257                 case RS2::Width18:
258                         pixmap = QPixmap(":/res/width09.xpm");
259                         break;
260                 case RS2::Width19:
261                 case RS2::Width20:
262                         pixmap = QPixmap(":/res/width10.xpm");
263                         break;
264                 case RS2::Width21:
265                 case RS2::Width22:
266                         pixmap = QPixmap(":/res/width11.xpm");
267                         break;
268                 case RS2::Width23:
269                         //case RS2::Width24:
270                         pixmap = QPixmap(":/res/width12.xpm");
271                         break;
272                 }
273
274                 setItemIcon(0, QIcon(pixmap));
275                 setItemText(0, tr("By Layer"));
276
277                 // ???needed for the first time a layer is added:
278                 slotWidthChanged(currentIndex());
279         }
280 }
281
282 bool WidthBox::isUnchanged()
283 {
284         return unchanged;
285 }
286
287 /**
288  * Called when the width has changed. This method
289  * sets the current width to the value chosen or even
290  * offers a dialog to the user that allows him/ her to
291  * choose an individual width.
292  */
293 void WidthBox::slotWidthChanged(int index)
294 {
295         DEBUG->print("WidthBox::slotWidthChanged %d\n", index);
296
297         bool done = false;
298
299         if (showUnchanged && index == 0)
300         {
301                 unchanged = true;
302                 done = true;
303         }
304         else
305         {
306                 unchanged = false;
307         }
308
309         if (!done && showByLayer)
310         {
311                 if (index == 0 + (int)showUnchanged)
312                 {
313                         currentWidth = RS2::WidthByLayer;
314                         done = true;
315                 }
316                 else if (index == 1 + (int)showUnchanged)
317                 {
318                         currentWidth = RS2::WidthByBlock;
319                         done = true;
320                 }
321         }
322
323         if (!done)
324         {
325                 switch (index - ((int)showByLayer * 2) - ((int)showUnchanged))
326                 {
327                 case 0:
328                         currentWidth = RS2::WidthDefault;
329                         break;
330                 case 1:
331                         currentWidth = RS2::Width00;
332                         break;
333                 case 2:
334                         currentWidth = RS2::Width01;
335                         break;
336                 case 3:
337                         currentWidth = RS2::Width02;
338                         break;
339                 case 4:
340                         currentWidth = RS2::Width03;
341                         break;
342                 case 5:
343                         currentWidth = RS2::Width04;
344                         break;
345                 case 6:
346                         currentWidth = RS2::Width05;
347                         break;
348                 case 7:
349                         currentWidth = RS2::Width06;
350                         break;
351                 case 8:
352                         currentWidth = RS2::Width07;
353                         break;
354                 case 9:
355                         currentWidth = RS2::Width08;
356                         break;
357                 case 10:
358                         currentWidth = RS2::Width09;
359                         break;
360                 case 11:
361                         currentWidth = RS2::Width10;
362                         break;
363                 case 12:
364                         currentWidth = RS2::Width11;
365                         break;
366                 case 13:
367                         currentWidth = RS2::Width12;
368                         break;
369                 case 14:
370                         currentWidth = RS2::Width13;
371                         break;
372                 case 15:
373                         currentWidth = RS2::Width14;
374                         break;
375                 case 16:
376                         currentWidth = RS2::Width15;
377                         break;
378                 case 17:
379                         currentWidth = RS2::Width16;
380                         break;
381                 case 18:
382                         currentWidth = RS2::Width17;
383                         break;
384                 case 19:
385                         currentWidth = RS2::Width18;
386                         break;
387                 case 20:
388                         currentWidth = RS2::Width19;
389                         break;
390                 case 21:
391                         currentWidth = RS2::Width20;
392                         break;
393                 case 22:
394                         currentWidth = RS2::Width21;
395                         break;
396                 case 23:
397                         currentWidth = RS2::Width22;
398                         break;
399                 case 24:
400                         currentWidth = RS2::Width23;
401                         break;
402                 default:
403                         break;
404                 }
405         }
406         //currentWidth = (RS2::LineWidth)(index-1);
407         //currentWidth = (RS2::LineWidth)(currentText().left(4).toDouble()*100);
408         //}
409
410         DEBUG->print("Current width is (%d): %d\n", index, ((int)currentWidth));
411
412         emit widthChanged(currentWidth);
413 }