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 
27 #include<QAction>
28 #include <QMouseEvent>
29 #include "rs_actiondrawlinetangent2.h"
30 
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_creation.h"
34 #include "rs_line.h"
35 #include "rs_preview.h"
36 #include "rs_debug.h"
37 
RS_ActionDrawLineTangent2(RS_EntityContainer & container,RS_GraphicView & graphicView)38 RS_ActionDrawLineTangent2::RS_ActionDrawLineTangent2(
39     RS_EntityContainer& container,
40     RS_GraphicView& graphicView)
41 	:RS_PreviewActionInterface("Draw Tangents 2", container, graphicView)
42 	,circle1(nullptr)
43 	,circle2(nullptr)
44 	,valid(false)
45 {
46 	actionType=RS2::ActionDrawLineTangent2;
47     setStatus(SetCircle1);
48 }
49 
50 RS_ActionDrawLineTangent2::~RS_ActionDrawLineTangent2() = default;
51 
52 
finish(bool updateTB)53 void RS_ActionDrawLineTangent2::finish(bool updateTB){
54     if(circle1){
55         circle1->setHighlighted(false);
56 		graphicView->drawEntity(circle1);
57     }
58     RS_PreviewActionInterface::finish(updateTB);
59 }
60 
trigger()61 void RS_ActionDrawLineTangent2::trigger() {
62     RS_PreviewActionInterface::trigger();
63 
64 	RS_Entity* newEntity = new RS_Line(container, *lineData);
65 
66     if (newEntity) {
67         newEntity->setLayerToActive();
68         newEntity->setPenToActive();
69         container->addEntity(newEntity);
70 
71         // upd. undo list:
72         if (document) {
73             document->startUndoCycle();
74             document->addUndoable(newEntity);
75             document->endUndoCycle();
76 		}
77 		clearHighlighted();
78 
79         setStatus(SetCircle1);
80     }
81     tangent.reset();
82 }
83 
clearHighlighted()84 void RS_ActionDrawLineTangent2::clearHighlighted()
85 {
86 	for(RS_Entity** p: {&circle1, &circle2}){
87 		if(*p){
88 			(*p)->setHighlighted(false);
89 			graphicView->drawEntity(*p);
90 			*p=nullptr;
91 		}
92 	}
93 }
94 
mouseMoveEvent(QMouseEvent * e)95 void RS_ActionDrawLineTangent2::mouseMoveEvent(QMouseEvent* e) {
96 //    RS_DEBUG->print("RS_ActionDrawLineTangent2::mouseMoveEvent begin");
97 	e->accept();
98     if(getStatus() != SetCircle2) return;
99 	RS_Entity* en= catchEntity(e, circleType, RS2::ResolveAll);
100 	if(!en || en==circle1) return;
101 	if(circle2){
102 		circle2->setHighlighted(false);
103 		graphicView->drawEntity(circle2);
104 	}
105 	circle2=en;
106 	circle2->setHighlighted(true);
107 	graphicView->drawEntity(circle2);
108 	RS_Creation creation(nullptr, nullptr);
109     RS_Vector mouse(graphicView->toGraphX(e->x()),
110                     graphicView->toGraphY(e->y()));
111     tangent.reset(creation.createTangent2(mouse,
112                                           circle1,
113                                           circle2));
114 	if(!tangent.get()){
115         valid=false;
116         return;
117     }
118     valid=true;
119 	lineData.reset(new RS_LineData(tangent->getData()));
120 
121     deletePreview();
122 	preview->addEntity(new RS_Line(preview.get(), *lineData));
123     drawPreview();
124 }
125 
mouseReleaseEvent(QMouseEvent * e)126 void RS_ActionDrawLineTangent2::mouseReleaseEvent(QMouseEvent* e) {
127 
128     if (e->button()==Qt::RightButton) {
129         deletePreview();
130 		init(getStatus()-1);
131 		if(getStatus()>=0){
132 			clearHighlighted();
133         }
134         return;
135     }
136     switch (getStatus()) {
137     case SetCircle1:
138     {
139         circle1 = catchEntity(e, circleType, RS2::ResolveAll);
140 		if(!circle1) return;
141         circle1->setHighlighted(true);
142 		graphicView->drawEntity(circle1);
143         setStatus(getStatus()+1);
144     }
145         break;
146 
147     case SetCircle2:
148         if(valid) trigger();
149         break;
150     }
151 }
152 
updateMouseButtonHints()153 void RS_ActionDrawLineTangent2::updateMouseButtonHints() {
154 	switch (getStatus()) {
155 	case SetCircle1:
156 		RS_DIALOGFACTORY->updateMouseWidget(tr("Select first circle or ellipse"),
157 											tr("Cancel"));
158 		break;
159 	case SetCircle2:
160 		RS_DIALOGFACTORY->updateMouseWidget(tr("Select second circle or ellipse"),
161 											tr("Back"));
162 		break;
163 	default:
164 		RS_DIALOGFACTORY->updateMouseWidget();
165 		break;
166 	}
167 }
168 
updateMouseCursor()169 void RS_ActionDrawLineTangent2::updateMouseCursor() {
170     graphicView->setMouseCursor(RS2::SelectCursor);
171 }
172 
173 // EOF
174