Navigation Menu

Skip to content

Commit

Permalink
do not use qaction in the result, build context menu in the widget
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Nov 29, 2018
1 parent c48a706 commit d272f3c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 31 deletions.
19 changes: 16 additions & 3 deletions python/core/auto_generated/locator/qgslocatorfilter.sip.in
Expand Up @@ -48,10 +48,23 @@ Constructor for QgsLocatorResult.

QString group;

QMap<int, QAction *> contextMenuActions = QMap<int, QAction *>();
struct ResultAction
{
public:
ResultAction();
%Docstring
Constructor for ResultAction
%End
ResultAction( int id, QString text );
int id;
QString text;
};

QList<ResultAction> actions;
};



class QgsLocatorFilter : QObject
{
%Docstring
Expand Down Expand Up @@ -177,10 +190,10 @@ E.g. a file search filter would open file associated with the triggered
result.
%End

virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id );
virtual void triggerResultFromAction( const QgsLocatorResult &result, const int actionId );
%Docstring
Triggers a filter ``result`` from this filter for an entry in the context menu.
The entry is identified by its \id as specified in the result of this filter.
The entry is identified by its ``actionId`` as specified in the result of this filter.

.. seealso:: :py:func:`triggerResult`

Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/locator/qgslocatormodel.sip.in
Expand Up @@ -36,7 +36,7 @@ in order to ensure correct sorting of results by priority and match level.
ResultScoreRole,
ResultFilterNameRole,
ResultFilterGroupSortingRole,
ResultContextMenuActionsRole,
ResultActionsRole,
};

QgsLocatorModel( QObject *parent /TransferThis/ = 0 );
Expand Down
8 changes: 4 additions & 4 deletions src/app/locator/qgsinbuiltlocatorfilters.cpp
Expand Up @@ -402,7 +402,7 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con
result.icon = preparedLayer.layerIcon;
result.score = static_cast< double >( string.length() ) / result.displayString.size();

result.contextMenuActions.insert( OpenForm, new QAction( tr( "Open form" ) ) );
result.actions << QgsLocatorResult::ResultAction( OpenForm, tr( "Open form" ) );
emit resultFetched( result );

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

void QgsAllLayersFeaturesLocatorFilter::triggerResult( const QgsLocatorResult &result )
{
triggerResultFromContextMenu( result, NoEntry );
triggerResultFromAction( result, NoEntry );
}

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

if ( id == OpenForm )
if ( actionId == OpenForm )
{
QgsFeature f;
QgsFeatureRequest request;
Expand Down
2 changes: 1 addition & 1 deletion src/app/locator/qgsinbuiltlocatorfilters.h
Expand Up @@ -146,7 +146,7 @@ class APP_EXPORT QgsAllLayersFeaturesLocatorFilter : public QgsLocatorFilter
void prepare( const QString &string, const QgsLocatorContext &context ) override;
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
void triggerResult( const QgsLocatorResult &result ) override;
void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id ) override;
void triggerResultFromAction( const QgsLocatorResult &result, const int actionId ) override;

private:
int mMaxResultsPerLayer = 6;
Expand Down
4 changes: 2 additions & 2 deletions src/core/locator/qgslocatorfilter.cpp
Expand Up @@ -33,10 +33,10 @@ QgsLocatorFilter::Flags QgsLocatorFilter::flags() const
return nullptr;
}

void QgsLocatorFilter::triggerResultFromContextMenu( const QgsLocatorResult &result, const int id )
void QgsLocatorFilter::triggerResultFromAction( const QgsLocatorResult &result, const int actionId )
{
Q_UNUSED( result );
Q_UNUSED( id );
Q_UNUSED( actionId );
}

bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &search )
Expand Down
40 changes: 31 additions & 9 deletions src/core/locator/qgslocatorfilter.h
Expand Up @@ -94,17 +94,39 @@ class CORE_EXPORT QgsLocatorResult
QString group = QString();

/**
* Actions to be used in a context menu for the result.
* The key of the map is populated with IDs used to recognized
* entry when the result is triggered. The IDs should be 0 or greater
* otherwise, the result will be triggered normally.
* Entries in the context menu will be ordered by IDs.
* The ResultActions stores basic informations for additional
* actions to be used in a locator widget, in a context menu
* for instance.
* The \a id used to recognized the action when the result is triggered.
* It should be 0 or greater as otherwise, the result will be triggered
* normally.
* \since QGIS 3.6
*/
struct CORE_EXPORT ResultAction
{
public:
//! Constructor for ResultAction
ResultAction() = default;
ResultAction( int id, QString text )
: id( id )
, text( text )
{}
int id = -1;
QString text;
};

