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 <QAction>
28 #include <QMouseEvent>
29 #include "rs_actionmodifystretch.h"
30 
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_modification.h"
34 #include "rs_line.h"
35 #include "rs_coordinateevent.h"
36 #include "rs_preview.h"
37 #include "rs_debug.h"
38 
39 struct RS_ActionModifyStretch::Points {
40 	RS_Vector firstCorner;
41 	RS_Vector secondCorner;
42 	RS_Vector referencePoint;
43 	RS_Vector targetPoint;
44 };
45 
RS_ActionModifyStretch(RS_EntityContainer & container,RS_GraphicView & graphicView)46 RS_ActionModifyStretch::RS_ActionModifyStretch(RS_EntityContainer& container,
47 											   RS_GraphicView& graphicView)
48 	:RS_PreviewActionInterface("Stretch Entities",
49 							   container, graphicView)
50 	, pPoints(new Points{})
51 {
52 	actionType=RS2::ActionModifyStretch;
53 }
54 
init(int status)55 void RS_ActionModifyStretch::init(int status) {
56     RS_ActionInterface::init(status);
57 }
58 
59 RS_ActionModifyStretch::~RS_ActionModifyStretch() = default;
60 
61 
trigger()62 void RS_ActionModifyStretch::trigger() {
63 
64     RS_DEBUG->print("RS_ActionModifyStretch::trigger()");
65 
66     deletePreview();
67 
68     RS_Modification m(*container, graphicView);
69 	m.stretch(pPoints->firstCorner,
70 			  pPoints->secondCorner,
71 			  pPoints->targetPoint - pPoints->referencePoint);
72 
73     setStatus(SetFirstCorner);
74 
75     RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected(),container->totalSelectedLength());
76 }
77 
78 
79 
mouseMoveEvent(QMouseEvent * e)80 void RS_ActionModifyStretch::mouseMoveEvent(QMouseEvent* e) {
81     RS_DEBUG->print("RS_ActionModifyStretch::mouseMoveEvent begin");
82 
83     RS_Vector mouse = snapPoint(e);
84     switch (getStatus()) {
85     case SetFirstCorner:
86         break;
87 
88     case SetSecondCorner:
89 		if (pPoints->firstCorner.valid) {
90 			pPoints->secondCorner = snapPoint(e);
91 			deletePreview();
92 			preview->addRectangle(pPoints->firstCorner, pPoints->secondCorner);
93             drawPreview();
94         }
95         break;
96 
97     case SetReferencePoint:
98         break;
99 
100     case SetTargetPoint:
101 		if (pPoints->referencePoint.valid) {
102 			pPoints->targetPoint = mouse;
103 
104             deletePreview();
105 			preview->addStretchablesFrom(*container, pPoints->firstCorner, pPoints->secondCorner);
106             //preview->move(targetPoint-referencePoint);
107 			preview->stretch(pPoints->firstCorner, pPoints->secondCorner,
108 							 pPoints->targetPoint-pPoints->referencePoint);
109             drawPreview();
110         }
111         break;
112 
113     default:
114         break;
115     }
116 
117     RS_DEBUG->print("RS_ActionModifyStretch::mouseMoveEvent end");
118 }
119 
120 
121 
mouseReleaseEvent(QMouseEvent * e)122 void RS_ActionModifyStretch::mouseReleaseEvent(QMouseEvent* e) {
123     if (e->button()==Qt::LeftButton) {
124         RS_CoordinateEvent ce(snapPoint(e));
125         coordinateEvent(&ce);
126     } else if (e->button()==Qt::RightButton) {
127         deletePreview();
128         init(getStatus()-1);
129     }
130 }
131 
132 
133 
coordinateEvent(RS_CoordinateEvent * e)134 void RS_ActionModifyStretch::coordinateEvent(RS_CoordinateEvent* e) {
135     if (e==NULL) {
136         return;
137     }
138 
139     RS_Vector mouse = e->getCoordinate();
140 
141     switch (getStatus()) {
142     case SetFirstCorner:
143 		pPoints->firstCorner = mouse;
144         setStatus(SetSecondCorner);
145         break;
146 
147     case SetSecondCorner:
148 		pPoints->secondCorner = mouse;
149         deletePreview();
150         setStatus(SetReferencePoint);
151         break;
152 
153     case SetReferencePoint:
154 		pPoints->referencePoint = mouse;
155 		graphicView->moveRelativeZero(pPoints->referencePoint);
156         setStatus(SetTargetPoint);
157         break;
158 
159     case SetTargetPoint:
160 		pPoints->targetPoint = mouse;
161 		graphicView->moveRelativeZero(pPoints->targetPoint);
162         trigger();
163         //finish();
164         break;
165 
166     default:
167         break;
168     }
169 
170 }
171 
172 
updateMouseButtonHints()173 void RS_ActionModifyStretch::updateMouseButtonHints() {
174     switch (getStatus()) {
175     case SetFirstCorner:
176         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first corner"),
177                                             tr("Cancel"));
178         break;
179     case SetSecondCorner:
180         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second corner"),
181                                             tr("Back"));
182         break;
183     case SetReferencePoint:
184         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
185                                             tr("Back"));
186         break;
187     case SetTargetPoint:
188         RS_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"),
189                                             tr("Back"));
190         break;
191     default:
192         RS_DIALOGFACTORY->updateMouseWidget();
193         break;
194     }
195 }
196 
197 
198 
updateMouseCursor()199 void RS_ActionModifyStretch::updateMouseCursor() {
200     graphicView->setMouseCursor(RS2::CadCursor);
201 }
202 
203 // EOF
204