QGIS API Documentation 3.29.0-Master (19d7edcfed)
qgslocatormodelbridge.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgslocatormodelbridge.cpp
3 ------------------
4 begin : November 2018
5 copyright : (C) 2018 by Denis Rouzaud
7 ***************************************************************************/
8
9/***************************************************************************
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 ***************************************************************************/
17
19#include "qgslocator.h"
20#include "qgslocatormodel.h"
21
22
24 : QObject( parent )
25 , mLocator( new QgsLocator( this ) )
26 , mLocatorModel( new QgsLocatorModel( this ) )
27{
28 mProxyModel = new QgsLocatorProxyModel( mLocatorModel );
29 mProxyModel->setSourceModel( mLocatorModel );
30
31 connect( mLocator, &QgsLocator::foundResult, this, &QgsLocatorModelBridge::addResult );
32 connect( mLocator, &QgsLocator::finished, this, &QgsLocatorModelBridge::searchFinished );
33}
34
36{
37 return mIsRunning;
38}
39
40void QgsLocatorModelBridge::triggerResult( const QModelIndex &index, const int actionId )
41{
42 mLocator->clearPreviousResults();
43 QgsLocatorResult result = mProxyModel->data( index, QgsLocatorModel::ResultDataRole ).value< QgsLocatorResult >();
44 if ( result.filter )
45 {
46 if ( actionId >= 0 )
47 result.filter->triggerResultFromAction( result, actionId );
48 else
49 result.filter->triggerResult( result );
50 }
51}
52
53void QgsLocatorModelBridge::setIsRunning( bool isRunning )
54{
55 if ( mIsRunning == isRunning )
56 return;
57
58 mIsRunning = isRunning;
59 emit isRunningChanged();
60}
61
63{
64 mLocator->cancelWithoutBlocking();
65 mLocatorModel->clear();
66}
67
69{
70 mCanvasExtent = extent;
71}
72
74{
75 mCanvasCrs = crs;
76}
77
78void QgsLocatorModelBridge::addResult( const QgsLocatorResult &result )
79{
80 mLocatorModel->addResult( result );
81 emit resultAdded();
82}
83
84
85void QgsLocatorModelBridge::searchFinished()
86{
87 if ( mHasQueuedRequest )
88 {
89 // a queued request was waiting for this - run the queued search now
90 const QString nextSearch = mNextRequestedString;
91 mNextRequestedString.clear();
92 mHasQueuedRequest = false;
93 performSearch( nextSearch );
94 }
95 else
96 {
97 if ( !mLocator->isRunning() )
98 setIsRunning( false );
99 }
100}
101
102void QgsLocatorModelBridge::performSearch( const QString &text )
103{
104 setIsRunning( true );
105
106 if ( mLocator->isRunning() )
107 {
108 // can't do anything while a query is running, and can't block
109 // here waiting for the current query to cancel
110 // so we queue up this string until cancel has happened
111 mLocator->cancelWithoutBlocking();
112 mNextRequestedString = text;
113 mHasQueuedRequest = true;
114 return;
115 }
116 else
117 {
118 emit resultsCleared();
119 mLocatorModel->deferredClear();
120 mLocator->fetchResults( text, createContext() );
121 }
122}
123
125{
126 return mLocator;
127}
128
130{
131 return mProxyModel;
132}
133
135{
136 return mHasQueuedRequest;
137}
138
139QgsLocatorContext QgsLocatorModelBridge::createContext()
140{
141 QgsLocatorContext context;
142 context.targetExtent = mCanvasExtent;
143 context.targetExtentCrs = mCanvasCrs;
144 context.transformContext = mTransformContext;
145 return context;
146}
This class represents a coordinate reference system (CRS).
Encapsulates the properties relating to the context of a locator search.
QgsCoordinateTransformContext transformContext
Coordinate transform context, to use whenever performing coordinate transformations inside a locator.
QgsCoordinateReferenceSystem targetExtentCrs
Coordinate reference system for the map extent variable.
QgsRectangle targetExtent
Map extent to target in results.
virtual void triggerResultFromAction(const QgsLocatorResult &result, const int actionId)
Triggers a filter result from this filter for an entry in the context menu.
virtual void triggerResult(const QgsLocatorResult &result)=0
Triggers a filter result from this filter.
QgsLocatorModelBridge(QObject *parent=nullptr)
Constructor of QgsLocatorModelBridge.
Q_INVOKABLE QgsLocatorProxyModel * proxyModel() const
Returns the proxy model.
void isRunningChanged()
Emitted when the running status changes.
void resultAdded()
Emitted when a result is added.
void triggerResult(const QModelIndex &index, const int actionId=-1)
Triggers the result at given index and with optional actionId if an additional action was triggered.
QgsLocator * locator() const
Returns the locator.
bool hasQueueRequested() const
Returns true if some text to be search is pending in the queue.
Q_INVOKABLE void performSearch(const QString &text)
Perform a search.
void resultsCleared()
Emitted when the results are cleared.
void updateCanvasCrs(const QgsCoordinateReferenceSystem &crs)
Update the canvas CRS used to create search context.
void updateCanvasExtent(const QgsRectangle &extent)
Update the canvas extent used to create search context.
void invalidateResults()
This will invalidate current search results.
An abstract list model for displaying the results of locator searches.
void deferredClear()
Resets the model and clears all existing results after a short delay, or whenever the next result is ...
void addResult(const QgsLocatorResult &result)
Adds a new result to the model.
void clear()
Resets the model and clears all existing results.
@ ResultDataRole
QgsLocatorResult data.
A sort proxy model for QgsLocatorModel, which automatically sorts results by precedence.
Encapsulates properties of an individual matching result found by a QgsLocatorFilter.
QgsLocatorFilter * filter
Filter from which the result was obtained.
Handles the management of QgsLocatorFilter objects and async collection of search results from them.
Definition: qgslocator.h:59
void finished()
Emitted when locator has finished a query, either as a result of successful completion or early cance...
void foundResult(const QgsLocatorResult &result)
Emitted whenever a filter encounters a matching result after the fetchResults() method is called.
void fetchResults(const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback=nullptr)
Triggers the background fetching of filter results for a specified search string.
Definition: qgslocator.cpp:136
void clearPreviousResults()
Will call clearPreviousResults on all filters.
Definition: qgslocator.cpp:278
bool isRunning() const
Returns true if a query is currently being executed by the locator.
Definition: qgslocator.cpp:273
void cancelWithoutBlocking()
Triggers cancellation of any current running query without blocking.
Definition: qgslocator.cpp:267
A rectangle specified with double values.
Definition: qgsrectangle.h:42
const QgsCoordinateReferenceSystem & crs