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