QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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
6  email : [email protected]
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 
18 #include "qgslocatormodelbridge.h"
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 
40 void 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 
53 void 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 
78 void QgsLocatorModelBridge::addResult( const QgsLocatorResult &result )
79 {
80  mLocatorModel->addResult( result );
81  emit resultAdded();
82 }
83 
84 
85 void 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 
102 void 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 
139 QgsLocatorContext QgsLocatorModelBridge::createContext()
140 {
141  QgsLocatorContext context;
142  context.targetExtent = mCanvasExtent;
143  context.targetExtentCrs = mCanvasCrs;
144  context.transformContext = mTransformContext;
145  return context;
146 }
QgsLocatorContext::transformContext
QgsCoordinateTransformContext transformContext
Coordinate transform context, to use whenever performing coordinate transformations inside a locator.
Definition: qgslocatorcontext.h:61
QgsLocator
Handles the management of QgsLocatorFilter objects and async collection of search results from them.
Definition: qgslocator.h:58
QgsLocatorResult::filter
QgsLocatorFilter * filter
Filter from which the result was obtained.
Definition: qgslocatorfilter.h:67
crs
const QgsCoordinateReferenceSystem & crs
Definition: qgswfsgetfeature.cpp:105
QgsLocatorModel::deferredClear
void deferredClear()
Resets the model and clears all existing results after a short delay, or whenever the next result is ...
Definition: qgslocatormodel.cpp:50
QgsLocatorModel::clear
void clear()
Resets the model and clears all existing results.
Definition: qgslocatormodel.cpp:38
qgslocatormodel.h
qgslocator.h
QgsLocatorModel::ResultDataRole
@ ResultDataRole
QgsLocatorResult data.
Definition: qgslocatormodel.h:53
QgsRectangle
A rectangle specified with double values.
Definition: qgsrectangle.h:41
QgsLocator::clearPreviousResults
void clearPreviousResults()
Will call clearPreviousResults on all filters.
Definition: qgslocator.cpp:272
QgsLocatorResult
Encapsulates properties of an individual matching result found by a QgsLocatorFilter.
Definition: qgslocatorfilter.h:38
QgsLocatorContext::targetExtent
QgsRectangle targetExtent
Map extent to target in results.
Definition: qgslocatorcontext.h:47
QgsLocatorModelBridge::resultAdded
void resultAdded()
Emitted when a result is added.
QgsLocatorModelBridge::proxyModel
Q_INVOKABLE QgsLocatorProxyModel * proxyModel() const
Returns the proxy model.
Definition: qgslocatormodelbridge.cpp:129
QgsLocatorModelBridge::triggerResult
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.
Definition: qgslocatormodelbridge.cpp:40
QgsLocatorModel
An abstract list model for displaying the results of locator searches.
Definition: qgslocatormodel.h:42
QgsLocatorContext
Encapsulates the properties relating to the context of a locator search.
Definition: qgslocatorcontext.h:32
QgsLocatorContext::targetExtentCrs
QgsCoordinateReferenceSystem targetExtentCrs
Coordinate reference system for the map extent variable.
Definition: qgslocatorcontext.h:53
QgsLocatorModelBridge::invalidateResults
void invalidateResults()
This will invalidate current search results.
Definition: qgslocatormodelbridge.cpp:62
QgsLocatorModel::addResult
void addResult(const QgsLocatorResult &result)
Adds a new result to the model.
Definition: qgslocatormodel.cpp:199
qgslocatormodelbridge.h
QgsLocator::fetchResults
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:130
QgsCoordinateReferenceSystem
This class represents a coordinate reference system (CRS).
Definition: qgscoordinatereferencesystem.h:211
QgsLocatorModelBridge::updateCanvasCrs
void updateCanvasCrs(const QgsCoordinateReferenceSystem &crs)
Update the canvas CRS used to create search context.
Definition: qgslocatormodelbridge.cpp:73
QgsLocatorProxyModel
A sort proxy model for QgsLocatorModel, which automatically sorts results by precedence.
Definition: qgslocatormodel.h:191
QgsLocatorModelBridge::isRunning
bool isRunning
Definition: qgslocatormodelbridge.h:44
QgsLocatorModelBridge::updateCanvasExtent
void updateCanvasExtent(const QgsRectangle &extent)
Update the canvas extent used to create search context.
Definition: qgslocatormodelbridge.cpp:68
QgsLocatorModelBridge::hasQueueRequested
bool hasQueueRequested() const
Returns true if some text to be search is pending in the queue.
Definition: qgslocatormodelbridge.cpp:134
QgsLocatorModelBridge::QgsLocatorModelBridge
QgsLocatorModelBridge(QObject *parent=nullptr)
Constructor of QgsLocatorModelBridge.
Definition: qgslocatormodelbridge.cpp:23
QgsLocatorModelBridge::performSearch
Q_INVOKABLE void performSearch(const QString &text)
Perform a search.
Definition: qgslocatormodelbridge.cpp:102
QgsLocatorModelBridge::resultsCleared
void resultsCleared()
Emitted when the results are cleared.
QgsLocator::isRunning
bool isRunning() const
Returns true if a query is currently being executed by the locator.
Definition: qgslocator.cpp:267
QgsLocator::cancelWithoutBlocking
void cancelWithoutBlocking()
Triggers cancellation of any current running query without blocking.
Definition: qgslocator.cpp:261
QgsLocatorModelBridge::locator
QgsLocator * locator() const
Returns the locator.
Definition: qgslocatormodelbridge.cpp:124
QgsLocatorFilter::triggerResult
virtual void triggerResult(const QgsLocatorResult &result)=0
Triggers a filter result from this filter.
QgsLocator::finished
void finished()
Emitted when locator has finished a query, either as a result of successful completion or early cance...
QgsLocator::foundResult
void foundResult(const QgsLocatorResult &result)
Emitted whenever a filter encounters a matching result after the fetchResults() method is called.
QgsLocatorModelBridge::isRunningChanged
void isRunningChanged()
Emitted when the running status changes.
QgsLocatorFilter::triggerResultFromAction
virtual void triggerResultFromAction(const QgsLocatorResult &result, const int actionId)
Triggers a filter result from this filter for an entry in the context menu.
Definition: qgslocatorfilter.cpp:36