QGIS API Documentation  3.8.0-Zanzibar (11aff65)
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  if ( mLocator->isRunning() )
106  {
107  // can't do anything while a query is running, and can't block
108  // here waiting for the current query to cancel
109  // so we queue up this string until cancel has happened
110  mLocator->cancelWithoutBlocking();
111  mNextRequestedString = text;
112  mHasQueuedRequest = true;
113  return;
114  }
115  else
116  {
117  emit resultsCleared();
118  mLocatorModel->deferredClear();
119  mLocator->fetchResults( text, createContext() );
120  }
121 }
122 
124 {
125  return mLocator;
126 }
127 
129 {
130  return mProxyModel;
131 }
132 
134 {
135  return mHasQueuedRequest;
136 }
137 
138 QgsLocatorContext QgsLocatorModelBridge::createContext()
139 {
140  QgsLocatorContext context;
141  context.targetExtent = mCanvasExtent;
142  context.targetExtentCrs = mCanvasCrs;
143  return context;
144 }
void updateCanvasCrs(const QgsCoordinateReferenceSystem &crs)
Update the canvas CRS used to create search context.
A rectangle specified with double values.
Definition: qgsrectangle.h:41
void cancelWithoutBlocking()
Triggers cancellation of any current running query without blocking.
Definition: qgslocator.cpp:231
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
A sort proxy model for QgsLocatorModel, which automatically sorts results by precedence.
Q_INVOKABLE void performSearch(const QString &text)
Perform a search.
bool isRunning() const
Returns true if a query is currently being executed by the locator.
Definition: qgslocator.cpp:237
bool isRunning() const
Returns true if the a search is currently running.
const QgsCoordinateReferenceSystem & crs
void updateCanvasExtent(const QgsRectangle &extent)
Update the canvas extent used to create search context.
void finished()
Emitted when locator has finished a query, either as a result of successful completion or early cance...
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.
void isRunningChanged()
Emitted when the running status changes.
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...
Encapsulates the properties relating to the context of a locator search.
Encapsulates properties of an individual matching result found by a QgsLocatorFilter.
Handles the management of QgsLocatorFilter objects and async collection of search results from them...
Definition: qgslocator.h:57
void foundResult(const QgsLocatorResult &result)
Emitted whenever a filter encounters a matching result after the fetchResults() method is called...
An abstract list model for displaying the results of locator searches.
QgsCoordinateReferenceSystem targetExtentCrs
Coordinate reference system for the map extent variable.
void resultsCleared()
Emitted when the results are cleared.
void deferredClear()
Resets the model and clears all existing results after a short delay, or whenever the next result is ...
QgsLocatorFilter * filter
Filter from which the result was obtained.
Q_INVOKABLE QgsLocatorProxyModel * proxyModel() const
Returns the proxy model.
void invalidateResults()
This will invalidate current search results.
QgsLocatorResult data.
This class represents a coordinate reference system (CRS).
bool hasQueueRequested() const
Returns true if some text to be search is pending in the queue.
void addResult(const QgsLocatorResult &result)
Adds a new result to the model.
void clear()
Resets the model and clears all existing results.
void clearPreviousResults()
Will call clearPreviousResults on all filters.
Definition: qgslocator.cpp:242
QgsLocator * locator() const
Returns the locator.
void resultAdded()
Emitted when a result is added.