Quantum GIS API Documentation
1.7.4
|
00001 /*************************************************************************** 00002 qgsmaptoolzoom.cpp - map tool for zooming 00003 ---------------------- 00004 begin : January 2006 00005 copyright : (C) 2006 by Martin Dobias 00006 email : wonder.sk at gmail dot com 00007 *************************************************************************** 00008 * * 00009 * This program is free software; you can redistribute it and/or modify * 00010 * it under the terms of the GNU General Public License as published by * 00011 * the Free Software Foundation; either version 2 of the License, or * 00012 * (at your option) any later version. * 00013 * * 00014 ***************************************************************************/ 00015 /* $Id$ */ 00016 00017 #include "qgsmaptoolzoom.h" 00018 #include "qgsmapcanvas.h" 00019 #include "qgsmaptopixel.h" 00020 #include "qgscursors.h" 00021 #include "qgsrubberband.h" 00022 00023 #include <QMouseEvent> 00024 #include <QRect> 00025 #include <QCursor> 00026 #include <QPixmap> 00027 #include "qgslogger.h" 00028 00029 00030 QgsMapToolZoom::QgsMapToolZoom( QgsMapCanvas* canvas, bool zoomOut ) 00031 : QgsMapTool( canvas ), mZoomOut( zoomOut ), mDragging( false ), mRubberBand( 0 ) 00032 { 00033 // set the cursor 00034 QPixmap myZoomQPixmap = QPixmap(( const char ** )( zoomOut ? zoom_out : zoom_in ) ); 00035 mCursor = QCursor( myZoomQPixmap, 7, 7 ); 00036 } 00037 00038 QgsMapToolZoom::~QgsMapToolZoom() 00039 { 00040 delete mRubberBand; 00041 } 00042 00043 00044 void QgsMapToolZoom::canvasMoveEvent( QMouseEvent * e ) 00045 { 00046 if ( !( e->buttons() & Qt::LeftButton ) ) 00047 return; 00048 00049 if ( !mDragging ) 00050 { 00051 mDragging = true; 00052 delete mRubberBand; 00053 mRubberBand = new QgsRubberBand( mCanvas, true ); 00054 mZoomRect.setTopLeft( e->pos() ); 00055 } 00056 mZoomRect.setBottomRight( e->pos() ); 00057 if ( mRubberBand ) 00058 { 00059 mRubberBand->setToCanvasRectangle( mZoomRect ); 00060 mRubberBand->show(); 00061 } 00062 } 00063 00064 00065 void QgsMapToolZoom::canvasPressEvent( QMouseEvent * e ) 00066 { 00067 if ( e->button() != Qt::LeftButton ) 00068 return; 00069 00070 mZoomRect.setRect( 0, 0, 0, 0 ); 00071 } 00072 00073 00074 void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e ) 00075 { 00076 if ( e->button() != Qt::LeftButton ) 00077 return; 00078 00079 if ( mDragging ) 00080 { 00081 mDragging = false; 00082 delete mRubberBand; 00083 mRubberBand = 0; 00084 00085 // store the rectangle 00086 mZoomRect.setRight( e->pos().x() ); 00087 mZoomRect.setBottom( e->pos().y() ); 00088 00089 const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform(); 00090 00091 // set the extent to the zoomBox 00092 QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() ); 00093 QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() ); 00094 00095 QgsRectangle r; 00096 r.setXMinimum( ll.x() ); 00097 r.setYMinimum( ll.y() ); 00098 r.setXMaximum( ur.x() ); 00099 r.setYMaximum( ur.y() ); 00100 r.normalize(); 00101 00102 // prevent zooming to an empty extent 00103 if ( r.width() == 0 || r.height() == 0 ) 00104 { 00105 return; 00106 } 00107 00108 if ( mZoomOut ) 00109 { 00110 QgsPoint cer = r.center(); 00111 QgsRectangle extent = mCanvas->extent(); 00112 00113 double sf; 00114 if ( mZoomRect.width() > mZoomRect.height() ) 00115 { 00116 sf = extent.width() / r.width(); 00117 } 00118 else 00119 { 00120 sf = extent.height() / r.height(); 00121 } 00122 r.expand( sf ); 00123 00124 QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) ); 00125 QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) ); 00126 00127 } 00128 00129 mCanvas->setExtent( r ); 00130 mCanvas->refresh(); 00131 } 00132 else // not dragging 00133 { 00134 // change to zoom in/out by the default multiple 00135 mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut ); 00136 } 00137 } 00138 00139 void QgsMapToolZoom::deactivate() 00140 { 00141 delete mRubberBand; 00142 mRubberBand = 0; 00143 }