patch_for_4173.diff

Steven Mizuno, 2011-08-18 11:37 AM

Download (4.26 KB)

View differences:

src/gui/qgsmaptoolzoom.cpp
50 50
    mDragging = true;
51 51
    delete mRubberBand;
52 52
    mRubberBand = new QgsRubberBand( mCanvas, true );
53
    mZoomRect.setTopLeft( e->pos() );
53
    // the starting point is already captured
54 54
  }
55 55
  mZoomRect.setBottomRight( e->pos() );
56 56
  if ( mRubberBand )
......
66 66
  if ( e->button() != Qt::LeftButton )
67 67
    return;
68 68

  
69
  mZoomRect.setRect( 0, 0, 0, 0 );
69
  // store the starting point
70
  mZoomRect.setRect( e->pos().x(), e->pos().y(), 0, 0 );
70 71
}
71 72

  
72 73

  
......
75 76
  if ( e->button() != Qt::LeftButton )
76 77
    return;
77 78

  
79
  // ctrl key reverses zoom direction
80
  bool isZoomOut = ( e->modifiers() & Qt::ControlModifier ) ? ! mZoomOut : mZoomOut;
81

  
78 82
  if ( mDragging )
79 83
  {
80 84
    mDragging = false;
......
82 86
    mRubberBand = 0;
83 87

  
84 88
    // store the rectangle
85
    mZoomRect.setRight( e->pos().x() );
86
    mZoomRect.setBottom( e->pos().y() );
87

  
88
    const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();
89

  
90
    // set the extent to the zoomBox
91
    QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() );
92
    QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right(), mZoomRect.top() );
89
    mZoomRect.setBottomRight( e->pos() );
93 90

  
94
    QgsRectangle r;
95
    r.setXMinimum( ll.x() );
96
    r.setYMinimum( ll.y() );
97
    r.setXMaximum( ur.x() );
98
    r.setYMaximum( ur.y() );
99
    r.normalize();
91
    // before getting map coords
92
    mZoomRect = mZoomRect.normalized();
100 93

  
101 94
    // prevent zooming to an empty extent
102
    if ( r.width() == 0 || r.height() == 0 )
95
    if ( mZoomRect.width() == 0)
96
    {
97
      mZoomRect.setWidth( 1 );
98
    }
99
    if ( mZoomRect.height() == 0 )
103 100
    {
104
      return;
101
      mZoomRect.setHeight( 1 );
105 102
    }
106 103

  
107
    if ( mZoomOut )
104
    const QgsMapToPixel* coordinateTransform = mCanvas->getCoordinateTransform();
105

  
106
    // set the extent to the zoomBox
107
    // Need to adjust the right and bottom to account for integer values equating to one edge of a pixel.
108
    // This gets the outside of the zoom rectangle.
109
    QgsPoint ll = coordinateTransform->toMapCoordinates( mZoomRect.left(), mZoomRect.bottom() + 1 );
110
    QgsPoint ur = coordinateTransform->toMapCoordinates( mZoomRect.right() + 1, mZoomRect.top() );
111

  
112
    QgsRectangle r( ll, ur );
113

  
114
    if ( isZoomOut )
108 115
    {
109 116
      QgsPoint cer = r.center();
110 117
      QgsRectangle extent = mCanvas->extent();
111 118

  
112
      double sf;
113
      if ( mZoomRect.width() > mZoomRect.height() )
114
      {
115
        sf = extent.width() / r.width();
116
      }
117
      else
118
      {
119
        sf = extent.height() / r.height();
120
      }
121
      r.expand( sf );
119
      // calculate the ratios: width, height - of old to new rectangles
120
      double wRatio = extent.width() / r.width();
121
      double hRatio = extent.height() / r.height();
122

  
123
      // take the smaller ratio (the direction the zoom rect is closer to the edges of the map)
124
      double sf = wRatio < hRatio ? wRatio : hRatio;
125

  
126
      //set the rectangle representing the new location to the original extent's size
127
      double w2 = extent.width() / 2.0;
128
      double h2 = extent.height() / 2.0;
129
      r.set( QgsPoint( cer.x() - w2, cer.y() - h2 ),
130
             QgsPoint( cer.x() + w2, cer.y() + h2 ) );
131

  
132
      // then scale it
133
      r.scale( sf );
122 134

  
123 135
      QgsDebugMsg( QString( "Extent scaled by %1 to %2" ).arg( sf ).arg( r.toString().toLocal8Bit().constData() ) );
124 136
      QgsDebugMsg( QString( "Center of currentExtent after scaling is %1" ).arg( r.center().toString().toLocal8Bit().constData() ) );
......
131 143
  else // not dragging
132 144
  {
133 145
    // change to zoom in/out by the default multiple
134
    mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
146
    mCanvas->zoomWithCenter( e->x(), e->y(), !isZoomOut );
135 147
  }
136 148
}
137 149