1 // actiondrawpolyline.cpp
3 // Part of the Architektonas Project
5 // Copyright (C) 2010 Underground Software
6 // See the README and GPLv2 files for licensing and warranty information
8 // JLH = James L. Hammons <jlhamm@acm.org>
11 // --- ---------- -----------------------------------------------------------
12 // JLH 09/13/2010 Created this file. :-)
15 #include "actiondrawpolyline.h"
18 #include "dialogfactory.h"
19 #include "graphicview.h"
22 ActionDrawPolyline::ActionDrawPolyline(EntityContainer & container,
23 GraphicView & graphicView):
24 ActionInterface("Draw polyline", container, graphicView), vertex(false), polyline(NULL)
26 // vertex = Vector(false);
29 ActionDrawPolyline::~ActionDrawPolyline()
35 /*virtual*/ RS2::ActionType ActionDrawPolyline::rtti()
37 return RS2::ActionDrawPolyline;
40 void ActionDrawPolyline::trigger()
44 container->addEntity(polyline);
48 document->startUndoCycle();
49 document->addUndoable(polyline);
50 document->endUndoCycle();
53 DEBUG->print("ActionDrawPolyline::trigger(): polyline added: %d", polyline->getId());
55 graphicView->redraw(); //hm.
59 void ActionDrawPolyline::mouseMoveEvent(QMouseEvent * e)
62 if (vertex.valid && polyline)
64 Vector v = snapPoint(e);
65 Entity * ent = polyline->addVertex(v);
66 ent->setLayerToActive();
67 ent->setPenToActive();
70 // graphicView->drawEntity(ent);
74 DEBUG->print("ActionDrawPolyline::mouseMoveEvent(): line added: %d", ent->getId());
77 Vector mouse = graphicView->SnapPoint(e);
79 if (getStatus() == SetNextPoint && startPoint.valid)
81 // We have to draw a line or arc depending on the state of the "Arc"
83 // We'll start with lines...
84 graphicView->preview.clear();
85 Line * line = new Line(&(graphicView->preview), LineData(startPoint, mouse));
88 graphicView->preview.addEntity(polyline);
90 graphicView->preview.addEntity(line);
93 graphicView->redraw(); //hm.
97 Vector mouse = graphicView->snapper.snapPoint(e);
98 DEBUG->print("ActionDrawLine::mouseMoveEvent: snap point: OK");
100 if (getStatus() == SetEndpoint && data.startpoint.valid)
102 DEBUG->print("ActionDrawLine::mouseMoveEvent: update preview");
103 // This is lame. Creating a new Line every time the endpoint moves.
104 // Surely we can alter the line entity inside the preview, no?
106 graphicView->preview.clear(); // Remove entities from the container
107 Line * line = new Line(&(graphicView->preview), LineData(data.startpoint, mouse));
108 graphicView->preview.addEntity(line);
110 // We can assume there's only one line in there, can't we?
111 Line * line = (Line *)graphicView->preview.firstEntity(RS2::ResolveNone);
115 line->setEndpoint(mouse);
119 line = new Line(&(graphicView->preview), LineData(data.startpoint, mouse));
120 graphicView->preview.addEntity(line);
125 //hm. [ok, this works. :-D]
126 graphicView->redraw();
130 void ActionDrawPolyline::mousePressEvent(QMouseEvent * e)
132 if (e->button() == Qt::LeftButton)
134 // vertex = snapPoint(e);
135 // polyline = new Polyline(container, PolylineData(vertex, vertex, 0));
136 // polyline->setLayerToActive();
137 // polyline->setPenToActive();
141 void ActionDrawPolyline::mouseReleaseEvent(QMouseEvent * e)
143 if (e->button() == Qt::LeftButton)
145 Vector ce(graphicView->SnapPoint(e));
146 coordinateEvent(&ce);
151 //printf("ActionDrawPolyline::mouseReleaseEvent(): Creating polyline...\n");
152 polyline = new Polyline(container, PolylineData(ce, ce, 0));
153 // vertex = snapPoint(e);
154 // polyline = new Polyline(container, PolylineData(vertex, vertex, 0));
155 polyline->setLayerToActive();
156 polyline->setPenToActive();
160 //printf("ActionDrawPolyline::mouseReleaseEvent(): Adding vertex to polyline...\n");
161 Entity * ent = polyline->addVertex(ce);
162 ent->setLayerToActive();
163 ent->setPenToActive();
166 else if (e->button() == Qt::RightButton)
175 // if (getStatus() == SetFirstPoint)
176 //ANY right button should trigger (? what about adding twice? won't happen)
177 trigger(); // this will set the polyline to NULL once added to the container
180 init(getStatus() - 1);
181 graphicView->redraw(); //hm.
185 void ActionDrawPolyline::coordinateEvent(Vector * coordinate)
187 DEBUG->print("ActionDrawPolyline::coordinateEvent");
191 DEBUG->print("ActionDrawPolyline::coordinateEvent: event was NULL");
195 // Vector mouse = *e;
200 startPoint = *coordinate;
202 // history.append(new Vector(mouse));
203 // start = data.startpoint;
204 setStatus(SetNextPoint);
205 graphicView->moveRelativeZero(*coordinate);
207 updateMouseButtonHints();
211 // data.endpoint = mouse;
212 // history.append(new Vector(mouse));
213 // startPoint = endPoint;
214 startPoint = *coordinate;
216 updateMouseButtonHints();
223 DEBUG->print("ActionDrawPolyline::coordinateEvent: OK");
226 void ActionDrawPolyline::updateMouseButtonHints()
231 DIALOGFACTORY->updateMouseWidget(tr("Set first point"), tr("Cancel"));
235 DIALOGFACTORY->updateMouseWidget(tr("Set next point"), tr("Back"));
239 DIALOGFACTORY->updateMouseWidget("", "");
244 void ActionDrawPolyline::updateMouseCursor()
246 graphicView->setMouseCursor(RS2::CadCursor);
249 void ActionDrawPolyline::updateToolBar()
252 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
254 DIALOGFACTORY->requestToolBar(RS2::ToolBarPolylines);
257 void ActionDrawPolyline::showOptions()
259 DEBUG->print("ActionDrawPolyline::showOptions");
260 ActionInterface::showOptions();
261 DIALOGFACTORY->requestOptions(this, true);
262 DEBUG->print("ActionDrawPolyline::showOptions: OK");
265 void ActionDrawPolyline::hideOptions()
267 ActionInterface::hideOptions();
268 DIALOGFACTORY->requestOptions(this, false);
271 void ActionDrawPolyline::close()
274 //NOTE: We should grey out the "close" button until the conditions for its use are satisfied.
275 // Though I can see how this would be called via cmd line... So I guess it's OK
276 if (history.count() > 2 && start.valid)
278 data.endpoint = start;
280 setStatus(SetFirstPoint);
281 graphicView->moveRelativeZero(start);
284 DIALOGFACTORY->commandMessage(tr("Cannot close sequence of lines: Not enough entities defined yet."));
286 DIALOGFACTORY->commandMessage(tr("Close functionality not defined yet..."));
290 void ActionDrawPolyline::undo()
293 //NOTE: We should grey out the "undo" button until the conditions for its use are satisfied.
294 if (history.count() > 1)
296 history.removeLast();
297 graphicView->setCurrentAction(new ActionEditUndo(true, *container, *graphicView));
298 data.startpoint = *history.last();
299 graphicView->moveRelativeZero(data.startpoint);
300 graphicView->preview.clear();
301 graphicView->redraw();
304 DIALOGFACTORY->commandMessage(tr("Cannot undo: Not enough entities defined yet."));
306 DIALOGFACTORY->commandMessage(tr("Undo functionality not defined yet..."));