Skip to content

Commit d272f3c

Browse files
committedNov 29, 2018
do not use qaction in the result, build context menu in the widget
1 parent c48a706 commit d272f3c

File tree

10 files changed

+66
-31
lines changed

10 files changed

+66
-31
lines changed
 

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,23 @@ Constructor for QgsLocatorResult.
4848

4949
QString group;
5050

51-
QMap<int, QAction *> contextMenuActions = QMap<int, QAction *>();
51+
struct ResultAction
52+
{
53+
public:
54+
ResultAction();
55+
%Docstring
56+
Constructor for ResultAction
57+
%End
58+
ResultAction( int id, QString text );
59+
int id;
60+
QString text;
61+
};
5262

63+
QList<ResultAction> actions;
5364
};
5465

66+
67+
5568
class QgsLocatorFilter : QObject
5669
{
5770
%Docstring
@@ -177,10 +190,10 @@ E.g. a file search filter would open file associated with the triggered
177190
result.
178191
%End
179192

180-
virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id );
193+
virtual void triggerResultFromAction( const QgsLocatorResult &result, const int actionId );
181194
%Docstring
182195
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.
196+
The entry is identified by its ``actionId`` as specified in the result of this filter.
184197

185198
.. seealso:: :py:func:`triggerResult`
186199

‎python/core/auto_generated/locator/qgslocatormodel.sip.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ in order to ensure correct sorting of results by priority and match level.
3636
ResultScoreRole,
3737
ResultFilterNameRole,
3838
ResultFilterGroupSortingRole,
39-
ResultContextMenuActionsRole,
39+
ResultActionsRole,
4040
};
4141

4242
QgsLocatorModel( QObject *parent /TransferThis/ = 0 );

‎src/app/locator/qgsinbuiltlocatorfilters.cpp

Lines changed: 4 additions & 4 deletions
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.insert( OpenForm, new QAction( tr( "Open form" ) ) );
405+
result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form" ) );
406406
emit resultFetched( result );
407407

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

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

423-
void QgsAllLayersFeaturesLocatorFilter::triggerResultFromContextMenu( const QgsLocatorResult &result, const int id )
423+
void QgsAllLayersFeaturesLocatorFilter::triggerResultFromAction( const QgsLocatorResult &result, const int actionId )
424424
{
425425
QVariantList dataList = result.userData.toList();
426426
QgsFeatureId fid = dataList.at( 0 ).toLongLong();
@@ -429,7 +429,7 @@ void QgsAllLayersFeaturesLocatorFilter::triggerResultFromContextMenu( const QgsL
429429
if ( !layer )
430430
return;
431431

432-
if ( id == OpenForm )
432+
if ( actionId == OpenForm )
433433
{
434434
QgsFeature f;
435435
QgsFeatureRequest request;

‎src/app/locator/qgsinbuiltlocatorfilters.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
146146
void prepare( const QString &string, const QgsLocatorContext &context ) override;
147147
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
148148
void triggerResult( const QgsLocatorResult &result ) override;
149-
void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id ) override;
149+
void triggerResultFromAction( const QgsLocatorResult &result, const int actionId ) override;
150150

151151
private:
152152
int mMaxResultsPerLayer = 6;

‎src/core/locator/qgslocatorfilter.cpp

Lines changed: 2 additions & 2 deletions
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 int id )
36+
void QgsLocatorFilter::triggerResultFromAction( const QgsLocatorResult &result, const int actionId )
3737
{
3838
Q_UNUSED( result );
39-
Q_UNUSED( id );
39+
Q_UNUSED( actionId );
4040
}
4141

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

‎src/core/locator/qgslocatorfilter.h

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,17 +94,39 @@ class CORE_EXPORT QgsLocatorResult
9494
QString group = QString();
9595

9696
/**
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.
97+
* The ResultActions stores basic informations for additional
98+
* actions to be used in a locator widget, in a context menu
99+
* for instance.
100+
* The \a id used to recognized the action when the result is triggered.
101+
* It should be 0 or greater as otherwise, the result will be triggered
102+
* normally.
103+
* \since QGIS 3.6
104+
*/
105+
struct CORE_EXPORT ResultAction
106+
{
107+
public:
108+
//! Constructor for ResultAction
109+
ResultAction() = default;
110+
ResultAction( int id, QString text )
111+
: id( id )
112+
, text( text )
113+
{}
114+
int id = -1;
115+
QString text;
116+
};
117+
118+
/**
119+
* Additional actions to be used in a locator widget
120+
* for the given result. They could be displayed in
121+
* a context menu.
102122
* \since QGIS 3.6
103123
*/
104-
QMap<int, QAction *> contextMenuActions = QMap<int, QAction *>();
105-
124+
QList<ResultAction> actions;
106125
};
107126

