QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
18
19#include <QItemSelection>
20
22#include "moc_qgslocatormodelbridge.cpp"
23#include "qgslocator.h"
24#include "qgslocatormodel.h"
25
26
28 : QObject( parent )
29 , mLocator( new QgsLocator( this ) )
30 , mLocatorModel( new QgsLocatorModel( this ) )
31{
32 mProxyModel = new QgsLocatorProxyModel( mLocatorModel );
33 mProxyModel->setSourceModel( mLocatorModel );
34
35 connect( mLocator, &QgsLocator::foundResult, this, &QgsLocatorModelBridge::addResult );
36 connect( mLocator, &QgsLocator::finished, this, &QgsLocatorModelBridge::searchFinished );
37}
38
40{
41 return mIsRunning;
42}
43
44void QgsLocatorModelBridge::triggerResult( const QModelIndex &index, const int actionId )
45{
46 QgsLocatorResult result = mProxyModel->data( index, static_cast< int >( QgsLocatorModel::CustomRole::ResultData ) ).value< QgsLocatorResult >();
47 mLocator->clearPreviousResults();
48 if ( result.filter )
49 {
50 if ( actionId >= 0 )
51 result.filter->triggerResultFromAction( result, actionId );
52 else
53 result.filter->triggerResult( result );
54 }
55}
56
57void QgsLocatorModelBridge::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
58{
59 if ( deselected.count() > 0 && deselected.indexes().at( 0 ).isValid() )
60 {
61 const QModelIndex deselectedIndex = deselected.indexes().at( 0 );
62 QgsLocatorResult result = mProxyModel->data( deselectedIndex, static_cast< int >( QgsLocatorModel::CustomRole::ResultData ) ).value< QgsLocatorResult >();
63 if ( result.filter )
64 {
65 result.filter->resultDeselected( result );
66 }
67 }
68
69 if ( selected.count() > 0 && selected.indexes().at( 0 ).isValid() )
70 {
71 const QModelIndex selectedIndex = selected.indexes().at( 0 );
72 QgsLocatorResult result = mProxyModel->data( selectedIndex, static_cast< int >( QgsLocatorModel::CustomRole::ResultData ) ).value< QgsLocatorResult >();
73 if ( result.filter )
74 {
75 result.filter->resultSelected( result );
76 }
77 }
78}
79
80void QgsLocatorModelBridge::setIsRunning( bool isRunning )
81{
82 if ( mIsRunning == isRunning )
83 return;
84
85 mIsRunning = isRunning;
86 emit isRunningChanged();
87}
88
90{
91 mLocator->cancelWithoutBlocking();
92 mLocatorModel->clear();
93}
94
96{
97 mCanvasExtent = extent;
98}
99
104
105void QgsLocatorModelBridge::addResult( const QgsLocatorResult &result )
106{
107 mLocatorModel->addResult( result );
108 emit resultAdded();
109}
110
111
112void QgsLocatorModelBridge::searchFinished()
113{
114 if ( mHasQueuedRequest )
115 {
116 // a queued request was waiting for this - run the queued search now
117 const QString nextSearch = mNextRequestedString;
118 mNextRequestedString.clear();
119 mHasQueuedRequest = false;
120 performSearch( nextSearch );
121 }
122 else
123 {
124 if ( !mLocator->isRunning() )
125 setIsRunning( false );
126 }
127}
128
129void QgsLocatorModelBridge::performSearch( const QString &text )
130{
131 setIsRunning( true );
132
133 if ( mLocator->isRunning() )
134 {
135 // can't do anything while a query is running, and can't block
136 // here waiting for the current query to cancel
137 // so we queue up this string until cancel has happened
138 mLocator->cancelWithoutBlocking();
139 mNextRequestedString = text;
140 mHasQueuedRequest = true;
141 return;
142 }
143 else
144 {
145 emit resultsCleared();
146 mLocatorModel->deferredClear();
147 mLocator->fetchResults( text, createContext() );
148 }
149}
150
152{
153 return mLocator;
154}
155
157{
158 return mProxyModel;
159}
160
162{
163 return mHasQueuedRequest;
164}
165
166QgsLocatorContext QgsLocatorModelBridge::createContext()
167{
168 QgsLocatorContext context;
169 context.targetExtent = mCanvasExtent;
170 context.targetExtentCrs = mCanvasCrs;
171 context.transformContext = mTransformContext;
172 return context;
173}
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.
virtual void resultDeselected(const QgsLocatorResult &result)
This is called when a result is deselected.
virtual void resultSelected(const QgsLocatorResult &result)
This is called when the result is selected by the user.
QgsLocatorModelBridge(QObject *parent=nullptr)
Constructor of QgsLocatorModelBridge.
Q_INVOKABLE QgsLocatorProxyModel * proxyModel() const
Returns the proxy model.
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
This will call filters implementation of selection/deselection of results.
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 ...
@ ResultData
QgsLocatorResult data.
void addResult(const QgsLocatorResult &result)
Adds a new result to the model.
void clear()
Resets the model and clears all existing results.
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:61
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.
void clearPreviousResults()
Will call clearPreviousResults on all filters.
bool isRunning() const
Returns true if a query is currently being executed by the locator.
void cancelWithoutBlocking()
Triggers cancellation of any current running query without blocking.
A rectangle specified with double values.
const QgsCoordinateReferenceSystem & crs