1 // rs_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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/22/2010 Added this text. :-)
15 #include "rs_actiondrawellipseaxis.h"
17 #include "rs_commandevent.h"
18 #include "rs_dialogfactory.h"
19 #include "graphicview.h"
20 #include "rs_preview.h"
25 * @param isArc true if this action will produce an ellipse arc.
26 * false if it will produce a full ellipse.
28 RS_ActionDrawEllipseAxis::RS_ActionDrawEllipseAxis(RS_EntityContainer & container, GraphicView & graphicView, bool isArc): RS_PreviewActionInterface("Draw ellipse with axis",
29 container, graphicView)
32 center = Vector(false);
33 major = Vector(false);
39 RS_ActionDrawEllipseAxis::~RS_ActionDrawEllipseAxis()
43 void RS_ActionDrawEllipseAxis::init(int status)
45 RS_PreviewActionInterface::init(status);
47 if (status == SetCenter)
48 center = Vector(false);
50 if (status <= SetMajor)
51 major = Vector(false);
53 if (status <= SetMinor)
56 if (status <= SetAngle1)
59 if (status <= SetAngle2)
63 void RS_ActionDrawEllipseAxis::trigger()
65 RS_PreviewActionInterface::trigger();
67 RS_EllipseData ellipseData(center, major, ratio, angle1, angle2, false);
68 RS_Ellipse * ellipse = new RS_Ellipse(container, ellipseData);
69 ellipse->setLayerToActive();
70 ellipse->setPenToActive();
72 container->addEntity(ellipse);
77 document->startUndoCycle();
78 document->addUndoable(ellipse);
79 document->endUndoCycle();
83 Vector rz = graphicView->getRelativeZero();
84 graphicView->moveRelativeZero(Vector(0.0, 0.0));
85 graphicView->drawEntity(ellipse);
86 graphicView->moveRelativeZero(rz);
91 RS_DEBUG->print("RS_ActionDrawEllipseAxis::trigger():"
92 " entity added: %d", ellipse->getId());
95 void RS_ActionDrawEllipseAxis::mouseMoveEvent(QMouseEvent * e)
97 RS_DEBUG->print("RS_ActionDrawEllipseAxis::mouseMoveEvent begin");
99 Vector mouse = snapPoint(e);
112 RS_EllipseData ed(center, mouse - center, 0.5, 0.0, 2 * M_PI, false);
113 preview->addEntity(new RS_Ellipse(preview, ed));
120 if (center.valid && major.valid)
124 RS_Line line(NULL, RS_LineData(center - major, center + major));
125 double d = line.getDistanceToPoint(mouse);
126 ratio = d / (line.getLength() / 2);
127 RS_EllipseData ed(center, major, ratio, 0.0, 2 * M_PI, false);
128 preview->addEntity(new RS_Ellipse(preview, ed));
135 if (center.valid && major.valid)
140 //angle1 = center.angleTo(mouse);
143 m.rotate(center, -major.angle());
144 Vector v = m - center;
145 v.scale(Vector(1.0, 1.0 / ratio));
146 angle1 = v.angle(); // + major.angle();
148 preview->addEntity(new RS_Line(preview, RS_LineData(center, mouse)));
150 RS_EllipseData ed(center, major, ratio, angle1, angle1 + 1.0, false);
151 preview->addEntity(new RS_Ellipse(preview, ed));
158 if (center.valid && major.valid)
162 //angle2 = center.angleTo(mouse);
165 m.rotate(center, -major.angle());
166 Vector v = m - center;
167 v.scale(Vector(1.0, 1.0 / ratio));
168 angle2 = v.angle(); // + major.angle();
170 preview->addEntity(new RS_Line(preview, RS_LineData(center, mouse)));
173 center, major, ratio, angle1, angle2, false);
174 preview->addEntity(new RS_Ellipse(preview, ed));
182 RS_DEBUG->print("RS_ActionDrawEllipseAxis::mouseMoveEvent end");
185 void RS_ActionDrawEllipseAxis::mouseReleaseEvent(QMouseEvent * e)
187 // Proceed to next status
188 // if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton)
189 if (e->button() == Qt::LeftButton)
191 Vector ce(snapPoint(e));
192 coordinateEvent(&ce);
194 // Return to last status:
195 // else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
196 else if (e->button() == Qt::RightButton)
200 init(getStatus() - 1);
204 void RS_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 RS_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 RS_ActionDrawEllipseAxis::getAvailableCommands()
343 void RS_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 RS_ActionDrawEllipseAxis::updateMouseCursor()
384 graphicView->setMouseCursor(RS2::CadCursor);
387 void RS_ActionDrawEllipseAxis::updateToolBar()
389 if (RS_DIALOGFACTORY != NULL)
392 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
394 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarEllipses);