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