]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawarctangential.cpp
GPL compliance check...
[architektonas] / src / actions / actiondrawarctangential.cpp
1 // actiondrawarctangential.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/03/2010  Added this text. :-)
15 //
16
17 #include "actiondrawarctangential.h"
18
19 #include "rs_commandevent.h"
20 #include "rs_dialogfactory.h"
21 #include "graphicview.h"
22 #include "rs_preview.h"
23
24 ActionDrawArcTangential::ActionDrawArcTangential(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Draw arcs tangential",
25                 container, graphicView)
26 {
27         reset();
28 }
29
30 ActionDrawArcTangential::~ActionDrawArcTangential()
31 {
32 }
33
34 /*virtual*/ RS2::ActionType ActionDrawArcTangential::rtti()
35 {
36         return RS2::ActionDrawArcTangential;
37 }
38
39 void ActionDrawArcTangential::reset()
40 {
41         baseEntity = NULL;
42         isStartPoint = false;
43         point = Vector(false);
44 }
45
46 void ActionDrawArcTangential::init(int status)
47 {
48         ActionInterface::init(status);
49         //reset();
50 }
51
52 void ActionDrawArcTangential::trigger()
53 {
54         ActionInterface::trigger();
55
56         if (point.valid == false || baseEntity == NULL)
57         {
58                 RS_DEBUG->print("ActionDrawArcTangential::trigger: "
59                         "conditions not met");
60                 return;
61         }
62
63         preparePreview();
64         RS_Arc * arc = new RS_Arc(container, data);
65         arc->setLayerToActive();
66         arc->setPenToActive();
67         container->addEntity(arc);
68
69         // upd. undo list:
70         if (document != NULL)
71         {
72                 document->startUndoCycle();
73                 document->addUndoable(arc);
74                 document->endUndoCycle();
75         }
76
77         deleteSnapper();
78         graphicView->moveRelativeZero(Vector(0.0, 0.0));
79         graphicView->drawEntity(arc);
80         graphicView->moveRelativeZero(arc->getCenter());
81         drawSnapper();
82
83         setStatus(SetBaseEntity);
84         reset();
85 }
86
87 void ActionDrawArcTangential::preparePreview()
88 {
89         if (baseEntity != NULL && point.valid)
90         {
91                 Vector startPoint;
92                 double direction;
93
94                 if (isStartPoint)
95                 {
96                         startPoint = baseEntity->getStartpoint();
97                         direction = RS_Math::correctAngle(baseEntity->getDirection1() + M_PI);
98                 }
99                 else
100                 {
101                         startPoint = baseEntity->getEndpoint();
102                         direction = RS_Math::correctAngle(baseEntity->getDirection2() + M_PI);
103                 }
104
105                 RS_Arc arc(NULL, RS_ArcData());
106                 bool suc = arc.createFrom2PDirectionRadius(startPoint, point, direction, data.radius);
107
108                 if (suc)
109                         data = arc.getData();
110         }
111 }
112
113 void ActionDrawArcTangential::mouseMoveEvent(QMouseEvent * e)
114 {
115         switch (getStatus())
116         {
117         case SetBaseEntity:
118                 break;
119
120         case SetEndAngle:
121                 point = snapPoint(e);
122                 preparePreview();
123
124                 if (data.isValid())
125                 {
126 //                      RS_Arc * arc = new RS_Arc(preview, data);
127 //                      deletePreview();
128 //                      clearPreview();
129 //                      preview->addEntity(arc);
130 //                      drawPreview();
131                 }
132                 break;
133
134         default:
135                 break;
136         }
137 }
138
139 void ActionDrawArcTangential::mouseReleaseEvent(QMouseEvent * e)
140 {
141         if (e->button() == Qt::LeftButton)
142         {
143                 switch (getStatus())
144                 {
145                 // set base entity:
146                 case SetBaseEntity:
147                 {
148                         Vector coord = graphicView->toGraph(e->x(), e->y());
149                         RS_Entity * entity = catchEntity(coord, RS2::ResolveAll);
150
151                         if (entity != NULL)
152                         {
153                                 if (entity->isAtomic())
154                                 {
155                                         baseEntity = (RS_AtomicEntity *)entity;
156
157                                         if (baseEntity->getStartpoint().distanceTo(coord)
158                                             < baseEntity->getEndpoint().distanceTo(coord))
159                                                 isStartPoint = true;
160                                         else
161                                                 isStartPoint = false;
162                                         setStatus(SetEndAngle);
163                                         updateMouseButtonHints();
164                                 }
165                                 else
166                                 {
167                                         // TODO: warning
168                                 }
169                         }
170                         else
171                                 deleteSnapper();
172                 }
173                         break;
174
175                 // set angle (point that defines the angle)
176                 case SetEndAngle:
177                 {
178                         Vector ce(snapPoint(e));
179                         coordinateEvent(&ce);
180                 }
181                         break;
182                 }
183         }
184         else if (e->button() == Qt::RightButton)
185         {
186                 deletePreview();
187                 deleteSnapper();
188                 init(getStatus() - 1);
189         }
190 }
191
192 void ActionDrawArcTangential::coordinateEvent(Vector * e)
193 {
194         if (!e)
195                 return;
196
197         Vector mouse = *e;
198
199         switch (getStatus())
200         {
201         case SetBaseEntity:
202                 break;
203
204         case SetEndAngle:
205                 point = mouse;
206                 trigger();
207                 break;
208
209         default:
210                 break;
211         }
212 }
213
214 void ActionDrawArcTangential::commandEvent(RS_CommandEvent * e)
215 {
216         QString c = e->getCommand().toLower();
217
218         if (checkCommand("help", c))
219         {
220                 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
221                         + getAvailableCommands().join(", "));
222                 return;
223         }
224 }
225
226 QStringList ActionDrawArcTangential::getAvailableCommands()
227 {
228         QStringList cmd;
229         return cmd;
230 }
231
232 void ActionDrawArcTangential::showOptions()
233 {
234         ActionInterface::showOptions();
235
236         if (RS_DIALOGFACTORY != NULL)
237                 RS_DIALOGFACTORY->requestOptions(this, true);
238         updateMouseButtonHints();
239 }
240
241 void ActionDrawArcTangential::hideOptions()
242 {
243         ActionInterface::hideOptions();
244
245         if (RS_DIALOGFACTORY != NULL)
246                 RS_DIALOGFACTORY->requestOptions(this, false);
247 }
248
249 void ActionDrawArcTangential::updateMouseButtonHints()
250 {
251         switch (getStatus())
252         {
253         case SetBaseEntity:
254                 RS_DIALOGFACTORY->updateMouseWidget(
255                         tr("Specify base entity"),
256                         tr("Cancel"));
257                 break;
258
259         case SetEndAngle:
260                 RS_DIALOGFACTORY->updateMouseWidget(
261                         tr("Specify end angle"), tr("Back"));
262                 break;
263
264         default:
265                 RS_DIALOGFACTORY->updateMouseWidget("", "");
266                 break;
267         }
268 }
269
270 void ActionDrawArcTangential::updateMouseCursor()
271 {
272         graphicView->setMouseCursor(RS2::CadCursor);
273 }
274
275 void ActionDrawArcTangential::updateToolBar()
276 {
277         if (!isFinished())
278                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
279         else
280                 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarArcs);
281 }
282
283 void ActionDrawArcTangential::setRadius(double r)
284 {
285         data.radius = r;
286 }
287
288 double ActionDrawArcTangential::getRadius()
289 {
290         return data.radius;
291 }