From e83ef2e8b59ee9aa4bd39994c5bfdf1c854957e5 Mon Sep 17 00:00:00 2001 From: Alessandro Pasotti Date: Tue, 18 Jul 2017 09:12:44 +0200 Subject: [PATCH] Store canvas into the base class and set extent/crs from the arcgis classes This modification was necessary because the current implementation of the source select dialogs within the unified add layer dialog create the provider dialogs the first time and do not destroy them, this means that the canvas extent and CRS can change from a dialog invocation to the next and the extent and CRS need to be updated at layer creation time. --- python/gui/qgsabstractdatasourcewidget.sip | 44 +++++++------------ src/gui/qgsabstractdatasourcewidget.cpp | 17 ++----- src/gui/qgsabstractdatasourcewidget.h | 31 ++++++------- src/gui/qgsdatasourcemanagerdialog.cpp | 3 +- .../qgsarcgisservicesourceselect.cpp | 19 ++++---- .../arcgisrest/qgsarcgisservicesourceselect.h | 9 +++- 6 files changed, 51 insertions(+), 72 deletions(-) diff --git a/python/gui/qgsabstractdatasourcewidget.sip b/python/gui/qgsabstractdatasourcewidget.sip index e50d3fbf469..ecc3145af2b 100644 --- a/python/gui/qgsabstractdatasourcewidget.sip +++ b/python/gui/qgsabstractdatasourcewidget.sip @@ -10,6 +10,7 @@ + class QgsAbstractDataSourceWidget : QDialog { %Docstring @@ -30,37 +31,12 @@ class QgsAbstractDataSourceWidget : QDialog Destructor %End - QgsProviderRegistry::WidgetMode widgetMode( ) const; + void setMapCanvas( const QgsMapCanvas *mapCanvas ); %Docstring -Return the widget mode - :rtype: QgsProviderRegistry.WidgetMode + Store a pointer to the map canvas to retrieve extent and CRS + Used to select an appropriate CRS and possibly to retrieve data only in the current extent %End - void setCurrentCrs( const QgsCoordinateReferenceSystem &crs ); -%Docstring - Set the current CRS - The CRS is normally the CRS of the map canvas, and it can be used - by the provider dialog to transform the extent and constraint the service -%End - - void setCurrentExtent( const QgsRectangle &extent ); -%Docstring - Set the current extent - The extent is normally the extent of the map canvas, and it can be used - by the provider dialog to constraint the service -%End - - QgsRectangle currentExtent( ) const; -%Docstring -Return the current extent - :rtype: QgsRectangle -%End - - QgsCoordinateReferenceSystem currentCrs( ) const; -%Docstring -Return the current CRS - :rtype: QgsCoordinateReferenceSystem -%End public slots: @@ -100,6 +76,18 @@ Emitted when a vector layer has been selected for addition Constructor %End + QgsProviderRegistry::WidgetMode widgetMode( ) const; +%Docstring +Return the widget mode + :rtype: QgsProviderRegistry.WidgetMode +%End + + const QgsMapCanvas *mapCanvas( ) const; +%Docstring + Return the map canvas (can be null) + :rtype: QgsMapCanvas +%End + }; /************************************************************************ diff --git a/src/gui/qgsabstractdatasourcewidget.cpp b/src/gui/qgsabstractdatasourcewidget.cpp index b6bab91dca0..8fddb860c64 100644 --- a/src/gui/qgsabstractdatasourcewidget.cpp +++ b/src/gui/qgsabstractdatasourcewidget.cpp @@ -30,22 +30,13 @@ QgsProviderRegistry::WidgetMode QgsAbstractDataSourceWidget::widgetMode() const return mWidgetMode; } -void QgsAbstractDataSourceWidget::setCurrentCrs( const QgsCoordinateReferenceSystem &crs ) +const QgsMapCanvas *QgsAbstractDataSourceWidget::mapCanvas() const { - mCurrentCrs = crs; + return mMapCanvas; } -void QgsAbstractDataSourceWidget::setCurrentExtent( const QgsRectangle &extent ) -{ - mCurrentExtent = extent; -} -QgsRectangle QgsAbstractDataSourceWidget::currentExtent() const +void QgsAbstractDataSourceWidget::setMapCanvas( const QgsMapCanvas *mapCanvas ) { - return mCurrentExtent; -} - -QgsCoordinateReferenceSystem QgsAbstractDataSourceWidget::currentCrs() const -{ - return mCurrentCrs; + mMapCanvas = mapCanvas; } diff --git a/src/gui/qgsabstractdatasourcewidget.h b/src/gui/qgsabstractdatasourcewidget.h index 7b57132f514..f8672b4d583 100644 --- a/src/gui/qgsabstractdatasourcewidget.h +++ b/src/gui/qgsabstractdatasourcewidget.h @@ -29,6 +29,8 @@ #include "qgscoordinatereferencesystem.h" #include +class QgsMapCanvas; + /** \ingroup gui * \brief Abstract base Data Source Widget to create connections and add layers * This class provides common functionality and the interface for all @@ -45,26 +47,11 @@ class GUI_EXPORT QgsAbstractDataSourceWidget : public QDialog //! Destructor ~QgsAbstractDataSourceWidget( ) = default; - //! Return the widget mode - QgsProviderRegistry::WidgetMode widgetMode( ) const; - - /** Set the current CRS - * The CRS is normally the CRS of the map canvas, and it can be used - * by the provider dialog to transform the extent and constraint the service + /** Store a pointer to the map canvas to retrieve extent and CRS + * Used to select an appropriate CRS and possibly to retrieve data only in the current extent */ - void setCurrentCrs( const QgsCoordinateReferenceSystem &crs ); + void setMapCanvas( const QgsMapCanvas *mapCanvas ); - /** Set the current extent - * The extent is normally the extent of the map canvas, and it can be used - * by the provider dialog to constraint the service - */ - void setCurrentExtent( const QgsRectangle &extent ); - - //! Return the current extent - QgsRectangle currentExtent( ) const; - - //! Return the current CRS - QgsCoordinateReferenceSystem currentCrs( ) const; public slots: @@ -94,11 +81,19 @@ class GUI_EXPORT QgsAbstractDataSourceWidget : public QDialog //! Constructor QgsAbstractDataSourceWidget( QWidget *parent SIP_TRANSFERTHIS = nullptr, Qt::WindowFlags fl = QgsGuiUtils::ModalDialogFlags, QgsProviderRegistry::WidgetMode widgetMode = QgsProviderRegistry::WidgetMode::None ); + //! Return the widget mode + QgsProviderRegistry::WidgetMode widgetMode( ) const; + + /** Return the map canvas (can be null) + */ + const QgsMapCanvas *mapCanvas( ) const; + private: QgsProviderRegistry::WidgetMode mWidgetMode; QgsCoordinateReferenceSystem mCurrentCrs; QgsRectangle mCurrentExtent; + QgsMapCanvas const *mMapCanvas = nullptr; }; diff --git a/src/gui/qgsdatasourcemanagerdialog.cpp b/src/gui/qgsdatasourcemanagerdialog.cpp index 4b6c43f2cf2..2288b7d2c5e 100644 --- a/src/gui/qgsdatasourcemanagerdialog.cpp +++ b/src/gui/qgsdatasourcemanagerdialog.cpp @@ -185,8 +185,7 @@ QgsAbstractDataSourceWidget *QgsDataSourceManagerDialog::providerDialog( const Q // Set crs and extent from canvas if ( mMapCanvas ) { - dlg->setCurrentExtent( mMapCanvas->extent() ); - dlg->setCurrentCrs( mMapCanvas->mapSettings().destinationCrs( ) ); + dlg->setMapCanvas( mMapCanvas ); } return dlg; } diff --git a/src/providers/arcgisrest/qgsarcgisservicesourceselect.cpp b/src/providers/arcgisrest/qgsarcgisservicesourceselect.cpp index 1bae28f0a2c..2462062c2f5 100644 --- a/src/providers/arcgisrest/qgsarcgisservicesourceselect.cpp +++ b/src/providers/arcgisrest/qgsarcgisservicesourceselect.cpp @@ -123,11 +123,6 @@ QgsArcGisServiceSourceSelect::~QgsArcGisServiceSourceSelect() delete mModelProxy; } -void QgsArcGisServiceSourceSelect::setCurrentExtentAndCrs( const QgsRectangle &canvasExtent, const QgsCoordinateReferenceSystem &canvasCrs ) -{ - mCanvasExtent = canvasExtent; - mCanvasCrs = canvasCrs; -} void QgsArcGisServiceSourceSelect::populateImageEncodings( const QStringList &availableEncodings ) { @@ -325,15 +320,21 @@ void QgsArcGisServiceSourceSelect::addButtonClicked() QString pCrsString( labelCoordRefSys->text() ); QgsCoordinateReferenceSystem pCrs( pCrsString ); //prepare canvas extent info for layers with "cache features" option not set - QgsRectangle extent = mCanvasExtent; + QgsRectangle extent; + QgsCoordinateReferenceSystem canvasCrs; + if ( mapCanvas() ) + { + extent = mapCanvas()->extent(); + canvasCrs = mapCanvas()->mapSettings().destinationCrs(); + } //does canvas have "on the fly" reprojection set? - if ( pCrs.isValid() && mCanvasCrs.isValid() ) + if ( pCrs.isValid() && canvasCrs.isValid() ) { try { - extent = QgsCoordinateTransform( mCanvasCrs, pCrs ).transform( extent ); + extent = QgsCoordinateTransform( canvasCrs, pCrs ).transform( extent ); QgsDebugMsg( QString( "canvas transform: Canvas CRS=%1, Provider CRS=%2, BBOX=%3" ) - .arg( mCanvasCrs.authid(), pCrs.authid(), extent.asWktCoordinates() ) ); + .arg( canvasCrs.authid(), pCrs.authid(), extent.asWktCoordinates() ) ); } catch ( const QgsCsException & ) { diff --git a/src/providers/arcgisrest/qgsarcgisservicesourceselect.h b/src/providers/arcgisrest/qgsarcgisservicesourceselect.h index 9653ed51929..7f8d94a8272 100644 --- a/src/providers/arcgisrest/qgsarcgisservicesourceselect.h +++ b/src/providers/arcgisrest/qgsarcgisservicesourceselect.h @@ -27,6 +27,7 @@ class QStandardItemModel; class QSortFilterProxyModel; class QgsProjectionSelectionDialog; class QgsOwsConnection; +class QgsMapCanvas; /** * Base class for listing ArcGis layers available from a remote service. @@ -78,8 +79,6 @@ class QgsArcGisServiceSourceSelect : public QgsAbstractDataSourceWidget, protect void populateImageEncodings( const QStringList &availableEncodings ); //! Returns the selected image encoding. QString getSelectedImageEncoding() const; - //! Sets the current extent and CRS. Used to select an appropriate CRS and possibly to retrieve data only in the current extent - void setCurrentExtentAndCrs( const QgsRectangle &canvasExtent, const QgsCoordinateReferenceSystem &canvasCrs ); private: void populateConnectionList(); @@ -91,6 +90,12 @@ class QgsArcGisServiceSourceSelect : public QgsAbstractDataSourceWidget, protect \returns the authority id of the crs or an empty string in case of error*/ QString getPreferredCrs( const QSet &crsSet ) const; + /** Store a pointer to map canvas to retrieve extent and CRS + * Used to select an appropriate CRS and possibly to retrieve data only in the current extent + */ + QgsMapCanvas *mMapCanvas = nullptr; + + public slots: //! Triggered when the provider's connections need to be refreshed