Skip to content

Commit c48a706

Browse files
committed
use int as IDs to recognize context menu entries
1 parent 91736fd commit c48a706

9 files changed

+45
-37
lines changed

python/core/auto_generated/locator/qgslocatorfilter.sip.in

+4-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Constructor for QgsLocatorResult.
4848

4949
QString group;
5050

51-
QList<QAction *> contextMenuActions;
51+
QMap<int, QAction *> contextMenuActions = QMap<int, QAction *>();
5252

5353
};
5454

@@ -177,11 +177,10 @@ E.g. a file search filter would open file associated with the triggered
177177
result.
178178
%End
179179

180-
virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action );
180+
virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id );
181181
%Docstring
182-
Triggers a filter ``result`` from this filter for a given action.
183-
Actions are specified in the result given by this filter and shown
184-
as context menu entries.
182+
Triggers a filter ``result`` from this filter for an entry in the context menu.
183+
The entry is identified by its \id as specified in the result of this filter.
185184

186185
.. seealso:: :py:func:`triggerResult`
187186

python/core/auto_generated/locator/qgslocatormodelbridge.sip.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ Returns true if some text to be search is pending in the queue
5757
Returns true if the a search is currently running
5858
%End
5959

60-
void triggerResult( const QModelIndex &index, const QAction *action = 0 );
60+
void triggerResult( const QModelIndex &index, const int id = -1 );
6161
%Docstring
6262
Triggers the result at given ``index`` and with optional ``action`` if context menu entry was triggered
6363
%End

src/app/locator/qgsinbuiltlocatorfilters.cpp

+10-11
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con
402402
result.icon = preparedLayer.layerIcon;
403403
result.score = static_cast< double >( string.length() ) / result.displayString.size();
404404

405-
result.contextMenuActions << new QAction( tr( "Open form" ) );
405+
result.contextMenuActions.insert( OpenForm, new QAction( tr( "Open form" ) ) );
406406
emit resultFetched( result );
407407

408408
foundInCurrentLayer++;
@@ -417,28 +417,23 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con
417417

418418
void QgsAllLayersFeaturesLocatorFilter::triggerResult( const QgsLocatorResult &result )
419419
{
420-
triggerResultFromContextMenu( result, nullptr );
420+
triggerResultFromContextMenu( result, NoEntry );
421421
}
422422

423-
void QgsAllLayersFeaturesLocatorFilter::triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action )
423+
void QgsAllLayersFeaturesLocatorFilter::triggerResultFromContextMenu( const QgsLocatorResult &result, const int id )
424424
{
425425
QVariantList dataList = result.userData.toList();
426-
QgsFeatureId id = dataList.at( 0 ).toLongLong();
426+
QgsFeatureId fid = dataList.at( 0 ).toLongLong();
427427
QString layerId = dataList.at( 1 ).toString();
428428
QgsVectorLayer *layer = qobject_cast< QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerId ) );
429429
if ( !layer )
430430
return;
431431

432-
if ( !action )
433-
{
434-
QgisApp::instance()->mapCanvas()->zoomToFeatureIds( layer, QgsFeatureIds() << id );
435-
}
436-
else
432+
if ( id == OpenForm )
437433
{
438-
// no need to check for which action, since the filter shows only one
439434
QgsFeature f;
440435
QgsFeatureRequest request;
441-
request.setFilterFid( id );
436+
request.setFilterFid( fid );
442437
bool fetched = layer->getFeatures( request ).nextFeature( f );
443438
if ( !fetched )
444439
return;
@@ -452,6 +447,10 @@ void QgsAllLayersFeaturesLocatorFilter::triggerResultFromContextMenu( const QgsL
452447
action.viewFeatureForm();
453448
}
454449
}
450+
else
451+
{
452+
QgisApp::instance()->mapCanvas()->zoomToFeatureIds( layer, QgsFeatureIds() << fid );
453+
}
455454
}
456455

457456
//

src/app/locator/qgsinbuiltlocatorfilters.h

+8-2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
119119
Q_OBJECT
120120

121121
public:
122+
enum ContextMenuEntry
123+
{
124+
NoEntry,
125+
OpenForm
126+
};
127+
122128
struct PreparedLayer
123129
{
124130
public:
@@ -128,7 +134,7 @@ class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
128134
QString layerName;
129135
QString layerId;
130136
QIcon layerIcon;
131-
} ;
137+
};
132138

133139
QgsAllLayersFeaturesLocatorFilter( QObject *parent = nullptr );
134140
QgsAllLayersFeaturesLocatorFilter *clone() const override;
@@ -140,7 +146,7 @@ class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
140146
void prepare( const QString &string, const QgsLocatorContext &context ) override;
141147
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
142148
void triggerResult( const QgsLocatorResult &result ) override;
143-
void triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action ) override;
149+
void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id ) override;
144150

