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<cmath>
27 #include <QAction>
28 #include <QMouseEvent>
29 #include "rs_actiondimdiametric.h"
30 #include "rs_dimdiametric.h"
31 
32 #include "rs_dialogfactory.h"
33 #include "rs_graphicview.h"
34 #include "rs_commandevent.h"
35 #include "rs_arc.h"
36 #include "rs_circle.h"
37 #include "rs_line.h"
38 #include "rs_coordinateevent.h"
39 #include "rs_math.h"
40 #include "rs_preview.h"
41 #include "rs_debug.h"
42 
43 
RS_ActionDimDiametric(RS_EntityContainer & container,RS_GraphicView & graphicView)44 RS_ActionDimDiametric::RS_ActionDimDiametric(
45     RS_EntityContainer& container,
46     RS_GraphicView& graphicView)
47         :RS_ActionDimension("Draw Diametric Dimensions",
48 					container, graphicView)
49 		, pos(new RS_Vector{})
50 {
51 	actionType=RS2::ActionDimDiametric;
52     reset();
53 }
54 
55 RS_ActionDimDiametric::~RS_ActionDimDiametric() = default;
56 
reset()57 void RS_ActionDimDiametric::reset() {
58     RS_ActionDimension::reset();
59 
60 	edata.reset(new RS_DimDiametricData(RS_Vector{false},
61 								0.0)
62 				);
63 	entity = nullptr;
64 	*pos = {};
65     RS_DIALOGFACTORY->requestOptions(this, true, true);
66 }
67 
68 
69 
trigger()70 void RS_ActionDimDiametric::trigger() {
71     RS_PreviewActionInterface::trigger();
72 
73     preparePreview();
74     if (entity) {
75 		RS_DimDiametric* newEntity = nullptr;
76 
77         newEntity = new RS_DimDiametric(container,
78 										*data,
79 										*edata);
80 
81         newEntity->setLayerToActive();
82         newEntity->setPenToActive();
83         newEntity->update();
84         container->addEntity(newEntity);
85 
86         // upd. undo list:
87         if (document) {
88             document->startUndoCycle();
89             document->addUndoable(newEntity);
90             document->endUndoCycle();
91         }
92         RS_Vector rz = graphicView->getRelativeZero();
93 		graphicView->redraw(RS2::RedrawDrawing);
94         graphicView->moveRelativeZero(rz);
95 		RS_Snapper::finish();
96 
97     } else {
98         RS_DEBUG->print("RS_ActionDimDiametric::trigger:"
99 						" Entity is nullptr\n");
100     }
101 }
102 
103 
104 
preparePreview()105 void RS_ActionDimDiametric::preparePreview() {
106     if (entity) {
107 		double radius{0.};
108 		RS_Vector center{false};
109         if (entity->rtti()==RS2::EntityArc) {
110 			RS_Arc* p = static_cast<RS_Arc*>(entity);
111 			radius = p->getRadius();
112 			center = p->getCenter();
113         } else if (entity->rtti()==RS2::EntityCircle) {
114 			RS_Circle* p = static_cast<RS_Circle*>(entity);
115 			radius = p->getRadius();
116 			center = p->getCenter();
117         }
118 		double angle = center.angleTo(*pos);
119 
120 		data->definitionPoint.setPolar(radius, angle + M_PI);
121 		data->definitionPoint += center;
122 
123 		edata->definitionPoint.setPolar(radius, angle);
124 		edata->definitionPoint += center;
125     }
126 }
127 
128 
129 
mouseMoveEvent(QMouseEvent * e)130 void RS_ActionDimDiametric::mouseMoveEvent(QMouseEvent* e) {
131     RS_DEBUG->print("RS_ActionDimDiametric::mouseMoveEvent begin");
132 
133 	switch (getStatus()) {
134 
135     case SetPos:
136 		if (entity) {
137 			*pos = snapPoint(e);
138 
139             preparePreview();
140 			RS_DimDiametric* d = new RS_DimDiametric(preview.get(), *data, *edata);
141 
142             deletePreview();
143             preview->addEntity(d);
144             d->update();
145             drawPreview();
146         }
147         break;
148 
149     default:
150         break;
151     }
152 
153     RS_DEBUG->print("RS_ActionDimDiametric::mouseMoveEvent end");
154 }
155 
156 
157 
mouseReleaseEvent(QMouseEvent * e)158 void RS_ActionDimDiametric::mouseReleaseEvent(QMouseEvent* e) {
159 
160     if (e->button()==Qt::LeftButton) {
161         switch (getStatus()) {
162         case SetEntity: {
163                 RS_Entity* en = catchEntity(e, RS2::ResolveAll);
164                 if (en) {
165                     if (en->rtti()==RS2::EntityArc ||
166                             en->rtti()==RS2::EntityCircle) {
167 
168                         entity = en;
169                         RS_Vector center;
170 						if (entity->rtti()==RS2::EntityArc) {
171 							center =
172 									static_cast<RS_Arc*>(entity)->getCenter();
173 						} else {
174 							center =
175 									static_cast<RS_Circle*>(entity)->getCenter();
176 						}
177                         graphicView->moveRelativeZero(center);
178                         setStatus(SetPos);
179 					} else
180 						RS_DIALOGFACTORY->commandMessage(tr("Not a circle "
181 															"or arc entity"));
182                 }
183             }
184             break;
185 
186         case SetPos: {
187                 RS_CoordinateEvent ce(snapPoint(e));
188                 coordinateEvent(&ce);
189             }
190             break;
191 
192         default:
193             break;
194         }
195     } else if (e->button()==Qt::RightButton) {
196         deletePreview();
197         init(getStatus()-1);
198     }
199 
200 }
201 
202 
203 
coordinateEvent(RS_CoordinateEvent * e)204 void RS_ActionDimDiametric::coordinateEvent(RS_CoordinateEvent* e) {
205 	if (!e) return;
206 
207     switch (getStatus()) {
208     case SetPos:
209 		*pos = e->getCoordinate();
210         trigger();
211         reset();
212         setStatus(SetEntity);
213         break;
214 
215     default:
216         break;
217     }
218 }
219 
220 
221 
commandEvent(RS_CommandEvent * e)222 void RS_ActionDimDiametric::commandEvent(RS_CommandEvent* e) {
223     QString c = e->getCommand().toLower();
224 
225     if (checkCommand("help", c)) {
226         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
227                                          + getAvailableCommands().join(", "));
228         return;
229     }
230 
231     // setting new text label:
232     if (getStatus()==SetText) {
233         setText(c);
234         RS_DIALOGFACTORY->requestOptions(this, true, true);
235         graphicView->enableCoordinateInput();
236         setStatus(lastStatus);
237         return;
238     }
239 
240     // command: text
241     if (checkCommand("text", c)) {
242         lastStatus = (Status)getStatus();
243         graphicView->disableCoordinateInput();
244         setStatus(SetText);
245     }
246 
247     // setting angle
248     if (getStatus()==SetPos) {
249         bool ok;
250         double a = RS_Math::eval(c, &ok);
251 		if (ok) {
252 			pos->setPolar(1.0, RS_Math::deg2rad(a));
253 			*pos += data->definitionPoint;
254             trigger();
255             reset();
256             setStatus(SetEntity);
257         } else {
258             RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
259         }
260         return;
261     }
262 }
263 
264 
265 
getAvailableCommands()266 QStringList RS_ActionDimDiametric::getAvailableCommands() {
267     QStringList cmd;
268 
269     switch (getStatus()) {
270     case SetEntity:
271     case SetPos:
272         cmd += command("text");
273         break;
274 
275     default:
276         break;
277     }
278 
279     return cmd;
280 }
281 
282 
updateMouseButtonHints()283 void RS_ActionDimDiametric::updateMouseButtonHints() {
284     switch (getStatus()) {
285     case SetEntity:
286         RS_DIALOGFACTORY->updateMouseWidget(tr("Select arc or circle entity"),
287                                             tr("Cancel"));
288         break;
289     case SetPos:
290         RS_DIALOGFACTORY->updateMouseWidget(
291             tr("Specify dimension line location"), tr("Cancel"));
292         break;
293     case SetText:
294         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter dimension text:"), "");
295         break;
296     default:
297 		RS_DIALOGFACTORY->updateMouseWidget();
298         break;
299     }
300 }
301 
302 
303 
showOptions()304 void RS_ActionDimDiametric::showOptions() {
305     RS_ActionInterface::showOptions();
306 
307     RS_DIALOGFACTORY->requestOptions(this, true);
308 }
309 
310 
311 
hideOptions()312 void RS_ActionDimDiametric::hideOptions() {
313     RS_ActionInterface::hideOptions();
314 
315     //RS_DIALOGFACTORY->requestDimDiametricOptions(edata, false);
316     RS_DIALOGFACTORY->requestOptions(this, false);
317 }
318 
319 
320 
321 // EOF
322