127+
Q_DECLARE_METATYPE( QgsLocatorResult::ResultAction )
128+
129+
108130
/**
109131
* \class QgsLocatorFilter
110132
* \ingroup core
@@ -223,11 +245,11 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
223245

224246
/**
225247
* 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.
248+
* The entry is identified by its \a actionId as specified in the result of this filter.
227249
* \see triggerResult()
228250
* \since QGIS 3.6
229251
*/
230-
virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id );
252+
virtual void triggerResultFromAction( const QgsLocatorResult &result, const int actionId );
231253

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

‎src/core/locator/qgslocatormodel.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
161161
else
162162
return 0;
163163

164-
case ResultContextMenuActionsRole:
165-
return QVariant::fromValue( mResults.at( index.row() ).result.contextMenuActions );
164+
case ResultActionsRole:
165+
return QVariant::fromValue( mResults.at( index.row() ).result.actions );
166166
}
167167

168168
return QVariant();
@@ -191,7 +191,7 @@ QHash<int, QByteArray> QgsLocatorModel::roleNames() const
191191
roles[ResultScoreRole] = "ResultScore";
192192
roles[ResultFilterNameRole] = "ResultFilterName";
193193
roles[ResultFilterGroupSortingRole] = "ResultFilterGroupSorting";
194-
roles[ResultContextMenuActionsRole] = "ResultContextMenuActions";
194+
roles[ResultActionsRole] = "ResultContextMenuActions";
195195
roles[Qt::DisplayRole] = "Text";
196196
return roles;
197197
}

‎src/core/locator/qgslocatormodel.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class CORE_EXPORT QgsLocatorModel : public QAbstractTableModel
5656
ResultScoreRole, //!< Result match score, used by QgsLocatorProxyModel for sorting roles.
5757
ResultFilterNameRole, //!< Associated filter name which created the result
5858
ResultFilterGroupSortingRole, //!< Group results within the same filter results
59-
ResultContextMenuActionsRole, //!< The actions to be shown for the given result in a context menu
59+
ResultActionsRole, //!< The actions to be shown for the given result in a context menu
6060
};
6161

6262
/**

‎src/core/locator/qgslocatormodelbridge.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void QgsLocatorModelBridge::triggerResult( const QModelIndex &index, const int i
4444
if ( result.filter )
4545
{
4646
if ( id >= 0 )
47-
result.filter->triggerResultFromContextMenu( result, id );
47+
result.filter->triggerResultFromAction( result, id );
4848
else
4949
result.filter->triggerResult( result );
5050
}

‎src/gui/locator/qgslocatorwidget.cpp

Lines changed: 6 additions & 6 deletions
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 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 )
184+
const QList<QgsLocatorResult::ResultAction> actions = mResultsView->model()->data( index, QgsLocatorModel::ResultActionsRole ).value<QList<QgsLocatorResult::ResultAction>>();
185+
QMenu *contextMenu = new QMenu( mResultsView );
186+
for ( auto action : actions )
187187
{
188-
connect( it.value(), &QAction::triggered, this, [ = ]() {mModelBridge->triggerResult( index, it.key() );} );
188+
QAction *menuAction = new QAction( action.text, contextMenu );
189+
connect( menuAction, &QAction::triggered, this, [ = ]() {mModelBridge->triggerResult( index, action.id );} );
190+
contextMenu->addAction( menuAction );
189191
}
190-
QMenu *contextMenu = new QMenu( mResultsView );
191-
contextMenu->addActions( actions.values() );
192192
contextMenu->exec( mResultsView->viewport()->mapToGlobal( point ) );
193193
}
194194

0 commit comments

Comments
 (0)
Please sign in to comment.