QGIS API Documentation 3.39.0-Master (9ea1ddbe645)
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 "qgslocator.h"
23#include "qgslocatormodel.h"
24
25
27 : QObject( parent )
28 , mLocator( new QgsLocator( this ) )
29 , mLocatorModel( new QgsLocatorModel( this ) )
30{
31 mProxyModel = new QgsLocatorProxyModel( mLocatorModel );
32 mProxyModel->setSourceModel( mLocatorModel );
33
34 connect( mLocator, &QgsLocator::foundResult, this, &QgsLocatorModelBridge::addResult );
35 connect( mLocator, &QgsLocator::finished, this, &QgsLocatorModelBridge::searchFinished );
36}
37
39{
40 return mIsRunning;
41}
42
43void QgsLocatorModelBridge::triggerResult( const QModelIndex &index, const int actionId )
44{
45 mLocator->clearPreviousResults();
46 QgsLocatorResult result = mProxyModel->data( index, static_cast< int >( QgsLocatorModel::CustomRole::ResultData ) ).value< QgsLocatorResult >();
47 if ( result.filter )
48 {
49 if ( actionId >= 0 )
50 result.filter->triggerResultFromAction( result, actionId );
51 else
52 result.filter->triggerResult( result );
53 }
54}
55
56void QgsLocatorModelBridge::selectionChanged( const QItemSelection &selected, const QItemSelection &deselected )
57{
58 if ( deselected.count() > 0 && deselected.indexes().at( 0 ).isValid() )
59 {
60 const QModelIndex deselectedIndex = deselected.indexes().at( 0 );
61 QgsLocatorResult result = mProxyModel->data( deselectedIndex, static_cast< int >( QgsLocatorModel::CustomRole::ResultData ) ).value< QgsLocatorResult >();
62 if ( result.filter )
63 {
64 result.filter->resultDeselected( result );
65 }
66 }
67
68 if ( selected.count() > 0 && selected.indexes().at( 0 ).isValid() )
69 {
70 const QModelIndex selectedIndex = selected.indexes().at( 0 );
71 QgsLocatorResult result = mProxyModel->data( selectedIndex, static_cast< int >( QgsLocatorModel::CustomRole::ResultData ) ).value< QgsLocatorResult >();
72 if ( result.filter )
73 {
74 result.filter->resultSelected( result );
75 }
76 }
77}
78
79void QgsLocatorModelBridge::setIsRunning( bool isRunning )
80{
81 if ( mIsRunning == isRunning )
82 return;
83
84 mIsRunning = isRunning;
85 emit isRunningChanged();
86}
87
89{
90 mLocator->cancelWithoutBlocking();
91 mLocatorModel->clear();
92}
93
95{
96 mCanvasExtent = extent;
97}
98
103
104void QgsLocatorModelBridge::addResult( const QgsLocatorResult &result )
105{
106 mLocatorModel->addResult( result );
107 emit resultAdded();
108}
109
110
111void QgsLocatorModelBridge::searchFinished()
112{
113 if ( mHasQueuedRequest )
114 {
115 // a queued request was waiting for this - run the queued search now
116 const QString nextSearch = mNextRequestedString;
117 mNextRequestedString.clear();
118 mHasQueuedRequest = false;
119 performSearch( nextSearch );
120 }
121 else
122 {
123 if ( !mLocator->isRunning() )
124 setIsRunning( false );
125 }
126}
127
128void QgsLocatorModelBridge::performSearch( const QString &text )
129{
130 setIsRunning( true );
131
132 if ( mLocator->isRunning() )
133 {
134 // can't do anything while a query is running, and can't block
135 // here waiting for the current query to cancel
136 // so we queue up this string until cancel has happened
137 mLocator->cancelWithoutBlocking();
138 mNextRequestedString = text;
139 mHasQueuedRequest = true;
140 return;
141 }
142 else
143 {
144 emit resultsCleared();
145 mLocatorModel->deferredClear();
146 mLocator->fetchResults( text, createContext() );
147 }
148}
149
151{
152 return mLocator;
153}
154
156{
157 return mProxyModel;
158}
159
161{
162 return mHasQueuedRequest;
163}
164
165QgsLocatorContext QgsLocatorModelBridge::createContext()
166{
167 QgsLocatorContext context;
168 context.targetExtent = mCanvasExtent;
169 context.targetExtentCrs = mCanvasCrs;
170 context.transformContext = mTransformContext;
171 return context;
172}
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