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