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