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