1 // rs_actiondrawline.cpp
3 // Originally part of QCad Community Edition by Andrew Mustun
4 // Extensively rewritten and refactored by James L. Hammons
5 // (C) 2010 Underground Software
7 // JLH = James L. Hammons <jlhamm@acm.org>
10 // --- ---------- -----------------------------------------------------------
11 // JLH 05/19/2010 Added this text. :-)
14 #include "rs_actiondrawline.h"
16 #include "rs_actioneditundo.h"
17 #include "rs_snapper.h"
19 RS_ActionDrawLine::RS_ActionDrawLine(RS_EntityContainer & container,
20 RS_GraphicView & graphicView):
21 RS_PreviewActionInterface("Draw lines", container, graphicView)
23 RS_DEBUG->print("RS_ActionDrawLine::RS_ActionDrawLine");
25 //[DONE]#warning "!!! Need to port setAutoDelete() behaviour from Qt3 to Qt4 !!!"
26 // history.setAutoDelete(true);
27 RS_DEBUG->print("RS_ActionDrawLine::RS_ActionDrawLine: OK");
30 RS_ActionDrawLine::~RS_ActionDrawLine()
35 /*virtual*/ RS2::ActionType RS_ActionDrawLine::rtti()
37 return RS2::ActionDrawLine;
40 /*static*/ QAction * RS_ActionDrawLine::createGUIAction(RS2::ActionType /*type*/, QObject * /*parent*/)
42 QAction * action = new QAction(tr("&2 Points"), 0);
43 action->setEnabled(true);
44 // QAction* action = new QAction(tr("Line: 2 Points"), tr("&2 Points"),
45 // QKeySequence(), NULL);
46 action->setStatusTip(tr("Draw lines"));
50 void RS_ActionDrawLine::reset()
52 RS_DEBUG->print("RS_ActionDrawLine::reset");
53 data = RS_LineData(Vector(false), Vector(false));
54 start = Vector(false);
57 RS_DEBUG->print("RS_ActionDrawLine::reset: OK");
60 void RS_ActionDrawLine::init(int status)
62 RS_DEBUG->print("RS_ActionDrawLine::init");
63 RS_PreviewActionInterface::init(status);
66 RS_DEBUG->print("RS_ActionDrawLine::init: OK");
69 void RS_ActionDrawLine::trigger()
71 RS_PreviewActionInterface::trigger();
73 RS_Line * line = new RS_Line(container, data);
74 line->setLayerToActive();
75 line->setPenToActive();
76 container->addEntity(line);
77 //std::cout << container;
78 //printf("RS_ActionDrawLine::trigger(): new line is %f,%f to %f,%f\n", data.startpoint.x, data.startpoint.y, data.endpoint.x, data.endpoint.y);
79 //This makes it come back as "Unknown Entity"
80 //std::cout << *((RS_Entity *)container);
81 //std::cout << *container;
86 document->startUndoCycle();
87 document->addUndoable(line);
88 document->endUndoCycle();
92 graphicView->moveRelativeZero(Vector(0.0, 0.0));
93 //This is unnecessary, because we added this to the container...
94 //#warning "!!! Here's the trouble... Trying to draw direct !!!"
95 // graphicView->drawEntity(line);
96 graphicView->moveRelativeZero(line->getEndpoint());
98 RS_DEBUG->print("RS_ActionDrawLine::trigger(): line added: %d", line->getId());
101 void RS_ActionDrawLine::mouseMoveEvent(QMouseEvent * e)
103 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent begin");
105 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: snap point");
106 Vector mouse = snapPoint(e);
107 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: snap point: OK");
109 if (getStatus() == SetEndpoint && data.startpoint.valid)
111 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: update preview");
114 preview->addEntity(new RS_Line(preview, RS_LineData(data.startpoint, mouse)));
115 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: draw preview");
119 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent end");
122 void RS_ActionDrawLine::mouseReleaseEvent(QMouseEvent * e)
124 if (RS2::qtToRsButtonState(e->button()) == RS2::LeftButton)
126 RS_CoordinateEvent ce(snapPoint(e));
127 coordinateEvent(&ce);
129 else if (RS2::qtToRsButtonState(e->button()) == RS2::RightButton)
134 init(getStatus() - 1);
138 void RS_ActionDrawLine::coordinateEvent(RS_CoordinateEvent * e)
140 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent");
144 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent: event was NULL");
148 Vector mouse = e->getCoordinate();
153 data.startpoint = mouse;
156 history.append(new Vector(mouse));
157 start = data.startpoint;
158 setStatus(SetEndpoint);
159 graphicView->moveRelativeZero(mouse);
160 updateMouseButtonHints();
164 data.endpoint = mouse;
165 history.append(new Vector(mouse));
167 data.startpoint = data.endpoint;
168 updateMouseButtonHints();
175 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent: OK");
178 void RS_ActionDrawLine::commandEvent(RS_CommandEvent * e)
180 RS_DEBUG->print("RS_ActionDrawLine::commandEvent");
181 QString c = e->getCommand().toLower();
186 if (checkCommand("help", c))
188 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
189 + getAvailableCommands().join(", "));
195 if (checkCommand("close", c))
198 updateMouseButtonHints();
202 if (checkCommand("undo", c))
205 updateMouseButtonHints();
214 RS_DEBUG->print("RS_ActionDrawLine::commandEvent: OK");
217 QStringList RS_ActionDrawLine::getAvailableCommands()
226 if (history.count() >= 2)
227 cmd += command("undo");
229 if (history.count() >= 3)
230 cmd += command("close");
240 void RS_ActionDrawLine::updateMouseButtonHints()
245 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first point"), tr("Cancel"));
251 if (history.count() >= 3)
253 msg += RS_COMMANDS->command("close");
257 if (history.count() >= 2)
259 msg += RS_COMMANDS->command("undo");
262 if (history.count() >= 2)
263 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify next point or [%1]").arg(msg), tr("Back"));
265 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify next point"), tr("Back"));
269 RS_DIALOGFACTORY->updateMouseWidget("", "");
274 void RS_ActionDrawLine::showOptions()
276 RS_DEBUG->print("RS_ActionDrawLine::showOptions");
277 RS_ActionInterface::showOptions();
279 RS_DIALOGFACTORY->requestOptions(this, true);
280 RS_DEBUG->print("RS_ActionDrawLine::showOptions: OK");
283 void RS_ActionDrawLine::hideOptions()
285 RS_ActionInterface::hideOptions();
287 RS_DIALOGFACTORY->requestOptions(this, false);
290 void RS_ActionDrawLine::updateMouseCursor()
292 graphicView->setMouseCursor(RS2::CadCursor);
295 void RS_ActionDrawLine::updateToolBar()
298 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
300 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
303 void RS_ActionDrawLine::close()
305 if (history.count() > 2 && start.valid)
307 data.endpoint = start;
309 setStatus(SetStartpoint);
310 graphicView->moveRelativeZero(start);
314 RS_DIALOGFACTORY->commandMessage(tr("Cannot close sequence of lines: Not enough entities defined yet."));
318 void RS_ActionDrawLine::undo()
320 if (history.count() > 1)
322 history.removeLast();
325 graphicView->setCurrentAction(new RS_ActionEditUndo(true, *container, *graphicView));
326 data.startpoint = *history.last();
327 graphicView->moveRelativeZero(data.startpoint);
331 RS_DIALOGFACTORY->commandMessage(tr("Cannot undo: Not enough entities defined yet."));
335 void RS_ActionDrawLine::ClearHistory(void)
337 while (!history.isEmpty())
338 delete history.takeFirst();