Skip to content
Permalink
Browse files

[locator] allow to have context menu entries for results

  • Loading branch information
3nids committed Nov 28, 2018
1 parent 7bce934 commit 1bc0fbf9d4b356862f06ea89f8ca6126f3be0906
@@ -10,6 +10,7 @@




class QgsLocatorResult
{
%Docstring
@@ -47,6 +48,8 @@ Constructor for QgsLocatorResult.

QString group;

QList<QAction *> contextMenuActions;

};

class QgsLocatorFilter : QObject
@@ -172,6 +175,17 @@ by a user. The filter subclass must implement logic here
to perform the desired operation for the search result.
E.g. a file search filter would open file associated with the triggered
result.
%End

virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action );
%Docstring
Triggers a filter ``result`` from this filter for a given action.
Actions are specified in the result given by this filter and shown
as context menu entries.

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

.. versionadded:: 3.6
%End

virtual void clearPreviousResults();
@@ -36,6 +36,7 @@ in order to ensure correct sorting of results by priority and match level.
ResultScoreRole,
ResultFilterNameRole,
ResultFilterGroupSortingRole,
ResultContextMenuActionsRole,
};

QgsLocatorModel( QObject *parent /TransferThis/ = 0 );
@@ -12,6 +12,7 @@




class QgsLocatorModelBridge : QObject
{
%Docstring
@@ -56,9 +57,9 @@ Returns true if some text to be search is pending in the queue
Returns true if the a search is currently running
%End

void triggerResult( const QModelIndex &index );
void triggerResult( const QModelIndex &index, const QAction *action = 0 );
%Docstring
Triggers the result at given index
Triggers the result at given ``index`` and with optional \action if context menu entry was triggered
%End

signals:
@@ -15,6 +15,8 @@
* *
***************************************************************************/

#include <QToolButton>
#include <QClipboard>

#include "qgsinbuiltlocatorfilters.h"
#include "qgsproject.h"
@@ -25,8 +27,7 @@
#include "qgsmaplayermodel.h"
#include "qgslayoutmanager.h"
#include "qgsmapcanvas.h"
#include <QToolButton>
#include <QClipboard>
#include "qgsfeatureaction.h"

QgsLayerTreeLocatorFilter::QgsLayerTreeLocatorFilter( QObject *parent )
: QgsLocatorFilter( parent )
@@ -400,6 +401,8 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con
result.userData = QVariantList() << f.id() << preparedLayer.layerId;
result.icon = preparedLayer.layerIcon;
result.score = static_cast< double >( string.length() ) / result.displayString.size();

result.contextMenuActions << new QAction( tr( "Open form" ) );
emit resultFetched( result );

foundInCurrentLayer++;
@@ -413,6 +416,11 @@ void QgsAllLayersFeaturesLocatorFilter::fetchResults( const QString &string, con
}

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

void QgsAllLayersFeaturesLocatorFilter::triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action )
{
QVariantList dataList = result.userData.toList();
QgsFeatureId id = dataList.at( 0 ).toLongLong();
@@ -421,7 +429,29 @@ void QgsAllLayersFeaturesLocatorFilter::triggerResult( const QgsLocatorResult &r
if ( !layer )
return;

QgisApp::instance()->mapCanvas()->zoomToFeatureIds( layer, QgsFeatureIds() << id );
if ( !action )
{
QgisApp::instance()->mapCanvas()->zoomToFeatureIds( layer, QgsFeatureIds() << id );
}
else
{
// no need to check for which action, since the filter shows only one
QgsFeature f;
QgsFeatureRequest request;
request.setFilterFid( id );
bool fetched = layer->getFeatures( request ).nextFeature( f );
if ( !fetched )
return;
QgsFeatureAction action( tr( "Attributes changed" ), f, layer, QString(), -1, QgisApp::instance() );
if ( layer->isEditable() )
{
action.editFeature( false );
}
else
{
action.viewFeatureForm();
}
}
}

//
@@ -140,6 +140,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 QAction *action ) override;

private:
int mMaxResultsPerLayer = 6;
@@ -33,6 +33,12 @@ QgsLocatorFilter::Flags QgsLocatorFilter::flags() const
return nullptr;
}

void QgsLocatorFilter::triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action )
{
Q_UNUSED( result );
Q_UNUSED( action );
}

bool QgsLocatorFilter::stringMatches( const QString &candidate, const QString &search )
{
return !search.isEmpty() && candidate.contains( search, Qt::CaseInsensitive );
@@ -18,12 +18,14 @@
#ifndef QGSLOCATORFILTER_H
#define QGSLOCATORFILTER_H

#include <QAction>
#include <QIcon>
#include <QString>
#include <QVariant>

#include "qgis_core.h"
#include "qgslocatorcontext.h"
#include "qgslogger.h"
#include <QString>
#include <QVariant>
#include <QIcon>

class QgsFeedback;
class QgsLocatorFilter;
@@ -91,6 +93,11 @@ class CORE_EXPORT QgsLocatorResult
*/
QString group = QString();

/**
* Actions to be used in a context menu for the result
*/
QList<QAction *> contextMenuActions = QList<QAction *>();

};

