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_actionselectwindow.h"
28 
29 #include <QAction>
30 #include <QMouseEvent>
31 #include "rs_dialogfactory.h"
32 #include "rs_graphicview.h"
33 #include "rs_selection.h"
34 #include "rs_overlaybox.h"
35 #include "rs_preview.h"
36 #include "rs_debug.h"
37 
38 struct RS_ActionSelectWindow::Points {
39 	RS_Vector v1;
40 	RS_Vector v2;
41 };
42 
43 
44 /**
45  * Constructor.
46  *
47  * @param select true: select window. false: deselect window
48  */
RS_ActionSelectWindow(RS_EntityContainer & container,RS_GraphicView & graphicView,bool select)49 RS_ActionSelectWindow::RS_ActionSelectWindow(RS_EntityContainer& container,
50         RS_GraphicView& graphicView,
51         bool select)
52         : RS_PreviewActionInterface("Select Window",
53 							container, graphicView)
54 		, select(select)
55 		, pPoints(new Points{})
56 {
57 	actionType=RS2::ActionSelectWindow;
58 }
59 
60 RS_ActionSelectWindow::~RS_ActionSelectWindow() = default;
61 
62 
init(int status)63 void RS_ActionSelectWindow::init(int status) {
64     RS_PreviewActionInterface::init(status);
65 	pPoints.reset(new Points{});
66     //snapMode.clear();
67     //snapMode.restriction = RS2::RestrictNothing;
68 }
69 
70 
71 
trigger()72 void RS_ActionSelectWindow::trigger() {
73     RS_PreviewActionInterface::trigger();
74 
75 	if (pPoints->v1.valid && pPoints->v2.valid) {
76 		if (graphicView->toGuiDX(pPoints->v1.distanceTo(pPoints->v2))>10) {
77 
78 			bool cross = (pPoints->v1.x>pPoints->v2.x);
79 
80             RS_Selection s(*container, graphicView);
81 			s.selectWindow(pPoints->v1, pPoints->v2, select, cross);
82 
83             RS_DIALOGFACTORY->updateSelectionWidget(container->countSelected(),container->totalSelectedLength());
84 
85             init();
86         }
87     }
88 }
89 
90 
91 
mouseMoveEvent(QMouseEvent * e)92 void RS_ActionSelectWindow::mouseMoveEvent(QMouseEvent* e) {
93     snapFree(e);
94     drawSnapper();
95 	if (getStatus()==SetCorner2 && pPoints->v1.valid) {
96 		pPoints->v2 = snapFree(e);
97         deletePreview();
98 		RS_OverlayBox* ob=new RS_OverlayBox(preview.get(), RS_OverlayBoxData(pPoints->v1, pPoints->v2));
99         preview->addEntity(ob);
100 
101         //RLZ: not needed overlay have contour
102         /*                RS_Pen pen(RS_Color(218,105,24), RS2::Width00, RS2::SolidLine);
103 
104                 // TODO change to a rs_box sort of entity
105 				RS_Line* e=new RS_Line(preview, RS_LineData(RS_Vector(v1->x, v1->y),  RS_Vector(v2->x, v1->y)));
106                 e->setPen(pen);
107         preview->addEntity(e);
108 
109 				e=new RS_Line(preview, RS_LineData(RS_Vector(v2->x, v1->y),  RS_Vector(v2->x, v2->y)));
110                 e->setPen(pen);
111         preview->addEntity(e);
112 
113 				e=new RS_Line(preview, RS_LineData(RS_Vector(v2->x, v2->y),  RS_Vector(v1->x, v2->y)));
114                 e->setPen(pen);
115         preview->addEntity(e);
116 
117 				e=new RS_Line(preview, RS_LineData(RS_Vector(v1->x, v2->y),  RS_Vector(v1->x, v1->y)));
118                 e->setPen(pen);
119         preview->addEntity(e);*/
120 
121         drawPreview();
122     }
123 }
124 
125 
126 
mousePressEvent(QMouseEvent * e)127 void RS_ActionSelectWindow::mousePressEvent(QMouseEvent* e) {
128     if (e->button()==Qt::LeftButton) {
129         switch (getStatus()) {
130         case SetCorner1:
131 			pPoints->v1 = snapFree(e);
132             setStatus(SetCorner2);
133             break;
134 
135         default:
136             break;
137         }
138     }
139 
140     RS_DEBUG->print("RS_ActionSelectWindow::mousePressEvent(): %f %f",
141 					pPoints->v1.x, pPoints->v1.y);
142 }
143 
144 
145 
mouseReleaseEvent(QMouseEvent * e)146 void RS_ActionSelectWindow::mouseReleaseEvent(QMouseEvent* e) {
147     RS_DEBUG->print("RS_ActionSelectWindow::mouseReleaseEvent()");
148 
149     if (e->button()==Qt::LeftButton) {
150         if (getStatus()==SetCorner2) {
151 			pPoints->v2 = snapFree(e);
152             trigger();
153         }
154     } else if (e->button()==Qt::RightButton) {
155         if (getStatus()==SetCorner2) {
156             deletePreview();
157         }
158         init(getStatus()-1);
159     }
160 }
161 
162 
163 
updateMouseButtonHints()164 void RS_ActionSelectWindow::updateMouseButtonHints() {
165     switch (getStatus()) {
166     case SetCorner1:
167         RS_DIALOGFACTORY->updateMouseWidget(tr("Click and drag for the selection window"), tr("Cancel"));
168         break;
169     case SetCorner2:
170         RS_DIALOGFACTORY->updateMouseWidget(tr("Choose second edge"), tr("Back"));
171         break;
172     default:
173         RS_DIALOGFACTORY->updateMouseWidget();
174         break;
175     }
176 }
177 
178 
179 
updateMouseCursor()180 void RS_ActionSelectWindow::updateMouseCursor() {
181     graphicView->setMouseCursor(RS2::SelectCursor);
182 }
183 
184 // EOF
185