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.
This commit is contained in:
Alessandro Pasotti 2017-07-18 09:12:44 +02:00
parent f459790fc6
commit e83ef2e8b5
6 changed files with 51 additions and 72 deletions

View File

@ -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
};
/************************************************************************

View File

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

View File

@ -29,6 +29,8 @@
#include "qgscoordinatereferencesystem.h"
#include <QDialog>
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;
};

View File

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

View File

@ -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 & )
{

View File

@ -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<QString> &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