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