Skip to content

Commit

Permalink
Allowing associating QgsLocatorWidget with a map canvas
Browse files Browse the repository at this point in the history
This allows the widget's locator to prioritise results which
are close to the current canvas extent
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent a7d590e commit ab02f2b
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
7 changes: 7 additions & 0 deletions python/gui/locator/qgslocatorwidget.sip
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/gui/locator/qgslocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
21 changes: 20 additions & 1 deletion src/gui/locator/qgslocatorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "qgslocatorwidget.h"
#include "qgslocator.h"
#include "qgsfilterlineedit.h"
#include "qgsmapcanvas.h"
#include <QLayout>
#include <QCompleter>
#include "qgsapplication.h"
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -207,7 +213,9 @@ void QgsLocatorWidget::updateResults( const QString &text )
{
mLocatorModel->clear();
if ( !text.isEmpty() )
mLocator->fetchResults( text );
{
mLocator->fetchResults( text, createContext() );
}
}
}

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

//
Expand Down
10 changes: 10 additions & 0 deletions src/gui/locator/qgslocatorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class QgsLocator;
class QgsFilterLineEdit;
class QgsLocatorModel;
class QgsLocatorResultsView;
class QgsMapCanvas;

/**
* \class QgsLocatorWidget
Expand All @@ -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:

/**
Expand All @@ -83,13 +91,15 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
QgsLocatorModel *mLocatorModel = nullptr;
QgsFloatingWidget *mResultsContainer = nullptr;
QgsLocatorResultsView *mResultsView = nullptr;
QgsMapCanvas *mMapCanvas = nullptr;

QString mNextRequestedString;
bool mHasQueuedRequest = false;
QTimer mPopupTimer;

void updateResults( const QString &text );
void acceptCurrentEntry();
QgsLocatorContext createContext();

};

Expand Down

0 comments on commit ab02f2b

Please sign in to comment.