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_coordinatewidget.h"
27 
28 #include "rs_settings.h"
29 #include "rs_vector.h"
30 #include "rs_graphic.h"
31 
32 /*
33  *  Constructs a QG_CoordinateWidget as a child of 'parent', with the
34  *  name 'name' and widget flags set to 'f'.
35  */
QG_CoordinateWidget(QWidget * parent,const char * name,Qt::WindowFlags fl)36 QG_CoordinateWidget::QG_CoordinateWidget(QWidget* parent, const char* name, Qt::WindowFlags fl)
37     : QWidget(parent, fl)
38 {
39     setObjectName(name);
40     setupUi(this);
41 
42     lCoord1->setText("");
43     lCoord2->setText("");
44     lCoord1b->setText("");
45     lCoord2b->setText("");
46 
47     graphic = NULL;
48     prec = 4;
49     format = RS2::Decimal;
50     aprec = 2;
51     aformat = RS2::DegreesDecimal;
52 }
53 
54 /*
55  *  Destroys the object and frees any allocated resources
56  */
~QG_CoordinateWidget()57 QG_CoordinateWidget::~QG_CoordinateWidget()
58 {
59     // no need to delete child widgets, Qt does it all for us
60 }
61 
62 /*
63  *  Sets the strings of the subwidgets using the current
64  *  language.
65  */
languageChange()66 void QG_CoordinateWidget::languageChange()
67 {
68     retranslateUi(this);
69 }
70 
setGraphic(RS_Graphic * graphic)71 void QG_CoordinateWidget::setGraphic(RS_Graphic* graphic) {
72     this->graphic = graphic;
73 
74     setCoordinates(RS_Vector(0.0,0.0), RS_Vector(0.0,0.0), true);
75 }
76 
77 
setCoordinates(const RS_Vector & abs,const RS_Vector & rel,bool updateFormat)78 void QG_CoordinateWidget::setCoordinates(const RS_Vector& abs,
79                                          const RS_Vector& rel, bool updateFormat) {
80     setCoordinates(abs.x, abs.y, rel.x, rel.y, updateFormat);
81 }
82 
83 
setCoordinates(double x,double y,double rx,double ry,bool updateFormat)84 void QG_CoordinateWidget::setCoordinates(double x, double y,
85         double rx, double ry, bool updateFormat) {
86 
87     if (graphic) {
88         if (updateFormat) {
89             format = graphic->getLinearFormat();
90             prec = graphic->getLinearPrecision();
91             aformat = graphic->getAngleFormat();
92             aprec = graphic->getAnglePrecision();
93         }
94 
95         // abs / rel coordinates:
96         QString absX = RS_Units::formatLinear(x,
97                                                graphic->getUnit(),
98                                                format, prec);
99         QString absY = RS_Units::formatLinear(y,
100                                                graphic->getUnit(),
101                                                format, prec);
102         QString relX = RS_Units::formatLinear(rx,
103                                                graphic->getUnit(),
104                                                format, prec);
105         QString relY = RS_Units::formatLinear(ry,
106                                                graphic->getUnit(),
107                                                format, prec);
108 
109         lCoord1->setText(absX + " , " + absY);
110         lCoord2->setText(relX + " , " + relY);
111 
112         // polar coordinates:
113         RS_Vector v;
114         v = RS_Vector(x, y);
115         QString str;
116         QString rStr = RS_Units::formatLinear(v.magnitude(),
117                                                graphic->getUnit(),
118                                                format, prec);
119         QString aStr = RS_Units::formatAngle(v.angle(),
120                                                aformat, aprec);
121 
122         str = rStr + " < " + aStr;
123         lCoord1b->setText(str);
124 
125         v = RS_Vector(rx, ry);
126         rStr = RS_Units::formatLinear(v.magnitude(),
127                                                graphic->getUnit(),
128                                                format, prec);
129         aStr = RS_Units::formatAngle(v.angle(),
130                                                aformat, aprec);
131         str = rStr + " < " + aStr;
132         lCoord2b->setText(str);
133     }
134 }
135