Skip to content
Permalink
Browse files

Add a locator filter for main window actions

Inspired by the ghost of processing's commander
  • Loading branch information
nyalldawson committed May 17, 2017
1 parent ab02f2b commit cb579bb9b6d5c6b94329378a345cec46a9c7e19c
Showing with 98 additions and 0 deletions.
  1. +63 −0 src/app/locator/qgsinbuiltlocatorfilters.cpp
  2. +18 −0 src/app/locator/qgsinbuiltlocatorfilters.h
  3. +17 −0 src/app/qgisapp.cpp
@@ -25,6 +25,7 @@
#include "qgsmaplayermodel.h"
#include "qgscomposition.h"
#include "qgslayoutmanager.h"
#include <QToolButton>

QgsLayerTreeLocatorFilter::QgsLayerTreeLocatorFilter( QObject *parent )
: QgsLocatorFilter( parent )
@@ -97,3 +98,65 @@ void QgsLayoutLocatorFilter::triggerResult( const QgsLocatorResult &result )




QgsActionLocatorFilter::QgsActionLocatorFilter( const QList<QWidget *> &parentObjectsForActions, QObject *parent )
: QgsLocatorFilter( parent )
, mActionParents( parentObjectsForActions )
{

}

void QgsActionLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback )
{
QList<QAction *> found;

Q_FOREACH ( QWidget *object, mActionParents )
{
if ( feedback->isCanceled() )
return;

searchActions( string, object, found );
}
}

void QgsActionLocatorFilter::triggerResult( const QgsLocatorResult &result )
{
QAction *action = qobject_cast< QAction * >( qvariant_cast<QObject *>( result.userData ) );
if ( action )
action->trigger();
}

void QgsActionLocatorFilter::searchActions( const QString &string, QWidget *parent, QList<QAction *> &found )
{
QList< QWidget *> children = parent->findChildren<QWidget *>();
Q_FOREACH ( QWidget *widget, children )
{
searchActions( string, widget, found );
}
Q_FOREACH ( QAction *action, parent->actions() )
{
if ( action->menu() )
{
searchActions( string, action->menu(), found );
continue;
}

if ( !action->isEnabled() || !action->isVisible() )
continue;
if ( found.contains( action ) )
continue;

QString searchText = action->text();
searchText.replace( '&', QString() );
if ( searchText.contains( string, Qt::CaseInsensitive ) )
{
QgsLocatorResult result;
result.filter = this;
result.displayString = searchText;
result.userData = QVariant::fromValue( action );
result.icon = action->icon();
emit resultFetched( result );
found << action;
}
}
}
@@ -19,6 +19,7 @@
#define QGSINBUILTLOCATORFILTERS_H

#include "qgslocatorfilter.h"
class QAction;

class QgsLayerTreeLocatorFilter : public QgsLocatorFilter
{
@@ -44,6 +45,23 @@ class QgsLayoutLocatorFilter : public QgsLocatorFilter

};

class QgsActionLocatorFilter : public QgsLocatorFilter
{
Q_OBJECT

public:

QgsActionLocatorFilter( const QList<QWidget *> &parentObjectsForActions, QObject *parent = nullptr );
void fetchResults( const QString &string, const QgsLocatorContext &context, QgsFeedback *feedback ) override;
void triggerResult( const QgsLocatorResult &result ) override;
private:

QList< QWidget * > mActionParents;

void searchActions( const QString &string, QWidget *parent, QList< QAction *> &found );

};

#endif // QGSINBUILTLOCATORFILTERS_H


@@ -2649,6 +2649,23 @@ void QgisApp::createStatusBar()

mLocatorWidget->locator()->registerFilter( new QgsLayerTreeLocatorFilter() );
mLocatorWidget->locator()->registerFilter( new QgsLayoutLocatorFilter() );
QList< QWidget *> actionObjects;
actionObjects << menuBar()
<< mAdvancedDigitizeToolBar
<< mFileToolBar
<< mLayerToolBar
<< mDigitizeToolBar
<< mMapNavToolBar
<< mAttributesToolBar
<< mPluginToolBar
<< mRasterToolBar
<< mLabelToolBar
<< mVectorToolBar
<< mDatabaseToolBar
<< mWebToolBar
<< mSnappingToolBar;

mLocatorWidget->locator()->registerFilter( new QgsActionLocatorFilter( actionObjects ) );

}

0 comments on commit cb579bb

Please sign in to comment.