Skip to content

Commit ab02f2b

Browse files
committedMay 17, 2017
Allowing associating QgsLocatorWidget with a map canvas
This allows the widget's locator to prioritise results which are close to the current canvas extent
1 parent a7d590e commit ab02f2b

File tree

5 files changed

+40
-2
lines changed

5 files changed

+40
-2
lines changed
 

‎python/gui/locator/qgslocatorwidget.sip

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ class QgsLocatorWidget : QWidget
3535
:rtype: QgsLocator
3636
%End
3737

38+
void setMapCanvas( QgsMapCanvas *canvas );
39+
%Docstring
40+
Sets a map ``canvas`` to associate with the widget. This allows the
41+
widget to customise the searches performed by its locator(), such
42+
as prioritizing results which are near the current canvas extent.
43+
%End
44+
3845
public slots:
3946

4047
void search( const QString &string );

‎src/app/qgisapp.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -966,6 +966,8 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipVersionCh
966966
// set graphical credential requester
967967
new QgsCredentialDialog( this );
968968

969+
mLocatorWidget->setMapCanvas( mMapCanvas );
970+
969971
qApp->processEvents();
970972

971973
// load providers

‎src/gui/locator/qgslocator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void QgsLocator::fetchResults( const QString &string, const QgsLocatorContext &c
7272
}
7373
mFeedback = feedback;
7474

75-
auto gatherFilterResults = [string, feedback]( QgsLocatorFilter * filter )
75+
auto gatherFilterResults = [string, context, feedback]( QgsLocatorFilter * filter )
7676
{
7777
if ( !feedback->isCanceled() )
7878
filter->fetchResults( string, context, feedback );

‎src/gui/locator/qgslocatorwidget.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "qgslocatorwidget.h"
2020
#include "qgslocator.h"
2121
#include "qgsfilterlineedit.h"
22+
#include "qgsmapcanvas.h"
2223
#include <QLayout>
2324
#include <QCompleter>
2425
#include "qgsapplication.h"
@@ -87,6 +88,11 @@ QgsLocator *QgsLocatorWidget::locator()
8788
return mLocator;
8889
}
8990

91+
void QgsLocatorWidget::setMapCanvas( QgsMapCanvas *canvas )
92+
{
93+
mMapCanvas = canvas;
94+
}
95+
9096
void QgsLocatorWidget::search( const QString &string )
9197
{
9298
mLineEdit->setText( string );
@@ -207,7 +213,9 @@ void QgsLocatorWidget::updateResults( const QString &text )
207213
{
208214
mLocatorModel->clear();
209215
if ( !text.isEmpty() )
210-
mLocator->fetchResults( text );
216+
{
217+
mLocator->fetchResults( text, createContext() );
218+
}
211219
}
212220
}
213221

@@ -233,6 +241,17 @@ void QgsLocatorWidget::acceptCurrentEntry()
233241
}
234242
}
235243

244+
QgsLocatorContext QgsLocatorWidget::createContext()
245+
{
246+
QgsLocatorContext context;
247+
if ( mMapCanvas )
248+
{
249+
context.targetExtent = mMapCanvas->mapSettings().visibleExtent();
250+
context.targetExtentCrs = mMapCanvas->mapSettings().destinationCrs();
251+
}
252+
return context;
253+
}
254+
236255
///@cond PRIVATE
237256

238257
//

‎src/gui/locator/qgslocatorwidget.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class QgsLocator;
3232
class QgsFilterLineEdit;
3333
class QgsLocatorModel;
3434
class QgsLocatorResultsView;
35+
class QgsMapCanvas;
3536

3637
/**
3738
* \class QgsLocatorWidget
@@ -57,6 +58,13 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
5758
*/
5859
QgsLocator *locator();
5960

61+
/**
62+
* Sets a map \a canvas to associate with the widget. This allows the
63+
* widget to customise the searches performed by its locator(), such
64+
* as prioritizing results which are near the current canvas extent.
65+
*/
66+
void setMapCanvas( QgsMapCanvas *canvas );
67+
6068
public slots:
6169

6270
/**
@@ -83,13 +91,15 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
8391
QgsLocatorModel *mLocatorModel = nullptr;
8492
QgsFloatingWidget *mResultsContainer = nullptr;
8593
QgsLocatorResultsView *mResultsView = nullptr;
94+
QgsMapCanvas *mMapCanvas = nullptr;
8695

8796
QString mNextRequestedString;
8897
bool mHasQueuedRequest = false;
8998
QTimer mPopupTimer;
9099

91100
void updateResults( const QString &text );
92101
void acceptCurrentEntry();
102+
QgsLocatorContext createContext();
93103

94104
};
95105

0 commit comments

Comments
 (0)
Please sign in to comment.