145151
private:
146152
int mMaxResultsPerLayer = 6;

src/core/locator/qgslocatorfilter.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ QgsLocatorFilter::Flags QgsLocatorFilter::flags() const
3333
return nullptr;
3434
}
3535

36-
void QgsLocatorFilter::triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action )
36+
void QgsLocatorFilter::triggerResultFromContextMenu( const QgsLocatorResult &result, const int id )
3737
{
3838
Q_UNUSED( result );
39-
Q_UNUSED( action );
39+
Q_UNUSED( id );
4040
}
4141

4242
bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &search )

src/core/locator/qgslocatorfilter.h

+11-7
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,19 @@ class CORE_EXPORT QgsLocatorResult
8989
* If left as empty string, this means that results are all shown without being grouped.
9090
* If a group is given, the results will be grouped by \a group under a header.
9191
* \note This should be translated.
92-
* \since 3.2
92+
* \since QGIS 3.2
9393
*/
9494
QString group = QString();
9595

9696
/**
97-
* Actions to be used in a context menu for the result
97+
* Actions to be used in a context menu for the result.
98+
* The key of the map is populated with IDs used to recognized
99+
* entry when the result is triggered. The IDs should be 0 or greater
100+
* otherwise, the result will be triggered normally.
101+
* Entries in the context menu will be ordered by IDs.
102+
* \since QGIS 3.6
98103
*/
99-
QList<QAction *> contextMenuActions = QList<QAction *>();
104+
QMap<int, QAction *> contextMenuActions = QMap<int, QAction *>();
100105

101106
};
102107

@@ -217,13 +222,12 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
217222
virtual void triggerResult( const QgsLocatorResult &result ) = 0;
218223

219224
/**
220-
* Triggers a filter \a result from this filter for a given action.
221-
* Actions are specified in the result given by this filter and shown
222-
* as context menu entries.
225+
* Triggers a filter \a result from this filter for an entry in the context menu.
226+
* The entry is identified by its \id as specified in the result of this filter.
223227
* \see triggerResult()
224228
* \since QGIS 3.6
225229
*/
226-
virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action );
230+
virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id );
227231

228232
/**
229233
* This method will be called on main thread on the original filter (not a clone)

src/core/locator/qgslocatormodelbridge.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ bool QgsLocatorModelBridge::isRunning() const
3737
return mIsRunning;
3838
}
3939

40-
void QgsLocatorModelBridge::triggerResult( const QModelIndex &index, const QAction *action )
40+
void QgsLocatorModelBridge::triggerResult( const QModelIndex &index, const int id )
4141
{
4242
mLocator->clearPreviousResults();
4343
QgsLocatorResult result = mProxyModel->data( index, QgsLocatorModel::ResultDataRole ).value< QgsLocatorResult >();
4444
if ( result.filter )
4545
{
46-
if ( action )
47-
result.filter->triggerResultFromContextMenu( result, action );
46+
if ( id >= 0 )
47+
result.filter->triggerResultFromContextMenu( result, id );
4848
else
4949
result.filter->triggerResult( result );
5050
}

src/core/locator/qgslocatormodelbridge.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class CORE_EXPORT QgsLocatorModelBridge : public QObject
6464
bool isRunning() const;
6565

6666
//! Triggers the result at given \a index and with optional \a action if context menu entry was triggered
67-
void triggerResult( const QModelIndex &index, const QAction *action = nullptr );
67+
void triggerResult( const QModelIndex &index, const int id = -1 );
6868

6969
signals:
7070
//! Emitted when a result is added

src/gui/locator/qgslocatorwidget.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,14 @@ void QgsLocatorWidget::showContextMenu( const QPoint &point )
181181
if ( !index.isValid() )
182182
return;
183183

184-
const QList<QAction *> actions = mResultsView->model()->data( index, QgsLocatorModel::ResultContextMenuActionsRole ).value<QList<QAction *>>();
185-
for ( const QAction *action : actions )
184+
const QMap<int, QAction *> actions = mResultsView->model()->data( index, QgsLocatorModel::ResultContextMenuActionsRole ).value<QMap<int, QAction *>>();
185+
QMap<int, QAction *>::const_iterator it = actions.constBegin();
186+
for ( ; it != actions.constEnd(); ++it )
186187
{
187-
connect( action, &QAction::triggered, this, [ = ]() {mModelBridge->triggerResult( index, action );} );
188+
connect( it.value(), &QAction::triggered, this, [ = ]() {mModelBridge->triggerResult( index, it.key() );} );
188189
}
189-
190190
QMenu *contextMenu = new QMenu( mResultsView );
191-
contextMenu->addActions( actions );
191+
contextMenu->addActions( actions.values() );
192192
contextMenu->exec( mResultsView->viewport()->mapToGlobal( point ) );
193193
}
194194

0 commit comments

Comments
 (0)