]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondimleader.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / actions / actiondimleader.cpp
1 // actiondimleader.cpp
2 //
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
9 //
10 // JLH = James L. Hammons <jlhamm@acm.org>
11 //
12 // Who  When        What
13 // ---  ----------  -----------------------------------------------------------
14 // JLH  06/03/2010  Added this text. :-)
15 // JLH  09/11/2010  Fixed preview/snapper rendering.
16 //
17
18 #include "actiondimleader.h"
19
20 #include "commandevent.h"
21 #include "debug.h"
22 #include "dialogfactory.h"
23 #include "graphicview.h"
24 #include "preview.h"
25
26 ActionDimLeader::ActionDimLeader(EntityContainer & container, GraphicView & graphicView):
27         ActionInterface("Draw leaders", container, graphicView)
28 {
29         reset();
30 }
31
32 ActionDimLeader::~ActionDimLeader()
33 {
34 }
35
36 /*virtual*/ RS2::ActionType ActionDimLeader::rtti()
37 {
38         return RS2::ActionDimLeader;
39 }
40
41 void ActionDimLeader::reset()
42 {
43         points.clear();
44 }
45
46 void ActionDimLeader::init(int status)
47 {
48         ActionInterface::init(status);
49
50         reset();
51 }
52
53 void ActionDimLeader::trigger()
54 {
55         ActionInterface::trigger();
56
57         if (points.count() > 0)
58         {
59                 Leader * leader = new Leader(container, LeaderData(true));
60                 leader->setLayerToActive();
61                 leader->setPenToActive();
62
63                 for(int i=0; i<points.size(); i++)
64                         leader->addVertex(*(points[i]));
65
66                 container->addEntity(leader);
67
68                 // upd. undo list:
69                 if (document)
70                 {
71                         document->startUndoCycle();
72                         document->addUndoable(leader);
73                         document->endUndoCycle();
74                 }
75
76 //              deletePreview();
77 //              clearPreview();
78 //              deleteSnapper();
79 //              Vector rz = graphicView->getRelativeZero();
80 //              graphicView->moveRelativeZero(Vector(0.0, 0.0));
81 //              graphicView->drawEntity(leader);
82 //              graphicView->moveRelativeZero(rz);
83                 graphicView->preview.clear();   //hm.
84                 graphicView->redraw();
85
86                 DEBUG->print("ActionDimLeader::trigger(): leader added: %d", leader->getId());
87         }
88 }
89
90 void ActionDimLeader::mouseMoveEvent(QMouseEvent * e)
91 {
92         DEBUG->print("ActionDimLeader::mouseMoveEvent begin");
93
94         Vector mouse = snapPoint(e);
95
96         if (getStatus() == SetEndpoint && points.last() != NULL)
97         {
98 //              deletePreview();
99 //              clearPreview();
100                 graphicView->preview.clear();
101
102                 // fill in lines that were already set:
103                 Vector last(false);
104
105                 for(int i=0; i<points.size(); i++)
106                 {
107                         Vector * v = points[i];
108
109 //                      if (last.valid)
110 //                              preview->addEntity(new Line(preview, LineData(last, *v)));
111                         if (last.valid)
112                                 graphicView->preview.addEntity(new Line(&(graphicView->preview), LineData(last, *v)));
113
114                         last = *v;
115                 }
116
117                 Vector p = *points.last();
118 //              preview->addEntity(new Line(preview, LineData(p, mouse)));
119 //              drawPreview();
120                 graphicView->preview.addEntity(new Line(&(graphicView->preview), LineData(p, mouse)));
121         }
122
123         graphicView->redraw();
124
125         DEBUG->print("ActionDimLeader::mouseMoveEvent end");
126 }
127
128 void ActionDimLeader::mouseReleaseEvent(QMouseEvent * e)
129 {
130         if (e->button() == Qt::LeftButton)
131         {
132                 Vector ce(snapPoint(e));
133                 coordinateEvent(&ce);
134         }
135         else if (e->button() == Qt::RightButton)
136         {
137                 if (getStatus() == SetEndpoint)
138                 {
139                         trigger();
140                         reset();
141                         setStatus(SetStartpoint);
142                 }
143                 else
144                 {
145 //                      deletePreview();
146 //                      deleteSnapper();
147                         init(getStatus() - 1);
148                         graphicView->redraw();  //hm.
149                 }
150         }
151 }
152
153 void ActionDimLeader::keyPressEvent(QKeyEvent * e)
154 {
155         if (getStatus() == SetEndpoint && e->key() == Qt::Key_Enter)
156         {
157                 trigger();
158                 reset();
159                 setStatus(SetStartpoint);
160         }
161 }
162
163 void ActionDimLeader::coordinateEvent(Vector * e)
164 {
165         if (e == NULL)
166                 return;
167
168         Vector mouse = *e;
169
170         switch (getStatus())
171         {
172         case SetStartpoint:
173                 //data.startpoint = mouse;
174                 points.clear();
175                 points.append(new Vector(mouse));
176                 //start = data.startpoint;
177                 setStatus(SetEndpoint);
178                 graphicView->moveRelativeZero(mouse);
179                 break;
180
181         case SetEndpoint:
182                 //data.endpoint = mouse;
183                 points.append(new Vector(mouse));
184                 //trigger();
185                 //data.startpoint = data.endpoint;
186                 graphicView->moveRelativeZero(mouse);
187                 break;
188
189         default:
190                 break;
191         }
192 }
193
194 void ActionDimLeader::commandEvent(CommandEvent * e)
195 {
196         QString c = e->getCommand().toLower();
197
198         if (checkCommand("help", c))
199         {
200                 if (DIALOGFACTORY)
201                         DIALOGFACTORY->commandMessage(msgAvailableCommands()
202                                 + getAvailableCommands().join(", "));
203
204                 return;
205         }
206
207         // enter to finish
208         if (c == "")
209         {
210                 trigger();
211                 reset();
212                 setStatus(SetStartpoint);
213                 //finish();
214         }
215 }
216
217 QStringList ActionDimLeader::getAvailableCommands()
218 {
219         QStringList cmd;
220
221         return cmd;
222 }
223
224 void ActionDimLeader::updateMouseButtonHints()
225 {
226         if (DIALOGFACTORY != NULL)
227         {
228                 switch (getStatus())
229                 {
230                 case SetStartpoint:
231                         DIALOGFACTORY->updateMouseWidget(tr("Specify target point"), tr("Cancel"));
232                         break;
233
234                 case SetEndpoint:
235                         DIALOGFACTORY->updateMouseWidget(tr("Specify next point"), tr("Finish"));
236                         break;
237
238                 default:
239                         DIALOGFACTORY->updateMouseWidget("", "");
240                         break;
241                 }
242         }
243 }
244
245 void ActionDimLeader::showOptions()
246 {
247         ActionInterface::showOptions();
248 }
249
250 void ActionDimLeader::hideOptions()
251 {
252         ActionInterface::hideOptions();
253 }
254
255 void ActionDimLeader::updateMouseCursor()
256 {
257         graphicView->setMouseCursor(RS2::CadCursor);
258 }
259
260 void ActionDimLeader::updateToolBar()
261 {
262         if (DIALOGFACTORY)
263         {
264                 if (!isFinished())
265                         DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
266                 else
267                         DIALOGFACTORY->requestToolBar(RS2::ToolBarDim);
268         }
269 }