]> Shamusworld >> Repos - architektonas/blob - src/forms/dlgtext.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / forms / dlgtext.cpp
1 // dlgtext.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/28/2010  Created this file. :-)
15 //
16
17 #include "dlgtext.h"
18
19 #include "filterdxf.h"
20 #include "font.h"
21 #include "system.h"
22 #include "text.h"
23 #include "settings.h"
24
25 DlgText::DlgText(QWidget * parent/*= 0*/, Qt::WindowFlags flags/*= 0*/):
26         QDialog(parent, flags)
27 {
28         ui.setupUi(this);
29
30         ui.cbFont->init();
31         font = NULL;
32         text = NULL;
33         isNew = false;
34         updateUniCharComboBox(0);
35         updateUniCharButton(0);
36 }
37
38 DlgText::~DlgText()
39 {
40         if (isNew)
41         {
42                 settings.beginGroup("Draw");
43                 settings.setValue("TextHeight", ui.leHeight->text());
44                 settings.setValue("TextFont", ui.cbFont->currentText());
45                 settings.setValue("TextDefault", ui.cbDefault->isChecked());
46                 settings.setValue("TextAlignment", getAlignment());
47                 //settings.setValue("/TextLetterSpacing", leLetterSpacing->text());
48                 //settings.setValue("/TextWordSpacing", leWordSpacing->text());
49                 settings.setValue("TextLineSpacingFactor", ui.leLineSpacingFactor->text());
50 //              settings.setValue("TextString", ui.teText->text());
51                 settings.setValue("TextString", ui.teText->toPlainText());
52                 //settings.setValue("/TextShape", getShape());
53                 settings.setValue("TextAngle", ui.leAngle->text());
54                 //settings.setValue("/TextRadius", leRadius->text());
55                 settings.endGroup();
56         }
57 }
58
59 void DlgText::updateUniCharComboBox(int)
60 {
61         QString t = ui.cbUniPage->currentText();
62 //      int i1 = t.find('-');
63         int i1 = t.indexOf('-');
64 //      int i2 = t.find(']');
65         int i2 = t.indexOf(']');
66         int min = t.mid(1, i1 - 1).toInt(NULL, 16);
67         int max = t.mid(i1 + 1, i2 - i1 - 1).toInt(NULL, 16);
68         ui.cbUniChar->clear();
69
70         for(int c=min; c<=max; c++)
71         {
72                 char buf[5];
73                 sprintf(buf, "%04X", c);
74 //              ui.cbUniChar->insertItem(QString("[%1] %2").arg(buf).arg(QChar(c)));
75                 ui.cbUniChar->addItem(QString("[%1] %2").arg(buf).arg(QChar(c)));
76         }
77 }
78
79 /**
80  * Sets the text entity represented by this dialog.
81  */
82 void DlgText::setText(Text & t, bool isNew)
83 {
84         text = &t;
85         this->isNew = isNew;
86
87         QString fon;
88         QString height;
89         QString def;
90         QString alignment;
91         //QString letterSpacing;
92         //QString wordSpacing;
93         QString lineSpacingFactor;
94         QString str;
95         //QString shape;
96         QString angle;
97
98         if (isNew)
99         {
100                 settings.beginGroup("Draw");
101                 //default font depending on locale
102 //              Q3CString iso = System::localeToISO( QTextCodec::locale() );
103 //              QString iso = System::localeToISO(QTextCodec::locale());
104                 QString iso = System::localeToISO(QLocale::system().name());
105
106                 if (iso == "ISO8859-1")
107                         fon = settings.value("TextFont", "normallatin1").toString();
108                 else if (iso == "ISO8859-2")
109                         fon = settings.value("TextFont", "normallatin2").toString();
110                 else if (iso == "ISO8859-7")
111                         fon = settings.value("TextFont", "greekc").toString();
112                 else if (iso == "KOI8-U" || iso == "KOI8-R")
113                         fon = settings.value("TextFont", "cyrillic_ii").toString();
114                 else
115                         fon = settings.value("TextFont", "standard").toString();
116
117                 height = settings.value("TextHeight", "1.0").toString();
118                 def = settings.value("TextDefault", true).toBool();
119                 alignment = settings.value("TextAlignment", 1).toInt();
120                 //letterSpacing = settings.value("/TextLetterSpacing", "0");
121                 //wordSpacing = settings.value("/TextWordSpacing", "0");
122                 lineSpacingFactor = settings.value("TextLineSpacingFactor", "1").toString();
123                 str = settings.value("TextString", "").toString();
124                 //shape = settings.value("/TextShape", "0");
125                 angle = settings.value("TextAngle", "0").toString();
126                 //radius = settings.value("/TextRadius", "10");
127                 settings.endGroup();
128         }
129         else
130         {
131                 fon = text->getStyle();
132                 setFont(fon);
133                 height = QString("%1").arg(text->getHeight());
134
135                 if (font != NULL)
136                 {
137                         if (font->getLineSpacingFactor() == text->getLineSpacingFactor())
138                                 def = "1";
139                         else
140                                 def = "0";
141                 }
142
143                 alignment = QString("%1").arg(text->getAlignment());
144                 //QString letterSpacing = settings.value("/TextLetterSpacing", "0");
145                 //QString wordSpacing = settings.value("/TextWordSpacing", "0");
146                 lineSpacingFactor = QString("%1").arg(text->getLineSpacingFactor());
147
148                 str = text->getText();
149                 //QString shape = settings.value("/TextShape", "0");
150                 angle = QString("%1").arg(Math::rad2deg(text->getAngle()));
151         }
152
153         ui.cbDefault->setChecked(def == "1");
154         setFont(fon);
155         ui.leHeight->setText(height);
156         setAlignment(alignment.toInt());
157
158         if (def != "1" || font == NULL)
159         {
160                 //leLetterSpacing->setText(letterSpacing);
161                 //leWordSpacing->setText(wordSpacing);
162                 ui.leLineSpacingFactor->setText(lineSpacingFactor);
163         }
164         else
165         {
166                 //leLetterSpacing->setText(font->getLetterSpacing());
167                 //leWordSpacing->setText(font->getWordSpacing());
168                 ui.leLineSpacingFactor->setText(QString("%1").arg(font->getLineSpacingFactor()));
169         }
170
171 //      ui.teText->setText(str);
172         ui.teText->setPlainText(str);
173         //setShape(shape.toInt());
174         ui.leAngle->setText(angle);
175         //leRadius->setText(radius);
176         ui.teText->setFocus();
177         ui.teText->selectAll();
178 }
179
180 /**
181  * Updates the text entity represented by the dialog to fit the choices of the user.
182  */
183 void DlgText::updateText()
184 {
185         if (text != NULL)
186         {
187                 text->setStyle(ui.cbFont->currentText());
188                 text->setHeight(ui.leHeight->text().toDouble());
189 //              text->setText(FilterDXF::toNativeString(ui.teText->text()));
190                 text->setText(FilterDXF::toNativeString(ui.teText->toPlainText()));
191                 //text->setLetterSpacing(leLetterSpacing.toDouble());
192                 text->setLineSpacingFactor(ui.leLineSpacingFactor->text().toDouble());
193                 text->setAlignment(getAlignment());
194                 text->setAngle(Math::deg2rad(ui.leAngle->text().toDouble()));
195         }
196 }
197
198 void DlgText::setAlignmentTL()
199 {
200         setAlignment(1);
201 }
202
203 void DlgText::setAlignmentTC()
204 {
205         setAlignment(2);
206 }
207
208 void DlgText::setAlignmentTR()
209 {
210         setAlignment(3);
211 }
212
213 void DlgText::setAlignmentML()
214 {
215         setAlignment(4);
216 }
217
218 void DlgText::setAlignmentMC()
219 {
220         setAlignment(5);
221 }
222
223 void DlgText::setAlignmentMR()
224 {
225         setAlignment(6);
226 }
227
228 void DlgText::setAlignmentBL()
229 {
230         setAlignment(7);
231 }
232
233 void DlgText::setAlignmentBC()
234 {
235         setAlignment(8);
236 }
237
238 void DlgText::setAlignmentBR()
239 {
240         setAlignment(9);
241 }
242
243 void DlgText::setAlignment(int a)
244 {
245         ui.bTL->setChecked(false);
246         ui.bTC->setChecked(false);
247         ui.bTR->setChecked(false);
248         ui.bML->setChecked(false);
249         ui.bMC->setChecked(false);
250         ui.bMR->setChecked(false);
251         ui.bBL->setChecked(false);
252         ui.bBC->setChecked(false);
253         ui.bBR->setChecked(false);
254
255         switch (a)
256         {
257         case 1:
258                 ui.bTL->setChecked(true);
259                 break;
260         case 2:
261                 ui.bTC->setChecked(true);
262                 break;
263         case 3:
264                 ui.bTR->setChecked(true);
265                 break;
266         case 4:
267                 ui.bML->setChecked(true);
268                 break;
269         case 5:
270                 ui.bMC->setChecked(true);
271                 break;
272         case 6:
273                 ui.bMR->setChecked(true);
274                 break;
275         case 7:
276                 ui.bBL->setChecked(true);
277                 break;
278         case 8:
279                 ui.bBC->setChecked(true);
280                 break;
281         case 9:
282                 ui.bBR->setChecked(true);
283                 break;
284         default:
285                 break;
286         }
287 }
288
289 int DlgText::getAlignment()
290 {
291         if (ui.bTL->isChecked())
292                 return 1;
293         else if (ui.bTC->isChecked())
294                 return 2;
295         else if (ui.bTR->isChecked())
296                 return 3;
297         else if (ui.bML->isChecked())
298                 return 4;
299         else if (ui.bMC->isChecked())
300                 return 5;
301         else if (ui.bMR->isChecked())
302                 return 6;
303         else if (ui.bBL->isChecked())
304                 return 7;
305         else if (ui.bBC->isChecked())
306                 return 8;
307         else if (ui.bBR->isChecked())
308                 return 9;
309
310         return 1;
311 }
312
313 void DlgText::setFont(const QString & f)
314 {
315 //      ui.cbFont->setCurrentText(f);
316         ui.cbFont->setItemText(ui.cbFont->currentIndex(), f);
317         font = ui.cbFont->getFont();
318         defaultChanged(false);
319 }
320
321 /*
322 void DlgText::setShape(int s) {
323     switch (s) {
324     case 0:
325         rbStraight->setChecked(true);
326         break;
327     case 1:
328         rbRound1->setChecked(true);
329         break;
330     case 2:
331         rbRound2->setChecked(true);
332         break;
333     default:
334         break;
335     }
336 }
337
338 int DlgText::getShape() {
339     if (rbStraight->isOn()) {
340         return 0;
341     } else if (rbRound1->isOn()) {
342         return 1;
343     } else if (rbRound2->isOn()) {
344         return 2;
345     }
346     return 1;
347 }
348 */
349
350 void DlgText::defaultChanged(bool)
351 {
352         if (ui.cbDefault->isChecked() && font != NULL)
353                 ui.leLineSpacingFactor->setText(QString("%1").arg(font->getLineSpacingFactor()));
354 }
355
356 void DlgText::loadText()
357 {
358 //      QString fn = Q3FileDialog::getOpenFileName(QString::null, QString::null, this);
359         QString fn = QFileDialog::getOpenFileName(this);
360
361         if (!fn.isEmpty())
362                 load(fn);
363 }
364
365 void DlgText::load(const QString & fn)
366 {
367         QFile f(fn);
368
369         if (!f.open(QIODevice::ReadOnly))
370                 return;
371
372 //      Q3TextStream ts(&f);
373         QTextStream ts(&f);
374 //      ui.teText->setText(ts.readAll());
375         ui.teText->setPlainText(ts.readAll());
376 }
377
378 void DlgText::saveText()
379 {
380 //      QString fn = Q3FileDialog::getSaveFileName(QString::null, QString::null, this);
381         QString fn = QFileDialog::getSaveFileName(this);
382
383         if (!fn.isEmpty())
384                 save(fn);
385 }
386
387 void DlgText::save(const QString & fn)
388 {
389 //      QString text = ui.teText->text();
390         QString text = ui.teText->toPlainText();
391         QFile f(fn);
392
393         if (f.open(QIODevice::WriteOnly))
394         {
395 //              Q3TextStream t(&f);
396                 QTextStream t(&f);
397                 t << text;
398                 f.close();
399         }
400 }
401
402 void DlgText::insertSymbol(int)
403 {
404         QString str = ui.cbSymbol->currentText();
405 //      int i = str.find('(');
406         int i = str.indexOf('(');
407
408         if (i != -1)
409 //              ui.teText->insert(QString("%1").arg(str.at(i + 1)));
410                 ui.teText->insertPlainText(QString("%1").arg(str.at(i + 1)));
411 }
412
413 void DlgText::updateUniCharButton(int)
414 {
415         QString t = ui.cbUniChar->currentText();
416 //      int i1 = t.find(']');
417         int i1 = t.indexOf(']');
418         int c = t.mid(1, i1 - 1).toInt(NULL, 16);
419         ui.bUnicode->setText(QString("%1").arg(QChar(c)));
420 }
421
422 void DlgText::insertChar()
423 {
424         QString t = ui.cbUniChar->currentText();
425 //      int i1 = t.find(']');
426         int i1 = t.indexOf(']');
427         int c = t.mid(1, i1 - 1).toInt(NULL, 16);
428 //      ui.teText->insert(QString("%1").arg(QChar(c)));
429         ui.teText->insertPlainText(QString("%1").arg(QChar(c)));
430 }