Skip to content

Commit

Permalink
Merge pull request #4005 from nyalldawson/comp_mapsettings
Browse files Browse the repository at this point in the history
Remove some more QgsComposition::mapSettings use, also support different CRS in composer map items
  • Loading branch information
nyalldawson authored Jan 18, 2017
2 parents 433d04b + 710a12c commit df3fdf5
Show file tree
Hide file tree
Showing 41 changed files with 412 additions and 128 deletions.
4 changes: 4 additions & 0 deletions python/core/composer/qgscomposermap.sip
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ class QgsComposerMap : QgsComposerItem
//! @note not available in python bindings
// QgsRectangle* currentMapExtent();

QgsCoordinateReferenceSystem crs() const;
QgsCoordinateReferenceSystem presetCrs() const;
void setCrs( const QgsCoordinateReferenceSystem& crs );

PreviewMode previewMode() const;
void setPreviewMode( PreviewMode m );

Expand Down
2 changes: 1 addition & 1 deletion python/core/composer/qgscomposerutils.sip
Original file line number Diff line number Diff line change
Expand Up @@ -249,5 +249,5 @@ class QgsComposerUtils
* QgsComposition::referenceMap().
* @note added in QGIS 3.0
*/
static QgsRenderContext createRenderContext( QgsComposition* composition, QPainter& painter );
static QgsRenderContext createRenderContext( QgsComposition* composition, QPainter* painter );
};
3 changes: 3 additions & 0 deletions python/gui/qgscomposerview.sip
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
2 changes: 1 addition & 1 deletion src/app/composer/qgscomposermapgridwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1035,7 +1035,7 @@ void QgsComposerMapGridWidget::on_mMapGridCRSButton_clicked()

QgsGenericProjectionSelector crsDialog( this );
QgsCoordinateReferenceSystem crs = mComposerMapGrid->crs();
QString currentAuthId = crs.isValid() ? crs.authid() : mComposerMap->composition()->mapSettings().destinationCrs().authid();
QString currentAuthId = crs.isValid() ? crs.authid() : mComposerMap->crs().authid();
crsDialog.setSelectedAuthId( currentAuthId );

if ( crsDialog.exec() == QDialog::Accepted )
Expand Down
78 changes: 78 additions & 0 deletions src/app/composer/qgscomposermapwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "qgsmapthemecollection.h"
#include "qgsmapthemes.h"
#include "qgisgui.h"
#include "qgscsexception.h"

#include <QMessageBox>

Expand All @@ -63,6 +64,9 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap )
mPreviewModeComboBox->insertItem( 1, tr( "Render" ) );
mPreviewModeComboBox->insertItem( 2, tr( "Rectangle" ) );

mCrsSelector->setOptionVisible( QgsProjectionSelectionWidget::CrsNotSet, true );
mCrsSelector->setNotSetText( tr( "Use project CRS" ) );

// follow preset combo
mFollowVisibilityPresetCombo->setModel( new QStringListModel( mFollowVisibilityPresetCombo ) );
connect( mFollowVisibilityPresetCombo, SIGNAL( currentIndexChanged( int ) ), this, SLOT( followVisibilityPresetSelected( int ) ) );
Expand Down Expand Up @@ -99,6 +103,8 @@ QgsComposerMapWidget::QgsComposerMapWidget( QgsComposerMap* composerMap )
connect( mOverviewFrameMapComboBox, SIGNAL( itemChanged( QgsComposerItem* ) ), this, SLOT( overviewMapChanged( QgsComposerItem* ) ) );
}

connect( mCrsSelector, &QgsProjectionSelectionWidget::crsChanged, this, &QgsComposerMapWidget::mapCrsChanged );

updateGuiElements();
loadGridEntries();
loadOverviewEntries();
Expand Down Expand Up @@ -260,6 +266,42 @@ void QgsComposerMapWidget::cleanUpOverviewFrameStyleSelector( QgsPanelWidget* co
mComposerMap->endCommand();
}

