1 /****************************************************************************
2 **
3  * Draw circle by foci and a point on circle
4 
5 Copyright (C) 2012 Dongxu Li (dongxuli2011@gmail.com)
6 Copyright (C) 2011 R. van Twisk (librecad@rvt.dds.nl)
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 **********************************************************************/
22 #include<vector>
23 #include <QAction>
24 #include <QMouseEvent>
25 #include "rs_actiondrawcircletan2_1p.h"
26 
27 #include "rs_dialogfactory.h"
28 #include "rs_graphicview.h"
29 #include "rs_commandevent.h"
30 #include "rs_arc.h"
31 #include "rs_circle.h"
32 #include "lc_quadratic.h"
33 #include "rs_coordinateevent.h"
34 #include "rs_point.h"
35 #include "rs_preview.h"
36 #include "rs_debug.h"
37 
38 struct RS_ActionDrawCircleTan2_1P::Points {
39 	RS_Vector point;
40 	RS_CircleData cData;
41 	RS_Vector coord;
42 	double radius = 0.;
43 	bool valid = false;
44 	//keep a list of centers found
45 	RS_VectorSolutions centers;
46         std::vector<RS_AtomicEntity*> circles;
47 };
48 
49 /**
50  * Constructor.
51  *
52  */
RS_ActionDrawCircleTan2_1P(RS_EntityContainer & container,RS_GraphicView & graphicView)53 RS_ActionDrawCircleTan2_1P::RS_ActionDrawCircleTan2_1P(
54         RS_EntityContainer& container,
55         RS_GraphicView& graphicView)
56     :RS_PreviewActionInterface("Draw tangent circle 2P",
57 							   container, graphicView)
58 	, pPoints(new Points{})
59 {
60 	actionType=RS2::ActionDrawCircleTan2_1P;
61 }
62 
63 RS_ActionDrawCircleTan2_1P::~RS_ActionDrawCircleTan2_1P() = default;
64 
init(int status)65 void RS_ActionDrawCircleTan2_1P::init(int status) {
66     if(status>=0) {
67         RS_Snapper::suspend();
68     }
69 	if(status>(int) pPoints->circles.size()) status=(int) pPoints->circles.size();
70     RS_PreviewActionInterface::init(status);
71 	for(int i=0; i<status; ++i){
72 		if(!pPoints->circles[i]) {
73             status=i;
74             break;
75         }
76     }
77     bool updateNeeded(false);
78 	for(size_t i=status>=0?status:0; i<pPoints->circles.size(); ++i){
79         if(pPoints->circles[i])
80             if(pPoints->circles[i]->isHighlighted()){
81                 pPoints->circles[i]->setHighlighted(false);
82                 updateNeeded=true;
83             }
84     }
85     if(updateNeeded) graphicView->redraw(RS2::RedrawDrawing);
86 	pPoints->circles.resize(status>=0?status:0);
87 }
88 
89 
finish(bool updateTB)90 void RS_ActionDrawCircleTan2_1P::finish(bool updateTB){
91     if( pPoints->circles.size() >0) {
92 		for(RS_AtomicEntity*const circle: pPoints->circles)
93             circle->setHighlighted(false);
94         graphicView->redraw(RS2::RedrawDrawing);
95     }
96     RS_PreviewActionInterface::finish(updateTB);
97 }
98 
trigger()99 void RS_ActionDrawCircleTan2_1P::trigger() {
100     RS_PreviewActionInterface::trigger();
101 	RS_Circle* c=new RS_Circle(container, pPoints->cData);
102 
103     container->addEntity(c);
104 
105     // upd. undo list:
106     if (document) {
107         document->startUndoCycle();
108         document->addUndoable(c);
109         document->endUndoCycle();
110     }
111 
112 
113 	for(RS_AtomicEntity*const circle: pPoints->circles)
114         circle->setHighlighted(false);
115     graphicView->redraw(RS2::RedrawDrawing);
116     pPoints->circles.clear();
117 
118 
119     RS_DEBUG->print("RS_ActionDrawCircleTan2_1P::trigger():"
120                     " entity added: %d", c->getId());
121     init(SetCircle1);
122 }
123 
124 
getCenters()125 bool RS_ActionDrawCircleTan2_1P::getCenters()
126 {
127     if(pPoints->circles.size()<2) return false;
128 	LC_Quadratic lc0(pPoints->circles[0], pPoints->point);
129 	LC_Quadratic lc1(pPoints->circles[1], pPoints->point);
130 
131 	auto const& list=LC_Quadratic::getIntersection(lc0,lc1);
132 	pPoints->centers.clear();
133 	for(const RS_Vector& vp: list){
134 		auto ds=vp.distanceTo(pPoints->point)-RS_TOLERANCE;
135         bool validBranch(true);
136         for(int j=0;j<2;j++){
137             if(pPoints->circles[j]->rtti()==RS2::EntityCircle||pPoints->circles[j]->rtti()==RS2::EntityArc){
138                 if( vp.distanceTo(pPoints->circles[j]->getCenter()) <= ds) {
139                     validBranch=false;
140                     break;
141                 }
142             }
143         }
144 		if(validBranch)  pPoints->centers.push_back(vp);
145     }
146 	return pPoints->centers.size()>0;
147 }
148 
mouseMoveEvent(QMouseEvent * e)149 void RS_ActionDrawCircleTan2_1P::mouseMoveEvent(QMouseEvent* e) {
150     RS_DEBUG->print("RS_ActionDrawCircleTan2_1P::mouseMoveEvent begin");
151 
152     switch( getStatus()){
153     case SetPoint:
154 		pPoints->coord=snapPoint(e);
155 		pPoints->point=pPoints->coord;
156         break;
157     case SetCenter:
158 		pPoints->coord=graphicView->toGraph(e->x(),e->y());
159         break;
160     default:
161         return;
162     }
163     deletePreview();
164     if(preparePreview()){
165 		RS_Circle* e=new RS_Circle(preview.get(), pPoints->cData);
166 		for (auto const& vp: pPoints->centers)
167 			preview->addEntity(new RS_Point(preview.get(), vp));
168         preview->addEntity(e);
169         drawPreview();
170     }
171     RS_DEBUG->print("RS_ActionDrawCircleTan2_1P::mouseMoveEvent end");
172 }
173 
preparePreview()174 bool RS_ActionDrawCircleTan2_1P::preparePreview(){
175 	if (!getCenters()) return false;
176 	pPoints->cData.center=pPoints->centers.getClosest(pPoints->coord);
177 	pPoints->cData.radius=pPoints->point.distanceTo(pPoints->cData.center);
178     return true;
179 }
180 
catchCircle(QMouseEvent * e)181 RS_Entity* RS_ActionDrawCircleTan2_1P::catchCircle(QMouseEvent* e) {
182 	RS_Entity* ret=nullptr;
183     RS_Entity*  en = catchEntity(e,enTypeList, RS2::ResolveAll);
184 	if (!en) return ret;
185 	if (!en->isVisible()) return ret;
186 	for(auto p: pPoints->circles){
187 		if(p && en->getId() == p->getId()) return ret; //do not pull in the same line again
188     }
189 	if(en->getParent() && en->getParent()->ignoredOnModification()){
190 		return nullptr;
191 	}
192 	return en;
193 }
194 
mouseReleaseEvent(QMouseEvent * e)195 void RS_ActionDrawCircleTan2_1P::mouseReleaseEvent(QMouseEvent* e) {
196     // Proceed to next status
197     if (e->button()==Qt::LeftButton) {
198 
199         switch (getStatus()) {
200         case SetCircle1:
201         case SetCircle2:
202         {
203             pPoints->circles.resize(getStatus());
204             RS_AtomicEntity*  en = static_cast<RS_AtomicEntity*>(catchCircle(e));
205 			if (!en) return;
206 //            circle = static_cast<RS_AtomicEntity*>(en);
207             en->setHighlighted(true);
208 			pPoints->circles.push_back(en);
209             graphicView->redraw(RS2::RedrawDrawing);
210             setStatus(getStatus()+1);
211         }
212             break;
213         case SetPoint:
214         {
215             RS_Vector snapped = snapPoint(e);
216             RS_CoordinateEvent ce(snapped);
217             coordinateEvent(&ce);
218         }
219             break;
220         case SetCenter:
221 			pPoints->coord=graphicView->toGraph(e->x(),e->y());
222             if(preparePreview()) trigger();
223             break;
224 
225         default:
226             break;
227         }
228     } else if (e->button()==Qt::RightButton) {
229         // Return to last status:
230         if(getStatus()>0){
231             deletePreview();
232         }
233         init(getStatus()-1);
234     }
235 }
236 
237 
coordinateEvent(RS_CoordinateEvent * e)238 void RS_ActionDrawCircleTan2_1P::coordinateEvent(RS_CoordinateEvent* e) {
239 
240     RS_Vector mouse = e->getCoordinate();
241     switch(getStatus()){
242 
243     case SetPoint:
244 		pPoints->point=mouse;
245 		pPoints->coord=mouse;
246         if(getCenters()) {
247 			if(pPoints->centers.size()==1) trigger();
248             else setStatus(getStatus()+1);
249         }
250         break;
251         default:
252         break;
253 //    case SetCenter:
254 //        coord=mouse;
255 //        trigger();
256     }
257 
258 }
259 
260 //fixme, support command line
261 
262 /*
263 void RS_ActionDrawCircleTan2_1P::commandEvent(RS_CommandEvent* e) {
264     QString c = e->getCommand().toLower();
265 
266     if (checkCommand("help", c)) {
267             RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
268                                              + getAvailableCommands().join(", "));
269         return;
270     }
271 
272     switch (getStatus()) {
273     case SetFocus1: {
274             bool ok;
275             double m = RS_Math::eval(c, &ok);
276 			if (ok) {
277                 ratio = m / major.magnitude();
278                 if (!isArc) {
279                     trigger();
280                 } else {
281                     setStatus(SetAngle1);
282                 }
283 			} else
284                     RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
285         }
286         break;
287 
288     case SetAngle1: {
289             bool ok;
290             double a = RS_Math::eval(c, &ok);
291 			if (ok) {
292                 angle1 = RS_Math::deg2rad(a);
293                 setStatus(SetAngle2);
294 			} else
295                     RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
296         }
297         break;
298 
299     case SetAngle2: {
300             bool ok;
301             double a = RS_Math::eval(c, &ok);
302 			if (ok) {
303                 angle2 = RS_Math::deg2rad(a);
304                 trigger();
305 			} else
306                     RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
307         }
308         break;
309 
310     default:
311         break;
312     }
313 }
314 */
315 
getAvailableCommands()316 QStringList RS_ActionDrawCircleTan2_1P::getAvailableCommands() {
317 	return {};
318 }
319 
updateMouseButtonHints()320 void RS_ActionDrawCircleTan2_1P::updateMouseButtonHints() {
321 	switch (getStatus()) {
322 	case SetCircle1:
323 		RS_DIALOGFACTORY->updateMouseWidget(tr("Specify a line/arc/circle"),
324 											tr("Cancel"));
325 		break;
326 
327 	case SetCircle2:
328 		RS_DIALOGFACTORY->updateMouseWidget(tr("Specify the another arc/circle"),
329 											tr("Back"));
330 		break;
331 
332 	case SetPoint:
333 		RS_DIALOGFACTORY->updateMouseWidget(tr("Specify a point on the tangent circle"),
334 											tr("Back"));
335 		break;
336 	case SetCenter:
337 		RS_DIALOGFACTORY->updateMouseWidget(tr("Select the center of the tangent circle"),
338 											tr("Back"));
339 		break;
340 	default:
341 		RS_DIALOGFACTORY->updateMouseWidget();
342 		break;
343 	}
344 }
345 
updateMouseCursor()346 void RS_ActionDrawCircleTan2_1P::updateMouseCursor()
347 {
348     switch (getStatus())
349     {
350         case SetCircle1:
351         case SetCircle2:
352         case SetCenter:
353             graphicView->setMouseCursor(RS2::SelectCursor);
354             break;
355         case SetPoint:
356             graphicView->setMouseCursor(RS2::CadCursor);
357             break;
358     }
359 }
360 
361 // EOF
362