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