]> Shamusworld >> Repos - architektonas/blob - src/forms/dlgoptionsdrawing.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / forms / dlgoptionsdrawing.cpp
1 // dlgoptionsdrawing.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/18/2010  Added this text. :-)
15 //
16
17 #include "dlgoptionsdrawing.h"
18
19 #include "enums.h"
20 #include "debug.h"
21 #include "filterdxf.h"
22 #include "drawing.h"
23 #include "math.h"
24 #include "units.h"
25 #include "vector.h"
26
27 DlgOptionsDrawing::DlgOptionsDrawing(QWidget * parent/*= 0*/, Qt::WindowFlags flags/*= 0*/):
28         QDialog(parent, flags)
29 {
30         ui.setupUi(this);
31
32         graphic = NULL;
33
34         // precision list:
35         QString s;
36         QString format;
37
38         for(int i=0; i<=8; i++)
39         {
40                 format.sprintf("%%.0%df", i);
41                 s.sprintf(format.toAscii().data(), 0.0);
42                 listPrec1 << s;
43         }
44
45         // Main drawing unit:
46         for(int i=RS2::None; i<RS2::LastUnit; i++)
47 //              ui.cbUnit->insertItem(Units::unitToString((RS2::Unit)i));
48                 ui.cbUnit->addItem(Units::unitToString((RS2::Unit)i));
49
50         // init units combobox:
51         QStringList unitList;
52         unitList << tr("Scientific")
53                 << tr("Decimal")
54                 << tr("Engineering")
55                 << tr("Architectural")
56                 << tr("Fractional");
57 //      ui.cbLengthFormat->insertStringList(unitList);
58         ui.cbLengthFormat->addItems(unitList);
59
60         // init angle units combobox:
61         QStringList aunitList;
62         aunitList << tr("Decimal Degrees")
63                 << tr("Deg/min/sec")
64                 << tr("Gradians")
65                 << tr("Radians")
66                 << tr("Surveyor's units");
67 //      ui.cbAngleFormat->insertStringList(aunitList);
68         ui.cbAngleFormat->addItems(aunitList);
69
70         // Paper format:
71         for(int i=RS2::Custom; i<=RS2::NPageSize; i++)
72 //              ui.cbPaperFormat->insertItem(Units::paperFormatToString((RS2::PaperFormat)i));
73                 ui.cbPaperFormat->addItem(Units::paperFormatToString((RS2::PaperFormat)i));
74
75         // Encodings:
76         /*
77         QStringList encodingList;
78         encodingList << "Latin1"
79         << "Big5"
80         << "Big5-HKSCS"
81         << "eucJP"
82         << "eucKR"
83         << "GB2312"
84         << "GBK"
85         << "GB18030"
86         << "JIS7"
87         << "Shift-JIS"
88         << "TSCII"
89         << "utf88-bit "
90         << "utf16"
91         << "KOI8-R"
92         << "KOI8-U"
93         << "ISO8859-1"
94         << "ISO8859-2"
95         << "ISO8859-3"
96         << "ISO8859-4"
97         << "ISO8859-5"
98         << "ISO8859-6"
99         << "ISO8859-7"
100         << "ISO8859-8"
101         << "ISO8859-8-i"
102         << "ISO8859-9"
103         << "ISO8859-10 "
104         << "ISO8859-13 "
105         << "ISO8859-14 "
106         << "ISO8859-15"
107         << "IBM 850 "
108         << "IBM 866 "
109         << "CP874 "
110         << "CP1250"
111         << "CP1251"
112         << "CP1252"
113         << "CP1253"
114         << "CP1254"
115         << "CP1255"
116         << "CP1256"
117         << "CP1257"
118         << "CP1258 "
119         << "Apple Roman "
120         << "TIS-620";
121         cbEncoding->insertStringList(encodingList);
122         */
123 }
124
125 DlgOptionsDrawing::~DlgOptionsDrawing()
126 {
127 }
128
129 /**
130 * Sets the graphic and updates the GUI to match the drawing.
131 */
132 void DlgOptionsDrawing::setGraphic(Drawing * g)
133 {
134         graphic = g;
135
136         if (graphic == NULL)
137                 return;
138
139         // main drawing unit:
140         int insunits = graphic->getVariableInt("$INSUNITS", 0);
141 //      ui.cbUnit->setCurrentText(Units::unitToString(FilterDXF::numberToUnit(insunits)));
142         ui.cbUnit->setItemText(ui.cbUnit->currentIndex(),
143                 Units::unitToString(FilterDXF::numberToUnit(insunits)));
144
145         // units / length format:
146         int lunits = graphic->getVariableInt("$LUNITS", 2);
147 //      ui.cbLengthFormat->setCurrentItem(lunits - 1);
148         ui.cbLengthFormat->setCurrentIndex(lunits - 1);
149
150         // units length precision:
151         int luprec = graphic->getVariableInt("$LUPREC", 4);
152         updateLengthPrecision();
153 //      ui.cbLengthPrecision->setCurrentItem(luprec);
154         ui.cbLengthPrecision->setCurrentIndex(luprec);
155
156         // units / angle format:
157         int aunits = graphic->getVariableInt("$AUNITS", 0);
158 //      ui.cbAngleFormat->setCurrentItem(aunits);
159         ui.cbAngleFormat->setCurrentIndex(aunits);
160
161         // units angle precision:
162         int auprec = graphic->getVariableInt("$AUPREC", 2);
163         updateAnglePrecision();
164 //      ui.cbAnglePrecision->setCurrentItem(auprec);
165         ui.cbAnglePrecision->setCurrentIndex(auprec);
166
167         // paper format:
168         bool landscape;
169         RS2::PaperFormat format = graphic->getPaperFormat(&landscape);
170         DEBUG->print("DlgOptionsDrawing::setGraphic: paper format is: %d", (int)format);
171 //      ui.cbPaperFormat->setCurrentItem((int)format);
172         ui.cbPaperFormat->setCurrentIndex((int)format);
173
174         // paper orientation:
175         if (landscape)
176                 ui.rbLandscape->setChecked(true);
177         else
178                 ui.rbPortrait->setChecked(true);
179
180         // Grid:
181         ui.cbGridOn->setChecked(graphic->isGridOn());
182
183         Vector spacing = graphic->getVariableVector("$GRIDUNIT", Vector(0.0, 0.0));
184 //      ui.cbXSpacing->setCurrentText(QString("%1").arg(spacing.x));
185 //      ui.cbYSpacing->setCurrentText(QString("%1").arg(spacing.y));
186         ui.cbXSpacing->setItemText(ui.cbXSpacing->currentIndex(), QString("%1").arg(spacing.x));
187         ui.cbYSpacing->setItemText(ui.cbYSpacing->currentIndex(), QString("%1").arg(spacing.y));
188
189         if (ui.cbXSpacing->currentText() == "0")
190 //              ui.cbXSpacing->setCurrentText(tr("auto"));
191                 ui.cbXSpacing->setItemText(ui.cbXSpacing->currentIndex(), tr("auto"));
192
193         if (ui.cbYSpacing->currentText() == "0")
194 //              ui.cbYSpacing->setCurrentText(tr("auto"));
195                 ui.cbYSpacing->setItemText(ui.cbYSpacing->currentIndex(), tr("auto"));
196
197         // dimension text height:
198         RS2::Unit unit = (RS2::Unit)ui.cbUnit->currentIndex();
199
200         double dimtxt = graphic->getVariableDouble("$DIMTXT", Units::convert(2.5, RS2::Millimeter, unit));
201 //      ui.cbDimTextHeight->setCurrentText(QString("%1").arg(dimtxt));
202         ui.cbDimTextHeight->setItemText(ui.cbDimTextHeight->currentIndex(), QString("%1").arg(dimtxt));
203
204         // dimension extension line extension:
205         double dimexe = graphic->getVariableDouble("$DIMEXE", Units::convert(1.25, RS2::Millimeter, unit));
206 //      ui.cbDimExe->setCurrentText(QString("%1").arg(dimexe));
207         ui.cbDimExe->setItemText(ui.cbDimExe->currentIndex(), QString("%1").arg(dimexe));
208
209         // dimension extension line offset:
210         double dimexo = graphic->getVariableDouble("$DIMEXO", Units::convert(0.625, RS2::Millimeter, unit));
211 //      ui.cbDimExo->setCurrentText(QString("%1").arg(dimexo));
212         ui.cbDimExo->setItemText(ui.cbDimExo->currentIndex(), QString("%1").arg(dimexo));
213
214         // dimension line gap:
215         double dimgap = graphic->getVariableDouble("$DIMGAP", Units::convert(0.625, RS2::Millimeter, unit));
216 //      ui.cbDimGap->setCurrentText(QString("%1").arg(dimgap));
217         ui.cbDimGap->setItemText(ui.cbDimGap->currentIndex(), QString("%1").arg(dimgap));
218
219         // dimension arrow size:
220         double dimasz = graphic->getVariableDouble("$DIMASZ", Units::convert(2.5, RS2::Millimeter, unit));
221 //      ui.cbDimAsz->setCurrentText(QString("%1").arg(dimasz));
222         ui.cbDimAsz->setItemText(ui.cbDimAsz->currentIndex(), QString("%1").arg(dimasz));
223
224         // spline line segments per patch:
225         int splinesegs = graphic->getVariableInt("$SPLINESEGS", 8);
226 //      ui.cbSplineSegs->setCurrentText(QString("%1").arg(splinesegs));
227         ui.cbSplineSegs->setItemText(ui.cbSplineSegs->currentIndex(), QString("%1").arg(splinesegs));
228
229         DEBUG->print("DlgOptionsDrawing::setGraphic: splinesegs is: %d", splinesegs);
230
231         // encoding:
232         /*
233         QString encoding = graphic->getVariableString("$DWGCODEPAGE",
234                                                                                                 "ANSI_1252");
235         encoding=System::getEncoding(encoding);
236         cbEncoding->setCurrentText(encoding);
237         */
238
239         updatePaperSize();
240         updatePreview();
241         updateUnitLabels();
242 }
243
244 /**
245 * Called when OK is clicked.
246 */
247 void DlgOptionsDrawing::validate()
248 {
249         RS2::LinearFormat f = (RS2::LinearFormat)ui.cbLengthFormat->currentIndex();
250
251         if (f == RS2::Engineering || f == RS2::Architectural)
252         {
253                 if (Units::stringToUnit(ui.cbUnit->currentText()) != RS2::Inch)
254                 {
255                         QMessageBox::warning( this, tr("Options"),
256                                 tr("For the length formats 'Engineering' and 'Architectural', the unit must be set to Inch."), QMessageBox::Ok, Qt::NoButton);
257                         return;
258                 }
259         }
260
261         if (graphic != NULL)
262         {
263                 // units:
264                 graphic->setUnit((RS2::Unit)ui.cbUnit->currentIndex());
265
266                 graphic->addVariable("$LUNITS", ui.cbLengthFormat->currentIndex() + 1, 70);
267                 graphic->addVariable("$DIMLUNIT", ui.cbLengthFormat->currentIndex() + 1, 70);
268                 graphic->addVariable("$LUPREC", ui.cbLengthPrecision->currentIndex(), 70);
269
270                 graphic->addVariable("$AUNITS", ui.cbAngleFormat->currentIndex(), 70);
271                 graphic->addVariable("$DIMAUNIT", ui.cbAngleFormat->currentIndex(), 70);
272                 graphic->addVariable("$AUPREC", ui.cbAnglePrecision->currentIndex(), 70);
273                 graphic->addVariable("$DIMADEC", ui.cbAnglePrecision->currentIndex(), 70);
274
275                 // paper:
276                 graphic->setPaperFormat((RS2::PaperFormat)ui.cbPaperFormat->currentIndex(),
277                         ui.rbLandscape->isChecked());
278
279                 // custom paper size:
280                 if ((RS2::PaperFormat)ui.cbPaperFormat->currentIndex() == RS2::Custom)
281                 {
282                         graphic->setPaperSize(Vector(Math::eval(ui.lePaperWidth->text()),
283                                 Math::eval(ui.lePaperHeight->text())));
284                 }
285
286                 // grid:
287                 //graphic->addVariable("$GRIDMODE", (int)cbGridOn->isChecked() , 70);
288                 graphic->setGridOn(ui.cbGridOn->isChecked());
289                 Vector spacing(0.0, 0.0, 0.0);
290
291                 if (ui.cbXSpacing->currentText() == tr("auto"))
292                         spacing.x = 0.0;
293                 else
294                 {
295                         spacing.x = ui.cbXSpacing->currentText().toDouble();
296                 }
297
298                 if (ui.cbYSpacing->currentText() == tr("auto"))
299                         spacing.y = 0.0;
300                 else
301                 {
302                         spacing.y = ui.cbYSpacing->currentText().toDouble();
303                 }
304
305                 graphic->addVariable("$GRIDUNIT", spacing, 10);
306
307                 // dim:
308                 graphic->addVariable("$DIMTXT", Math::eval(ui.cbDimTextHeight->currentText()), 40);
309                 graphic->addVariable("$DIMEXE", Math::eval(ui.cbDimExe->currentText()), 40);
310                 graphic->addVariable("$DIMEXO", Math::eval(ui.cbDimExo->currentText()), 40);
311                 graphic->addVariable("$DIMGAP", Math::eval(ui.cbDimGap->currentText()), 40);
312                 graphic->addVariable("$DIMASZ", Math::eval(ui.cbDimAsz->currentText()), 40);
313
314                 // splines:
315                 graphic->addVariable("$SPLINESEGS", (int)Math::eval(ui.cbSplineSegs->currentText()), 70);
316
317                 DEBUG->print("DlgOptionsDrawing::validate: splinesegs is: %s",
318                         ui.cbSplineSegs->currentText().toLatin1().data());
319
320                 // update all dimension and spline entities in the graphic to match the new settings:
321                 graphic->updateDimensions();
322                 graphic->updateSplines();
323
324                 graphic->setModified(true);
325         }
326
327         accept();
328 }
329
330 /**
331 * Updates the length precision combobox
332 */
333 void DlgOptionsDrawing::updateLengthPrecision()
334 {
335         int index = ui.cbLengthPrecision->currentIndex();
336         ui.cbLengthPrecision->clear();
337
338         switch (ui.cbLengthFormat->currentIndex())
339         {
340                 // scientific
341         case 0:
342                 ui.cbLengthPrecision->addItem("0E+01");
343                 ui.cbLengthPrecision->addItem("0.0E+01");
344                 ui.cbLengthPrecision->addItem("0.00E+01");
345                 ui.cbLengthPrecision->addItem("0.000E+01");
346                 ui.cbLengthPrecision->addItem("0.0000E+01");
347                 ui.cbLengthPrecision->addItem("0.00000E+01");
348                 ui.cbLengthPrecision->addItem("0.000000E+01");
349                 ui.cbLengthPrecision->addItem("0.0000000E+01");
350                 ui.cbLengthPrecision->addItem("0.00000000E+01");
351                 break;
352
353                 // decimal
354                 //   (0, 0.1, 0.01, ...)
355         case 1:
356                 ui.cbLengthPrecision->addItems(listPrec1);
357                 break;
358
359                 // architectural:
360         case 3:
361                 ui.cbLengthPrecision->addItem("0'-0\"");
362                 ui.cbLengthPrecision->addItem("0'-0 1/2\"");
363                 ui.cbLengthPrecision->addItem("0'-0 1/4\"");
364                 ui.cbLengthPrecision->addItem("0'-0 1/8\"");
365                 ui.cbLengthPrecision->addItem("0'-0 1/16\"");
366                 ui.cbLengthPrecision->addItem("0'-0 1/32\"");
367                 ui.cbLengthPrecision->addItem("0'-0 1/64\"");
368                 ui.cbLengthPrecision->addItem("0'-0 1/128\"");
369                 break;
370
371                 // engineering:
372         case 2:
373                 ui.cbLengthPrecision->addItem("0'-0\"");
374                 ui.cbLengthPrecision->addItem("0'-0.0\"");
375                 ui.cbLengthPrecision->addItem("0'-0.00\"");
376                 ui.cbLengthPrecision->addItem("0'-0.000\"");
377                 ui.cbLengthPrecision->addItem("0'-0.0000\"");
378                 ui.cbLengthPrecision->addItem("0'-0.00000\"");
379                 ui.cbLengthPrecision->addItem("0'-0.000000\"");
380                 ui.cbLengthPrecision->addItem("0'-0.0000000\"");
381                 ui.cbLengthPrecision->addItem("0'-0.00000000\"");
382                 break;
383
384                 // fractional
385         case 4:
386                 ui.cbLengthPrecision->addItem("0");
387                 ui.cbLengthPrecision->addItem("0 1/2");
388                 ui.cbLengthPrecision->addItem("0 1/4");
389                 ui.cbLengthPrecision->addItem("0 1/8");
390                 ui.cbLengthPrecision->addItem("0 1/16");
391                 ui.cbLengthPrecision->addItem("0 1/32");
392                 ui.cbLengthPrecision->addItem("0 1/64");
393                 ui.cbLengthPrecision->addItem("0 1/128");
394                 break;
395
396         default:
397                 DEBUG->print(Debug::D_ERROR,
398                         "DlgOptionsDrawing::updateLengthPrecision: error");
399                 break;
400         }
401
402 //      ui.cbLengthPrecision->setCurrentItem(index);
403         ui.cbLengthPrecision->setCurrentIndex(index);
404 }
405
406 /**
407 * Updates the angle precision combobox
408 */
409 void DlgOptionsDrawing::updateAnglePrecision()
410 {
411         int index = ui.cbAnglePrecision->currentIndex();
412         ui.cbAnglePrecision->clear();
413
414         switch (ui.cbAngleFormat->currentIndex())
415         {
416                 // decimal degrees:
417         case 0:
418                 ui.cbAnglePrecision->addItems(listPrec1);
419                 break;
420
421                 // deg/min/sec:
422         case 1:
423                 ui.cbAnglePrecision->addItem(QString("0%1").arg(QChar(0xB0)));
424                 ui.cbAnglePrecision->addItem(QString("0%100'").arg(QChar(0xB0)));
425                 ui.cbAnglePrecision->addItem(QString("0%100'00\"").arg(QChar(0xB0)));
426                 ui.cbAnglePrecision->addItem(QString("0%100'00.0\"").arg(QChar(0xB0)));
427                 ui.cbAnglePrecision->addItem(QString("0%100'00.00\"").arg(QChar(0xB0)));
428                 ui.cbAnglePrecision->addItem(QString("0%100'00.000\"").arg(QChar(0xB0)));
429                 ui.cbAnglePrecision->addItem(QString("0%100'00.0000\"").arg(QChar(0xB0)));
430                 break;
431
432                 // gradians:
433         case 2:
434                 ui.cbAnglePrecision->addItem("0g");
435                 ui.cbAnglePrecision->addItem("0.0g");
436                 ui.cbAnglePrecision->addItem("0.00g");
437                 ui.cbAnglePrecision->addItem("0.000g");
438                 ui.cbAnglePrecision->addItem("0.0000g");
439                 ui.cbAnglePrecision->addItem("0.00000g");
440                 ui.cbAnglePrecision->addItem("0.000000g");
441                 ui.cbAnglePrecision->addItem("0.0000000g");
442                 ui.cbAnglePrecision->addItem("0.00000000g");
443                 break;
444
445                 // radians:
446         case 3:
447                 ui.cbAnglePrecision->addItem("0r");
448                 ui.cbAnglePrecision->addItem("0.0r");
449                 ui.cbAnglePrecision->addItem("0.00r");
450                 ui.cbAnglePrecision->addItem("0.000r");
451                 ui.cbAnglePrecision->addItem("0.0000r");
452                 ui.cbAnglePrecision->addItem("0.00000r");
453                 ui.cbAnglePrecision->addItem("0.000000r");
454                 ui.cbAnglePrecision->addItem("0.0000000r");
455                 ui.cbAnglePrecision->addItem("0.00000000r");
456                 break;
457
458                 // surveyor's units:
459         case 4:
460                 ui.cbAnglePrecision->addItem("N 0d E");
461                 ui.cbAnglePrecision->addItem("N 0d00' E");
462                 ui.cbAnglePrecision->addItem("N 0d00'00\" E");
463                 ui.cbAnglePrecision->addItem("N 0d00'00.0\" E");
464                 ui.cbAnglePrecision->addItem("N 0d00'00.00\" E");
465                 ui.cbAnglePrecision->addItem("N 0d00'00.000\" E");
466                 ui.cbAnglePrecision->addItem("N 0d00'00.0000\" E");
467                 break;
468
469         default:
470                 break;
471         }
472
473         ui.cbAnglePrecision->setCurrentIndex(index);
474 }
475
476 /**
477 * Updates the preview of unit display.
478 */
479 void DlgOptionsDrawing::updatePreview()
480 {
481         QString prev;
482         prev = Units::formatLinear(14.43112351,
483                 (RS2::Unit)ui.cbUnit->currentIndex(),
484                 (RS2::LinearFormat)(ui.cbLengthFormat->currentIndex()),
485                 ui.cbLengthPrecision->currentIndex());
486         ui.lLinear->setText(prev);
487
488         prev = Units::formatAngle(0.5327714,
489                 (RS2::AngleFormat)ui.cbAngleFormat->currentIndex(),
490                 ui.cbAnglePrecision->currentIndex());
491         ui.lAngular->setText(prev);
492 }
493
494 /**
495 * Updates the paper size. Called for initialisation as well as when the
496 * paper format changes.
497 */
498 void DlgOptionsDrawing::updatePaperSize()
499 {
500         RS2::PaperFormat format = (RS2::PaperFormat)ui.cbPaperFormat->currentIndex();
501
502         if (format == RS2::Custom)
503         {
504                 Vector s = graphic->getPaperSize();
505                 //Vector plimmin = graphic->getVariableVector("$PLIMMIN", Vector(0,0));
506                 //Vector plimmax = graphic->getVariableVector("$PLIMMAX", Vector(100,100));
507                 ui.lePaperWidth->setText(QString("%1").arg(s.x));
508                 ui.lePaperHeight->setText(QString("%1").arg(s.y));
509         }
510         else
511         {
512                 Vector s = Units::paperFormatToSize(format);
513                 ui.lePaperWidth->setText(QString("%1").arg(s.x));
514                 ui.lePaperHeight->setText(QString("%1").arg(s.y));
515         }
516
517         if (ui.cbPaperFormat->currentIndex() == 0)
518         {
519                 ui.lePaperWidth->setEnabled(true);
520                 ui.lePaperHeight->setEnabled(true);
521         }
522         else
523         {
524                 ui.lePaperWidth->setEnabled(false);
525                 ui.lePaperHeight->setEnabled(false);
526         }
527 }
528
529 /**
530 * Updates all unit labels that depend on the global unit.
531 */
532 void DlgOptionsDrawing::updateUnitLabels()
533 {
534         RS2::Unit u = (RS2::Unit)ui.cbUnit->currentIndex();
535         QString sign = Units::unitToSign(u);
536         ui.lDimUnit1->setText(sign);
537         ui.lDimUnit2->setText(sign);
538         ui.lDimUnit3->setText(sign);
539         ui.lDimUnit4->setText(sign);
540         ui.lDimUnit5->setText(sign);
541 }