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 #include<cmath>
27 #include <QAction>
28 #include <QMouseEvent>
29 #include "rs_actiondrawlineangle.h"
30
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_commandevent.h"
34 #include "rs_settings.h"
35 #include "rs_line.h"
36 #include "rs_coordinateevent.h"
37 #include "rs_math.h"
38 #include "rs_preview.h"
39 #include "rs_debug.h"
40
41 struct RS_ActionDrawLineAngle::Points {
42 /**
43 * Line data defined so far.
44 */
45 RS_LineData data;
46 /**
47 * Position.
48 */
49 RS_Vector pos;
50 /**
51 * Line angle.
52 */
53 double angle;
54 /**
55 * Line length.
56 */
57 double length{1.};
58 /**
59 * Is the angle fixed?
60 */
61 bool fixedAngle;
62 /**
63 * Snap point (start, middle, end).
64 */
65 int snpPoint{0};
66 };
67
RS_ActionDrawLineAngle(RS_EntityContainer & container,RS_GraphicView & graphicView,double angle,bool fixedAngle,RS2::ActionType actionType)68 RS_ActionDrawLineAngle::RS_ActionDrawLineAngle(RS_EntityContainer& container,
69 RS_GraphicView& graphicView,
70 double angle,
71 bool fixedAngle, RS2::ActionType actionType)
72 :RS_PreviewActionInterface("Draw lines with given angle",
73 container, graphicView)
74 , pPoints(new Points{})
75 {
76
77 this->actionType=actionType;
78 pPoints->angle = angle;
79 pPoints->fixedAngle = fixedAngle;
80
81 RS_DIALOGFACTORY->requestOptions(this, true,false);
82 reset();
83 }
84
85
86
~RS_ActionDrawLineAngle()87 RS_ActionDrawLineAngle::~RS_ActionDrawLineAngle() {
88 RS_SETTINGS->beginGroup("/Draw");
89 if (!hasFixedAngle()) {
90 RS_SETTINGS->writeEntry("/LineAngleAngle", RS_Math::rad2deg(getAngle()));
91 }
92 RS_SETTINGS->writeEntry("/LineAngleLength", getLength());
93 RS_SETTINGS->writeEntry("/LineAngleSnapPoint", getSnapPoint());
94 RS_SETTINGS->endGroup();
95 }
96
97
reset()98 void RS_ActionDrawLineAngle::reset() {
99 pPoints->data = {{}, {}};
100 }
101
init(int status)102 void RS_ActionDrawLineAngle::init(int status) {
103 RS_PreviewActionInterface::init(status);
104
105 reset();
106 }
107
trigger()108 void RS_ActionDrawLineAngle::trigger() {
109 RS_PreviewActionInterface::trigger();
110
111 preparePreview();
112 RS_Line* line = new RS_Line{container, pPoints->data};
113 line->setLayerToActive();
114 line->setPenToActive();
115 container->addEntity(line);
116
117 // upd. undo list:
118 if (document) {
119 document->startUndoCycle();
120 document->addUndoable(line);
121 document->endUndoCycle();
122 }
123
124 graphicView->moveRelativeZero(pPoints->data.startpoint);
125 graphicView->redraw(RS2::RedrawDrawing);
126 RS_DEBUG->print("RS_ActionDrawLineAngle::trigger(): line added: %d",
127 line->getId());
128 }
129
mouseMoveEvent(QMouseEvent * e)130 void RS_ActionDrawLineAngle::mouseMoveEvent(QMouseEvent* e) {
131 RS_DEBUG->print("RS_ActionDrawLineAngle::mouseMoveEvent begin");
132
133 if (getStatus()==SetPos) {
134 pPoints->pos = snapPoint(e);
135 deletePreview();
136 preparePreview();
137 preview->addEntity(new RS_Line(preview.get(),
138 pPoints->data));
139 drawPreview();
140 }
141
142 RS_DEBUG->print("RS_ActionDrawLineAngle::mouseMoveEvent end");
143 }
144
mouseReleaseEvent(QMouseEvent * e)145 void RS_ActionDrawLineAngle::mouseReleaseEvent(QMouseEvent* e) {
146 if (e->button()==Qt::LeftButton) {
147 if (getStatus()==SetPos) {
148 RS_CoordinateEvent ce(snapPoint(e));
149 coordinateEvent(&ce);
150 }
151 } else if (e->button()==Qt::RightButton) {
152 deletePreview();
153 init(getStatus()-1);
154 }
155 }
156
157
preparePreview()158 void RS_ActionDrawLineAngle::preparePreview() {
159 RS_Vector p1, p2;
160 // End:
161 if (pPoints->snpPoint == 2) {
162 p2.setPolar(-pPoints->length, pPoints->angle);
163 } else {
164 p2.setPolar(pPoints->length, pPoints->angle);
165 }
166
167 // Middle:
168 if (pPoints->snpPoint == 1) {
169 p1 = pPoints->pos - (p2 / 2);
170 } else {
171 p1 = pPoints->pos;
172 }
173
174 p2 += p1;
175 pPoints->data = {p1, p2};
176 }
177
coordinateEvent(RS_CoordinateEvent * e)178 void RS_ActionDrawLineAngle::coordinateEvent(RS_CoordinateEvent* e) {
179 if (!e) return;
180
181 switch (getStatus()) {
182 case SetPos:
183 pPoints->pos = e->getCoordinate();
184 trigger();
185 break;
186
187 default:
188 break;
189 }
190 }
191
commandEvent(RS_CommandEvent * e)192 void RS_ActionDrawLineAngle::commandEvent(RS_CommandEvent* e) {
193 QString c = e->getCommand().toLower();
194
195 if (checkCommand("help", c)) {
196 RS_DIALOGFACTORY->commandMessage(msgAvailableCommands()
197 + getAvailableCommands().join(", "));
198 return;
199 }
200
201 switch (getStatus()) {
202 case SetPos:
203 if (!pPoints->fixedAngle && checkCommand("angle", c)) {
204 deletePreview();
205 setStatus(SetAngle);
206 } else if (checkCommand("length", c)) {
207 deletePreview();
208 setStatus(SetLength);
209 }
210 break;
211
212 case SetAngle: {
213 bool ok;
214 double a = RS_Math::eval(c, &ok);
215 if (ok) {
216 e->accept();
217 pPoints->angle = RS_Math::deg2rad(a);
218 } else {
219 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
220 }
221 RS_DIALOGFACTORY->requestOptions(this, true, true);
222 setStatus(SetPos);
223 }
224 break;
225
226 case SetLength: {
227 bool ok;
228 double l = RS_Math::eval(c, &ok);
229 if (ok) {
230 e->accept();
231 pPoints->length = l;
232 } else {
233 RS_DIALOGFACTORY->commandMessage(tr("Not a valid expression"));
234 }
235 RS_DIALOGFACTORY->requestOptions(this, true, true);
236 setStatus(SetPos);
237 }
238 break;
239
240 default:
241 break;
242 }
243 }
244
setSnapPoint(int sp)245 void RS_ActionDrawLineAngle::setSnapPoint(int sp) {
246 pPoints->snpPoint = sp;
247 }
248
getSnapPoint() const249 int RS_ActionDrawLineAngle::getSnapPoint() const{
250 return pPoints->snpPoint;
251 }
252
setAngle(double a)253 void RS_ActionDrawLineAngle::setAngle(double a) {
254 pPoints->angle = a;
255 }
256
getAngle() const257 double RS_ActionDrawLineAngle::getAngle() const{
258 return pPoints->angle;
259 }
260
setLength(double l)261 void RS_ActionDrawLineAngle::setLength(double l) {
262 pPoints->length = l;
263 }
264
getLength() const265 double RS_ActionDrawLineAngle::getLength() const{
266 return pPoints->length;
267 }
268
hasFixedAngle() const269 bool RS_ActionDrawLineAngle::hasFixedAngle() const{
270 switch(rtti()){
271 case RS2::ActionDrawLineHorizontal:
272 case RS2::ActionDrawLineVertical:
273 return true;
274 default:
275 return false;
276 }
277 }
278
getAvailableCommands()279 QStringList RS_ActionDrawLineAngle::getAvailableCommands() {
280 QStringList cmd;
281
282 switch (getStatus()) {
283 case SetPos:
284 if (!pPoints->fixedAngle) {
285 cmd += command("angle");
286 }
287 cmd += command("length");
288 break;
289 default:
290 break;
291 }
292
293 return cmd;
294 }
295
296
updateMouseButtonHints()297 void RS_ActionDrawLineAngle::updateMouseButtonHints() {
298 switch (getStatus()) {
299 case SetPos:
300 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify position"),
301 tr("Cancel"));
302 break;
303
304 case SetAngle:
305 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter angle:"), tr("Back"));
306 break;
307
308 case SetLength:
309 RS_DIALOGFACTORY->updateMouseWidget(tr("Enter length:"), tr("Back"));
310 break;
311
312 default:
313 break;
314 }
315 }
316
showOptions()317 void RS_ActionDrawLineAngle::showOptions() {
318 RS_ActionInterface::showOptions();
319
320 RS_DIALOGFACTORY->requestOptions(this, true,true);
321 }
322
hideOptions()323 void RS_ActionDrawLineAngle::hideOptions() {
324 RS_ActionInterface::hideOptions();
325
326 RS_DIALOGFACTORY->requestOptions(this, false);
327 }
328
updateMouseCursor()329 void RS_ActionDrawLineAngle::updateMouseCursor() {
330 graphicView->setMouseCursor(RS2::CadCursor);
331 }
332
333 // EOF
334