QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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  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  return context;
145 }
QgsLocator
Definition: qgslocator.h:57
QgsLocatorResult::filter
QgsLocatorFilter * filter
Filter from which the result was obtained.
Definition: qgslocatorfilter.h:60
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
Definition: qgsrectangle.h:41
QgsLocator::clearPreviousResults
void clearPreviousResults()
Will call clearPreviousResults on all filters.
Definition: qgslocator.cpp:242
QgsLocatorResult
Definition: qgslocatorfilter.h:39
QgsLocatorContext::targetExtent
QgsRectangle targetExtent
Map extent to target in results.
Definition: qgslocatorcontext.h:46
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
Definition: qgslocatormodel.h:42
QgsLocatorContext
Definition: qgslocatorcontext.h:31
QgsLocatorContext::targetExtentCrs
QgsCoordinateReferenceSystem targetExtentCrs
Coordinate reference system for the map extent variable.
Definition: qgslocatorcontext.h:52
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:124
QgsCoordinateReferenceSystem
Definition: qgscoordinatereferencesystem.h:206
QgsLocatorModelBridge::updateCanvasCrs
void updateCanvasCrs(const QgsCoordinateReferenceSystem &crs)
Update the canvas CRS used to create search context.
Definition: qgslocatormodelbridge.cpp:73
QgsLocatorProxyModel
Definition: qgslocatormodel.h:191
QgsLocatorModelBridge::isRunning
bool isRunning
Definition: qgslocatormodelbridge.h:45
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:237
QgsLocator::cancelWithoutBlocking
void cancelWithoutBlocking()
Triggers cancellation of any current running query without blocking.
Definition: qgslocator.cpp:231
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