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_actionmodifymove.h"
28
29 #include "rs_dialogfactory.h"
30 #include "rs_graphicview.h"
31 #include "rs_line.h"
32 #include "rs_coordinateevent.h"
33 #include "rs_modification.h"
34 #include "rs_preview.h"
35 #include "rs_debug.h"
36
37 #include <QAction>
38 #include <QMouseEvent>
39
40 #include <cmath>
41
42 struct RS_ActionModifyMove::Points {
43 RS_MoveData data;
44 RS_Vector referencePoint;
45 RS_Vector targetPoint;
46 };
47
RS_ActionModifyMove(RS_EntityContainer & container,RS_GraphicView & graphicView)48 RS_ActionModifyMove::RS_ActionModifyMove(RS_EntityContainer& container,
49 RS_GraphicView& graphicView)
50 :RS_PreviewActionInterface("Move Entities",
51 container, graphicView)
52 , pPoints(new Points{})
53 {
54 actionType=RS2::ActionModifyMove;
55 }
56
57 RS_ActionModifyMove::~RS_ActionModifyMove() = default;
58
trigger()59 void RS_ActionModifyMove::trigger() {
60
61 RS_DEBUG->print("RS_ActionModifyMove::trigger()");
62
63 RS_Modification m(*container, graphicView);
64 m.move(pPoints->data);
65
66 RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected(),container->totalSelectedLength());
67 finish(false);
68 }
69
70
mouseMoveEvent(QMouseEvent * e)71 void RS_ActionModifyMove::mouseMoveEvent(QMouseEvent* e) {
72 RS_DEBUG->print("RS_ActionModifyMove::mouseMoveEvent begin");
73
74 if (getStatus()==SetReferencePoint || getStatus()==SetTargetPoint) {
75 RS_Vector mouse = snapPoint(e);
76 switch (getStatus()) {
77 case SetReferencePoint:
78 pPoints->referencePoint = mouse;
79 break;
80
81 case SetTargetPoint:
82 if (pPoints->referencePoint.valid) {
83 if (e->modifiers() & Qt::ShiftModifier) {
84 mouse = snapToAngle(mouse, pPoints->referencePoint, 15.);
85 }
86
87 pPoints->targetPoint = mouse;
88
89 deletePreview();
90 preview->addSelectionFrom(*container);
91 preview->move(pPoints->targetPoint-pPoints->referencePoint);
92
93 if (e->modifiers() & Qt::ShiftModifier) {
94 RS_Line *line = new RS_Line(pPoints->referencePoint, mouse);
95 preview->addEntity(line);
96 line->setSelected(true);
97 line->setLayerToActive();
98 line->setPenToActive();
99 }
100 drawPreview();
101 }
102 break;
103
104 default:
105 break;
106 }
107 }
108
109 RS_DEBUG->print("RS_ActionModifyMove::mouseMoveEvent end");
110 }
111
112
mouseReleaseEvent(QMouseEvent * e)113 void RS_ActionModifyMove::mouseReleaseEvent(QMouseEvent* e) {
114 if (e->button()==Qt::LeftButton) {
115 RS_Vector snapped = snapPoint(e);
116 if((e->modifiers() & Qt::ShiftModifier) && getStatus() == SetTargetPoint )
117 snapped = snapToAngle(snapped, pPoints->referencePoint, 15.);
118
119 RS_CoordinateEvent ce(snapped);
120 coordinateEvent(&ce);
121 } else if (e->button()==Qt::RightButton) {
122 deletePreview();
123 init(getStatus()-1);
124 }
125 }
126
127
coordinateEvent(RS_CoordinateEvent * e)128 void RS_ActionModifyMove::coordinateEvent(RS_CoordinateEvent* e) {
129
130 if (e==NULL) {
131 return;
132 }
133
134 RS_Vector pos = e->getCoordinate();
135
136 switch (getStatus()) {
137 case SetReferencePoint:
138 pPoints->referencePoint = pos;
139 graphicView->moveRelativeZero(pPoints->referencePoint);
140 setStatus(SetTargetPoint);
141 break;
142
143 case SetTargetPoint:
144 pPoints->targetPoint = pos;
145 graphicView->moveRelativeZero(pPoints->targetPoint);
146 setStatus(ShowDialog);
147 if (RS_DIALOGFACTORY->requestMoveDialog(pPoints->data)) {
148 if(pPoints->data.number<0){
149 pPoints->data.number=abs(pPoints->data.number);
150 RS_DIALOGFACTORY->commandMessage(tr("Invalid number of copies, use %1 ").arg(pPoints->data.number));
151 }
152 pPoints->data.offset = pPoints->targetPoint - pPoints->referencePoint;
153 trigger();
154 }
155 break;
156
157 default:
158 break;
159 }
160 }
161
162
updateMouseButtonHints()163 void RS_ActionModifyMove::updateMouseButtonHints() {
164 switch (getStatus()) {
165 /*case Select:
166 RS_DIALOGFACTORY->updateMouseWidget(tr("Pick entities to move"),
167 tr("Cancel"));
168 break;*/
169 case SetReferencePoint:
170 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
171 tr("Cancel"));
172 break;
173 case SetTargetPoint:
174 RS_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"),
175 tr("Back"));
176 break;
177 default:
178 RS_DIALOGFACTORY->updateMouseWidget();
179 break;
180 }
181 }
182
183
updateMouseCursor()184 void RS_ActionModifyMove::updateMouseCursor() {
185 if(graphicView != NULL){
186 graphicView->setMouseCursor(RS2::CadCursor);
187 }
188 }
189
190 // EOF
191