]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinepolygon.cpp
In the middle of major refactoring...
[architektonas] / src / actions / actiondrawlinepolygon.cpp
1 // actiondrawlinepolygon.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 "actiondrawlinepolygon.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 ActionDrawLinePolygon::ActionDrawLinePolygon(RS_EntityContainer & container,
24         GraphicView & graphicView): ActionInterface("Draw Polygons",
25         container, graphicView)
26 {
27         center = Vector(false);
28         corner = Vector(false);
29
30         number = 3;
31 }
32
33 ActionDrawLinePolygon::~ActionDrawLinePolygon()
34 {
35 }
36
37 /*virtual*/ RS2::ActionType ActionDrawLinePolygon::rtti()
38 {
39         return RS2::ActionDrawLinePolygon;
40 }
41
42 void ActionDrawLinePolygon::trigger()
43 {
44         ActionInterface::trigger();
45
46 //      deleteSnapper();
47 //      deletePreview();
48 //      clearPreview();
49
50         RS_Creation creation(container, graphicView);
51         bool ok = creation.createPolygon(center, corner, number);
52
53         if (!ok)
54                 RS_DEBUG->print("ActionDrawLinePolygon::trigger: No polygon added\n");
55 }
56
57 void ActionDrawLinePolygon::mouseMoveEvent(QMouseEvent * e)
58 {
59         RS_DEBUG->print("ActionDrawLinePolygon::mouseMoveEvent begin");
60
61         Vector mouse = snapPoint(e);
62
63         switch (getStatus())
64         {
65         case SetCenter:
66                 break;
67
68         case SetCorner:
69
70                 if (center.valid)
71                 {
72                         corner = mouse;
73                         deletePreview();
74                         clearPreview();
75
76 //                      RS_Creation creation(preview, NULL, false);
77 //                      creation.createPolygon(center, corner, number);
78
79                         drawPreview();
80                 }
81                 break;
82
83         default:
84                 break;
85         }
86 }
87
88 void ActionDrawLinePolygon::mouseReleaseEvent(QMouseEvent * e)
89 {
90         if (e->button() == Qt::LeftButton)
91         {
92                 Vector ce(snapPoint(e));
93                 coordinateEvent(&ce);
94         }
95         else if (e->button() == Qt::RightButton)
96         {
97                 deletePreview();
98                 clearPreview();
99                 deleteSnapper();
100                 init(getStatus() - 1);
101         }
102 }
103
104 void ActionDrawLinePolygon::coordinateEvent(Vector * e)
105 {
106         if (e == NULL)
107                 return;
108
109         Vector mouse = *e;
110
111         switch (getStatus())
112         {
113         case SetCenter:
114                 center = mouse;
115                 setStatus(SetCorner);
116                 graphicView->moveRelativeZero(mouse);
117                 break;
118
119         case SetCorner:
120                 corner = mouse;
121                 trigger();
122                 break;
123
124         default:
125                 break;
126         }
127 }
128
129 void ActionDrawLinePolygon::updateMouseButtonHints()
130 {
131         if (RS_DIALOGFACTORY != NULL)
132         {
133                 switch (getStatus())
134                 {
135                 case SetCenter:
136                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify center"),
137                                 "");
138                         break;
139
140                 case SetCorner:
141                         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify a corner"), "");
142                         break;
143
144                 case SetNumber:
145                         RS_DIALOGFACTORY->updateMouseWidget(tr("Enter number:"), "");
146                         break;
147
148                 default:
149                         RS_DIALOGFACTORY->updateMouseWidget("", "");
150                         break;
151                 }
152         }
153 }
154
155 void ActionDrawLinePolygon::showOptions()
156 {
157         ActionInterface::showOptions();
158
159         if (RS_DIALOGFACTORY != NULL)
160                 RS_DIALOGFACTORY->requestOptions(this, true);
161 }
162
163 void ActionDrawLinePolygon::hideOptions()
164 {
165         ActionInterface::hideOptions();
166
167         if (RS_DIALOGFACTORY != NULL)
168                 RS_DIALOGFACTORY->requestOptions(this, false);
169 }
170
171 void ActionDrawLinePolygon::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 SetCenter:
186         case SetCorner:
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 ActionDrawLinePolygon::getAvailableCommands()
226 {
227         QStringList cmd;
228
229         switch (getStatus())
230         {
231         case SetCenter:
232         case SetCorner:
233                 cmd += command("number");
234                 break;
235
236         default:
237                 break;
238         }
239
240         return cmd;
241 }
242
243 void ActionDrawLinePolygon::updateMouseCursor()
244 {
245         graphicView->setMouseCursor(RS2::CadCursor);
246 }
247
248 void ActionDrawLinePolygon::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 ActionDrawLinePolygon::getNumber()
260 {
261         return number;
262 }
263
264 void ActionDrawLinePolygon::setNumber(int n)
265 {
266         number = n;
267 }