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"
18 #include "rs_dialogfactory.h"
19 #include "rs_graphicview.h"
20 #include "rs_preview.h"
22 RS_ActionDrawLine::RS_ActionDrawLine(RS_EntityContainer & container, RS_GraphicView & graphicView):
23 RS_PreviewActionInterface("Draw lines", container, graphicView)
25 RS_DEBUG->print("RS_ActionDrawLine::RS_ActionDrawLine");
27 //[DONE]#warning "!!! Need to port setAutoDelete() behaviour from Qt3 to Qt4 !!!"
28 // history.setAutoDelete(true);
29 RS_DEBUG->print("RS_ActionDrawLine::RS_ActionDrawLine: OK");
32 RS_ActionDrawLine::~RS_ActionDrawLine()
37 /*virtual*/ RS2::ActionType RS_ActionDrawLine::rtti()
39 return RS2::ActionDrawLine;
42 void RS_ActionDrawLine::reset()
44 RS_DEBUG->print("RS_ActionDrawLine::reset");
45 data = RS_LineData(Vector(false), Vector(false));
46 start = Vector(false);
49 RS_DEBUG->print("RS_ActionDrawLine::reset: OK");
52 void RS_ActionDrawLine::init(int status)
54 RS_DEBUG->print("RS_ActionDrawLine::init");
55 RS_PreviewActionInterface::init(status);
58 RS_DEBUG->print("RS_ActionDrawLine::init: OK");
61 void RS_ActionDrawLine::trigger()
63 RS_PreviewActionInterface::trigger();
65 RS_Line * line = new RS_Line(container, data);
66 line->setLayerToActive();
67 line->setPenToActive();
68 container->addEntity(line);
69 //std::cout << container;
70 //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);
71 //This makes it come back as "Unknown Entity"
72 //std::cout << *((RS_Entity *)container);
73 //std::cout << *container;
78 document->startUndoCycle();
79 document->addUndoable(line);
80 document->endUndoCycle();
84 graphicView->moveRelativeZero(Vector(0.0, 0.0));
85 //This is unnecessary, because we added this to the container...
86 //#warning "!!! Here's the trouble... Trying to draw direct !!!"
87 // graphicView->drawEntity(line);
88 graphicView->moveRelativeZero(line->getEndpoint());
90 RS_DEBUG->print("RS_ActionDrawLine::trigger(): line added: %d", line->getId());
93 void RS_ActionDrawLine::mouseMoveEvent(QMouseEvent * e)
95 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent begin");
97 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: snap point");
98 Vector mouse = snapPoint(e);
99 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: snap point: OK");
101 if (getStatus() == SetEndpoint && data.startpoint.valid)
103 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: update preview");
106 preview->addEntity(new RS_Line(preview, RS_LineData(data.startpoint, mouse)));
107 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: draw preview");
111 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent end");
114 void RS_ActionDrawLine::mouseReleaseEvent(QMouseEvent * e)
116 if (e->button() == Qt::LeftButton)
118 Vector ce(snapPoint(e));
119 coordinateEvent(&ce);
121 else if (e->button() == Qt::RightButton)
126 init(getStatus() - 1);
130 void RS_ActionDrawLine::coordinateEvent(Vector * e)
132 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent");
136 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent: event was NULL");
145 data.startpoint = mouse;
148 history.append(new Vector(mouse));
149 start = data.startpoint;
150 setStatus(SetEndpoint);
151 graphicView->moveRelativeZero(mouse);
152 updateMouseButtonHints();
156 data.endpoint = mouse;
157 history.append(new Vector(mouse));
159 data.startpoint = data.endpoint;
160 updateMouseButtonHints();
167 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent: OK");
170 void RS_ActionDrawLine::commandEvent(RS_CommandEvent * e)
172 RS_DEBUG->print("RS_ActionDrawLine::commandEvent");
173 QString c = e->getCommand().toLower();
179 if (checkCommand("help", c))
181 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
182 + getAvailableCommands().join(", "));
189 if (checkCommand("close", c))
192 updateMouseButtonHints();
196 if (checkCommand("undo", c))
199 updateMouseButtonHints();
208 RS_DEBUG->print("RS_ActionDrawLine::commandEvent: OK");
211 QStringList RS_ActionDrawLine::getAvailableCommands()
222 if (history.count() >= 2)
223 cmd += command("undo");
225 if (history.count() >= 3)
226 cmd += command("close");
237 void RS_ActionDrawLine::updateMouseButtonHints()
242 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first point"), tr("Cancel"));
249 if (history.count() >= 3)
251 msg += RS_COMMANDS->command("close");
255 if (history.count() >= 2)
256 msg += RS_COMMANDS->command("undo");
258 if (history.count() >= 2)
259 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify next point or [%1]").arg(msg), tr("Back"));
261 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify next point"), tr("Back"));
266 RS_DIALOGFACTORY->updateMouseWidget("", "");
271 void RS_ActionDrawLine::showOptions()
273 RS_DEBUG->print("RS_ActionDrawLine::showOptions");
274 RS_ActionInterface::showOptions();
276 RS_DIALOGFACTORY->requestOptions(this, true);
277 RS_DEBUG->print("RS_ActionDrawLine::showOptions: OK");
280 void RS_ActionDrawLine::hideOptions()
282 RS_ActionInterface::hideOptions();
284 RS_DIALOGFACTORY->requestOptions(this, false);
287 void RS_ActionDrawLine::updateMouseCursor()
289 graphicView->setMouseCursor(RS2::CadCursor);
292 void RS_ActionDrawLine::updateToolBar()
295 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
297 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
300 void RS_ActionDrawLine::close()
302 if (history.count() > 2 && start.valid)
304 data.endpoint = start;
306 setStatus(SetStartpoint);
307 graphicView->moveRelativeZero(start);
310 RS_DIALOGFACTORY->commandMessage(tr("Cannot close sequence of lines: Not enough entities defined yet."));
313 void RS_ActionDrawLine::undo()
315 if (history.count() > 1)
317 history.removeLast();
320 graphicView->setCurrentAction(new RS_ActionEditUndo(true, *container, *graphicView));
321 data.startpoint = *history.last();
322 graphicView->moveRelativeZero(data.startpoint);
325 RS_DIALOGFACTORY->commandMessage(tr("Cannot undo: Not enough entities defined yet."));
328 void RS_ActionDrawLine::ClearHistory(void)
330 while (!history.isEmpty())
331 delete history.takeFirst();