Skip to content

Commit

Permalink
Fix duplicate shortcut registered warning on startup
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jan 25, 2022
1 parent 91fb92f commit c4d2dc2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/app/qgisapp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,7 @@ QgisApp::QgisApp( QSplashScreen *splash, bool restorePlugins, bool skipBadLayers
zoomInToolShortCut->setProperty( "Icon", QgsApplication::getThemeIcon( QStringLiteral( "/mActionZoomIn.svg" ) ) );

QShortcut *shortcutTracing = new QShortcut( QKeySequence( tr( "Ctrl+Shift+." ) ), this );
shortcutTracing->setObjectName( QStringLiteral( "ToggleTracing" ) );
connect( shortcutTracing, &QShortcut::activated, this, &QgisApp::toggleEventTracing );

if ( ! QTouchDevice::devices().isEmpty() )
Expand Down
26 changes: 18 additions & 8 deletions src/gui/qgsshortcutsmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include <QShortcut>
#include <QRegularExpression>
#include <QWidgetAction>

QgsShortcutsManager::QgsShortcutsManager( QObject *parent, const QString &settingsRoot )
: QObject( parent )
Expand All @@ -37,16 +38,15 @@ void QgsShortcutsManager::registerAllChildActions( QObject *object, bool recursi
if ( recursive )
{
const QList< QAction * > actions = object->findChildren< QAction * >();
const auto constActions = actions;
for ( QAction *a : constActions )
for ( QAction *a : actions )
{
registerAction( a, a->shortcut().toString( QKeySequence::NativeText ) );
}
}
else
{
const auto constChildren = object->children();
for ( QObject *child : constChildren )
const QList< QObject *> children = object->children();
for ( QObject *child : children )
{
if ( QAction *a = qobject_cast<QAction *>( child ) )
{
Expand Down Expand Up @@ -82,12 +82,20 @@ void QgsShortcutsManager::registerAllChildShortcuts( QObject *object, bool recur

bool QgsShortcutsManager::registerAction( QAction *action, const QString &defaultSequence )
{
if ( qobject_cast< QWidgetAction * >( action ) )
return false;

if ( mActions.contains( action ) )
return false; // already registered

#ifdef QGISDEBUG
// if using a debug build, warn on duplicate actions
if ( actionByName( action->text() ) || shortcutByName( action->text() ) )
// if using a debug build, warn on duplicate or non-compliant actions
if ( action->text().isEmpty() )
{
QgsLogger::warning( QStringLiteral( "Action has no text set: %1" ).arg( action->objectName() ) );
return false;
}
else if ( actionByName( action->text() ) || shortcutByName( action->text() ) )
QgsLogger::warning( QStringLiteral( "Duplicate shortcut registered: %1" ).arg( action->text() ) );
#endif

Expand Down Expand Up @@ -122,8 +130,10 @@ bool QgsShortcutsManager::registerAction( QAction *action, const QString &defaul
bool QgsShortcutsManager::registerShortcut( QShortcut *shortcut, const QString &defaultSequence )
{
#ifdef QGISDEBUG
// if using a debug build, warn on duplicate actions
if ( actionByName( shortcut->objectName() ) || shortcutByName( shortcut->objectName() ) )
// if using a debug build, warn on duplicate or non-compliant actions
if ( shortcut->objectName().isEmpty() )
QgsLogger::warning( QStringLiteral( "Shortcut has no object name set: %1" ).arg( shortcut->key().toString() ) );
else if ( actionByName( shortcut->objectName() ) || shortcutByName( shortcut->objectName() ) )
QgsLogger::warning( QStringLiteral( "Duplicate shortcut registered: %1" ).arg( shortcut->objectName() ) );
#endif

Expand Down

0 comments on commit c4d2dc2

Please sign in to comment.