void QgsComposerMapWidget::mapCrsChanged( const QgsCoordinateReferenceSystem& crs )
{
if ( !mComposerMap )
{
return;
}

if ( mComposerMap->presetCrs() == crs )
return;

// try to reproject to maintain extent
QgsCoordinateReferenceSystem oldCrs = mComposerMap->crs();

bool updateExtent = false;
QgsRectangle newExtent;
try
{
QgsCoordinateTransform xForm( oldCrs, crs.isValid() ? crs : QgsProject::instance()->crs() );
QgsRectangle prevExtent = *mComposerMap->currentMapExtent();
newExtent = xForm.transformBoundingBox( prevExtent );
updateExtent = true;
}
catch ( QgsCsException & )
{
//transform failed, don't update extent
}

mComposerMap->beginCommand( tr( "Map CRS changed" ) );
mComposerMap->setCrs( crs );
if ( updateExtent )
mComposerMap->zoomToExtent( newExtent );
mComposerMap->endCommand();
mComposerMap->cache();
mComposerMap->update();
}

void QgsComposerMapWidget::on_mAtlasCheckBox_toggled( bool checked )
{
if ( !mComposerMap )
Expand Down Expand Up @@ -471,6 +513,23 @@ void QgsComposerMapWidget::on_mSetToMapCanvasExtentButton_clicked()

QgsRectangle newExtent = QgisApp::instance()->mapCanvas()->mapSettings().visibleExtent();

//transform?
if ( QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs()
!= mComposerMap->crs() )
{
try
{
QgsCoordinateTransform xForm( QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs(),
mComposerMap->crs() );
newExtent = xForm.transformBoundingBox( newExtent );
}
catch ( QgsCsException & )
{
//transform failed, better not proceed
return;
}
}

mComposerMap->beginCommand( tr( "Map extent changed" ) );
mComposerMap->zoomToExtent( newExtent );
mComposerMap->endCommand();
Expand All @@ -487,6 +546,23 @@ void QgsComposerMapWidget::on_mViewExtentInCanvasButton_clicked()

if ( !currentMapExtent.isEmpty() )
{
//transform?
if ( QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs()
!= mComposerMap->crs() )
{
try
{
QgsCoordinateTransform xForm( mComposerMap->crs(),
QgisApp::instance()->mapCanvas()->mapSettings().destinationCrs() );
currentMapExtent = xForm.transformBoundingBox( currentMapExtent );
}
catch ( QgsCsException & )
{
//transform failed, better not proceed
return;
}
}

QgisApp::instance()->mapCanvas()->setExtent( currentMapExtent );
QgisApp::instance()->mapCanvas()->refresh();
}
Expand Down Expand Up @@ -532,6 +608,8 @@ void QgsComposerMapWidget::updateGuiElements()

blockAllSignals( true );

whileBlocking( mCrsSelector )->setCrs( mComposerMap->presetCrs() );

//width, height, scale
double scale = mComposerMap->scale();

Expand Down
2 changes: 2 additions & 0 deletions src/app/composer/qgscomposermapwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class QgsComposerMapWidget: public QgsComposerItemBaseWidget, private Ui::QgsCom
void updateOverviewFrameStyleFromWidget();
void cleanUpOverviewFrameStyleSelector( QgsPanelWidget* container );

void mapCrsChanged( const QgsCoordinateReferenceSystem& crs );

private:
QgsComposerMap* mComposerMap;

Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgsatlascomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ void QgsAtlasComposition::computeExtent( QgsComposerMap* map )
// QgsGeometry::boundingBox is expressed in the geometry"s native CRS
// We have to transform the grometry to the destination CRS and ask for the bounding box
// Note: we cannot directly take the transformation of the bounding box, since transformations are not linear
mTransformedFeatureBounds = currentGeometry( map->composition()->mapSettings().destinationCrs() ).boundingBox();
mTransformedFeatureBounds = currentGeometry( map->crs() ).boundingBox();
}

void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
Expand Down Expand Up @@ -481,7 +481,7 @@ void QgsAtlasComposition::prepareMap( QgsComposerMap* map )
if ( map->atlasScalingMode() == QgsComposerMap::Fixed || map->atlasScalingMode() == QgsComposerMap::Predefined || isPointLayer )
{
QgsScaleCalculator calc;
calc.setMapUnits( composition()->mapSettings().mapUnits() );
calc.setMapUnits( map->crs().mapUnits() );
calc.setDpi( 25.4 );
double originalScale = calc.calculate( mOrigExtent, map->rect().width() );
double geomCenterX = ( xa1 + xa2 ) / 2.0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerarrow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ void QgsComposerArrow::drawLine( QPainter *painter )
painter->scale( 1 / dotsPerMM, 1 / dotsPerMM ); //scale painter from mm to dots

//setup render context
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, *painter );
QgsRenderContext context = QgsComposerUtils::createRenderContext( mComposition, painter );
context.setForceVectorOutput( true );

