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