diff --git a/python/gui/locator/qgslocatorwidget.sip b/python/gui/locator/qgslocatorwidget.sip index 9f25efa7eff..2aa35f3b106 100644 --- a/python/gui/locator/qgslocatorwidget.sip +++ b/python/gui/locator/qgslocatorwidget.sip @@ -35,6 +35,13 @@ class QgsLocatorWidget : QWidget :rtype: QgsLocator %End + void setMapCanvas( QgsMapCanvas *canvas ); +%Docstring + Sets a map ``canvas`` to associate with the widget. This allows the + widget to customise the searches performed by its locator(), such + as prioritizing results which are near the current canvas extent. +%End + public slots: void search( const QString &string ); diff --git a/src/app/qgisapp.cpp b/src/app/qgisapp.cpp index ca786ec979b..8e9954321cf 100644 --- a/src/app/qgisapp.cpp +++ b/src/app/qgisapp.cpp @@ -966,6 +966,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh // set graphical credential requester new QgsCredentialDialog( this ); + mLocatorWidget->setMapCanvas( mMapCanvas ); + qApp->processEvents(); // load providers diff --git a/src/gui/locator/qgslocator.cpp b/src/gui/locator/qgslocator.cpp index 33cb90c29f8..12c50c89f31 100644 --- a/src/gui/locator/qgslocator.cpp +++ b/src/gui/locator/qgslocator.cpp @@ -72,7 +72,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c } mFeedback = feedback; - auto gatherFilterResults = [string, feedback]( QgsLocatorFilter * filter ) + auto gatherFilterResults = [string, context, feedback]( QgsLocatorFilter * filter ) { if ( !feedback->isCanceled() ) filter->fetchResults( string, context, feedback ); diff --git a/src/gui/locator/qgslocatorwidget.cpp b/src/gui/locator/qgslocatorwidget.cpp index e1d6770627b..fc61271f232 100644 --- a/src/gui/locator/qgslocatorwidget.cpp +++ b/src/gui/locator/qgslocatorwidget.cpp @@ -19,6 +19,7 @@ #include "qgslocatorwidget.h" #include "qgslocator.h" #include "qgsfilterlineedit.h" +#include "qgsmapcanvas.h" #include #include #include "qgsapplication.h" @@ -87,6 +88,11 @@ QgsLocator *QgsLocatorWidget::locator() return mLocator; } +void QgsLocatorWidget::setMapCanvas( QgsMapCanvas *canvas ) +{ + mMapCanvas = canvas; +} + void QgsLocatorWidget::search( const QString &string ) { mLineEdit->setText( string ); @@ -207,7 +213,9 @@ void QgsLocatorWidget::updateResults( const QString &text ) { mLocatorModel->clear(); if ( !text.isEmpty() ) - mLocator->fetchResults( text ); + { + mLocator->fetchResults( text, createContext() ); + } } } @@ -233,6 +241,17 @@ void QgsLocatorWidget::acceptCurrentEntry() } } +QgsLocatorContext QgsLocatorWidget::createContext() +{ + QgsLocatorContext context; + if ( mMapCanvas ) + { + context.targetExtent = mMapCanvas->mapSettings().visibleExtent(); + context.targetExtentCrs = mMapCanvas->mapSettings().destinationCrs(); + } + return context; +} + ///@cond PRIVATE // diff --git a/src/gui/locator/qgslocatorwidget.h b/src/gui/locator/qgslocatorwidget.h index 2450a96f4a1..d5b9fc85c62 100644 --- a/src/gui/locator/qgslocatorwidget.h +++ b/src/gui/locator/qgslocatorwidget.h @@ -32,6 +32,7 @@ class QgsLocator; class QgsFilterLineEdit; class QgsLocatorModel; class QgsLocatorResultsView; +class QgsMapCanvas; /** * \class QgsLocatorWidget @@ -57,6 +58,13 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget */ QgsLocator *locator(); + /** + * Sets a map \a canvas to associate with the widget. This allows the + * widget to customise the searches performed by its locator(), such + * as prioritizing results which are near the current canvas extent. + */ + void setMapCanvas( QgsMapCanvas *canvas ); + public slots: /** @@ -83,6 +91,7 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget QgsLocatorModel *mLocatorModel = nullptr; QgsFloatingWidget *mResultsContainer = nullptr; QgsLocatorResultsView *mResultsView = nullptr; + QgsMapCanvas *mMapCanvas = nullptr; QString mNextRequestedString; bool mHasQueuedRequest = false; @@ -90,6 +99,7 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget void updateResults( const QString &text ); void acceptCurrentEntry(); + QgsLocatorContext createContext(); };