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