Allowing associating QgsLocatorWidget with a map canvas

This allows the widget's locator to prioritise results which
are close to the current canvas extent
This commit is contained in:
Nyall Dawson 2017-05-08 19:55:57 +10:00
parent a7d590e041
commit ab02f2b79c
5 changed files with 40 additions and 2 deletions

View File

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

View File

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

View File

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

View File

@ -19,6 +19,7 @@
#include "qgslocatorwidget.h"
#include "qgslocator.h"
#include "qgsfilterlineedit.h"
#include "qgsmapcanvas.h"
#include <QLayout>
#include <QCompleter>
#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
//

View File

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