1 /****************************************************************************
2 **
3 ** This file is part of the LibreCAD project, a 2D CAD program
4 **
5 ** Copyright (C) 2011 Rallaz (rallazz@gmail.com)
6 ** Copyright (C) 2010 R. van Twisk (librecad@rvt.dds.nl)
7 **
8 **
9 ** This file is free software; you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation; either version 2 of the License, or
12 ** (at your option) any later version.
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 "qc_actiongetpoint.h"
28
29 #include <QPointF>
30 #include <QMouseEvent>
31 #include "rs_snapper.h"
32 #include "rs_dialogfactory.h"
33 #include "rs_graphicview.h"
34 #include "rs_line.h"
35 #include "rs_coordinateevent.h"
36 #include "rs_preview.h"
37 #include "rs_debug.h"
38
39 struct QC_ActionGetPoint::Points {
40 RS_MoveData data;
41 RS_Vector referencePoint;
42 RS_Vector targetPoint;
43 QString message;
44 };
45
QC_ActionGetPoint(RS_EntityContainer & container,RS_GraphicView & graphicView)46 QC_ActionGetPoint::QC_ActionGetPoint(RS_EntityContainer& container,
47 RS_GraphicView& graphicView)
48 :RS_PreviewActionInterface("Get Point",
49 container, graphicView)
50 , canceled(false)
51 , completed{false}
52 , setTargetPoint{false}
53 , pPoints(new Points{})
54 {
55 pPoints->targetPoint = RS_Vector(0,0);
56 }
57
58 QC_ActionGetPoint::~QC_ActionGetPoint() = default;
59
60
trigger()61 void QC_ActionGetPoint::trigger() {
62
63 RS_DEBUG->print("QC_ActionGetPoint::trigger()");
64 completed = true;
65 updateMouseButtonHints();
66 }
67
68
mouseMoveEvent(QMouseEvent * e)69 void QC_ActionGetPoint::mouseMoveEvent(QMouseEvent* e) {
70 RS_DEBUG->print("QC_ActionGetPoint::mouseMoveEvent begin");
71
72 RS_Vector mouse = snapPoint(e);
73 if(setTargetPoint){
74 if (pPoints->referencePoint.valid) {
75 pPoints->targetPoint = mouse;
76 deletePreview();
77 RS_Line *line =new RS_Line{preview.get(),
78 pPoints->referencePoint, mouse};
79 line->setPen(RS_Pen(RS_Color(0,0,0), RS2::Width00, RS2::DotLine ));
80 preview->addEntity(line);
81 RS_DEBUG->print("QC_ActionGetPoint::mouseMoveEvent: draw preview");
82 drawPreview();
83 preview->addSelectionFrom(*container);
84 }
85 } else {
86 pPoints->targetPoint = mouse;
87 }
88
89 RS_DEBUG->print("QC_ActionGetPoint::mouseMoveEvent end");
90 }
91
92
93
mouseReleaseEvent(QMouseEvent * e)94 void QC_ActionGetPoint::mouseReleaseEvent(QMouseEvent* e) {
95 if (e->button()==Qt::LeftButton) {
96 RS_CoordinateEvent ce(snapPoint(e));
97 coordinateEvent(&ce);
98 } else if (e->button()==Qt::RightButton) {
99 canceled = true;
100 completed = true;
101 finish();
102 }
103 }
104
coordinateEvent(RS_CoordinateEvent * e)105 void QC_ActionGetPoint::coordinateEvent(RS_CoordinateEvent* e) {
106
107 if (e==nullptr) return;
108
109 RS_Vector pos = e->getCoordinate();
110
111 pPoints->targetPoint = pos;
112 graphicView->moveRelativeZero(pPoints->targetPoint);
113 trigger();
114 }
115
116
updateMouseButtonHints()117 void QC_ActionGetPoint::updateMouseButtonHints() {
118 if (!completed)
119 RS_DIALOGFACTORY->updateMouseWidget(pPoints->message, tr("Cancel"));
120 else
121 RS_DIALOGFACTORY->updateMouseWidget();
122 }
123
124
updateMouseCursor()125 void QC_ActionGetPoint::updateMouseCursor() {
126 graphicView->setMouseCursor(RS2::CadCursor);
127 }
128
129
setBasepoint(QPointF * basepoint)130 void QC_ActionGetPoint::setBasepoint(QPointF* basepoint){
131 pPoints->referencePoint.x = basepoint->x();
132 pPoints->referencePoint.y = basepoint->y();
133 setTargetPoint = true;
134 }
135
136
setMessage(QString msg)137 void QC_ActionGetPoint::setMessage(QString msg){
138 pPoints->message = msg;
139 }
140
141
getPoint(QPointF * point)142 void QC_ActionGetPoint::getPoint(QPointF *point)
143 {
144 if (pPoints)
145 {
146 point->setX(pPoints->targetPoint.x);
147 point->setY(pPoints->targetPoint.y);
148 }
149
150 }
151
152 // EOF
153