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 // (C) 2010 Underground Software
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 05/22/2010 Added this text. :-)
15 #include "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 ActionDrawEllipseAxis::ActionDrawEllipseAxis(RS_EntityContainer & container, GraphicView & graphicView, bool isArc): ActionInterface("Draw ellipse with axis",
29 container, graphicView)
32 center = Vector(false);
33 major = Vector(false);
39 ActionDrawEllipseAxis::~ActionDrawEllipseAxis()
43 void ActionDrawEllipseAxis::init(int status)
45 ActionInterface::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 ActionDrawEllipseAxis::trigger()
65 ActionInterface::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("ActionDrawEllipseAxis::trigger():"
92 " entity added: %d", ellipse->getId());
95 void ActionDrawEllipseAxis::mouseMoveEvent(QMouseEvent * e)
97 RS_DEBUG->print("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)));
172 RS_EllipseData ed(center, major, ratio, angle1, angle2, false);
173 // preview->addEntity(new RS_Ellipse(preview, ed));
181 RS_DEBUG->print("ActionDrawEllipseAxis::mouseMoveEvent end");
184 void ActionDrawEllipseAxis::mouseReleaseEvent(QMouseEvent * e)
186 // Proceed to next status
187 if (e->button() == Qt::LeftButton)
189 Vector ce(snapPoint(e));
190 coordinateEvent(&ce);
192 // Return to last status:
193 else if (e->button() == Qt::RightButton)
197 init(getStatus() - 1);
201 void ActionDrawEllipseAxis::coordinateEvent(Vector * e)
212 graphicView->moveRelativeZero(mouse);
217 major = mouse - center;
223 RS_Line line(NULL, RS_LineData(center - major, center + major));
224 double d = line.getDistanceToPoint(mouse);
225 ratio = d / (line.getLength() / 2);
230 setStatus(SetCenter);
233 setStatus(SetAngle1);
239 //angle1 = center.angleTo(mouse);
241 m.rotate(center, -major.angle());
242 Vector v = m - center;
243 v.scale(Vector(1.0, 1.0 / ratio));
245 setStatus(SetAngle2);
251 //angle2 = center.angleTo(mouse);
253 m.rotate(center, -major.angle());
254 Vector v = m - center;
255 v.scale(Vector(1.0, 1.0 / ratio));
266 void ActionDrawEllipseAxis::commandEvent(RS_CommandEvent * e)
268 QString c = e->getCommand().toLower();
270 if (checkCommand("help", c))
272 if (RS_DIALOGFACTORY != NULL)
273 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
274 + getAvailableCommands().join(", "));
282 double m = RS_Math::eval(c, &ok);
286 ratio = m / major.magnitude();
291 setStatus(SetAngle1);
293 else if (RS_DIALOGFACTORY != NULL)
294 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
301 double a = RS_Math::eval(c, &ok);
305 angle1 = RS_Math::deg2rad(a);
306 setStatus(SetAngle2);
308 else if (RS_DIALOGFACTORY != NULL)
309 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
316 double a = RS_Math::eval(c, &ok);
320 angle2 = RS_Math::deg2rad(a);
323 else if (RS_DIALOGFACTORY != NULL)
324 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
334 QStringList ActionDrawEllipseAxis::getAvailableCommands()
340 void ActionDrawEllipseAxis::updateMouseButtonHints()
342 if (RS_DIALOGFACTORY != NULL)
347 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify ellipse center"),
352 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify endpoint of major axis"),
357 RS_DIALOGFACTORY->updateMouseWidget(
358 tr("Specify endpoint or length of minor axis:"),
363 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify start angle"),
368 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify end angle"),
373 RS_DIALOGFACTORY->updateMouseWidget("", "");
379 void ActionDrawEllipseAxis::updateMouseCursor()
381 graphicView->setMouseCursor(RS2::CadCursor);
384 void ActionDrawEllipseAxis::updateToolBar()
386 if (RS_DIALOGFACTORY)
389 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
391 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarEllipses);