~ubuntu-branches/ubuntu/raring/qgis/raring

« back to all changes in this revision

Viewing changes to src/gui/qgsmaptoolzoom.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Johan Van de Wauw
  • Date: 2010-07-11 20:23:24 UTC
  • mfrom: (3.1.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100711202324-5ktghxa7hracohmr
Tags: 1.4.0+12730-3ubuntu1
* Merge from Debian unstable (LP: #540941).
* Fix compilation issues with QT 4.7
* Add build-depends on libqt4-webkit-dev 

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
#include <QRect>
25
25
#include <QCursor>
26
26
#include <QPixmap>
27
 
 
28
 
 
29
 
QgsMapToolZoom::QgsMapToolZoom(QgsMapCanvas* canvas, bool zoomOut)
30
 
  : QgsMapTool(canvas), mZoomOut(zoomOut), mDragging(false)
 
27
#include "qgslogger.h"
 
28
 
 
29
 
 
30
QgsMapToolZoom::QgsMapToolZoom( QgsMapCanvas* canvas, bool zoomOut )
 
31
    : QgsMapTool( canvas ), mZoomOut( zoomOut ), mDragging( false )
31
32
{
32
33
  // set the cursor
33
 
  QPixmap myZoomQPixmap = QPixmap((const char **) (zoomOut ? zoom_out : zoom_in));  
34
 
  mCursor = QCursor(myZoomQPixmap, 7, 7);
 
34
  QPixmap myZoomQPixmap = QPixmap(( const char ** )( zoomOut ? zoom_out : zoom_in ) );
 
35
  mCursor = QCursor( myZoomQPixmap, 7, 7 );
35
36
}
36
37
 
37
38
 
38
 
void QgsMapToolZoom::canvasMoveEvent(QMouseEvent * e)
 
39
void QgsMapToolZoom::canvasMoveEvent( QMouseEvent * e )
39
40
{
40
 
  if (e->state() != Qt::LeftButton)
 
41
  if ( !( e->buttons() & Qt::LeftButton ) )
41
42
    return;
42
43
 
43
 
  if (!mDragging)
 
44
  if ( !mDragging )
44
45
  {
45
46
    mDragging = true;
46
 
    mRubberBand = new QRubberBand(QRubberBand::Rectangle, mCanvas);
47
 
    mZoomRect.setTopLeft(e->pos());
 
47
    mRubberBand = new QRubberBand( QRubberBand::Rectangle, mCanvas );
 
48
    mZoomRect.setTopLeft( e->pos() );
48
49
  }
49
 
  mZoomRect.setBottomRight(e->pos());
50
 
  mRubberBand->setGeometry(mZoomRect.normalized());
 
50
  mZoomRect.setBottomRight( e->pos() );
 
51
  mRubberBand->setGeometry( mZoomRect.normalized() );
51
52
  mRubberBand->show();
52
53
}
53
54
 
54
55
 
55
 
void QgsMapToolZoom::canvasPressEvent(QMouseEvent * e)
 
56
void QgsMapToolZoom::canvasPressEvent( QMouseEvent * e )
56
57
{
57
 
  mZoomRect.setRect(0, 0, 0, 0);
 
58
  if ( e->button() != Qt::LeftButton )
 
59
    return;
 
60
 
 
61
  mZoomRect.setRect( 0, 0, 0, 0 );
58
62
}
59
63
 
60
64
 
61
 
void QgsMapToolZoom::canvasReleaseEvent(QMouseEvent * e)
 
65
void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
62
66
{
63
 
  if (mDragging)
 
67
  if ( e->button() != Qt::LeftButton )
 
68
    return;
 
69
 
 
70
  if ( mDragging )
64
71
  {
65
72
    mDragging = false;
66
73
    delete mRubberBand;
67
74
    mRubberBand = 0;
68
 
    
 
75
 
69
76
    // store the rectangle
70
 
    mZoomRect.setRight(e->pos().x());
71
 
    mZoomRect.setBottom(e->pos().y());
72
 
    
73
 
    QgsMapToPixel *coordXForm = mCanvas->getCoordinateTransform();
74
 
    
75
 
    // set the extent to the zoomBox  
76
 
    QgsPoint ll = coordXForm->toMapCoordinates(mZoomRect.left(), mZoomRect.bottom());
77
 
    QgsPoint ur = coordXForm->toMapCoordinates(mZoomRect.right(), mZoomRect.top());       
78
 
        
79
 
    QgsRect r;
80
 
    r.setXmin(ll.x());
81
 
    r.setYmin(ll.y());
82
 
    r.setXmax(ur.x());
83
 
    r.setYmax(ur.y());
 
77
    mZoomRect.setRight( e->pos().x() );
 
78
    mZoomRect.setBottom( e->pos().y() );
 
79
 
 
80
    const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();
 
81
 
 
82
    // set the extent to the zoomBox
 
83
    QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() );
 
84
    QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() );
 
85
 
 
86
    QgsRectangle r;
 
87
    r.setXMinimum( ll.x() );
 
88
    r.setYMinimum( ll.y() );
 
89
    r.setXMaximum( ur.x() );
 
90
    r.setYMaximum( ur.y() );
84
91
    r.normalize();
85
 
    
86
 
    if (mZoomOut)
 
92
 
 
93
    // prevent zooming to an empty extent
 
94
    if ( r.width() == 0 || r.height() == 0 )
 
95
    {
 
96
      return;
 
97
    }
 
98
 
 
99
    if ( mZoomOut )
87
100
    {
88
101
      QgsPoint cer = r.center();
89
 
      QgsRect extent = mCanvas->extent();
90
 
    
 
102
      QgsRectangle extent = mCanvas->extent();
 
103
 
91
104
      double sf;
92
 
      if (mZoomRect.width() > mZoomRect.height())
 
105
      if ( mZoomRect.width() > mZoomRect.height() )
93
106
      {
94
 
        if(r.width() == 0)//prevent nan map extent
95
 
          {
96
 
            return;
97
 
          }
98
107
        sf = extent.width() / r.width();
99
108
      }
100
109
      else
101
110
      {
102
 
        if(r.height() == 0)//prevent nan map extent
103
 
          {
104
 
            return;
105
 
          }
106
111
        sf = extent.height() / r.height();
107
112
      }
108
 
      r.expand(sf);
109
 
            
110
 
  #ifdef QGISDEBUG
111
 
      std::cout << "Extent scaled by " << sf << " to " << r << std::endl;
112
 
      std::cout << "Center of currentExtent after scaling is " << r.center() << std::endl;
113
 
  #endif
114
 
  
 
113
      r.expand( sf );
 
114
 
 
115
      QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
 
116
      QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );
 
117
 
115
118
    }
116
 
  
117
 
    mCanvas->setExtent(r);
 
119
 
 
120
    mCanvas->setExtent( r );
118
121
    mCanvas->refresh();
119
122
  }
120
123
  else // not dragging
121
124
  {
122
125
    // change to zoom in/out by the default multiple
123
 
    mCanvas->zoomWithCenter(e->x(), e->y(), !mZoomOut);
 
126
    mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
124
127
  }
125
128
}