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"
20 #include "rs_dialogfactory.h"
21 #include "graphicview.h"
22 #include "rs_preview.h"
27 * @param isArc true if this action will produce an ellipse arc.
28 * false if it will produce a full ellipse.
30 ActionDrawEllipseAxis::ActionDrawEllipseAxis(RS_EntityContainer & container, GraphicView & graphicView, bool isArc): ActionInterface("Draw ellipse with axis",
31 container, graphicView)
34 center = Vector(false);
35 major = Vector(false);
41 ActionDrawEllipseAxis::~ActionDrawEllipseAxis()
45 void ActionDrawEllipseAxis::init(int status)
47 ActionInterface::init(status);
49 if (status == SetCenter)
50 center = Vector(false);
52 if (status <= SetMajor)
53 major = Vector(false);
55 if (status <= SetMinor)
58 if (status <= SetAngle1)
61 if (status <= SetAngle2)
65 void ActionDrawEllipseAxis::trigger()
67 ActionInterface::trigger();
69 RS_EllipseData ellipseData(center, major, ratio, angle1, angle2, false);
70 RS_Ellipse * ellipse = new RS_Ellipse(container, ellipseData);
71 ellipse->setLayerToActive();
72 ellipse->setPenToActive();
74 container->addEntity(ellipse);
79 document->startUndoCycle();
80 document->addUndoable(ellipse);
81 document->endUndoCycle();
85 Vector rz = graphicView->getRelativeZero();
86 graphicView->moveRelativeZero(Vector(0.0, 0.0));
87 graphicView->drawEntity(ellipse);
88 graphicView->moveRelativeZero(rz);
93 RS_DEBUG->print("ActionDrawEllipseAxis::trigger():"
94 " entity added: %d", ellipse->getId());
97 void ActionDrawEllipseAxis::mouseMoveEvent(QMouseEvent * e)
99 RS_DEBUG->print("ActionDrawEllipseAxis::mouseMoveEvent begin");
101 Vector mouse = snapPoint(e);
114 RS_EllipseData ed(center, mouse - center, 0.5, 0.0, 2 * M_PI, false);
115 // preview->addEntity(new RS_Ellipse(preview, ed));
122 if (center.valid && major.valid)
126 RS_Line line(NULL, RS_LineData(center - major, center + major));
127 double d = line.getDistanceToPoint(mouse);
128 ratio = d / (line.getLength() / 2);
129 RS_EllipseData ed(center, major, ratio, 0.0, 2 * M_PI, false);
130 // preview->addEntity(new RS_Ellipse(preview, ed));
137 if (center.valid && major.valid)
142 //angle1 = center.angleTo(mouse);
145 m.rotate(center, -major.angle());
146 Vector v = m - center;
147 v.scale(Vector(1.0, 1.0 / ratio));
148 angle1 = v.angle(); // + major.angle();
150 // preview->addEntity(new RS_Line(preview, RS_LineData(center, mouse)));
152 RS_EllipseData ed(center, major, ratio, angle1, angle1 + 1.0, false);
153 // preview->addEntity(new RS_Ellipse(preview, ed));
160 if (center.valid && major.valid)
164 //angle2 = center.angleTo(mouse);
167 m.rotate(center, -major.angle());
168 Vector v = m - center;
169 v.scale(Vector(1.0, 1.0 / ratio));
170 angle2 = v.angle(); // + major.angle();
172 // preview->addEntity(new RS_Line(preview, RS_LineData(center, mouse)));
174 RS_EllipseData ed(center, major, ratio, angle1, angle2, false);
175 // preview->addEntity(new RS_Ellipse(preview, ed));
183 RS_DEBUG->print("ActionDrawEllipseAxis::mouseMoveEvent end");
186 void ActionDrawEllipseAxis::mouseReleaseEvent(QMouseEvent * e)
188 // Proceed to next status
189 if (e->button() == Qt::LeftButton)
191 Vector ce(snapPoint(e));
192 coordinateEvent(&ce);
194 // Return to last status:
195 else if (e->button() == Qt::RightButton)
199 init(getStatus() - 1);
203 void 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 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 ActionDrawEllipseAxis::getAvailableCommands()
342 void 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 ActionDrawEllipseAxis::updateMouseCursor()
383 graphicView->setMouseCursor(RS2::CadCursor);
386 void ActionDrawEllipseAxis::updateToolBar()
388 if (RS_DIALOGFACTORY)
391 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
393 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarEllipses);