]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawpolyline.cpp
Phase two of adding polyline functionality...
[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)
25 {
26         vertex = Vector(false);
27         polyline = NULL;
28 }
29
30 ActionDrawPolyline::~ActionDrawPolyline()
31 {
32         if (polyline)
33                 delete polyline;
34 }
35
36 void ActionDrawPolyline::trigger()
37 {
38         if (polyline)
39         {
40                 container->addEntity(polyline);
41 //              deleteSnapper();
42
43                 if (document)
44                 {
45                         document->startUndoCycle();
46                         document->addUndoable(polyline);
47                         document->endUndoCycle();
48                 }
49
50                 DEBUG->print("ActionDrawPolyline::trigger(): polyline added: %d", polyline->getId());
51                 polyline = NULL;
52         }
53 }
54
55 void ActionDrawPolyline::mouseMoveEvent(QMouseEvent * e)
56 {
57         if (vertex.valid && polyline)
58         {
59                 Vector v = snapPoint(e);
60                 Entity * ent = polyline->addVertex(v);
61                 ent->setLayerToActive();
62                 ent->setPenToActive();
63
64 //              deleteSnapper();
65 //              graphicView->drawEntity(ent);
66 //              drawSnapper();
67
68                 vertex = v;
69                 DEBUG->print("ActionDrawPolyline::mouseMoveEvent(): line added: %d", ent->getId());
70         }
71 }
72
73 void ActionDrawPolyline::mousePressEvent(QMouseEvent * e)
74 {
75         if (e->button() == Qt::LeftButton)
76         {
77                 vertex = snapPoint(e);
78                 polyline = new Polyline(container, PolylineData(vertex, vertex, 0));
79                 polyline->setLayerToActive();
80                 polyline->setPenToActive();
81         }
82 }
83
84 void ActionDrawPolyline::mouseReleaseEvent(QMouseEvent * e)
85 {
86         if (e->button() == Qt::LeftButton)
87         {
88                 vertex = Vector(false);
89                 trigger();
90         }
91         else if (e->button() == Qt::RightButton)
92         {
93                 if (polyline)
94                 {
95                         delete polyline;
96                         polyline = NULL;
97                 }
98
99 //              deleteSnapper();
100                 init(getStatus() - 1);
101                 graphicView->redraw();  //hm.
102         }
103 }
104
105 void ActionDrawPolyline::updateMouseButtonHints()
106 {
107         switch (getStatus())
108         {
109         case 0:
110                 DIALOGFACTORY->updateMouseWidget(tr("Click and drag to draw a line"), tr("Cancel"));
111                 break;
112
113         default:
114                 DIALOGFACTORY->updateMouseWidget("", "");
115                 break;
116         }
117 }
118
119 void ActionDrawPolyline::updateMouseCursor()
120 {
121         graphicView->setMouseCursor(RS2::CadCursor);
122 }
123
124 void ActionDrawPolyline::updateToolBar()
125 {
126         if (!isFinished())
127                 DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
128         else
129                 DIALOGFACTORY->requestToolBar(RS2::ToolBarPolylines);
130 }
131
132 void ActionDrawPolyline::close()
133 {
134 #if 0
135 //NOTE: We should grey out the "close" button until the conditions for its use are satisfied.
136 //      Though I can see how this would be called via cmd line... So I guess it's OK
137         if (history.count() > 2 && start.valid)
138         {
139                 data.endpoint = start;
140                 trigger();
141                 setStatus(SetFirstPoint);
142                 graphicView->moveRelativeZero(start);
143         }
144         else
145                 DIALOGFACTORY->commandMessage(tr("Cannot close sequence of lines: Not enough entities defined yet."));
146 #else
147         DIALOGFACTORY->commandMessage(tr("Close functionality not defined yet..."));
148 #endif
149 }
150
151 void ActionDrawPolyline::undo()
152 {
153 #if 0
154 //NOTE: We should grey out the "undo" button until the conditions for its use are satisfied.
155         if (history.count() > 1)
156         {
157                 history.removeLast();
158                 graphicView->setCurrentAction(new ActionEditUndo(true, *container, *graphicView));
159                 data.startpoint = *history.last();
160                 graphicView->moveRelativeZero(data.startpoint);
161                 graphicView->preview.clear();
162                 graphicView->redraw();
163         }
164         else
165                 DIALOGFACTORY->commandMessage(tr("Cannot undo: Not enough entities defined yet."));
166 #else
167         DIALOGFACTORY->commandMessage(tr("Undo functionality not defined yet..."));
168 #endif
169 }