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_actionmodifytrim.h"
28 
29 #include <QAction>
30 #include <QMouseEvent>
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_modification.h"
34 #include "rs_preview.h"
35 #include "rs_debug.h"
36 
37 struct RS_ActionModifyTrim::Points {
38 	RS_Vector limitCoord;
39 	RS_Vector trimCoord;
40 };
41 
42 
43 /**
44  * @param both Trim both entities.
45  */
RS_ActionModifyTrim(RS_EntityContainer & container,RS_GraphicView & graphicView,bool both)46 RS_ActionModifyTrim::RS_ActionModifyTrim(RS_EntityContainer& container,
47         RS_GraphicView& graphicView, bool both)
48         :RS_PreviewActionInterface("Trim Entity",
49 						   container, graphicView)
50 		, trimEntity{nullptr}
51 		, limitEntity{nullptr}
52 		, pPoints(new Points{})
53 		, both{both}
54 {
55 }
56 
~RS_ActionModifyTrim()57 RS_ActionModifyTrim::~RS_ActionModifyTrim(){
58     unhighlightLimitingEntity();
59 }
60 
init(int status)61 void RS_ActionModifyTrim::init(int status) {
62 
63     snapMode.clear();
64     snapMode.restriction = RS2::RestrictNothing;
65     RS_PreviewActionInterface::init(status);
66 
67 }
68 
finish(bool updateTB)69 void RS_ActionModifyTrim::finish(bool updateTB) {
70     RS_PreviewActionInterface::finish(updateTB);
71     unhighlightLimitingEntity();
72 }
73 
trigger()74 void RS_ActionModifyTrim::trigger() {
75 
76     RS_DEBUG->print("RS_ActionModifyTrim::trigger()");
77 
78     if (trimEntity && trimEntity->isAtomic() &&
79             limitEntity /* && limitEntity->isAtomic()*/) {
80 
81         RS_Modification m(*container, graphicView);
82 		m.trim(pPoints->trimCoord, (RS_AtomicEntity*)trimEntity,
83 			   pPoints->limitCoord, /*(RS_AtomicEntity*)*/limitEntity,
84                both);
85 
86 		trimEntity = nullptr;
87         if (both) {
88             limitEntity->setHighlighted(false);
89             graphicView->drawEntity(limitEntity);
90             setStatus(ChooseLimitEntity);
91         } else {
92             setStatus(ChooseTrimEntity);
93         }
94 
95         RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected(),container->totalSelectedLength());
96     }
97 }
98 
99 
100 
mouseMoveEvent(QMouseEvent * e)101 void RS_ActionModifyTrim::mouseMoveEvent(QMouseEvent* e) {
102     RS_DEBUG->print("RS_ActionModifyTrim::mouseMoveEvent begin");
103 
104     RS_Vector mouse = graphicView->toGraph(e->x(), e->y());
105     RS_Entity* se = catchEntity(e);
106 
107     switch (getStatus()) {
108     case ChooseLimitEntity:
109 		pPoints->limitCoord = mouse;
110         limitEntity = se;
111         break;
112 
113     case ChooseTrimEntity:
114 		pPoints->trimCoord = mouse;
115         trimEntity = se;
116         break;
117 
118     default:
119         break;
120     }
121 
122     RS_DEBUG->print("RS_ActionModifyTrim::mouseMoveEvent end");
123 }
124 
125 
126 
mouseReleaseEvent(QMouseEvent * e)127 void RS_ActionModifyTrim::mouseReleaseEvent(QMouseEvent* e) {
128     if (e->button()==Qt::LeftButton) {
129 
130         RS_Vector mouse = graphicView->toGraph(e->x(), e->y());
131         RS_Entity* se = catchEntity(e);
132 
133         switch (getStatus()) {
134         case ChooseLimitEntity:
135 			pPoints->limitCoord = mouse;
136             limitEntity = se;
137             if (limitEntity && limitEntity->rtti() != RS2::EntityPolyline/*&& limitEntity->isAtomic()*/) {
138                 limitEntity->setHighlighted(true);
139                 graphicView->drawEntity(limitEntity);
140                 setStatus(ChooseTrimEntity);
141             }
142             break;
143 
144         case ChooseTrimEntity:
145 			pPoints->trimCoord = mouse;
146             trimEntity = se;
147             if (trimEntity && trimEntity->isAtomic()) {
148                 trigger();
149             }
150             break;
151 
152         default:
153             break;
154         }
155     } else if (e->button()==Qt::RightButton) {
156         deletePreview();
157         if (limitEntity) {
158             limitEntity->setHighlighted(false);
159             graphicView->drawEntity(limitEntity);
160         }
161         init(getStatus()-1);
162     }
163 }
164 
165 //void RS_ActionModifyTrim::finish(bool updateTB) {
166 //    if (limitEntity->isHighlighted()){
167 //        limitEntity->setHighlighted(false);
168 //        graphicView->drawEntity(limitEntity);
169 //    }
170 //    RS_PreviewActionInterface::finish(updateTB);
171 //}
172 
updateMouseButtonHints()173 void RS_ActionModifyTrim::updateMouseButtonHints() {
174     switch (getStatus()) {
175     case ChooseLimitEntity:
176         if (both) {
177             RS_DIALOGFACTORY->updateMouseWidget(tr("Select first trim entity"),
178                                                 tr("Cancel"));
179         } else {
180             RS_DIALOGFACTORY->updateMouseWidget(tr("Select limiting entity"),
181                                                 tr("Back"));
182         }
183         break;
184     case ChooseTrimEntity:
185         if (both) {
186             RS_DIALOGFACTORY->updateMouseWidget(tr("Select second trim entity"),
187                                                 tr("Cancel"));
188         } else {
189             RS_DIALOGFACTORY->updateMouseWidget(tr("Select entity to trim"),
190                                                 tr("Back"));
191         }
192         break;
193     default:
194         RS_DIALOGFACTORY->updateMouseWidget();
195         break;
196     }
197 }
198 
199 
200 
updateMouseCursor()201 void RS_ActionModifyTrim::updateMouseCursor() {
202     graphicView->setMouseCursor(RS2::SelectCursor);
203 }
204 
unhighlightLimitingEntity()205 void RS_ActionModifyTrim::unhighlightLimitingEntity() {
206     if (graphicView != nullptr && graphicView->isCleanUp()==false){
207         if (limitEntity!= nullptr){
208             if(limitEntity->isHighlighted()){
209                 limitEntity->setHighlighted(false);
210                 graphicView->drawEntity(limitEntity);
211             }
212         }
213     }
214 }
215 
216