1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
6 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
7 **
8 **
9 ** This file may be distributed and/or modified under the terms of the
10 ** GNU General Public License version 2 as published by the Free Software
11 ** Foundation and appearing in the file gpl-2.0.txt included in the
12 ** packaging of this file.
13 **
14 ** This program is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with this program; if not, write to the Free Software
21 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 **
23 ** This copyright notice MUST APPEAR in all copies of the script!
24 **
25 **********************************************************************/
26
27 #include "rs_actiondrawlinepolygon2.h"
28
29 #include <QAction>
30 #include <QMouseEvent>
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_commandevent.h"
34 #include "rs_creation.h"
35 #include "rs_coordinateevent.h"
36 #include "rs_preview.h"
37 #include "rs_debug.h"
38
39 struct RS_ActionDrawLinePolygonCorCor::Points {
40 /** 1st corner */
41 RS_Vector corner1;
42 /** 2nd corner */
43 RS_Vector corner2;
44 };
45
RS_ActionDrawLinePolygonCorCor(RS_EntityContainer & container,RS_GraphicView & graphicView)46 RS_ActionDrawLinePolygonCorCor::RS_ActionDrawLinePolygonCorCor(
47 RS_EntityContainer& container,
48 RS_GraphicView& graphicView)
49 :RS_PreviewActionInterface("Draw Polygons (Corner,Corner)", container, graphicView)
50 , pPoints(new Points{})
51 ,number(3)
52 ,lastStatus(SetCorner1)
53 {
54 actionType=RS2::ActionDrawLinePolygonCorCor;
55 }
56
57 RS_ActionDrawLinePolygonCorCor::~RS_ActionDrawLinePolygonCorCor() = default;
58
trigger()59 void RS_ActionDrawLinePolygonCorCor::trigger() {
60 RS_PreviewActionInterface::trigger();
61
62 deletePreview();
63
64 RS_Creation creation(container, graphicView);
65 bool ok = creation.createPolygon2(pPoints->corner1, pPoints->corner2, number);
66
67 if (!ok) {
68 RS_DEBUG->print("RS_ActionDrawLinePolygon2::trigger:"
69 " No polygon added\n");
70 }
71 }
72
73
74
mouseMoveEvent(QMouseEvent * e)75 void RS_ActionDrawLinePolygonCorCor::mouseMoveEvent(QMouseEvent* e) {
76 RS_DEBUG->print("RS_ActionDrawLinePolygon2::mouseMoveEvent begin");
77
78 RS_Vector mouse = snapPoint(e);
79
80 switch (getStatus()) {
81 case SetCorner1:
82 break;
83
84 case SetCorner2:
85 if (pPoints->corner1.valid) {
86 pPoints->corner2 = mouse;
87 deletePreview();
88
89 RS_Creation creation(preview.get(), NULL, false);
90 creation.createPolygon2(pPoints->corner1, pPoints->corner2, number);
91
92 drawPreview();
93 }
94 break;
95
96 default:
97 break;
98 }
99 }
100
101
102
mouseReleaseEvent(QMouseEvent * e)103 void RS_ActionDrawLinePolygonCorCor::mouseReleaseEvent(QMouseEvent* e) {
104 if (e->button()==Qt::LeftButton) {
105 RS_CoordinateEvent ce(snapPoint(e));
106 coordinateEvent(&ce);
107 } else if (e->button()==Qt::RightButton) {
108 deletePreview();
109 init(getStatus()-1);
110 }
111 }
112
113
114
coordinateEvent(RS_CoordinateEvent * e)115 void RS_ActionDrawLinePolygonCorCor::coordinateEvent(RS_CoordinateEvent* e) {
116 if (e==NULL) {
117 return;
118 }
119
120 RS_Vector mouse = e->getCoordinate();
121
122 switch (getStatus()) {
123 case SetCorner1:
124 pPoints->corner1 = mouse;
125 setStatus(SetCorner2);
126 graphicView->moveRelativeZero(mouse);
127 break;
128
129 case SetCorner2:
130 pPoints->corner2 = mouse;
131 trigger();
132 break;
133
134 default:
135 break;
136 }
137 }
138
139
140
updateMouseButtonHints()141 void RS_ActionDrawLinePolygonCorCor::updateMouseButtonHints() {
142 switch (getStatus()) {
143 case SetCorner1:
144 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
145 tr("Cancel"));
146 break;
147
148 case SetCorner2:
149 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
150 tr("Back"));
151 break;
152
153 case SetNumber:
154 RS_DIALOGFACTORY->updateMouseWidget(tr("Number:"), tr("Back"));
155 break;
156
157 default:
158 RS_DIALOGFACTORY->updateMouseWidget();
159 break;
160 }
161 }
162
163
164
showOptions()165 void RS_ActionDrawLinePolygonCorCor::showOptions() {
166 RS_ActionInterface::showOptions();
167
168 RS_DIALOGFACTORY->requestOptions(this, true);
169 }
170
171
172
hideOptions()173 void RS_ActionDrawLinePolygonCorCor::hideOptions() {
174 RS_ActionInterface::hideOptions();
175
176 RS_DIALOGFACTORY->requestOptions(this, false);
177 }
178
179
180
commandEvent(RS_CommandEvent * e)181 void RS_ActionDrawLinePolygonCorCor::commandEvent(RS_CommandEvent* e) {
182 QString c = e->getCommand().toLower();
183
184 if (checkCommand("help", c)) {
185 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
186 + getAvailableCommands().join(", "));
187 return;
188 }
189
190 switch (getStatus()) {
191 case SetCorner1:
192 case SetCorner2:
193 if (checkCommand("number", c)) {
194 deletePreview();
195 lastStatus = (Status)getStatus();
196 setStatus(SetNumber);
197 }
198 break;
199
200 case SetNumber: {
201 bool ok;
202 int n = c.toInt(&ok);
203 if (ok) {
204 e->accept();
205 if (n>0 && n<10000) {
206 number = n;
207 } else
208 RS_DIALOGFACTORY->commandMessage(tr("Not a valid number. "
209 "Try 1..9999"));
210 } else
211 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression."));
212 RS_DIALOGFACTORY->requestOptions(this, true, true);
213 setStatus(lastStatus);
214 }
215 break;
216
217 default:
218 break;
219 }
220 }
221
222
223
getAvailableCommands()224 QStringList RS_ActionDrawLinePolygonCorCor::getAvailableCommands() {
225 QStringList cmd;
226
227 switch (getStatus()) {
228 case SetCorner1:
229 case SetCorner2:
230 cmd += command("number");
231 break;
232 default:
233 break;
234 }
235
236 return cmd;
237 }
238
239
240
updateMouseCursor()241 void RS_ActionDrawLinePolygonCorCor::updateMouseCursor() {
242 graphicView->setMouseCursor(RS2::CadCursor);
243 }
244
245 // EOF
246