From f652e8dcba373e633281de6df2509bf657688614 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Tue, 6 Nov 2018 13:37:39 -0400 Subject: [PATCH] save current extent and CRS in locator core remove remaining bits of interface --- .../locator/qgslocatorwidgetcore.sip.in | 15 ++++++++---- src/core/locator/qgslocatorwidgetcore.cpp | 23 ++++++++++--------- src/core/locator/qgslocatorwidgetcore.h | 20 ++++++++++++---- src/gui/locator/qgslocatorwidget.cpp | 17 +++++++++++++- src/gui/locator/qgslocatorwidget.h | 1 + 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/python/core/auto_generated/locator/qgslocatorwidgetcore.sip.in b/python/core/auto_generated/locator/qgslocatorwidgetcore.sip.in index 916fce8cdba2..bb29e0baa350 100644 --- a/python/core/auto_generated/locator/qgslocatorwidgetcore.sip.in +++ b/python/core/auto_generated/locator/qgslocatorwidgetcore.sip.in @@ -28,11 +28,6 @@ to be used in a locator widget. explicit QgsLocatorWidgetCore( QObject *parent = 0 ); %Docstring Constructor of QgsLocatorWidgetCore -%End - - void setMapCanvasInterface( QgsMapCanvasInterface *canvasInterface ); -%Docstring -Set the map canvas interface %End void performSearch( const QString &text ); @@ -80,6 +75,16 @@ Emitted when the results are cleared void invalidateResults(); %Docstring This will invalidate current search results +%End + + void updateCanvasExtent( const QgsRectangle &extent ); +%Docstring +Update the canvas extent used to create search context +%End + + void updateCanvasCrs( const QgsCoordinateReferenceSystem &crs ); +%Docstring +Update the canvas CRS used to create search context %End }; diff --git a/src/core/locator/qgslocatorwidgetcore.cpp b/src/core/locator/qgslocatorwidgetcore.cpp index ef0cca0910b1..cdf648fbbf5a 100644 --- a/src/core/locator/qgslocatorwidgetcore.cpp +++ b/src/core/locator/qgslocatorwidgetcore.cpp @@ -18,7 +18,6 @@ #include "qgslocatorwidgetcore.h" #include "qgslocator.h" #include "qgslocatormodel.h" -#include "qgsmapcanvasinterface.h" #include "qgsmapsettings.h" QgsLocatorWidgetCore::QgsLocatorWidgetCore( QObject *parent ) @@ -33,11 +32,6 @@ QgsLocatorWidgetCore::QgsLocatorWidgetCore( QObject *parent ) connect( mLocator, &QgsLocator::finished, this, &QgsLocatorWidgetCore::searchFinished ); } -void QgsLocatorWidgetCore::setMapCanvasInterface( QgsMapCanvasInterface *canvasInterface ) -{ - mCanvasInterface = canvasInterface; -} - bool QgsLocatorWidgetCore::isRunning() const { return mIsRunning; @@ -58,6 +52,16 @@ void QgsLocatorWidgetCore::invalidateResults() mLocatorModel->clear(); } +void QgsLocatorWidgetCore::updateCanvasExtent( const QgsRectangle &extent ) +{ + mCanvasExtent = extent; +} + +void QgsLocatorWidgetCore::updateCanvasCrs( const QgsCoordinateReferenceSystem &crs ) +{ + mCanvasCrs = crs; +} + void QgsLocatorWidgetCore::addResult( const QgsLocatorResult &result ) { mLocatorModel->addResult( result ); @@ -121,10 +125,7 @@ bool QgsLocatorWidgetCore::hasQueueRequested() const QgsLocatorContext QgsLocatorWidgetCore::createContext() { QgsLocatorContext context; - if ( mCanvasInterface ) - { - context.targetExtent = mCanvasInterface->mapSettings().visibleExtent(); - context.targetExtentCrs = mCanvasInterface->mapSettings().destinationCrs(); - } + context.targetExtent = mCanvasExtent; + context.targetExtentCrs = mCanvasCrs; return context; } diff --git a/src/core/locator/qgslocatorwidgetcore.h b/src/core/locator/qgslocatorwidgetcore.h index 84ecd3cc7666..c6e9b5fa5d46 100644 --- a/src/core/locator/qgslocatorwidgetcore.h +++ b/src/core/locator/qgslocatorwidgetcore.h @@ -21,13 +21,14 @@ #include #include "qgis_core.h" +#include "qgscoordinatereferencesystem.h" +#include "qgsrectangle.h" class QgsLocatorResult; class QgsLocator; class QgsLocatorContext; class QgsLocatorModel; class QgsLocatorProxyModel; -class QgsMapCanvasInterface; /** @@ -44,9 +45,6 @@ class CORE_EXPORT QgsLocatorWidgetCore : public QObject //! Constructor of QgsLocatorWidgetCore explicit QgsLocatorWidgetCore( QObject *parent = nullptr ); - //! Set the map canvas interface - void setMapCanvasInterface( QgsMapCanvasInterface *canvasInterface ); - //! Perform a search Q_INVOKABLE void performSearch( const QString &text ); @@ -76,6 +74,12 @@ class CORE_EXPORT QgsLocatorWidgetCore : public QObject //! This will invalidate current search results void invalidateResults(); + //! Update the canvas extent used to create search context + void updateCanvasExtent( const QgsRectangle &extent ); + + //! Update the canvas CRS used to create search context + void updateCanvasCrs( const QgsCoordinateReferenceSystem &crs ); + private slots: void searchFinished(); void addResult( const QgsLocatorResult &result ); @@ -87,11 +91,17 @@ class CORE_EXPORT QgsLocatorWidgetCore : public QObject QgsLocator *mLocator = nullptr; QgsLocatorModel *mLocatorModel = nullptr; QgsLocatorProxyModel *mProxyModel = nullptr; - QgsMapCanvasInterface *mCanvasInterface = nullptr; QString mNextRequestedString; bool mHasQueuedRequest = false; bool mIsRunning = false; + + // keep track of map canvas extent and CRS + // if much if needed, it would be possible to add + // a QgsMapCanvasController in core to achieve this + // see discussion in https://github.com/qgis/QGIS/pull/8404 + QgsRectangle mCanvasExtent; + QgsCoordinateReferenceSystem mCanvasCrs; }; #endif // QGSLOCATORWIDGETCORE_H diff --git a/src/gui/locator/qgslocatorwidget.cpp b/src/gui/locator/qgslocatorwidget.cpp index 3380b3bc2413..4233caf6d17a 100644 --- a/src/gui/locator/qgslocatorwidget.cpp +++ b/src/gui/locator/qgslocatorwidget.cpp @@ -116,7 +116,22 @@ QgsLocator *QgsLocatorWidget::locator() void QgsLocatorWidget::setMapCanvas( QgsMapCanvas *canvas ) { - mWidgetCore->setMapCanvasInterface( canvas ); + if ( mMapCanvas == canvas ) + return; + + for ( const QMetaObject::Connection &conn : qgis::as_const( mCanvasConnections ) ) + { + disconnect( conn ); + } + mCanvasConnections.clear(); + + mMapCanvas = canvas; + if ( mMapCanvas ) + { + mCanvasConnections + << connect( mMapCanvas, &QgsMapCanvas::extentsChanged, this, [ = ]() {mWidgetCore->updateCanvasExtent( mMapCanvas->extent() );} ) + << connect( mMapCanvas, &QgsMapCanvas::destinationCrsChanged, this, [ = ]() {mWidgetCore->updateCanvasCrs( mMapCanvas->mapSettings().destinationCrs() );} ) ; + } } void QgsLocatorWidget::search( const QString &string ) diff --git a/src/gui/locator/qgslocatorwidget.h b/src/gui/locator/qgslocatorwidget.h index 5a03abe4a864..99075da434c5 100644 --- a/src/gui/locator/qgslocatorwidget.h +++ b/src/gui/locator/qgslocatorwidget.h @@ -101,6 +101,7 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget QgsFloatingWidget *mResultsContainer = nullptr; QgsLocatorResultsView *mResultsView = nullptr; QgsMapCanvas *mMapCanvas = nullptr; + QList mCanvasConnections; QMenu *mMenu = nullptr; QTimer mFocusTimer;