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