Skip to content

Commit

Permalink
Avoid use of composition map settings to set initial composer map extent
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 18, 2017
1 parent a3dd380 commit 710a12c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
3 changes: 3 additions & 0 deletions python/gui/qgscomposerview.sip
Expand Up @@ -133,6 +133,9 @@ class QgsComposerView : QGraphicsView
*/
void setPreviewMode( QgsPreviewEffect::PreviewMode mode );

void setMapCanvas( QgsMapCanvas* canvas );
QgsMapCanvas* mapCanvas() const;

protected:
void mousePressEvent( QMouseEvent* );
void mouseReleaseEvent( QMouseEvent* );
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -4027,6 +4027,7 @@ void QgsComposer::createComposerView()

delete mView;
mView = new QgsComposerView();
mView->setMapCanvas( mQgis->mapCanvas() );
mView->setContentsMargins( 0, 0, 0, 0 );
mView->setHorizontalRuler( mHorizontalRuler );
mView->setVerticalRuler( mVerticalRuler );
Expand Down
10 changes: 6 additions & 4 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -85,9 +85,6 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
int bgBlueInt = project->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorBluePart" ), 255 );
setBackgroundColor( QColor( bgRedInt, bgGreenInt, bgBlueInt ) );

//calculate mExtent based on width/height ratio and map canvas extent
mExtent = mComposition->mapSettings().visibleExtent();

init();

setSceneRect( QRectF( x, y, width, height ) );
Expand Down Expand Up @@ -775,9 +772,14 @@ void QgsComposerMap::setNewExtent( const QgsRectangle& extent )
void QgsComposerMap::zoomToExtent( const QgsRectangle &extent )
{
QgsRectangle newExtent = extent;
QgsRectangle currentExtent = *currentMapExtent();
//Make sure the width/height ratio is the same as the current composer map extent.
//This is to keep the map item frame size fixed
double currentWidthHeightRatio = currentMapExtent()->width() / currentMapExtent()->height();
double currentWidthHeightRatio = 1.0;
if ( !currentExtent.isNull() )
currentWidthHeightRatio = currentExtent.width() / currentExtent.height();
else
currentWidthHeightRatio = rect().width() / rect().height();
double newWidthHeightRatio = newExtent.width() / newExtent.height();

if ( currentWidthHeightRatio < newWidthHeightRatio )
Expand Down
15 changes: 14 additions & 1 deletion src/gui/qgscomposerview.cpp
Expand Up @@ -44,7 +44,7 @@
#include "qgscomposerattributetablev2.h"
#include "qgsaddremovemultiframecommand.h"
#include "qgspaperitem.h"
#include "qgsmapcanvas.h" //for QgsMapCanvas::WheelAction
#include "qgsmapcanvas.h"
#include "qgscursors.h"
#include "qgscomposerutils.h"

Expand Down Expand Up @@ -997,6 +997,9 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
else
{
QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() );
if ( mCanvas )
composerMap->zoomToExtent( mCanvas->mapSettings().visibleExtent() );

composition()->addComposerMap( composerMap );

composition()->setAllDeselected();
Expand Down Expand Up @@ -2103,6 +2106,16 @@ void QgsComposerView::setPreviewMode( QgsPreviewEffect::PreviewMode mode )
mPreviewEffect->setMode( mode );
}

void QgsComposerView::setMapCanvas( QgsMapCanvas* canvas )
{
mCanvas = canvas;
}

QgsMapCanvas*QgsComposerView::mapCanvas() const
{
return mCanvas;
}

void QgsComposerView::paintEvent( QPaintEvent* event )
{
if ( mPaintingEnabled )
Expand Down
17 changes: 17 additions & 0 deletions src/gui/qgscomposerview.h
Expand Up @@ -39,6 +39,7 @@ class QgsComposerScaleBar;
class QgsComposerShape;
class QgsComposerNodesItem;
class QgsComposerAttributeTableV2;
class QgsMapCanvas;

/** \ingroup gui
* Widget to display the composer items. Manages the composer tools and the
Expand Down Expand Up @@ -170,6 +171,20 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
*/
void setPreviewMode( QgsPreviewEffect::PreviewMode mode );

/** Sets the map canvas associated with the view. This allows the
* view to retrieve map settings from the canvas.
* @note added in QGIS 3.0
* @see mapCanvas()
*/
void setMapCanvas( QgsMapCanvas* canvas );

/**
* Returns the map canvas associated with the view.
* @see setMapCanvas()
* @note added in QGIS 3.0
*/
QgsMapCanvas* mapCanvas() const;

protected:
void mousePressEvent( QMouseEvent* ) override;
void mouseReleaseEvent( QMouseEvent* ) override;
Expand Down Expand Up @@ -218,6 +233,8 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
QgsComposerRuler* mHorizontalRuler;
QgsComposerRuler* mVerticalRuler;

QgsMapCanvas* mCanvas = nullptr;

//! Draw a shape on the canvas
void addShape( Tool currentTool );

Expand Down

0 comments on commit 710a12c

Please sign in to comment.