-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #43461 from 3nids/loc-fil-mov-files
move locator filters implemenation to their own files
- Loading branch information
Showing
25 changed files
with
1,942 additions
and
1,501 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.