23#include <QRegularExpression>
26#include <QWidgetAction>
28#include "moc_qgsshortcutsmanager.cpp"
30using namespace Qt::StringLiterals;
34 , mSettingsPath( settingsRoot )
37 auto registerCommonAction =
38 [
this](
CommonAction commonAction,
const QIcon &icon,
const QString &text,
const QString &toolTip,
const QString &sequence,
const QString &objectName,
const QString §ion ) {
39 QAction *action =
new QAction( icon, text,
this );
40 action->setToolTip( toolTip );
41 setObjectName( objectName );
44 action->setEnabled(
false );
45 action->setProperty(
"commonAction",
static_cast< int >( commonAction ) );
47 mCommonActions.insert(
static_cast< int >( commonAction ), action );
53 QgsApplication::getThemeIcon( u
"console/iconCommentEditorConsole.svg"_s, QgsApplication::palette().color( QPalette::ColorRole::WindowText ) ),
54 tr(
"Toggle Comment" ),
55 tr(
"Toggle comment" ),
57 u
"mEditorToggleComment"_s,
63 QgsApplication::getThemeIcon( u
"console/iconCommentEditorConsole.svg"_s, QgsApplication::palette().color( QPalette::ColorRole::WindowText ) ),
64 tr(
"Toggle Comment" ),
65 tr(
"Toggle comment" ),
67 u
"mEditorToggleComment"_s,
80 const QHash< int, QAction * > commonActionsToCleanup = std::move( mCommonActions );
81 for (
auto it = commonActionsToCleanup.constBegin(); it != commonActionsToCleanup.constEnd(); ++it )
95 const QList<QObject *> children =
object->children();
96 for ( QObject *child : children )
98 if ( QAction *a = qobject_cast<QAction *>( child ) )
100 registerAction( a, a->shortcut().toString( QKeySequence::NativeText ), section );
102 else if ( recursive )
104 const QString newSection = child->objectName().isEmpty() ? section : section + child->objectName() +
"/";
112 const QList<QObject *> children =
object->children();
113 for ( QObject *child : children )
115 if ( QShortcut *s = qobject_cast<QShortcut *>( child ) )
117 registerShortcut( s, s->key().toString( QKeySequence::NativeText ), section );
119 else if ( recursive )
121 const QString newSection = child->objectName().isEmpty() ? section : section + child->objectName() +
"/";
129 if ( qobject_cast<QWidgetAction *>( action ) )
132 if ( mActions.contains( action ) )
136 if ( action->text().isEmpty() && action->objectName().isEmpty() )
144 QString key = action->objectName().isEmpty() ? action->text() : action->objectName();
152 const QString settingKey = mSettingsPath + ( section.isEmpty() || section.endsWith(
'/'_L1 ) ? section : section + u
"/"_s ) + key;
154 mActions.insert( action, { defaultSequence, settingKey } );
155 connect( action, &QObject::destroyed,
this, [action,
this]() { actionDestroyed( action ); } );
159 const QString sequence = settings.
value( settingKey, defaultSequence ).toString();
161 action->setShortcut( sequence );
162 if ( !action->toolTip().isEmpty() )
164 action->setToolTip( formatActionToolTip( action->toolTip() ) );
165 updateActionToolTip( action, sequence );
173 const auto it = mCommonActions.constFind(
static_cast< int >( commonAction ) );
174 if ( it == mCommonActions.constEnd() )
178 action->setText( it.value()->text() );
179 action->setToolTip( it.value()->toolTip() );
180 action->setShortcut( it.value()->shortcut() );
182 mLinkedCommonActions.insert( action, commonAction );
183 connect( action, &QObject::destroyed,
this, [action,
this]() { actionDestroyed( action ); } );
190 if ( shortcut->objectName().isEmpty() )
191 QgsLogger::warning( u
"Shortcut has no object name set: %1"_s.arg( shortcut->key().toString() ) );
193 QgsLogger::warning( u
"Duplicate shortcut registered: %1"_s.arg( shortcut->objectName() ) );
196 const QString settingKey = mSettingsPath + ( section.isEmpty() || section.endsWith(
'/'_L1 ) ? section : section + u
"/"_s ) + shortcut->objectName();
198 mShortcuts.insert( shortcut, { defaultSequence, settingKey } );
199 connect( shortcut, &QObject::destroyed,
this, [shortcut,
this]() { shortcutDestroyed( shortcut ); } );
203 const QString keySequence = settings.
value( settingKey, defaultSequence ).toString();
205 shortcut->setKey( keySequence );
212 if ( !mActions.contains( action ) )
215 mActions.remove( action );
221 if ( !mShortcuts.contains( shortcut ) )
224 mShortcuts.remove( shortcut );
230 return mActions.keys();
235 return mShortcuts.keys();
240 QList<QObject *> list;
241 ActionsHash::const_iterator actionIt = mActions.constBegin();
242 for ( ; actionIt != mActions.constEnd(); ++actionIt )
244 list << actionIt.key();
246 ShortcutsHash::const_iterator shortcutIt = mShortcuts.constBegin();
247 for ( ; shortcutIt != mShortcuts.constEnd(); ++shortcutIt )
249 list << shortcutIt.key();
256 if ( QAction *action = qobject_cast<QAction *>(
object ) )
258 else if ( QShortcut *shortcut = qobject_cast<QShortcut *>(
object ) )
266 return mActions.value( action ).first;
271 return mShortcuts.value( shortcut ).first;
286 if ( QAction *action = qobject_cast<QAction *>(
object ) )
288 else if ( QShortcut *shortcut = qobject_cast<QShortcut *>(
object ) )
296 if ( !mActions.contains( action ) )
300 action->setShortcut( sequence );
301 this->updateActionToolTip( action, sequence );
303 if ( action->property(
"commonAction" ).isValid() )
308 for (
auto it = mLinkedCommonActions.constBegin(); it != mLinkedCommonActions.constEnd(); ++it )
310 if ( it.value() == commonAction )
312 it.key()->setShortcut( action->shortcut() );
313 it.key()->setToolTip( action->toolTip() );
318 const QString settingKey = mActions[action].second;
322 settings.
setValue( settingKey, sequence );
328 if ( !mShortcuts.contains( shortcut ) )
332 shortcut->setKey( sequence );
334 const QString settingKey = mShortcuts[shortcut].second;
338 settings.
setValue( settingKey, sequence );
354 if ( sequence.isEmpty() )
357 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
359 if ( it.key()->shortcut() == sequence )
368 if ( sequence.isEmpty() )
371 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
373 if ( it.key()->key() == sequence )
382 const auto it = mCommonActions.constFind(
static_cast< int >( action ) );
383 if ( it == mCommonActions.constEnd() )
384 return QKeySequence();
386 return it.value()->shortcut();
391 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
393 if ( it.key()->objectName() == name )
396 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
398 QString key = it.key()->text();
409 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
411 if ( it.key()->objectName() == name )
418void QgsShortcutsManager::actionDestroyed( QAction *action )
420 mActions.remove( action );
421 mLinkedCommonActions.remove( action );
426 if (
auto action = qobject_cast<QAction *>(
object ) )
428 return mActions.value( action ).second;
430 else if (
auto shortcut = qobject_cast<QShortcut *>(
object ) )
432 return mShortcuts.value( shortcut ).second;
439 for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
441 if ( it.value().second == settingKey )
444 for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
446 if ( it.value().second == settingKey )
452void QgsShortcutsManager::shortcutDestroyed( QShortcut *shortcut )
454 mShortcuts.remove( shortcut );
457QString QgsShortcutsManager::formatActionToolTip(
const QString &toolTip )
459 if ( toolTip.isEmpty() )
462 const QStringList parts = toolTip.split(
'\n' );
463 QString formatted = u
"<b>%1</b>"_s.arg( parts.at( 0 ) );
464 if ( parts.count() > 1 )
466 for (
int i = 1; i < parts.count(); ++i )
467 formatted += u
"<p>%1</p>"_s.arg( parts.at( i ) );
473void QgsShortcutsManager::updateActionToolTip( QAction *action,
const QString &sequence )
475 QString current = action->toolTip();
476 const thread_local QRegularExpression rx( u
"\\s*\\((.*)\\)"_s );
478 QRegularExpressionMatch match;
479 if ( current.lastIndexOf( rx, -1, &match ) != -1 )
482 const QStringList parts = QKeySequence( match.captured( 1 ) ).toString().split(
"," );
483 if ( std::all_of( parts.constBegin(), parts.constEnd(), [](
const QString &part ) { return !part.trimmed().isEmpty(); } ) )
485 current = current.remove( match.capturedStart( 0 ), match.capturedLength( 0 ) );
489 if ( !sequence.isEmpty() )
491 action->setToolTip( current +
" (" + sequence +
")" );
495 action->setToolTip( current );
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
static void warning(const QString &msg)
Goes to qWarning.
Stores settings for use within QGIS.
QVariant value(const QString &key, const QVariant &defaultValue=QVariant(), Section section=NoSection) const
Returns the value for setting key.
void setValue(const QString &key, const QVariant &value, QgsSettings::Section section=QgsSettings::NoSection)
Sets the value of setting key to value.
bool setKeySequence(const QString &name, const QString &sequence)
Modifies an action or shortcut's key sequence.
bool registerShortcut(QShortcut *shortcut, const QString &defaultSequence=QString(), const QString §ion=QString())
Registers a QShortcut with the manager so the shortcut can be configured in GUI.
QList< QObject * > listAll() const
Returns a list of both actions and shortcuts in the manager.
void registerAllChildActions(QObject *object, bool recursive=false, const QString §ion=QString())
Automatically registers all QActions which are children of the passed object.
QObject * objectForSettingKey(const QString &name) const
Returns the QShortcut or QAction matching the the full setting key Return nullptr if the key was not ...
void registerAllChildShortcuts(QObject *object, bool recursive=false, const QString §ion=QString())
Automatically registers all QShortcuts which are children of the passed object.
QgsShortcutsManager(QObject *parent=nullptr, const QString &settingsRoot="/shortcuts/")
Constructor for QgsShortcutsManager.
QString objectDefaultKeySequence(QObject *object) const
Returns the default sequence for an object (either a QAction or QShortcut).
QList< QShortcut * > listShortcuts() const
Returns a list of shortcuts in the manager.
CommonAction
Contains common actions which are used across a variety of classes.
@ CodeRunScript
Run script.
@ CodeReformat
Reformat code.
@ CodeRunSelection
Run selection from script.
@ CodeToggleComment
Toggle code comments.
bool unregisterShortcut(QShortcut *shortcut)
Removes a shortcut from the manager.
QString defaultKeySequence(QAction *action) const
Returns the default sequence for an action.
bool setObjectKeySequence(QObject *object, const QString &sequence)
Modifies an object's (either a QAction or a QShortcut) key sequence.
bool registerAction(QAction *action, const QString &defaultShortcut=QString(), const QString §ion=QString())
Registers an action with the manager so the shortcut can be configured in GUI.
void registerAllChildren(QObject *object, bool recursive=false, const QString §ion=QString())
Automatically registers all QActions and QShortcuts which are children of the passed object.
QShortcut * shortcutByName(const QString &name) const
Returns a shortcut by its name, or nullptr if nothing found.
QAction * actionByName(const QString &name) const
Returns an action by its name, or nullptr if nothing found.
QShortcut * shortcutForSequence(const QKeySequence &sequence) const
Returns the shortcut which is associated for a key sequence, or nullptr if no shortcut is associated.
QKeySequence sequenceForCommonAction(CommonAction action) const
Returns the key sequence which is associated with a common action, or an empty sequence if no shortcu...
QList< QAction * > listActions() const
Returns a list of all actions in the manager.
QString objectSettingKey(QObject *object) const
Returns the full settings key matching the QShortcut or QAction Return an empty QString if the QObjec...
QAction * actionForSequence(const QKeySequence &sequence) const
Returns the action which is associated for a shortcut sequence, or nullptr if no action is associated...
void initializeCommonAction(QAction *action, CommonAction commonAction)
Initializes an action as a common action.
bool unregisterAction(QAction *action)
Removes an action from the manager.
~QgsShortcutsManager() override
QObject * objectForSequence(const QKeySequence &sequence) const
Returns the object (QAction or QShortcut) matching the specified key sequence,.