Skip to content

Commit

Permalink
Port zoom rectangle from QRubberBand to QgsRubberBand. Fixes zoom rec…
Browse files Browse the repository at this point in the history
…tangle fill on x11 platform after resize bug workaround

git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@15838 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Apr 28, 2011
1 parent 09aa1b3 commit f35fe9b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 9 deletions.
5 changes: 5 additions & 0 deletions python/gui/qgsrubberband.sip
Expand Up @@ -44,6 +44,11 @@ class QgsRubberBand: QgsMapCanvasItem
*/
void addGeometry(QgsGeometry* geom, QgsVectorLayer* layer);

/**Sets this rubber band to a map canvas rectangle
@param rect rectangle in canvas coordinates
@note added in version 1.7*/
void setToCanvasRectangle( const QRect& rect );

/**Adds translation to original coordinates (all in map coordinates)*/
void setTranslationOffset(double dx, double dy);

Expand Down
25 changes: 20 additions & 5 deletions src/gui/qgsmaptoolzoom.cpp
Expand Up @@ -18,23 +18,28 @@
#include "qgsmapcanvas.h"
#include "qgsmaptopixel.h"
#include "qgscursors.h"
#include "qgsrubberband.h"

#include <QMouseEvent>
#include <QRubberBand>
#include <QRect>
#include <QCursor>
#include <QPixmap>
#include "qgslogger.h"


QgsMapToolZoom::QgsMapToolZoom( QgsMapCanvas* canvas, bool zoomOut )
: QgsMapTool( canvas ), mZoomOut( zoomOut ), mDragging( false )
: QgsMapTool( canvas ), mZoomOut( zoomOut ), mDragging( false ), mRubberBand( 0 )
{
// set the cursor
QPixmap myZoomQPixmap = QPixmap(( const char ** )( zoomOut ? zoom_out : zoom_in ) );
mCursor = QCursor( myZoomQPixmap, 7, 7 );
}

QgsMapToolZoom::~QgsMapToolZoom()
{
delete mRubberBand;
}


void QgsMapToolZoom::canvasMoveEvent( QMouseEvent * e )
{
Expand All @@ -44,12 +49,16 @@ void QgsMapToolZoom::canvasMoveEvent( QMouseEvent * e )
if ( !mDragging )
{
mDragging = true;
mRubberBand = new QRubberBand( QRubberBand::Rectangle, mCanvas );
delete mRubberBand;
mRubberBand = new QgsRubberBand( mCanvas, true );
mZoomRect.setTopLeft( e->pos() );
}
mZoomRect.setBottomRight( e->pos() );
mRubberBand->setGeometry( mZoomRect.normalized() );
mRubberBand->show();
if ( mRubberBand )
{
mRubberBand->setToCanvasRectangle( mZoomRect );
mRubberBand->show();
}
}


Expand Down Expand Up @@ -126,3 +135,9 @@ void QgsMapToolZoom::canvasReleaseEvent( QMouseEvent * e )
mCanvas->zoomWithCenter( e->x(), e->y(), !mZoomOut );
}
}

void QgsMapToolZoom::deactivate()
{
delete mRubberBand;
mRubberBand = 0;
}
10 changes: 6 additions & 4 deletions src/gui/qgsmaptoolzoom.h
Expand Up @@ -20,8 +20,7 @@
#include "qgsmaptool.h"
#include <QRect>


class QRubberBand;
class QgsRubberBand;

/** \ingroup gui
* A map tool for zooming into the map.
Expand All @@ -33,6 +32,8 @@ class GUI_EXPORT QgsMapToolZoom : public QgsMapTool
//! constructor
QgsMapToolZoom( QgsMapCanvas* canvas, bool zoomOut );

~QgsMapToolZoom();

//! Overridden mouse move event
virtual void canvasMoveEvent( QMouseEvent * e );

Expand All @@ -44,6 +45,8 @@ class GUI_EXPORT QgsMapToolZoom : public QgsMapTool

virtual bool isTransient() { return true; }

virtual void deactivate();

protected:
//! stores actual zoom rect
QRect mZoomRect;
Expand All @@ -54,8 +57,7 @@ class GUI_EXPORT QgsMapToolZoom : public QgsMapTool
//! Flag to indicate a map canvas drag operation is taking place
bool mDragging;

//! TODO: to be changed to a canvas item
QRubberBand* mRubberBand;
QgsRubberBand* mRubberBand;
};

#endif
18 changes: 18 additions & 0 deletions src/gui/qgsrubberband.cpp
Expand Up @@ -341,6 +341,24 @@ void QgsRubberBand::addGeometry( QgsGeometry* geom, QgsVectorLayer* layer )
update();
}

void QgsRubberBand::setToCanvasRectangle( const QRect& rect )
{
if ( !mMapCanvas )
{
return;
}

const QgsMapToPixel* transform = mMapCanvas->getCoordinateTransform();
QgsPoint ll = transform->toMapCoordinates( rect.left(), rect.bottom() );
QgsPoint ur = transform->toMapCoordinates( rect.right(), rect.top() );

reset( true );
addPoint( ll, false );
addPoint( QgsPoint( ur.x(), ll.y() ), false );
addPoint( ur, false );
addPoint( QgsPoint( ll.x(), ur.y() ), true );
}

/*!
Draw the shape in response to an update event.
*/
Expand Down
5 changes: 5 additions & 0 deletions src/gui/qgsrubberband.h
Expand Up @@ -61,6 +61,11 @@ class GUI_EXPORT QgsRubberBand: public QgsMapCanvasItem
*/
void setToGeometry( QgsGeometry* geom, QgsVectorLayer* layer );

/**Sets this rubber band to a map canvas rectangle
@param rect rectangle in canvas coordinates
@note added in version 1.7*/
void setToCanvasRectangle( const QRect& rect );

/**Add the geometry of an existing feature to a rubberband
This is useful for multi feature highlighting.
@param geom the geometry object
Expand Down

0 comments on commit f35fe9b

Please sign in to comment.