]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawpolyline.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / actions / actiondrawpolyline.cpp
1 // actiondrawpolyline.cpp
2 //
3 // Part of the Architektonas Project
4 // by James L. Hammons
5 // Copyright (C) 2010 Underground Software
6 // See the README and GPLv2 files for licensing and warranty information
7 //
8 // JLH = James L. Hammons <jlhamm@acm.org>
9 //
10 // Who  When        What
11 // ---  ----------  -----------------------------------------------------------
12 // JLH  09/13/2010  Created this file. :-)
13 //
14
15 #include "actiondrawpolyline.h"
16
17 #include "debug.h"
18 #include "dialogfactory.h"
19 #include "graphicview.h"
20 #include "polyline.h"
21
22 ActionDrawPolyline::ActionDrawPolyline(EntityContainer & container,
23         GraphicView & graphicView):
24         ActionInterface("Draw polyline", container, graphicView), vertex(false), polyline(NULL)
25 {
26 //      vertex = Vector(false);
27 }
28
29 ActionDrawPolyline::~ActionDrawPolyline()
30 {
31         if (polyline)
32                 delete polyline;
33 }
34
35 /*virtual*/ RS2::ActionType ActionDrawPolyline::rtti()
36 {
37         return RS2::ActionDrawPolyline;
38 }
39
40 void ActionDrawPolyline::trigger()
41 {
42         if (polyline)
43         {
44                 container->addEntity(polyline);
45
46                 if (document)
47                 {
48                         document->startUndoCycle();
49                         document->addUndoable(polyline);
50                         document->endUndoCycle();
51                 }
52
53                 DEBUG->print("ActionDrawPolyline::trigger(): polyline added: %d", polyline->getId());
54                 polyline = NULL;
55                 graphicView->redraw();  //hm.
56         }
57 }
58
59 void ActionDrawPolyline::mouseMoveEvent(QMouseEvent * e)
60 {
61 #if 0
62         if (vertex.valid && polyline)
63         {
64                 Vector v = snapPoint(e);
65                 Entity * ent = polyline->addVertex(v);
66                 ent->setLayerToActive();
67                 ent->setPenToActive();
68
69 //              deleteSnapper();
70 //              graphicView->drawEntity(ent);
71 //              drawSnapper();
72
73                 vertex = v;
74                 DEBUG->print("ActionDrawPolyline::mouseMoveEvent(): line added: %d", ent->getId());
75         }
76 #else
77         Vector mouse = graphicView->SnapPoint(e);
78
79         if (getStatus() == SetNextPoint && startPoint.valid)
80         {
81                 // We have to draw a line or arc depending on the state of the "Arc"
82                 // checkbox.
83                 // We'll start with lines...
84                 graphicView->preview.clear();
85                 Line * line = new Line(&(graphicView->preview), LineData(startPoint, mouse));
86
87                 if (polyline)
88                         graphicView->preview.addEntity(polyline);
89
90                 graphicView->preview.addEntity(line);
91         }
92
93         graphicView->redraw();  //hm.
94 #endif
95
96 #if 0
97         Vector mouse = graphicView->snapper.snapPoint(e);
98         DEBUG->print("ActionDrawLine::mouseMoveEvent: snap point: OK");
99
100         if (getStatus() == SetEndpoint && data.startpoint.valid)
101         {
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?
105 #if 0
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);
109 #else
110                 // We can assume there's only one line in there, can't we?
111                 Line * line = (Line *)graphicView->preview.firstEntity(RS2::ResolveNone);
112
113                 if (line)
114                 {
115                         line->setEndpoint(mouse);
116                 }
117                 else
118                 {
119                         line = new Line(&(graphicView->preview), LineData(data.startpoint, mouse));
120                         graphicView->preview.addEntity(line);
121                 }
122 #endif
123         }
124
125         //hm. [ok, this works. :-D]
126         graphicView->redraw();
127 #endif
128 }
129
130 void ActionDrawPolyline::mousePressEvent(QMouseEvent * e)
131 {
132         if (e->button() == Qt::LeftButton)
133         {
134 //              vertex = snapPoint(e);
135 //              polyline = new Polyline(container, PolylineData(vertex, vertex, 0));
136 //              polyline->setLayerToActive();
137 //              polyline->setPenToActive();
138         }
139 }
140
141 void ActionDrawPolyline::mouseReleaseEvent(QMouseEvent * e)
142 {
143         if (e->button() == Qt::LeftButton)
144         {
145                 Vector ce(graphicView->SnapPoint(e));
146                 coordinateEvent(&ce);
147
148 //let's try this...
149                 if (!polyline)
150                 {
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();
157                 }
158                 else
159                 {
160 //printf("ActionDrawPolyline::mouseReleaseEvent(): Adding vertex to polyline...\n");
161                         Entity * ent = polyline->addVertex(ce);
162                         ent->setLayerToActive();
163                         ent->setPenToActive();
164                 }
165         }
166         else if (e->button() == Qt::RightButton)
167         {
168 #if 0
169                 if (polyline)
170                 {
171                         delete polyline;
172                         polyline = NULL;
173                 }
174 #else
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
178 #endif
179
180                 init(getStatus() - 1);
181                 graphicView->redraw();  //hm.
182         }
183 }
184
185 void ActionDrawPolyline::coordinateEvent(Vector * coordinate)
186 {
187         DEBUG->print("ActionDrawPolyline::coordinateEvent");
188
189         if (!coordinate)
190         {
191                 DEBUG->print("ActionDrawPolyline::coordinateEvent: event was NULL");
192                 return;
193         }
194
195 //      Vector mouse = *e;
196
197         switch (getStatus())
198         {
199         case SetFirstPoint:
200                 startPoint = *coordinate;
201 //              ClearHistory();
202 //              history.append(new Vector(mouse));
203 //              start = data.startpoint;
204                 setStatus(SetNextPoint);
205                 graphicView->moveRelativeZero(*coordinate);
206 //              trigger();
207                 updateMouseButtonHints();
208                 break;
209
210         case SetNextPoint:
211 //              data.endpoint = mouse;
212 //              history.append(new Vector(mouse));
213 //              startPoint = endPoint;
214                 startPoint = *coordinate;
215 //              trigger();
216                 updateMouseButtonHints();
217                 break;
218
219         default:
220                 break;
221         }
222
223         DEBUG->print("ActionDrawPolyline::coordinateEvent: OK");
224 }
225
226 void ActionDrawPolyline::updateMouseButtonHints()
227 {
228         switch (getStatus())
229         {
230         case SetFirstPoint:
231                 DIALOGFACTORY->updateMouseWidget(tr("Set first point"), tr("Cancel"));
232                 break;
233
234         case SetNextPoint:
235                 DIALOGFACTORY->updateMouseWidget(tr("Set next point"), tr("Back"));
236                 break;
237
238         default:
239                 DIALOGFACTORY->updateMouseWidget("", "");
240                 break;
241         }
242 }
243
244 void ActionDrawPolyline::updateMouseCursor()
245 {
246         graphicView->setMouseCursor(RS2::CadCursor);
247 }
248
249 void ActionDrawPolyline::updateToolBar()
250 {
251         if (!isFinished())
252                 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
253         else
254                 DIALOGFACTORY->requestToolBar(RS2::ToolBarPolylines);
255 }
256
257 void ActionDrawPolyline::showOptions()
258 {
259         DEBUG->print("ActionDrawPolyline::showOptions");
260         ActionInterface::showOptions();
261         DIALOGFACTORY->requestOptions(this, true);
262         DEBUG->print("ActionDrawPolyline::showOptions: OK");
263 }
264
265 void ActionDrawPolyline::hideOptions()
266 {
267         ActionInterface::hideOptions();
268         DIALOGFACTORY->requestOptions(this, false);
269 }
270
271 void ActionDrawPolyline::close()
272 {
273 #if 0
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)
277         {
278                 data.endpoint = start;
279                 trigger();
280                 setStatus(SetFirstPoint);
281                 graphicView->moveRelativeZero(start);
282         }
283         else
284                 DIALOGFACTORY->commandMessage(tr("Cannot close sequence of lines: Not enough entities defined yet."));
285 #else
286         DIALOGFACTORY->commandMessage(tr("Close functionality not defined yet..."));
287 #endif
288 }
289
290 void ActionDrawPolyline::undo()
291 {
292 #if 0
293 //NOTE: We should grey out the "undo" button until the conditions for its use are satisfied.
294         if (history.count() > 1)
295         {
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();
302         }
303         else
304                 DIALOGFACTORY->commandMessage(tr("Cannot undo: Not enough entities defined yet."));
305 #else
306         DIALOGFACTORY->commandMessage(tr("Undo functionality not defined yet..."));
307 #endif
308 }