Skip to content

Commit 710a12c

Browse files
committedJan 18, 2017
Avoid use of composition map settings to set initial composer map extent
1 parent a3dd380 commit 710a12c

File tree

5 files changed

+41
-5
lines changed

5 files changed

+41
-5
lines changed
 

‎python/gui/qgscomposerview.sip‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ class QgsComposerView : QGraphicsView
133133
*/
134134
void setPreviewMode( QgsPreviewEffect::PreviewMode mode );
135135

136+
void setMapCanvas( QgsMapCanvas* canvas );
137+
QgsMapCanvas* mapCanvas() const;
138+
136139
protected:
137140
void mousePressEvent( QMouseEvent* );
138141
void mouseReleaseEvent( QMouseEvent* );

‎src/app/composer/qgscomposer.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4027,6 +4027,7 @@ void QgsComposer::createComposerView()
40274027

40284028
delete mView;
40294029
mView = new QgsComposerView();
4030+
mView->setMapCanvas( mQgis->mapCanvas() );
40304031
mView->setContentsMargins( 0, 0, 0, 0 );
40314032
mView->setHorizontalRuler( mHorizontalRuler );
40324033
mView->setVerticalRuler( mVerticalRuler );

‎src/core/composer/qgscomposermap.cpp‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
8585
int bgBlueInt = project->readNumEntry( QStringLiteral( "Gui" ), QStringLiteral( "/CanvasColorBluePart" ), 255 );
8686
setBackgroundColor( QColor( bgRedInt, bgGreenInt, bgBlueInt ) );
8787

88-
//calculate mExtent based on width/height ratio and map canvas extent
89-
mExtent = mComposition->mapSettings().visibleExtent();
90-
9188
init();
9289

9390
setSceneRect( QRectF( x, y, width, height ) );
@@ -775,9 +772,14 @@ void QgsComposerMap::setNewExtent( const QgsRectangle& extent )
775772
void QgsComposerMap::zoomToExtent( const QgsRectangle &extent )
776773
{
777774
QgsRectangle newExtent = extent;
775+
QgsRectangle currentExtent = *currentMapExtent();
778776
//Make sure the width/height ratio is the same as the current composer map extent.
779777
//This is to keep the map item frame size fixed
780-
double currentWidthHeightRatio = currentMapExtent()->width() / currentMapExtent()->height();
778+
double currentWidthHeightRatio = 1.0;
779+
if ( !currentExtent.isNull() )
780+
currentWidthHeightRatio = currentExtent.width() / currentExtent.height();
781+
else
782+
currentWidthHeightRatio = rect().width() / rect().height();
781783
double newWidthHeightRatio = newExtent.width() / newExtent.height();
782784

783785
if ( currentWidthHeightRatio < newWidthHeightRatio )

‎src/gui/qgscomposerview.cpp‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
#include "qgscomposerattributetablev2.h"
4545
#include "qgsaddremovemultiframecommand.h"
4646
#include "qgspaperitem.h"
47-
#include "qgsmapcanvas.h" //for QgsMapCanvas::WheelAction
47+
#include "qgsmapcanvas.h"
4848
#include "qgscursors.h"
4949
#include "qgscomposerutils.h"
5050

@@ -997,6 +997,9 @@ void QgsComposerView::mouseReleaseEvent( QMouseEvent* e )
997997
else
998998
{
999999
QgsComposerMap* composerMap = new QgsComposerMap( composition(), mRubberBandItem->transform().dx(), mRubberBandItem->transform().dy(), mRubberBandItem->rect().width(), mRubberBandItem->rect().height() );
1000+
if ( mCanvas )
1001+
composerMap->zoomToExtent( mCanvas->mapSettings().visibleExtent() );
1002+
10001003
composition()->addComposerMap( composerMap );
10011004

10021005
composition()->setAllDeselected();
@@ -2103,6 +2106,16 @@ void QgsComposerView::setPreviewMode( QgsPreviewEffect::PreviewMode mode )
21032106
mPreviewEffect->setMode( mode );
21042107
}
21052108

2109+
void QgsComposerView::setMapCanvas( QgsMapCanvas* canvas )
2110+
{
2111+
mCanvas = canvas;
2112+
}
2113+
2114+
QgsMapCanvas*QgsComposerView::mapCanvas() const
2115+
{
2116+
return mCanvas;
2117+
}
2118+
21062119
void QgsComposerView::paintEvent( QPaintEvent* event )
21072120
{
21082121
if ( mPaintingEnabled )

‎src/gui/qgscomposerview.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class QgsComposerScaleBar;
3939
class QgsComposerShape;
4040
class QgsComposerNodesItem;
4141
class QgsComposerAttributeTableV2;
42+
class QgsMapCanvas;
4243

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

174+
/** Sets the map canvas associated with the view. This allows the
175+
* view to retrieve map settings from the canvas.
176+
* @note added in QGIS 3.0
177+
* @see mapCanvas()
178+
*/
179+
void setMapCanvas( QgsMapCanvas* canvas );
180+
181+
/**
182+
* Returns the map canvas associated with the view.
183+
* @see setMapCanvas()
184+
* @note added in QGIS 3.0
185+
*/
186+
QgsMapCanvas* mapCanvas() const;
187+
173188
protected:
174189
void mousePressEvent( QMouseEvent* ) override;
175190
void mouseReleaseEvent( QMouseEvent* ) override;
@@ -218,6 +233,8 @@ class GUI_EXPORT QgsComposerView: public QGraphicsView
218233
QgsComposerRuler* mHorizontalRuler;
219234
QgsComposerRuler* mVerticalRuler;
220235

236+
QgsMapCanvas* mCanvas = nullptr;
237+
221238
//! Draw a shape on the canvas
222239
void addShape( Tool currentTool );
223240

0 commit comments

Comments
 (0)
Please sign in to comment.