Skip to content
Permalink
Browse files

Merge pull request #4005 from nyalldawson/comp_mapsettings

Remove some more QgsComposition::mapSettings use, also support different CRS in composer map items
  • Loading branch information
nyalldawson committed Jan 18, 2017
2 parents 433d04b + 710a12c commit df3fdf52a8de07e26419bcc36b215cb843bcff6c
Showing with 412 additions and 128 deletions.
  1. +4 −0 python/core/composer/qgscomposermap.sip
  2. +1 −1 python/core/composer/qgscomposerutils.sip
  3. +3 −0 python/gui/qgscomposerview.sip
  4. +1 −0 src/app/composer/qgscomposer.cpp
  5. +1 −1 src/app/composer/qgscomposermapgridwidget.cpp
  6. +78 −0 src/app/composer/qgscomposermapwidget.cpp
  7. +2 −0 src/app/composer/qgscomposermapwidget.h
  8. +2 −2 src/core/composer/qgsatlascomposition.cpp
  9. +1 −1 src/core/composer/qgscomposerarrow.cpp
  10. +1 −1 src/core/composer/qgscomposerattributetablev2.cpp
  11. +4 −1 src/core/composer/qgscomposerhtml.cpp
  12. +5 −2 src/core/composer/qgscomposerlabel.cpp
  13. +5 −5 src/core/composer/qgscomposerlegend.cpp
  14. +56 −23 src/core/composer/qgscomposermap.cpp
  15. +36 −0 src/core/composer/qgscomposermap.h
  16. +7 −14 src/core/composer/qgscomposermapgrid.cpp
  17. +2 −7 src/core/composer/qgscomposermapoverview.cpp
  18. +2 −2 src/core/composer/qgscomposernodesitem.cpp
  19. +1 −1 src/core/composer/qgscomposerpicture.cpp
  20. +1 −1 src/core/composer/qgscomposerpolygon.cpp
  21. +1 −1 src/core/composer/qgscomposerpolyline.cpp
  22. +1 −1 src/core/composer/qgscomposerscalebar.cpp
  23. +1 −1 src/core/composer/qgscomposershape.cpp
  24. +6 −4 src/core/composer/qgscomposerutils.cpp
  25. +1 −1 src/core/composer/qgscomposerutils.h
  26. +7 −1 src/core/composer/qgscomposition.cpp
  27. +1 −1 src/core/composer/qgspaperitem.cpp
  28. +14 −1 src/gui/qgscomposerview.cpp
  29. +17 −0 src/gui/qgscomposerview.h
  30. +1 −0 src/gui/qgsprojectionselectionwidget.cpp
  31. +6 −0 src/server/qgswmsconfigparser.cpp
  32. +57 −34 src/ui/composer/qgscomposermapwidgetbase.ui
  33. +1 −1 tests/src/core/testqgsatlascomposition.cpp
  34. +4 −6 tests/src/core/testqgscomposerscalebar.cpp
  35. +20 −4 tests/src/core/testqgscomposerutils.cpp
  36. +1 −0 tests/src/core/testqgscomposition.cpp
  37. +1 −3 tests/src/python/test_qgsatlascomposition.py
  38. +58 −6 tests/src/python/test_qgscomposermap.py
  39. +1 −1 tests/src/python/test_qgscomposerpicture.py
  40. BIN ...estdata/control_images/composer_map/expected_composermap_crs3857/expected_composermap_crs3857.png
  41. BIN ...estdata/control_images/composer_map/expected_composermap_crs4326/expected_composermap_crs4326.png
@@ -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 );

@@ -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 );
};
@@ -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* );
@@ -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 );
@@ -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 )
@@ -37,6 +37,7 @@
#include "qgsmapthemecollection.h"
#include "qgsmapthemes.h"
#include "qgisgui.h"
#include "qgscsexception.h"

#include <QMessageBox>

@@ -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 ) ) );
@@ -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();
@@ -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 )
@@ -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();
@@ -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();
}
@@ -532,6 +608,8 @@ void QgsComposerMapWidget::updateGuiElements()

blockAllSignals( true );

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

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

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

@@ -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 )
@@ -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;
@@ -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();
@@ -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 );
@@ -27,6 +27,7 @@
#include "qgsdistancearea.h"
#include "qgsjsonutils.h"
#include "qgsmapsettings.h"
#include "qgscomposermap.h"

#include "qgswebpage.h"
#include "qgswebframe.h"
@@ -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 )
{
@@ -27,6 +27,7 @@
#include "qgsfontutils.h"
#include "qgsexpressioncontext.h"
#include "qgsmapsettings.h"
#include "qgscomposermap.h"

#include "qgswebview.h"
#include "qgswebframe.h"
@@ -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() );
@@ -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;
@@ -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 );
}

0 comments on commit df3fdf5

Please sign in to comment.
You can’t perform that action at this time.