00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "qgsmaptoolzoom.h"
00018 #include "qgsmapcanvas.h"
00019 #include "qgsmaptopixel.h"
00020 #include "qgscursors.h"
00021
00022 #include <QMouseEvent>
00023 #include <QRubberBand>
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 )
00032 {
00033
00034 QPixmap myZoomQPixmap = QPixmap(( const char ** )( zoomOut ? zoom_out : zoom_in ) );
00035 mCursor = QCursor( myZoomQPixmap, 7, 7 );
00036 }
00037
00038
00039 void QgsMapToolZoom::canvasMoveEvent( QMouseEvent * e )
00040 {
00041 if ( !( e->buttons() & Qt::LeftButton ) )
00042 return;
00043
00044 if ( !mDragging )
00045 {
00046 mDragging = true;
00047 mRubberBand = new QRubberBand( QRubberBand::Rectangle, mCanvas );
00048 mZoomRect.setTopLeft( e->pos() );
00049 }
00050 mZoomRect.setBottomRight( e->pos() );
00051 mRubberBand->setGeometry( mZoomRect.normalized() );
00052 mRubberBand->show();
00053 }
00054
00055
00056 void QgsMapToolZoom::canvasPressEvent( QMouseEvent * e )
00057 {
00058 if ( e->button() != Qt::LeftButton )
00059 return;
00060
00061 mZoomRect.setRect( 0, 0, 0, 0 );
00062 }
00063
00064
00065 void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
00066 {
00067 if ( e->button() != Qt::LeftButton )
00068 return;
00069
00070 if ( mDragging )
00071 {
00072 mDragging = false;
00073 delete mRubberBand;
00074 mRubberBand = 0;
00075
00076
00077 mZoomRect.setRight( e->pos().x() );
00078 mZoomRect.setBottom( e->pos().y() );
00079
00080 const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();
00081
00082
00083 QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() );
00084 QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() );
00085
00086 QgsRectangle r;
00087 r.setXMinimum( ll.x() );
00088 r.setYMinimum( ll.y() );
00089 r.setXMaximum( ur.x() );
00090 r.setYMaximum( ur.y() );
00091 r.normalize();
00092
00093
00094 if ( r.width() == 0 || r.height() == 0 )
00095 {
00096 return;
00097 }
00098
00099 if ( mZoomOut )
00100 {
00101 QgsPoint cer = r.center();
00102 QgsRectangle extent = mCanvas->extent();
00103
00104 double sf;
00105 if ( mZoomRect.width() > mZoomRect.height() )
00106 {
00107 sf = extent.width() / r.width();
00108 }
00109 else
00110 {
00111 sf = extent.height() / r.height();
00112 }
00113 r.expand( sf );
00114
00115 QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
00116 QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );
00117
00118 }
00119
00120 mCanvas->setExtent( r );
00121 mCanvas->refresh();
00122 }
00123 else
00124 {
00125
00126 mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
00127 }
00128 }