/**
@@ -209,6 +216,15 @@ class CORE_EXPORT QgsLocatorFilter : public QObject
*/
virtual void triggerResult( const QgsLocatorResult &result ) = 0;

/**
* Triggers a filter \a result from this filter for a given action.
* Actions are specified in the result given by this filter and shown
* as context menu entries.
* \see triggerResult()
* \since QGIS 3.6
*/
virtual void triggerResultFromContextMenu( const QgsLocatorResult &result, const QAction *action );

/**
* This method will be called on main thread on the original filter (not a clone)
* before fetching results or before triggering a result to clear any change made
@@ -160,6 +160,9 @@ QVariant QgsLocatorModel::data( const QModelIndex &index, int role ) const
return 1;
else
return 0;

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

return QVariant();
@@ -56,6 +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
};

/**
@@ -37,12 +37,17 @@ bool QgsLocatorModelBridge::isRunning() const
return mIsRunning;
}

void QgsLocatorModelBridge::triggerResult( const QModelIndex &index )
void QgsLocatorModelBridge::triggerResult( const QModelIndex &index, const QAction *action )
{
mLocator->clearPreviousResults();
QgsLocatorResult result = mProxyModel->data( index, QgsLocatorModel::ResultDataRole ).value< QgsLocatorResult >();
if ( result.filter )
result.filter->triggerResult( result );
{
if ( action )
result.filter->triggerResultFromContextMenu( result, action );
else
result.filter->triggerResult( result );
}
}

void QgsLocatorModelBridge::setIsRunning( bool isRunning )
@@ -24,6 +24,8 @@
#include "qgscoordinatereferencesystem.h"
#include "qgsrectangle.h"

class QAction;

class QgsLocatorResult;
class QgsLocator;
class QgsLocatorContext;
@@ -61,8 +63,8 @@ class CORE_EXPORT QgsLocatorModelBridge : public QObject
//! Returns true if the a search is currently running
bool isRunning() const;

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

signals:
//! Emitted when a result is added
@@ -74,9 +74,12 @@ QgsLocatorWidget::QgsLocatorWidget( QWidget *parent )
mResultsView->setUniformRowHeights( true );
mResultsView->setIconSize( QSize( 16, 16 ) );
mResultsView->recalculateSize();
mResultsView->setContextMenuPolicy( Qt::CustomContextMenu );

connect( mLineEdit, &QLineEdit::textChanged, this, &QgsLocatorWidget::scheduleDelayedPopup );
connect( mResultsView, &QAbstractItemView::activated, this, &QgsLocatorWidget::acceptCurrentEntry );
connect( mResultsView, &QAbstractItemView::customContextMenuRequested, this, &QgsLocatorWidget::showContextMenu );

connect( mModelBridge, &QgsLocatorModelBridge::resultAdded, this, &QgsLocatorWidget::resultAdded );
connect( mModelBridge, &QgsLocatorModelBridge::isRunningChanged, this, [ = ]() {mLineEdit->setShowSpinner( mModelBridge->isRunning() );} );
connect( mModelBridge, & QgsLocatorModelBridge::resultsCleared, this, [ = ]() {mHasSelectedResult = false;} );
@@ -172,6 +175,23 @@ void QgsLocatorWidget::resultAdded()
}
}

void QgsLocatorWidget::showContextMenu( const QPoint &point )
{
QModelIndex index = mResultsView->indexAt( point );
if ( !index.isValid() )
return;

const QList<QAction *> actions = mResultsView->model()->data( index, QgsLocatorModel::ResultContextMenuActionsRole ).value<QList<QAction *>>();
for ( const QAction *action : actions )
{
connect( action, &QAction::triggered, this, [ = ]() {mModelBridge->triggerResult( index, action );} );
}

QMenu *contextMenu = new QMenu( mResultsView );
contextMenu->addActions( actions );
contextMenu->exec( mResultsView->viewport()->mapToGlobal( point ) );
}

void QgsLocatorWidget::performSearch()
{
mPopupTimer.stop();
@@ -94,6 +94,7 @@ class GUI_EXPORT QgsLocatorWidget : public QWidget
void configMenuAboutToShow();
void scheduleDelayedPopup();
void resultAdded();
void showContextMenu( const QPoint &point );

private:
QgsLocatorModelBridge *mModelBridge = nullptr;

0 comments on commit 1bc0fbf

Please sign in to comment.
You can’t perform that action at this time.