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