1 // actiondrawellipseaxis.cpp
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
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/22/2010 Added this text. :-)
17 #include "actiondrawellipseaxis.h"
19 #include "commandevent.h"
21 #include "dialogfactory.h"
22 #include "graphicview.h"
28 * @param isArc true if this action will produce an ellipse arc.
29 * false if it will produce a full ellipse.
31 ActionDrawEllipseAxis::ActionDrawEllipseAxis(EntityContainer & container, GraphicView & graphicView, bool isArc): ActionInterface("Draw ellipse with axis",
32 container, graphicView)
35 center = Vector(false);
36 major = Vector(false);
42 ActionDrawEllipseAxis::~ActionDrawEllipseAxis()
46 void ActionDrawEllipseAxis::init(int status)
48 ActionInterface::init(status);
50 if (status == SetCenter)
51 center = Vector(false);
53 if (status <= SetMajor)
54 major = Vector(false);
56 if (status <= SetMinor)
59 if (status <= SetAngle1)
62 if (status <= SetAngle2)
66 void ActionDrawEllipseAxis::trigger()
68 ActionInterface::trigger();
70 EllipseData ellipseData(center, major, ratio, angle1, angle2, false);
71 Ellipse * ellipse = new Ellipse(container, ellipseData);
72 ellipse->setLayerToActive();
73 ellipse->setPenToActive();
75 container->addEntity(ellipse);
80 document->startUndoCycle();
81 document->addUndoable(ellipse);
82 document->endUndoCycle();
86 Vector rz = graphicView->getRelativeZero();
87 graphicView->moveRelativeZero(Vector(0.0, 0.0));
88 graphicView->drawEntity(ellipse);
89 graphicView->moveRelativeZero(rz);
94 DEBUG->print("ActionDrawEllipseAxis::trigger():"
95 " entity added: %d", ellipse->getId());
98 void ActionDrawEllipseAxis::mouseMoveEvent(QMouseEvent * e)
100 DEBUG->print("ActionDrawEllipseAxis::mouseMoveEvent begin");
102 Vector mouse = snapPoint(e);
115 EllipseData ed(center, mouse - center, 0.5, 0.0, 2 * M_PI, false);
116 // preview->addEntity(new Ellipse(preview, ed));
123 if (center.valid && major.valid)
127 Line line(NULL, LineData(center - major, center + major));
128 double d = line.getDistanceToPoint(mouse);
129 ratio = d / (line.getLength() / 2);
130 EllipseData ed(center, major, ratio, 0.0, 2 * M_PI, false);
131 // preview->addEntity(new Ellipse(preview, ed));
138 if (center.valid && major.valid)
143 //angle1 = center.angleTo(mouse);
146 m.rotate(center, -major.angle());
147 Vector v = m - center;
148 v.scale(Vector(1.0, 1.0 / ratio));
149 angle1 = v.angle(); // + major.angle();
151 // preview->addEntity(new Line(preview, LineData(center, mouse)));
153 EllipseData ed(center, major, ratio, angle1, angle1 + 1.0, false);
154 // preview->addEntity(new Ellipse(preview, ed));
161 if (center.valid && major.valid)
165 //angle2 = center.angleTo(mouse);
168 m.rotate(center, -major.angle());
169 Vector v = m - center;
170 v.scale(Vector(1.0, 1.0 / ratio));
171 angle2 = v.angle(); // + major.angle();
173 // preview->addEntity(new Line(preview, LineData(center, mouse)));
175 EllipseData ed(center, major, ratio, angle1, angle2, false);
176 // preview->addEntity(new Ellipse(preview, ed));
184 DEBUG->print("ActionDrawEllipseAxis::mouseMoveEvent end");
187 void ActionDrawEllipseAxis::mouseReleaseEvent(QMouseEvent * e)
189 // Proceed to next status
190 if (e->button() == Qt::LeftButton)
192 Vector ce(snapPoint(e));
193 coordinateEvent(&ce);
195 // Return to last status:
196 else if (e->button() == Qt::RightButton)
200 init(getStatus() - 1);
204 void ActionDrawEllipseAxis::coordinateEvent(Vector * e)
215 graphicView->moveRelativeZero(mouse);
220 major = mouse - center;
226 Line line(NULL, LineData(center - major, center + major));
227 double d = line.getDistanceToPoint(mouse);
228 ratio = d / (line.getLength() / 2);
233 setStatus(SetCenter);
236 setStatus(SetAngle1);
242 //angle1 = center.angleTo(mouse);
244 m.rotate(center, -major.angle());
245 Vector v = m - center;
246 v.scale(Vector(1.0, 1.0 / ratio));
248 setStatus(SetAngle2);
254 //angle2 = center.angleTo(mouse);
256 m.rotate(center, -major.angle());
257 Vector v = m - center;
258 v.scale(Vector(1.0, 1.0 / ratio));
269 void ActionDrawEllipseAxis::commandEvent(CommandEvent * e)
271 QString c = e->getCommand().toLower();
273 if (checkCommand("help", c))
275 if (DIALOGFACTORY != NULL)
276 DIALOGFACTORY->commandMessage(msgAvailableCommands()
277 + getAvailableCommands().join(", "));
285 double m = Math::eval(c, &ok);
289 ratio = m / major.magnitude();
294 setStatus(SetAngle1);
296 else if (DIALOGFACTORY != NULL)
297 DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
304 double a = Math::eval(c, &ok);
308 angle1 = Math::deg2rad(a);
309 setStatus(SetAngle2);
311 else if (DIALOGFACTORY != NULL)
312 DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
319 double a = Math::eval(c, &ok);
323 angle2 = Math::deg2rad(a);
326 else if (DIALOGFACTORY != NULL)
327 DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
337 QStringList ActionDrawEllipseAxis::getAvailableCommands()
343 void ActionDrawEllipseAxis::updateMouseButtonHints()
345 if (DIALOGFACTORY != NULL)
350 DIALOGFACTORY->updateMouseWidget(tr("Specify ellipse center"),
355 DIALOGFACTORY->updateMouseWidget(tr("Specify endpoint of major axis"),
360 DIALOGFACTORY->updateMouseWidget(
361 tr("Specify endpoint or length of minor axis:"),
366 DIALOGFACTORY->updateMouseWidget(tr("Specify start angle"),
371 DIALOGFACTORY->updateMouseWidget(tr("Specify end angle"),
376 DIALOGFACTORY->updateMouseWidget("", "");
382 void ActionDrawEllipseAxis::updateMouseCursor()
384 graphicView->setMouseCursor(RS2::CadCursor);
387 void ActionDrawEllipseAxis::updateToolBar()
392 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
394 DIALOGFACTORY->requestToolBar(RS2::ToolBarEllipses);