Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgsmaptoolzoom.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsmaptoolzoom.cpp - map tool for zooming
3  ----------------------
4  begin : January 2006
5  copyright : (C) 2006 by Martin Dobias
6  email : wonder.sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 /* $Id$ */
16 
17 #include "qgsmaptoolzoom.h"
18 #include "qgsmapcanvas.h"
19 #include "qgsmaptopixel.h"
20 #include "qgscursors.h"
21 #include "qgsrubberband.h"
22 
23 #include <QMouseEvent>
24 #include <QRect>
25 #include <QCursor>
26 #include <QPixmap>
27 #include "qgslogger.h"
28 
29 
31  : QgsMapTool( canvas ), mZoomOut( zoomOut ), mDragging( false ), mRubberBand( 0 )
32 {
33  // set the cursor
34  QPixmap myZoomQPixmap = QPixmap(( const char ** )( zoomOut ? zoom_out : zoom_in ) );
35  mCursor = QCursor( myZoomQPixmap, 7, 7 );
36 }
37 
39 {
40  delete mRubberBand;
41 }
42 
43 
44 void QgsMapToolZoom::canvasMoveEvent( QMouseEvent * e )
45 {
46  if ( !( e->buttons() & Qt::LeftButton ) )
47  return;
48 
49  if ( !mDragging )
50  {
51  mDragging = true;
52  delete mRubberBand;
53  mRubberBand = new QgsRubberBand( mCanvas, true );
54  mZoomRect.setTopLeft( e->pos() );
55  }
56  mZoomRect.setBottomRight( e->pos() );
57  if ( mRubberBand )
58  {
60  mRubberBand->show();
61  }
62 }
63 
64 
65 void QgsMapToolZoom::canvasPressEvent( QMouseEvent * e )
66 {
67  if ( e->button() != Qt::LeftButton )
68  return;
69 
70  mZoomRect.setRect( 0, 0, 0, 0 );
71 }
72 
73 
74 void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
75 {
76  if ( e->button() != Qt::LeftButton )
77  return;
78 
79  if ( mDragging )
80  {
81  mDragging = false;
82  delete mRubberBand;
83  mRubberBand = 0;
84 
85  // store the rectangle
86  mZoomRect.setRight( e->pos().x() );
87  mZoomRect.setBottom( e->pos().y() );
88 
89  const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();
90 
91  // set the extent to the zoomBox
92  QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() );
93  QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() );
94 
95  QgsRectangle r;
96  r.setXMinimum( ll.x() );
97  r.setYMinimum( ll.y() );
98  r.setXMaximum( ur.x() );
99  r.setYMaximum( ur.y() );
100  r.normalize();
101 
102  // prevent zooming to an empty extent
103  if ( r.width() == 0 || r.height() == 0 )
104  {
105  return;
106  }
107 
108  if ( mZoomOut )
109  {
110  QgsPoint cer = r.center();
111  QgsRectangle extent = mCanvas->extent();
112 
113  double sf;
114  if ( mZoomRect.width() > mZoomRect.height() )
115  {
116  sf = extent.width() / r.width();
117  }
118  else
119  {
120  sf = extent.height() / r.height();
121  }
122  r.expand( sf );
123 
124  QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
125  QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );
126 
127  }
128 
129  mCanvas->setExtent( r );
130  mCanvas->refresh();
131  }
132  else // not dragging
133  {
134  // change to zoom in/out by the default multiple
135  mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
136  }
137 }
138 
140 {
141  delete mRubberBand;
142  mRubberBand = 0;
143 }