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_actiondrawlinepolygon.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_ActionDrawLinePolygonCenCor::Points {
40 	/** Center of polygon */
41 	RS_Vector center;
42 	/** Edge */
43 	RS_Vector corner;
44 };
45 
RS_ActionDrawLinePolygonCenCor(RS_EntityContainer & container,RS_GraphicView & graphicView)46 RS_ActionDrawLinePolygonCenCor::RS_ActionDrawLinePolygonCenCor(
47     RS_EntityContainer& container,
48     RS_GraphicView& graphicView)
49 		:RS_PreviewActionInterface("Draw Polygons (Center,Corner)", container, graphicView)
50 		, pPoints(new Points{})
51 		,number(3)
52 		,lastStatus(SetCenter)
53 {
54 	actionType=RS2::ActionDrawLinePolygonCenCor;
55 }
56 
57 RS_ActionDrawLinePolygonCenCor::~RS_ActionDrawLinePolygonCenCor() = default;
58 
trigger()59 void RS_ActionDrawLinePolygonCenCor::trigger() {
60     RS_PreviewActionInterface::trigger();
61 
62     deletePreview();
63 
64     RS_Creation creation(container, graphicView);
65 	bool ok = creation.createPolygon(pPoints->center, pPoints->corner, number);
66 
67     if (!ok) {
68         RS_DEBUG->print("RS_ActionDrawLinePolygon::trigger:"
69                         " No polygon added\n");
70     }
71 }
72 
73 
74 
mouseMoveEvent(QMouseEvent * e)75 void RS_ActionDrawLinePolygonCenCor::mouseMoveEvent(QMouseEvent* e) {
76     RS_DEBUG->print("RS_ActionDrawLinePolygon::mouseMoveEvent begin");
77 
78     RS_Vector mouse = snapPoint(e);
79 
80     switch (getStatus()) {
81     case SetCenter:
82         break;
83 
84     case SetCorner:
85 		if (pPoints->center.valid) {
86 			pPoints->corner = mouse;
87             deletePreview();
88 
89 			RS_Creation creation(preview.get(), nullptr, false);
90 			creation.createPolygon(pPoints->center, pPoints->corner, number);
91 
92             drawPreview();
93         }
94         break;
95 
96     default:
97         break;
98     }
99 }
100 
101 
102 
mouseReleaseEvent(QMouseEvent * e)103 void RS_ActionDrawLinePolygonCenCor::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 
coordinateEvent(RS_CoordinateEvent * e)113 void RS_ActionDrawLinePolygonCenCor::coordinateEvent(RS_CoordinateEvent* e) {
114 	if (!e)  return;
115 
116     RS_Vector mouse = e->getCoordinate();
117 
118     switch (getStatus()) {
119     case SetCenter:
120 		pPoints->center = mouse;
121         setStatus(SetCorner);
122         graphicView->moveRelativeZero(mouse);
123         break;
124 
125     case SetCorner:
126 		pPoints->corner = mouse;
127         trigger();
128         break;
129 
130     default:
131         break;
132     }
133 }
134 
updateMouseButtonHints()135 void RS_ActionDrawLinePolygonCenCor::updateMouseButtonHints() {
136 	switch (getStatus()) {
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 
showOptions()158 void RS_ActionDrawLinePolygonCenCor::showOptions() {
159     RS_ActionInterface::showOptions();
160 
161 	RS_DIALOGFACTORY->requestOptions(this, true);
162 }
163 
hideOptions()164 void RS_ActionDrawLinePolygonCenCor::hideOptions() {
165     RS_ActionInterface::hideOptions();
166 
167 	RS_DIALOGFACTORY->requestOptions(this, false);
168 }
169 
commandEvent(RS_CommandEvent * e)170 void RS_ActionDrawLinePolygonCenCor::commandEvent(RS_CommandEvent* e) {
171     QString c = e->getCommand().toLower();
172 
173 	if (checkCommand("help", c)) {
174 		RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
175 										 + getAvailableCommands().join(", "));
176         return;
177     }
178 
179     switch (getStatus()) {
180     case SetCenter:
181     case SetCorner:
182         if (checkCommand("number", c)) {
183             deletePreview();
184             lastStatus = (Status)getStatus();
185             setStatus(SetNumber);
186         }
187         break;
188 
189     case SetNumber: {
190             bool ok;
191             int n = c.toInt(&ok);
192             if (ok) {
193                 e->accept();
194                 if (n>0 && n<10000) {
195                     number = n;
196 				} else
197 					RS_DIALOGFACTORY->commandMessage(tr("Not a valid number. "
198 														"Try 1..9999"));
199 			} else
200 				RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
201 			RS_DIALOGFACTORY->requestOptions(this, true, true);
202             setStatus(lastStatus);
203         }
204         break;
205 
206     default:
207         break;
208     }
209 }
210 
211 
212 
getAvailableCommands()213 QStringList RS_ActionDrawLinePolygonCenCor::getAvailableCommands() {
214     QStringList cmd;
215 
216     switch (getStatus()) {
217     case SetCenter:
218     case SetCorner:
219         cmd += command("number");
220         break;
221     default:
222         break;
223     }
224 
225     return cmd;
226 }
227 
228 
229 
updateMouseCursor()230 void RS_ActionDrawLinePolygonCenCor::updateMouseCursor() {
231     graphicView->setMouseCursor(RS2::CadCursor);
232 }
233 
234 // EOF
235