QgsExpressionContext expressionContext = createExpressionContext();
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgscomposerattributetablev2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ bool QgsComposerAttributeTableV2::getTableContents( QgsComposerTableContents &co
if ( layer )
{
//transform back to layer CRS
QgsCoordinateTransform coordTransform( layer->crs(), mComposition->mapSettings().destinationCrs() );
QgsCoordinateTransform coordTransform( layer->crs(), mComposerMap->crs() );
try
{
selectionRect = coordTransform.transformBoundingBox( selectionRect, QgsCoordinateTransform::ReverseTransform );
Expand Down
5 changes: 4 additions & 1 deletion src/core/composer/qgscomposerhtml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "qgsdistancearea.h"
#include "qgsjsonutils.h"
#include "qgsmapsettings.h"
#include "qgscomposermap.h"

#include "qgswebpage.h"
#include "qgswebframe.h"
Expand Down Expand Up @@ -542,7 +543,9 @@ void QgsComposerHtml::setExpressionContext( const QgsFeature &feature, QgsVector
else if ( mComposition )
{
//set to composition's mapsettings' crs
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
QgsComposerMap* referenceMap = mComposition->referenceMap();
if ( referenceMap )
mDistanceArea->setSourceCrs( referenceMap->crs().srsid() );
}
if ( mComposition )
{
Expand Down
7 changes: 5 additions & 2 deletions src/core/composer/qgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "qgsfontutils.h"
#include "qgsexpressioncontext.h"
#include "qgsmapsettings.h"
#include "qgscomposermap.h"

#include "qgswebview.h"
#include "qgswebframe.h"
Expand Down Expand Up @@ -261,8 +262,10 @@ void QgsComposerLabel::refreshExpressionContext()
}
else
{
//set to composition's mapsettings' crs
mDistanceArea->setSourceCrs( mComposition->mapSettings().destinationCrs().srsid() );
//set to composition's reference map's crs
QgsComposerMap* referenceMap = mComposition->referenceMap();
if ( referenceMap )
mDistanceArea->setSourceCrs( referenceMap->crs().srsid() );
}
mDistanceArea->setEllipsoidalMode( true );
mDistanceArea->setEllipsoid( mComposition->project()->ellipsoid() );
Expand Down
10 changes: 5 additions & 5 deletions src/core/composer/qgscomposerlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,10 @@ void QgsComposerLegend::paint( QPainter* painter, const QStyleOptionGraphicsItem
mSettings.setMmPerMapUnit( mComposerMap->mapUnitsToMM() );

// use a temporary QgsMapSettings to find out real map scale
QgsMapSettings ms = mComposerMap->composition()->mapSettings();
ms.setOutputSize( QSizeF( mComposerMap->rect().width() * dotsPerMM, mComposerMap->rect().height() * dotsPerMM ).toSize() );
ms.setExtent( *mComposerMap->currentMapExtent() );
ms.setOutputDpi( dpi );
QSizeF mapSizePixels = QSizeF( mComposerMap->rect().width() * dotsPerMM, mComposerMap->rect().height() * dotsPerMM );
QgsRectangle mapExtent = *mComposerMap->currentMapExtent();

QgsMapSettings ms = mComposerMap->mapSettings( mapExtent, mapSizePixels, dpi );
mSettings.setMapScale( ms.scale() );
}
mInitialMapScaleCalculated = true;
Expand Down Expand Up @@ -704,7 +704,7 @@ void QgsComposerLegend::doUpdateFilterByMap()
QgsGeometry filterPolygon;
if ( mInAtlas )
{
filterPolygon = composition()->atlasComposition().currentGeometry( composition()->mapSettings().destinationCrs() );
filterPolygon = composition()->atlasComposition().currentGeometry( mComposerMap->crs() );
}
mLegendModel->setLegendFilter( &ms, /* useExtent */ mInAtlas || mLegendFilterByMap, filterPolygon, /* useExpressions */ true );
}
Expand Down
Loading

0 comments on commit df3fdf5

Please sign in to comment.