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_commandevent.h"
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_preview.h"
23 RS_ActionDrawLine::RS_ActionDrawLine(RS_EntityContainer & container, GraphicView & graphicView):
24 RS_PreviewActionInterface("Draw lines", container, graphicView)
26 RS_DEBUG->print("RS_ActionDrawLine::RS_ActionDrawLine");
28 //[DONE]#warning "!!! Need to port setAutoDelete() behaviour from Qt3 to Qt4 !!!"
29 // history.setAutoDelete(true);
30 RS_DEBUG->print("RS_ActionDrawLine::RS_ActionDrawLine: OK");
33 RS_ActionDrawLine::~RS_ActionDrawLine()
38 /*virtual*/ RS2::ActionType RS_ActionDrawLine::rtti()
40 return RS2::ActionDrawLine;
43 void RS_ActionDrawLine::reset()
45 RS_DEBUG->print("RS_ActionDrawLine::reset");
46 data = RS_LineData(Vector(false), Vector(false));
47 start = Vector(false);
50 RS_DEBUG->print("RS_ActionDrawLine::reset: OK");
53 void RS_ActionDrawLine::init(int status)
55 RS_DEBUG->print("RS_ActionDrawLine::init");
56 RS_PreviewActionInterface::init(status);
59 RS_DEBUG->print("RS_ActionDrawLine::init: OK");
62 void RS_ActionDrawLine::trigger()
64 RS_PreviewActionInterface::trigger();
66 RS_Line * line = new RS_Line(container, data);
67 line->setLayerToActive();
68 line->setPenToActive();
69 container->addEntity(line);
70 //std::cout << container;
71 //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);
72 //This makes it come back as "Unknown Entity"
73 //std::cout << *((RS_Entity *)container);
74 //std::cout << *container;
79 document->startUndoCycle();
80 document->addUndoable(line);
81 document->endUndoCycle();
85 graphicView->moveRelativeZero(Vector(0.0, 0.0));
86 //This is unnecessary, because we added this to the container...
87 //#warning "!!! Here's the trouble... Trying to draw direct !!!"
88 // graphicView->drawEntity(line);
89 graphicView->moveRelativeZero(line->getEndpoint());
91 RS_DEBUG->print("RS_ActionDrawLine::trigger(): line added: %d", line->getId());
94 void RS_ActionDrawLine::mouseMoveEvent(QMouseEvent * e)
96 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent begin");
98 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: snap point");
99 Vector mouse = snapPoint(e);
100 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: snap point: OK");
102 if (getStatus() == SetEndpoint && data.startpoint.valid)
104 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: update preview");
107 preview->addEntity(new RS_Line(preview, RS_LineData(data.startpoint, mouse)));
108 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent: draw preview");
112 RS_DEBUG->print("RS_ActionDrawLine::mouseMoveEvent end");
115 void RS_ActionDrawLine::mouseReleaseEvent(QMouseEvent * e)
117 if (e->button() == Qt::LeftButton)
119 Vector ce(snapPoint(e));
120 coordinateEvent(&ce);
122 else if (e->button() == Qt::RightButton)
127 init(getStatus() - 1);
131 void RS_ActionDrawLine::coordinateEvent(Vector * e)
133 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent");
137 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent: event was NULL");
146 data.startpoint = mouse;
149 history.append(new Vector(mouse));
150 start = data.startpoint;
151 setStatus(SetEndpoint);
152 graphicView->moveRelativeZero(mouse);
153 updateMouseButtonHints();
157 data.endpoint = mouse;
158 history.append(new Vector(mouse));
160 data.startpoint = data.endpoint;
161 updateMouseButtonHints();
168 RS_DEBUG->print("RS_ActionDrawLine::coordinateEvent: OK");
171 void RS_ActionDrawLine::commandEvent(RS_CommandEvent * e)
173 RS_DEBUG->print("RS_ActionDrawLine::commandEvent");
174 QString c = e->getCommand().toLower();
180 if (checkCommand("help", c))
182 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
183 + getAvailableCommands().join(", "));
190 if (checkCommand("close", c))
193 updateMouseButtonHints();
197 if (checkCommand("undo", c))
200 updateMouseButtonHints();
209 RS_DEBUG->print("RS_ActionDrawLine::commandEvent: OK");
212 QStringList RS_ActionDrawLine::getAvailableCommands()
223 if (history.count() >= 2)
224 cmd += command("undo");
226 if (history.count() >= 3)
227 cmd += command("close");
238 void RS_ActionDrawLine::updateMouseButtonHints()
243 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first point"), tr("Cancel"));
250 if (history.count() >= 3)
252 msg += RS_COMMANDS->command("close");
256 if (history.count() >= 2)
257 msg += RS_COMMANDS->command("undo");
259 if (history.count() >= 2)
260 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify next point or [%1]").arg(msg), tr("Back"));
262 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify next point"), tr("Back"));
267 RS_DIALOGFACTORY->updateMouseWidget("", "");
272 void RS_ActionDrawLine::showOptions()
274 RS_DEBUG->print("RS_ActionDrawLine::showOptions");
275 RS_ActionInterface::showOptions();
277 RS_DIALOGFACTORY->requestOptions(this, true);
278 RS_DEBUG->print("RS_ActionDrawLine::showOptions: OK");
281 void RS_ActionDrawLine::hideOptions()
283 RS_ActionInterface::hideOptions();
285 RS_DIALOGFACTORY->requestOptions(this, false);
288 void RS_ActionDrawLine::updateMouseCursor()
290 graphicView->setMouseCursor(RS2::CadCursor);
293 void RS_ActionDrawLine::updateToolBar()
296 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
298 RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
301 void RS_ActionDrawLine::close()
303 if (history.count() > 2 && start.valid)
305 data.endpoint = start;
307 setStatus(SetStartpoint);
308 graphicView->moveRelativeZero(start);
311 RS_DIALOGFACTORY->commandMessage(tr("Cannot close sequence of lines: Not enough entities defined yet."));
314 void RS_ActionDrawLine::undo()
316 if (history.count() > 1)
318 history.removeLast();
321 graphicView->setCurrentAction(new RS_ActionEditUndo(true, *container, *graphicView));
322 data.startpoint = *history.last();
323 graphicView->moveRelativeZero(data.startpoint);
326 RS_DIALOGFACTORY->commandMessage(tr("Cannot undo: Not enough entities defined yet."));
329 void RS_ActionDrawLine::ClearHistory(void)
331 while (!history.isEmpty())
332 delete history.takeFirst();