Skip to content

Commit

Permalink
Merge pull request #43461 from 3nids/loc-fil-mov-files
Browse files Browse the repository at this point in the history
move locator filters implemenation to their own files
  • Loading branch information
3nids committed May 31, 2021
1 parent b7ea7f2 commit f044c95
Show file tree
Hide file tree
Showing 25 changed files with 1,942 additions and 1,501 deletions.
12 changes: 11 additions & 1 deletion src/app/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,17 @@ set(QGIS_APP_SRCS
layout/qgsreportsectionmodel.cpp
layout/qgsreportsectionwidget.cpp

locator/qgsinbuiltlocatorfilters.cpp
locator/qgsactionlocatorfilter.cpp
locator/qgsactivelayerfeatureslocatorfilter.cpp
locator/qgsalllayersfeatureslocatorfilter.cpp
locator/qgsbookmarklocatorfilter.cpp
locator/qgsexpressioncalculatorlocatorfilter.cpp
locator/qgsgotolocatorfilter.cpp
locator/qgslayertreelocatorfilter.cpp
locator/qgslayoutlocatorfilter.cpp
locator/qgsnominatimlocatorfilter.cpp
locator/qgssettingslocatorfilter.cpp

locator/qgslocatoroptionswidget.cpp

maptools/qgsappmaptools.cpp
Expand Down
119 changes: 119 additions & 0 deletions src/app/locator/qgsactionlocatorfilter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/***************************************************************************
qgsactionlocatorfilters.cpp
----------------------------
begin : May 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include "qgsactionlocatorfilter.h"

#include <QAction>
#include <QMenu>
#include <QRegularExpression>



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

QgsActionLocatorFilter *QgsActionLocatorFilter::clone() const
{
return new QgsActionLocatorFilter( mActionParents );
}

void QgsActionLocatorFilter::fetchResults( const QString &string, const QgsLocatorContext &, QgsFeedback * )
{
// collect results in main thread, since this method is inexpensive and
// accessing the gui actions is not thread safe

QList<QAction *> found;

for ( QWidget *object : std::as_const( mActionParents ) )
{
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 )
{
const QList< QWidget *> children = parent->findChildren<QWidget *>();
for ( QWidget *widget : children )
{
searchActions( string, widget, found );
}

QRegularExpression extractFromTooltip( QStringLiteral( "<b>(.*)</b>" ) );
QRegularExpression newLineToSpace( QStringLiteral( "[\\s\\n\\r]+" ) );

const auto constActions = parent->actions();
for ( QAction *action : constActions )
{
if ( action->menu() )
{
searchActions( string, action->menu(), found );
continue;
}

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

QString searchText = action->text();
searchText.replace( '&', QString() );

QString tooltip = action->toolTip();
tooltip.replace( newLineToSpace, QStringLiteral( " " ) );
QRegularExpressionMatch match = extractFromTooltip.match( tooltip );
if ( match.hasMatch() )
{
tooltip = match.captured( 1 );
}
tooltip.replace( QLatin1String( "..." ), QString() );
tooltip.replace( QString( QChar( 0x2026 ) ), QString() );
searchText.replace( QLatin1String( "..." ), QString() );
searchText.replace( QString( QChar( 0x2026 ) ), QString() );
bool uniqueTooltip = searchText.trimmed().compare( tooltip.trimmed(), Qt::CaseInsensitive ) != 0;
if ( action->isChecked() )
{
searchText += QStringLiteral( " [%1]" ).arg( tr( "Active" ) );
}
if ( uniqueTooltip )
{
searchText += QStringLiteral( " (%1)" ).arg( tooltip.trimmed() );
}

QgsLocatorResult result;
result.displayString = searchText;
result.userData = QVariant::fromValue( action );
result.icon = action->icon();
result.score = fuzzyScore( result.displayString, string );

if ( result.score > 0 )
{
found << action;
emit resultFetched( result );
}
}
}
55 changes: 55 additions & 0 deletions src/app/locator/qgsactionlocatorfilter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/***************************************************************************
qgsactionlocatorfilter.h
--------------------------
begin : May 2017
copyright : (C) 2017 by Nyall Dawson
email : nyall dot dawson at gmail dot com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSACTIONLOCATORFILTERS_H
#define QGSACTIONLOCATORFILTERS_H

#include "qgis_app.h"
#include "qgslocatorfilter.h"



class QAction;



class APP_EXPORT QgsActionLocatorFilter : public QgsLocatorFilter
{
Q_OBJECT

public:

QgsActionLocatorFilter( const QList<QWidget *> &parentObjectsForActions, QObject *parent = nullptr );
QgsActionLocatorFilter *clone() const override;
QString name() const override { return QStringLiteral( "actions" ); }
QString displayName() const override { return tr( "Actions" ); }
Priority priority() const override { return Lowest; }
QString prefix() const override { return QStringLiteral( "." ); }
QgsLocatorFilter::Flags flags() const override { return QgsLocatorFilter::FlagFast; }

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 // QGSACTIONLOCATORFILTERS_H
Loading

0 comments on commit f044c95

Please sign in to comment.