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 "rs_commandevent.h"
21 #include "rs_dialogfactory.h"
22 #include "graphicview.h"
23 #include "rs_preview.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(RS_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 RS_EllipseData ellipseData(center, major, ratio, angle1, angle2, false);
71 RS_Ellipse * ellipse = new RS_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 RS_DEBUG->print("ActionDrawEllipseAxis::trigger():"
95 " entity added: %d", ellipse->getId());
98 void ActionDrawEllipseAxis::mouseMoveEvent(QMouseEvent * e)
100 RS_DEBUG->print("ActionDrawEllipseAxis::mouseMoveEvent begin");
102 Vector mouse = snapPoint(e);
115 RS_EllipseData ed(center, mouse - center, 0.5, 0.0, 2 * M_PI, false);
116 // preview->addEntity(new RS_Ellipse(preview, ed));
123 if (center.valid && major.valid)
127 RS_Line line(NULL, RS_LineData(center - major, center + major));
128 double d = line.getDistanceToPoint(mouse);
129 ratio = d / (line.getLength() / 2);
130 RS_EllipseData ed(center, major, ratio, 0.0, 2 * M_PI, false);
131 // preview->addEntity(new RS_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 RS_Line(preview, RS_LineData(center, mouse)));
153 RS_EllipseData ed(center, major, ratio, angle1, angle1 + 1.0, false);
154 // preview->addEntity(new RS_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 RS_Line(preview, RS_LineData(center, mouse)));
175 RS_EllipseData ed(center, major, ratio, angle1, angle2, false);
176 // preview->addEntity(new RS_Ellipse(preview, ed));
184 RS_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 RS_Line line(NULL, RS_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(RS_CommandEvent * e)
271 QString c = e->getCommand().toLower();
273 if (checkCommand("help", c))
275 if (RS_DIALOGFACTORY != NULL)
276 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
277 + getAvailableCommands().join(", "));
285 double m = RS_Math::eval(c, &ok);
289 ratio = m / major.magnitude();
294 setStatus(SetAngle1);
296 else if (RS_DIALOGFACTORY != NULL)
297 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
304 double a = RS_Math::eval(c, &ok);
308 angle1 = RS_Math::deg2rad(a);
309 setStatus(SetAngle2);
311 else if (RS_DIALOGFACTORY != NULL)
312 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
319 double a = RS_Math::eval(c, &ok);
323 angle2 = RS_Math::deg2rad(a);
326 else if (RS_DIALOGFACTORY != NULL)
327 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
337 QStringList ActionDrawEllipseAxis::getAvailableCommands()
343 void ActionDrawEllipseAxis::updateMouseButtonHints()
345 if (RS_DIALOGFACTORY != NULL)
350 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify ellipse center"),
355 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify endpoint of major axis"),
360 RS_DIALOGFACTORY->updateMouseWidget(
361 tr("Specify endpoint or length of minor axis:"),
366 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify start angle"),
371 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify end angle"),
376 RS_DIALOGFACTORY->updateMouseWidget("", "");
382 void ActionDrawEllipseAxis::updateMouseCursor()
384 graphicView->setMouseCursor(RS2::CadCursor);
387 void ActionDrawEllipseAxis::updateToolBar()
389 if (RS_DIALOGFACTORY)
392 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
394 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarEllipses);