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"
16 #include "rs_snapper.h"
21 * @param isArc true if this action will produce an ellipse arc.
22 * false if it will produce a full ellipse.
24 RS_ActionDrawEllipseAxis::RS_ActionDrawEllipseAxis(
25 RS_EntityContainer& container,
26 RS_GraphicView& graphicView,
28 :RS_PreviewActionInterface("Draw ellipse with axis",
29 container, graphicView) {
32 center = Vector(false);
33 major = Vector(false);
39 RS_ActionDrawEllipseAxis::~RS_ActionDrawEllipseAxis()
43 QAction * RS_ActionDrawEllipseAxis::createGUIAction(RS2::ActionType type, QObject * /*parent*/)
47 if (type==RS2::ActionDrawEllipseArcAxis)
49 action = new QAction(tr("&Ellipse Arc (Axis)"), 0);
50 // action = new QAction(tr("Ellipse Arc with Axis"),
51 // tr("&Ellipse Arc (Axis)"), QKeySequence(), NULL);
52 action->setStatusTip(tr("Draw Ellipse Arcs"));
56 action = new QAction(tr("&Ellipse (Axis)"), 0);
57 // action = new QAction(tr("Ellipse with Axis"),
58 // tr("&Ellipse (Axis)"), QKeySequence(), NULL);
59 action->setStatusTip(tr("Draw Ellipses"));
64 void RS_ActionDrawEllipseAxis::init(int status)
66 RS_PreviewActionInterface::init(status);
68 if (status==SetCenter)
70 center = Vector(false);
74 major = Vector(false);
80 if (status<=SetAngle1)
84 if (status<=SetAngle2)
92 void RS_ActionDrawEllipseAxis::trigger() {
93 RS_PreviewActionInterface::trigger();
95 RS_EllipseData ellipseData(center, major,
99 RS_Ellipse* ellipse = new RS_Ellipse(container, ellipseData);
100 ellipse->setLayerToActive();
101 ellipse->setPenToActive();
103 container->addEntity(ellipse);
106 if (document!=NULL) {
107 document->startUndoCycle();
108 document->addUndoable(ellipse);
109 document->endUndoCycle();
113 Vector rz = graphicView->getRelativeZero();
114 graphicView->moveRelativeZero(Vector(0.0,0.0));
115 graphicView->drawEntity(ellipse);
116 graphicView->moveRelativeZero(rz);
119 setStatus(SetCenter);
121 RS_DEBUG->print("RS_ActionDrawEllipseAxis::trigger():"
122 " entity added: %d", ellipse->getId());
127 void RS_ActionDrawEllipseAxis::mouseMoveEvent(QMouseEvent* e) {
128 RS_DEBUG->print("RS_ActionDrawEllipseAxis::mouseMoveEvent begin");
130 Vector mouse = snapPoint(e);
132 switch (getStatus()) {
140 RS_EllipseData ed(center, mouse-center,
144 preview->addEntity(new RS_Ellipse(preview, ed));
150 if (center.valid && major.valid) {
153 RS_Line line(NULL, RS_LineData(center-major, center+major));
154 double d = line.getDistanceToPoint(mouse);
155 ratio = d/(line.getLength()/2);
156 RS_EllipseData ed(center, major,
160 preview->addEntity(new RS_Ellipse(preview, ed));
166 if (center.valid && major.valid) {
170 //angle1 = center.angleTo(mouse);
173 m.rotate(center, -major.angle());
175 v.scale(Vector(1.0, 1.0/ratio));
176 angle1 = v.angle(); // + major.angle();
178 preview->addEntity(new RS_Line(preview, RS_LineData(center, mouse)));
180 RS_EllipseData ed(center, major,
184 preview->addEntity(new RS_Ellipse(preview, ed));
190 if (center.valid && major.valid) {
193 //angle2 = center.angleTo(mouse);
196 m.rotate(center, -major.angle());
198 v.scale(Vector(1.0, 1.0/ratio));
199 angle2 = v.angle(); // + major.angle();
201 preview->addEntity(new RS_Line(preview, RS_LineData(center, mouse)));
208 preview->addEntity(new RS_Ellipse(preview, ed));
216 RS_DEBUG->print("RS_ActionDrawEllipseAxis::mouseMoveEvent end");
221 void RS_ActionDrawEllipseAxis::mouseReleaseEvent(QMouseEvent* e) {
222 // Proceed to next status
223 if (RS2::qtToRsButtonState(e->button())==RS2::LeftButton) {
224 RS_CoordinateEvent ce(snapPoint(e));
225 coordinateEvent(&ce);
228 // Return to last status:
229 else if (RS2::qtToRsButtonState(e->button())==RS2::RightButton) {
237 void RS_ActionDrawEllipseAxis::coordinateEvent(RS_CoordinateEvent* e) {
241 Vector mouse = e->getCoordinate();
243 switch (getStatus()) {
246 graphicView->moveRelativeZero(mouse);
251 major = mouse-center;
256 RS_Line line(NULL, RS_LineData(center-major, center+major));
257 double d = line.getDistanceToPoint(mouse);
258 ratio = d/(line.getLength()/2);
261 setStatus(SetCenter);
263 setStatus(SetAngle1);
269 //angle1 = center.angleTo(mouse);
271 m.rotate(center, -major.angle());
273 v.scale(Vector(1.0, 1.0/ratio));
275 setStatus(SetAngle2);
279 //angle2 = center.angleTo(mouse);
281 m.rotate(center, -major.angle());
283 v.scale(Vector(1.0, 1.0/ratio));
295 void RS_ActionDrawEllipseAxis::commandEvent(RS_CommandEvent* e) {
296 QString c = e->getCommand().toLower();
298 if (checkCommand("help", c)) {
299 if (RS_DIALOGFACTORY!=NULL) {
300 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
301 + getAvailableCommands().join(", "));
306 switch (getStatus()) {
309 double m = RS_Math::eval(c, &ok);
311 ratio = m / major.magnitude();
315 setStatus(SetAngle1);
318 if (RS_DIALOGFACTORY!=NULL) {
319 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
327 double a = RS_Math::eval(c, &ok);
329 angle1 = RS_Math::deg2rad(a);
330 setStatus(SetAngle2);
332 if (RS_DIALOGFACTORY!=NULL) {
333 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
341 double a = RS_Math::eval(c, &ok);
343 angle2 = RS_Math::deg2rad(a);
346 if (RS_DIALOGFACTORY!=NULL) {
347 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
360 QStringList RS_ActionDrawEllipseAxis::getAvailableCommands() {
367 void RS_ActionDrawEllipseAxis::updateMouseButtonHints() {
368 if (RS_DIALOGFACTORY!=NULL) {
369 switch (getStatus()) {
371 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify ellipse center"),
376 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify endpoint of major axis"),
381 RS_DIALOGFACTORY->updateMouseWidget(
382 tr("Specify endpoint or length of minor axis:"),
387 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify start angle"),
392 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify end angle"),
397 RS_DIALOGFACTORY->updateMouseWidget("", "");
405 void RS_ActionDrawEllipseAxis::updateMouseCursor() {
406 graphicView->setMouseCursor(RS2::CadCursor);
411 void RS_ActionDrawEllipseAxis::updateToolBar() {
412 if (RS_DIALOGFACTORY!=NULL) {
414 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
416 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarEllipses);