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_dialogfactory.h"
18 #include "rs_graphicview.h"
19 #include "rs_preview.h"
24 * @param isArc true if this action will produce an ellipse arc.
25 * false if it will produce a full ellipse.
27 RS_ActionDrawEllipseAxis::RS_ActionDrawEllipseAxis(RS_EntityContainer & container, RS_GraphicView & graphicView, bool isArc): RS_PreviewActionInterface("Draw ellipse with axis",
28 container, graphicView)
31 center = Vector(false);
32 major = Vector(false);
38 RS_ActionDrawEllipseAxis::~RS_ActionDrawEllipseAxis()
42 void RS_ActionDrawEllipseAxis::init(int status)
44 RS_PreviewActionInterface::init(status);
46 if (status == SetCenter)
47 center = Vector(false);
49 if (status <= SetMajor)
50 major = Vector(false);
52 if (status <= SetMinor)
55 if (status <= SetAngle1)
58 if (status <= SetAngle2)
62 void RS_ActionDrawEllipseAxis::trigger()
64 RS_PreviewActionInterface::trigger();
66 RS_EllipseData ellipseData(center, major, ratio, angle1, angle2, false);
67 RS_Ellipse * ellipse = new RS_Ellipse(container, ellipseData);
68 ellipse->setLayerToActive();
69 ellipse->setPenToActive();
71 container->addEntity(ellipse);
76 document->startUndoCycle();
77 document->addUndoable(ellipse);
78 document->endUndoCycle();
82 Vector rz = graphicView->getRelativeZero();
83 graphicView->moveRelativeZero(Vector(0.0, 0.0));
84 graphicView->drawEntity(ellipse);
85 graphicView->moveRelativeZero(rz);
90 RS_DEBUG->print("RS_ActionDrawEllipseAxis::trigger():"
91 " entity added: %d", ellipse->getId());
94 void RS_ActionDrawEllipseAxis::mouseMoveEvent(QMouseEvent * e)
96 RS_DEBUG->print("RS_ActionDrawEllipseAxis::mouseMoveEvent begin");
98 Vector mouse = snapPoint(e);
111 RS_EllipseData ed(center, mouse - center, 0.5, 0.0, 2 * M_PI, false);
112 preview->addEntity(new RS_Ellipse(preview, ed));
119 if (center.valid && major.valid)
123 RS_Line line(NULL, RS_LineData(center - major, center + major));
124 double d = line.getDistanceToPoint(mouse);
125 ratio = d / (line.getLength() / 2);
126 RS_EllipseData ed(center, major, ratio, 0.0, 2 * M_PI, false);
127 preview->addEntity(new RS_Ellipse(preview, ed));
134 if (center.valid && major.valid)
139 //angle1 = center.angleTo(mouse);
142 m.rotate(center, -major.angle());
143 Vector v = m - center;
144 v.scale(Vector(1.0, 1.0 / ratio));
145 angle1 = v.angle(); // + major.angle();
147 preview->addEntity(new RS_Line(preview, RS_LineData(center, mouse)));
149 RS_EllipseData ed(center, major, ratio, angle1, angle1 + 1.0, false);
150 preview->addEntity(new RS_Ellipse(preview, ed));
157 if (center.valid && major.valid)
161 //angle2 = center.angleTo(mouse);
164 m.rotate(center, -major.angle());
165 Vector v = m - center;
166 v.scale(Vector(1.0, 1.0 / ratio));
167 angle2 = v.angle(); // + major.angle();
169 preview->addEntity(new RS_Line(preview, RS_LineData(center, mouse)));
172 center, major, ratio, angle1, angle2, false);
173 preview->addEntity(new RS_Ellipse(preview, ed));
181 RS_DEBUG->print("RS_ActionDrawEllipseAxis::mouseMoveEvent end");
184 void RS_ActionDrawEllipseAxis::mouseReleaseEvent(QMouseEvent * e)
186 // Proceed to next status
187 // if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton)
188 if (e->button() == Qt::LeftButton)
190 Vector ce(snapPoint(e));
191 coordinateEvent(&ce);
193 // Return to last status:
194 // else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton)
195 else if (e->button() == Qt::RightButton)
199 init(getStatus() - 1);
203 void RS_ActionDrawEllipseAxis::coordinateEvent(Vector * e)
214 graphicView->moveRelativeZero(mouse);
219 major = mouse - center;
225 RS_Line line(NULL, RS_LineData(center - major, center + major));
226 double d = line.getDistanceToPoint(mouse);
227 ratio = d / (line.getLength() / 2);
232 setStatus(SetCenter);
235 setStatus(SetAngle1);
241 //angle1 = center.angleTo(mouse);
243 m.rotate(center, -major.angle());
244 Vector v = m - center;
245 v.scale(Vector(1.0, 1.0 / ratio));
247 setStatus(SetAngle2);
253 //angle2 = center.angleTo(mouse);
255 m.rotate(center, -major.angle());
256 Vector v = m - center;
257 v.scale(Vector(1.0, 1.0 / ratio));
268 void RS_ActionDrawEllipseAxis::commandEvent(RS_CommandEvent * e)
270 QString c = e->getCommand().toLower();
272 if (checkCommand("help", c))
274 if (RS_DIALOGFACTORY != NULL)
275 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
276 + getAvailableCommands().join(", "));
284 double m = RS_Math::eval(c, &ok);
288 ratio = m / major.magnitude();
293 setStatus(SetAngle1);
295 else if (RS_DIALOGFACTORY != NULL)
296 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
303 double a = RS_Math::eval(c, &ok);
307 angle1 = RS_Math::deg2rad(a);
308 setStatus(SetAngle2);
310 else if (RS_DIALOGFACTORY != NULL)
311 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
318 double a = RS_Math::eval(c, &ok);
322 angle2 = RS_Math::deg2rad(a);
325 else if (RS_DIALOGFACTORY != NULL)
326 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
336 QStringList RS_ActionDrawEllipseAxis::getAvailableCommands()
342 void RS_ActionDrawEllipseAxis::updateMouseButtonHints()
344 if (RS_DIALOGFACTORY != NULL)
349 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify ellipse center"),
354 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify endpoint of major axis"),
359 RS_DIALOGFACTORY->updateMouseWidget(
360 tr("Specify endpoint or length of minor axis:"),
365 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify start angle"),
370 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify end angle"),
375 RS_DIALOGFACTORY->updateMouseWidget("", "");
381 void RS_ActionDrawEllipseAxis::updateMouseCursor()
383 graphicView->setMouseCursor(RS2::CadCursor);
386 void RS_ActionDrawEllipseAxis::updateToolBar()
388 if (RS_DIALOGFACTORY != NULL)
391 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
393 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarEllipses);