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_lineangleoptions.h"
27 
28 #include "rs_actioninterface.h"
29 #include "rs_actiondrawlineangle.h"
30 #include "rs_settings.h"
31 #include "rs_math.h"
32 #include "rs_debug.h"
33 #include "ui_qg_lineangleoptions.h"
34 
35 /*
36  *  Constructs a QG_LineAngleOptions as a child of 'parent', with the
37  *  name 'name' and widget flags set to 'f'.
38  */
QG_LineAngleOptions(QWidget * parent,Qt::WindowFlags fl)39 QG_LineAngleOptions::QG_LineAngleOptions(QWidget* parent, Qt::WindowFlags fl)
40     : QWidget(parent, fl)
41 	, ui(new Ui::Ui_LineAngleOptions{})
42 {
43 	ui->setupUi(this);
44 }
45 
46 /*
47  *  Destroys the object and frees any allocated resources
48  */
~QG_LineAngleOptions()49 QG_LineAngleOptions::~QG_LineAngleOptions()
50 {
51 	saveSettings();
52 }
53 
54 /*
55  *  Sets the strings of the subwidgets using the current
56  *  language.
57  */
languageChange()58 void QG_LineAngleOptions::languageChange()
59 {
60 	ui->retranslateUi(this);
61 }
62 
setAction(RS_ActionInterface * a,bool update)63 void QG_LineAngleOptions::setAction(RS_ActionInterface* a, bool update) {
64     if (a && (
65                 a->rtti()==RS2::ActionDrawLineAngle
66                 ||a->rtti()==RS2::ActionDrawLineHorizontal
67                 ||a->rtti()==RS2::ActionDrawLineVertical
68                 )
69     ){
70 		action = static_cast<RS_ActionDrawLineAngle*>(a);
71         m_bFixedAngle=action->hasFixedAngle();
72 		ui->leLength->show();
73 		ui->lLength->show();
74 		ui->leAngle->setVisible(!action->hasFixedAngle());
75 		ui->lAngle->setVisible(!action->hasFixedAngle());
76 
77         QString sa;
78         QString sl;
79         int sp;
80 
81         // settings from action:
82         if (update) {
83 			if (!action->hasFixedAngle())
84                 sa = QString("%1").arg(RS_Math::rad2deg(action->getAngle()));
85             sl = QString("%1").arg(action->getLength());
86 			sp = action->getSnapPoint();
87         } else {
88         // settings from config file:
89             RS_SETTINGS->beginGroup("/Draw");
90 			if (!action->hasFixedAngle()) {
91                 sa = RS_SETTINGS->readEntry("/LineAngleAngle", "30.0");
92             } else {
93                 sa = QString("%1").arg(action->getAngle());
94             }
95             sl = RS_SETTINGS->readEntry("/LineAngleLength", "10.0");
96             sp = RS_SETTINGS->readNumEntry("/LineAngleSnapPoint", 0);
97             RS_SETTINGS->endGroup();
98 			action->setSnapPoint(sp);
99         }
100 
101 		ui->leAngle->setText(sa);
102 		ui->leLength->setText(sl);
103 				ui->cbSnapPoint->setCurrentIndex(sp);
104     } else {
105         RS_DEBUG->print(RS_Debug::D_ERROR,
106 			"QG_LineAngleOptions::setAction: wrong action type");
107 		this->action = nullptr;
108     }
109 }
110 
111 /** fixme, action could be deleted already, moved the saving into the action
112   class
113   need to implement in shared_ptr*/
saveSettings()114 void QG_LineAngleOptions::saveSettings() {
115 //    if (action) {
116 //        RS_SETTINGS->beginGroup("/Draw");
117 //        if (!action->hasFixedAngle()) {
118 //            RS_SETTINGS->writeEntry("/LineAngleAngle", RS_Math::rad2deg(action->getAngle()));
119 //        }
120 //        RS_SETTINGS->writeEntry("/LineAngleLength", action->getLength());
121 //        RS_SETTINGS->writeEntry("/LineAngleSnapPoint", action->getSnapPoint());
122 //        RS_SETTINGS->endGroup();
123 //    }
124 }
125 
updateAngle(const QString & a)126 void QG_LineAngleOptions::updateAngle(const QString& a) {
127     if (action && !action->hasFixedAngle()) {
128         action->setAngle(RS_Math::deg2rad(RS_Math::eval(a)));
129     }
130 }
131 
updateLength(const QString & l)132 void QG_LineAngleOptions::updateLength(const QString& l) {
133     if (action) {
134         bool ok(false);
135         double length=RS_Math::eval(l,&ok);
136         if(ok) action->setLength(length);
137     }
138 }
139 
updateSnapPoint(int sp)140 void QG_LineAngleOptions::updateSnapPoint(int sp) {
141     if (action) {
142         action->setSnapPoint(sp);
143     }
144 }
145