]> Shamusworld >> Repos - architektonas/blob - src/actions/actiondrawlinepolygon.cpp
Bugfixes related to removing Snapper class.
[architektonas] / src / actions / actiondrawlinepolygon.cpp
1 // 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 // 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 "actiondrawlinepolygon.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 ActionDrawLinePolygon::ActionDrawLinePolygon(EntityContainer & container,
27         GraphicView & graphicView): ActionInterface("Draw Polygons",
28         container, graphicView)
29 {
30         center = Vector(false);
31         corner = Vector(false);
32
33         number = 3;
34 }
35
36 ActionDrawLinePolygon::~ActionDrawLinePolygon()
37 {
38 }
39
40 /*virtual*/ RS2::ActionType ActionDrawLinePolygon::rtti()
41 {
42         return RS2::ActionDrawLinePolygon;
43 }
44
45 void ActionDrawLinePolygon::trigger()
46 {
47         ActionInterface::trigger();
48
49 //      deleteSnapper();
50 //      deletePreview();
51 //      clearPreview();
52
53         Creation creation(container, graphicView);
54         bool ok = creation.createPolygon(center, corner, number);
55
56         if (!ok)
57                 DEBUG->print("ActionDrawLinePolygon::trigger: No polygon added\n");
58 }
59
60 void ActionDrawLinePolygon::mouseMoveEvent(QMouseEvent * e)
61 {
62         DEBUG->print("ActionDrawLinePolygon::mouseMoveEvent begin");
63
64         Vector mouse = snapPoint(e);
65
66         switch (getStatus())
67         {
68         case SetCenter:
69                 break;
70
71         case SetCorner:
72
73                 if (center.valid)
74                 {
75                         corner = mouse;
76                         deletePreview();
77                         clearPreview();
78
79 //                      Creation creation(preview, NULL, false);
80 //                      creation.createPolygon(center, corner, number);
81
82                         drawPreview();
83                 }
84                 break;
85
86         default:
87                 break;
88         }
89 }
90
91 void ActionDrawLinePolygon::mouseReleaseEvent(QMouseEvent * e)
92 {
93         if (e->button() == Qt::LeftButton)
94         {
95                 Vector ce(snapPoint(e));
96                 coordinateEvent(&ce);
97         }
98         else if (e->button() == Qt::RightButton)
99         {
100                 deletePreview();
101                 clearPreview();
102                 deleteSnapper();
103                 init(getStatus() - 1);
104         }
105 }
106
107 void ActionDrawLinePolygon::coordinateEvent(Vector * e)
108 {
109         if (e == NULL)
110                 return;
111
112         Vector mouse = *e;
113
114         switch (getStatus())
115         {
116         case SetCenter:
117                 center = mouse;
118                 setStatus(SetCorner);
119                 graphicView->moveRelativeZero(mouse);
120                 break;
121
122         case SetCorner:
123                 corner = mouse;
124                 trigger();
125                 break;
126
127         default:
128                 break;
129         }
130 }
131
132 void ActionDrawLinePolygon::updateMouseButtonHints()
133 {
134         if (DIALOGFACTORY != NULL)
135         {
136                 switch (getStatus())
137                 {
138                 case SetCenter:
139                         DIALOGFACTORY->updateMouseWidget(tr("Specify center"),
140                                 "");
141                         break;
142
143                 case SetCorner:
144                         DIALOGFACTORY->updateMouseWidget(tr("Specify a corner"), "");
145                         break;
146
147                 case SetNumber:
148                         DIALOGFACTORY->updateMouseWidget(tr("Enter number:"), "");
149                         break;
150
151                 default:
152                         DIALOGFACTORY->updateMouseWidget("", "");
153                         break;
154                 }
155         }
156 }
157
158 void ActionDrawLinePolygon::showOptions()
159 {
160         ActionInterface::showOptions();
161
162         if (DIALOGFACTORY != NULL)
163                 DIALOGFACTORY->requestOptions(this, true);
164 }
165
166 void ActionDrawLinePolygon::hideOptions()
167 {
168         ActionInterface::hideOptions();
169
170         if (DIALOGFACTORY != NULL)
171                 DIALOGFACTORY->requestOptions(this, false);
172 }
173
174 void ActionDrawLinePolygon::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 SetCenter:
189         case SetCorner:
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 ActionDrawLinePolygon::getAvailableCommands()
229 {
230         QStringList cmd;
231
232         switch (getStatus())
233         {
234         case SetCenter:
235         case SetCorner:
236                 cmd += command("number");
237                 break;
238
239         default:
240                 break;
241         }
242
243         return cmd;
244 }
245
246 void ActionDrawLinePolygon::updateMouseCursor()
247 {
248         graphicView->setMouseCursor(RS2::CadCursor);
249 }
250
251 void ActionDrawLinePolygon::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 ActionDrawLinePolygon::getNumber()
263 {
264         return number;
265 }
266
267 void ActionDrawLinePolygon::setNumber(int n)
268 {
269         number = n;
270 }