Avoid use of composition map settings to set initial composer map extent

This commit is contained in:
Nyall Dawson 2017-01-18 12:29:59 +10:00
parent a3dd380d35
commit 710a12cabd
5 changed files with 41 additions and 5 deletions

View File

@ -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* );

View File

@ -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 );

View File

@ -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 ) );
@ -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 )

View File

@ -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"
@ -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();
@ -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 )

View File

@ -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
@ -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;
@ -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 );