]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinepolygon2.cpp
In the middle of major refactoring...
[architektonas] / src / actions / actiondrawlinepolygon2.cpp
1 // actiondrawlinepolygon2.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/04/2010  Added this text. :-)
13 //
14
15 #include "actiondrawlinepolygon2.h"
16
17 #include "rs_commandevent.h"
18 #include "rs_creation.h"
19 #include "rs_dialogfactory.h"
20 #include "graphicview.h"
21 #include "rs_preview.h"
22
23 ActionDrawLinePolygon2::ActionDrawLinePolygon2(RS_EntityContainer & container, GraphicView & graphicView): ActionInterface("Draw Polygons",
24                 container, graphicView)
25 {
26         corner1 = Vector(false);
27         corner2 = Vector(false);
28
29         number = 3;
30 }
31
32 ActionDrawLinePolygon2::~ActionDrawLinePolygon2()
33 {
34 }
35
36 /*virtual*/ RS2::ActionType ActionDrawLinePolygon2::rtti()
37 {
38         return RS2::ActionDrawLinePolygon2;
39 }
40
41 void ActionDrawLinePolygon2::trigger()
42 {
43         ActionInterface::trigger();
44
45         deleteSnapper();
46         deletePreview();
47         clearPreview();
48
49         RS_Creation creation(container, graphicView);
50         bool ok = creation.createPolygon2(corner1, corner2, number);
51
52         if (!ok)
53                 RS_DEBUG->print("ActionDrawLinePolygon2::trigger: No polygon added\n");
54 }
55
56 void ActionDrawLinePolygon2::mouseMoveEvent(QMouseEvent * e)
57 {
58         RS_DEBUG->print("ActionDrawLinePolygon2::mouseMoveEvent begin");
59
60         Vector mouse = snapPoint(e);
61
62         switch (getStatus())
63         {
64         case SetCorner1:
65                 break;
66
67         case SetCorner2:
68
69                 if (corner1.valid)
70                 {
71                         corner2 = mouse;
72                         deletePreview();
73                         clearPreview();
74
75 //                      RS_Creation creation(preview, NULL, false);
76 //                      creation.createPolygon2(corner1, corner2, number);
77
78                         drawPreview();
79                 }
80                 break;
81
82         default:
83                 break;
84         }
85 }
86
87 void ActionDrawLinePolygon2::mouseReleaseEvent(QMouseEvent * e)
88 {
89         if (e->button() == Qt::LeftButton)
90         {
91                 Vector ce(snapPoint(e));
92                 coordinateEvent(&ce);
93         }
94         else if (e->button() == Qt::RightButton)
95         {
96                 deletePreview();
97                 clearPreview();
98                 deleteSnapper();
99                 init(getStatus() - 1);
100         }
101 }
102
103 void ActionDrawLinePolygon2::coordinateEvent(Vector * e)
104 {
105         if (!e)
106                 return;
107
108         Vector mouse = *e;
109
110         switch (getStatus())
111         {
112         case SetCorner1:
113                 corner1 = mouse;
114                 setStatus(SetCorner2);
115                 graphicView->moveRelativeZero(mouse);
116                 break;
117
118         case SetCorner2:
119                 corner2 = mouse;
120                 trigger();
121                 break;
122
123         default:
124                 break;
125         }
126 }
127
128 void ActionDrawLinePolygon2::updateMouseButtonHints()
129 {
130         if (RS_DIALOGFACTORY != NULL)
131         {
132                 switch (getStatus())
133                 {
134                 case SetCorner1:
135                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
136                                 tr("Cancel"));
137                         break;
138
139                 case SetCorner2:
140                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
141                                 tr("Back"));
142                         break;
143
144                 case SetNumber:
145                         RS_DIALOGFACTORY->updateMouseWidget(tr("Number:"), tr("Back"));
146                         break;
147
148                 default:
149                         RS_DIALOGFACTORY->updateMouseWidget("", "");
150                         break;
151                 }
152         }
153 }
154
155 void ActionDrawLinePolygon2::showOptions()
156 {
157         ActionInterface::showOptions();
158
159         if (RS_DIALOGFACTORY != NULL)
160                 RS_DIALOGFACTORY->requestOptions(this, true);
161 }
162
163 void ActionDrawLinePolygon2::hideOptions()
164 {
165         ActionInterface::hideOptions();
166
167         if (RS_DIALOGFACTORY != NULL)
168                 RS_DIALOGFACTORY->requestOptions(this, false);
169 }
170
171 void ActionDrawLinePolygon2::commandEvent(RS_CommandEvent * e)
172 {
173         QString c = e->getCommand().toLower();
174
175         if (checkCommand("help", c))
176         {
177                 if (RS_DIALOGFACTORY != NULL)
178                         RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
179                                 + getAvailableCommands().join(", "));
180                 return;
181         }
182
183         switch (getStatus())
184         {
185         case SetCorner1:
186         case SetCorner2:
187
188                 if (checkCommand("number", c))
189                 {
190                         deleteSnapper();
191                         deletePreview();
192                         clearPreview();
193                         lastStatus = (Status)getStatus();
194                         setStatus(SetNumber);
195                 }
196                 break;
197
198         case SetNumber: {
199                 bool ok;
200                 int n = c.toInt(&ok);
201
202                 if (ok == true)
203                 {
204                         if (n > 0 && n < 10000)
205                                 number = n;
206                         else if (RS_DIALOGFACTORY != NULL)
207                                 RS_DIALOGFACTORY->commandMessage(tr("Not a valid number. "
208                                                 "Try 1..9999"));
209
210                 }
211                 else if (RS_DIALOGFACTORY != NULL)
212                         RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression."));
213
214                 if (RS_DIALOGFACTORY != NULL)
215                         RS_DIALOGFACTORY->requestOptions(this, true, true);
216                 setStatus(lastStatus);
217         }
218         break;
219
220         default:
221                 break;
222         }
223 }
224
225 QStringList ActionDrawLinePolygon2::getAvailableCommands()
226 {
227         QStringList cmd;
228
229         switch (getStatus())
230         {
231         case SetCorner1:
232         case SetCorner2:
233                 cmd += command("number");
234                 break;
235
236         default:
237                 break;
238         }
239
240         return cmd;
241 }
242
243 void ActionDrawLinePolygon2::updateMouseCursor()
244 {
245         graphicView->setMouseCursor(RS2::CadCursor);
246 }
247
248 void ActionDrawLinePolygon2::updateToolBar()
249 {
250         if (RS_DIALOGFACTORY != NULL)
251         {
252                 if (!isFinished())
253                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarSnap);
254                 else
255                         RS_DIALOGFACTORY->requestToolBar(RS2::ToolBarLines);
256         }
257 }
258
259 int ActionDrawLinePolygon2::getNumber()
260 {
261         return number;
262 }
263
264 void ActionDrawLinePolygon2::setNumber(int n)
265 {
266         number = n;
267 }