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<cmath>
28 #include <QAction>
29 #include <QMouseEvent>
30 #include "rs_actionzoomwindow.h"
31 
32 #include "rs_dialogfactory.h"
33 #include "rs_graphicview.h"
34 #include "rs_line.h"
35 #include "rs_preview.h"
36 #include "rs_debug.h"
37 
38 struct RS_ActionZoomWindow::Points {
39 	RS_Vector v1;
40 	RS_Vector v2;
41 };
42 
43 
44 /**
45  * Default constructor.
46  *
47  * @param keepAspectRatio Keep the aspect ratio. true: the factors
48  *          in x and y will stay the same. false Exactly the chosen
49  *          area will be fit to the viewport.
50  */
RS_ActionZoomWindow(RS_EntityContainer & container,RS_GraphicView & graphicView,bool keepAspectRatio)51 RS_ActionZoomWindow::RS_ActionZoomWindow(RS_EntityContainer& container,
52         RS_GraphicView& graphicView, bool keepAspectRatio)
53         : RS_PreviewActionInterface("Zoom Window",
54 							container, graphicView)
55 		, pPoints(new Points{})
56 		, keepAspectRatio(keepAspectRatio)
57 {
58 }
59 
60 RS_ActionZoomWindow::~RS_ActionZoomWindow() = default;
61 
62 
init(int status)63 void RS_ActionZoomWindow::init(int status) {
64     RS_DEBUG->print("RS_ActionZoomWindow::init()");
65 
66     RS_PreviewActionInterface::init(status);
67 	pPoints.reset(new Points{});
68 	//deleteSnapper();
69    // snapMode.clear();
70    // snapMode.restriction = RS2::RestrictNothing;
71 }
72 
73 
74 
trigger()75 void RS_ActionZoomWindow::trigger() {
76     RS_DEBUG->print("RS_ActionZoomWindow::trigger()");
77 
78     RS_PreviewActionInterface::trigger();
79 
80 	if (pPoints->v1.valid && pPoints->v2.valid) {
81         deletePreview();
82         //deleteSnapper();
83 		if (graphicView->toGuiDX(pPoints->v1.distanceTo(pPoints->v2))>5) {
84 			graphicView->zoomWindow(pPoints->v1, pPoints->v2, keepAspectRatio);
85             init();
86         }
87     }
88 }
89 
90 
91 
mouseMoveEvent(QMouseEvent * e)92 void RS_ActionZoomWindow::mouseMoveEvent(QMouseEvent* e) {
93     snapFree(e);
94     drawSnapper();
95 	if (getStatus()==SetSecondCorner && pPoints->v1.valid) {
96 		pPoints->v2 = snapFree(e);
97         deletePreview();
98 		preview->addRectangle(pPoints->v1, pPoints->v2);
99         drawPreview();
100     }
101 }
102 
103 
104 
mousePressEvent(QMouseEvent * e)105 void RS_ActionZoomWindow::mousePressEvent(QMouseEvent* e) {
106     if (e->button()==Qt::LeftButton) {
107         switch (getStatus()) {
108         case SetFirstCorner:
109 			pPoints->v1 = snapFree(e);
110             drawSnapper();
111             setStatus(SetSecondCorner);
112             break;
113 
114         default:
115             break;
116         }
117     }
118 
119     RS_DEBUG->print("RS_ActionZoomWindow::mousePressEvent(): %f %f",
120 					pPoints->v1.x, pPoints->v1.y);
121 }
122 
123 
124 
mouseReleaseEvent(QMouseEvent * e)125 void RS_ActionZoomWindow::mouseReleaseEvent(QMouseEvent* e) {
126     RS_DEBUG->print("RS_ActionZoomWindow::mouseReleaseEvent()");
127 
128     if (e->button()==Qt::RightButton) {
129         if (getStatus()==SetSecondCorner) {
130             deletePreview();
131         }
132         init(getStatus()-1);
133     } else if (e->button()==Qt::LeftButton) {
134         if (getStatus()==SetSecondCorner) {
135 			pPoints->v2 = snapFree(e);
136 			if( fabs(pPoints->v1.x-pPoints->v2.x) < RS_TOLERANCE
137 					|| fabs(pPoints->v1.y-pPoints->v2.y) < RS_TOLERANCE ) {//invalid zoom window
138                 deletePreview();
139                 init(getStatus()-1);
140             }
141             trigger();
142         }
143     }
144 }
145 
146 
147 
updateMouseButtonHints()148 void RS_ActionZoomWindow::updateMouseButtonHints() {
149     RS_DEBUG->print("RS_ActionZoomWindow::updateMouseButtonHints()");
150 
151     switch (getStatus()) {
152     case SetFirstCorner:
153 		RS_DIALOGFACTORY->updateMouseWidget(tr("Specify first edge"), tr("Cancel"));
154         break;
155     case SetSecondCorner:
156 		RS_DIALOGFACTORY->updateMouseWidget(tr("Specify second edge"), tr("Back"));
157         break;
158     default:
159 		RS_DIALOGFACTORY->updateMouseWidget();
160         break;
161     }
162 }
163 
164 
165 
updateMouseCursor()166 void RS_ActionZoomWindow::updateMouseCursor() {
167     graphicView->setMouseCursor(RS2::MagnifierCursor);
168 }
169 
170 
171 // EOF
172