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