/**
* Additional actions to be used in a locator widget
* for the given result. They could be displayed in
* a context menu.
* \since QGIS 3.6
*/
QMap<int, QAction *> contextMenuActions = QMap<int, QAction *>();

QList<ResultAction> actions;
};

Q_DECLARE_METATYPE( QgsLocatorResult::ResultAction )


/**
* \class QgsLocatorFilter
* \ingroup core
Expand Down Expand Up @@ -223,11 +245,11 @@ class CORE_EXPORT QgsLocatorFilter : public QObject

/**
* Triggers a filter \a result from this filter for an entry in the context menu.
* The entry is identified by its \id as specified in the result of this filter.
* The entry is identified by its \a actionId as specified in the result of this filter.
* \see triggerResult()
* \since QGIS 3.6
*/
virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const int id );
virtual void triggerResultFromAction( const QgsLocatorResult &result, const int actionId );

/**
* This method will be called on main thread on the original filter (not a clone)
Expand Down
6 changes: 3 additions & 3 deletions src/core/locator/qgslocatormodel.cpp
Expand Up @@ -161,8 +161,8 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
else
return 0;

case ResultContextMenuActionsRole:
return QVariant::fromValue( mResults.at( index.row() ).result.contextMenuActions );
case ResultActionsRole:
return QVariant::fromValue( mResults.at( index.row() ).result.actions );
}

return QVariant();
Expand Down Expand Up @@ -191,7 +191,7 @@ QHash<int, QByteArray> QgsLocatorModel::roleNames() const
roles[ResultScoreRole] = "ResultScore";
roles[ResultFilterNameRole] = "ResultFilterName";
roles[ResultFilterGroupSortingRole] = "ResultFilterGroupSorting";
roles[ResultContextMenuActionsRole] = "ResultContextMenuActions";
roles[ResultActionsRole] = "ResultContextMenuActions";
roles[Qt::DisplayRole] = "Text";
return roles;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/locator/qgslocatormodel.h
Expand Up @@ -56,7 +56,7 @@ class CORE_EXPORT QgsLocatorModel : public QAbstractTableModel
ResultScoreRole, //!< Result match score, used by QgsLocatorProxyModel for sorting roles.
ResultFilterNameRole, //!< Associated filter name which created the result
ResultFilterGroupSortingRole, //!< Group results within the same filter results
ResultContextMenuActionsRole, //!< The actions to be shown for the given result in a context menu
ResultActionsRole, //!< The actions to be shown for the given result in a context menu
};

/**
Expand Down
2 changes: 1 addition & 1 deletion src/core/locator/qgslocatormodelbridge.cpp
Expand Up @@ -44,7 +44,7 @@ void QgsLocatorModelBridge::triggerResult( const QModelIndex &index, const int i
if ( result.filter )
{
if ( id >= 0 )
result.filter->triggerResultFromContextMenu( result, id );
result.filter->triggerResultFromAction( result, id );
else
result.filter->triggerResult( result );
}
Expand Down
12 changes: 6 additions & 6 deletions src/gui/locator/qgslocatorwidget.cpp
Expand Up @@ -181,14 +181,14 @@ void QgsLocatorWidget::showContextMenu( const QPoint &point )
if ( !index.isValid() )
return;

const QMap<int, QAction *> actions = mResultsView->model()->data( index, QgsLocatorModel::ResultContextMenuActionsRole ).value<QMap<int, QAction *>>();
QMap<int, QAction *>::const_iterator it = actions.constBegin();
for ( ; it != actions.constEnd(); ++it )
const QList<QgsLocatorResult::ResultAction> actions = mResultsView->model()->data( index, QgsLocatorModel::ResultActionsRole ).value<QList<QgsLocatorResult::ResultAction>>();
QMenu *contextMenu = new QMenu( mResultsView );
for ( auto action : actions )
{
connect( it.value(), &QAction::triggered, this, [ = ]() {mModelBridge->triggerResult( index, it.key() );} );
QAction *menuAction = new QAction( action.text, contextMenu );
connect( menuAction, &QAction::triggered, this, [ = ]() {mModelBridge->triggerResult( index, action.id );} );
contextMenu->addAction( menuAction );
}
QMenu *contextMenu = new QMenu( mResultsView );
contextMenu->addActions( actions.values() );
contextMenu->exec( mResultsView->viewport()->mapToGlobal( point ) );
}

Expand Down

0 comments on commit d272f3c

Please sign in to comment.