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