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