1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 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 #include "qg_dimensionlabeleditor.h"
27 
28 /*
29  *  Constructs a QG_DimensionLabelEditor as a child of 'parent', with the
30  *  name 'name' and widget flags set to 'f'.
31  */
QG_DimensionLabelEditor(QWidget * parent,Qt::WindowFlags fl)32 QG_DimensionLabelEditor::QG_DimensionLabelEditor(QWidget* parent, Qt::WindowFlags fl)
33     : QWidget(parent, fl)
34 {
35     setupUi(this);
36 
37 }
38 
39 /*
40  *  Destroys the object and frees any allocated resources
41  */
~QG_DimensionLabelEditor()42 QG_DimensionLabelEditor::~QG_DimensionLabelEditor()
43 {
44     // no need to delete child widgets, Qt does it all for us
45 }
46 
47 /*
48  *  Sets the strings of the subwidgets using the current
49  *  language.
50  */
languageChange()51 void QG_DimensionLabelEditor::languageChange()
52 {
53     retranslateUi(this);
54 }
55 
setLabel(const QString & l)56 void QG_DimensionLabelEditor::setLabel(const QString& l) {
57     int i0, i1a, i1b, i2;
58     QString label, tol1, tol2;
59     bool hasDiameter = false;
60 
61     label = l;
62 
63     if ( !label.isEmpty()) {
64         if (label.at(0)==QChar(0x2205) || label.at(0)==QChar(0xF8)) {
65             hasDiameter = true;
66             bDiameter->setChecked(true);
67         }
68     }
69 
70     i0 = l.indexOf("\\S");
71     if (i0>=0) {
72         i1a = l.indexOf("^ ", i0);
73         i1b = i1a+1;
74         if (i1a<0) {
75             i1a = i1b = l.indexOf('^', i0);
76         }
77         if (i1a>=0) {
78             i2 = l.indexOf(';', i1b);
79             label = l.mid(0, i0);
80             tol1 = l.mid(i0+2, i1a-i0-2);
81             tol2 = l.mid(i1b+1, i2-i1b-1);
82         }
83     }
84 
85     leLabel->setText(label.mid(hasDiameter));
86     leTol1->setText(tol1);
87     leTol2->setText(tol2);
88 }
89 
getLabel()90 QString QG_DimensionLabelEditor::getLabel() {
91     QString l = leLabel->text();
92 
93     // diameter:
94     if (bDiameter->isChecked()) {
95         if (l.isEmpty()) {
96             l = QString("%1<>").arg(QChar(0x2205));
97         }
98         else {
99             l = QChar(0x2205) + l;
100         }
101     }
102 
103     if (leTol1->text().isEmpty() && leTol2->text().isEmpty()) {
104         return l;
105     }
106     else {
107         return l + "\\S" + leTol1->text() +
108             "^ " + leTol2->text() + ";";
109     }
110 }
111 
insertSign(const QString & s)112 void QG_DimensionLabelEditor::insertSign(const QString& s) {
113     leLabel->insert(s.left(1));
114 }
115