21 #include <QRegularExpression> 
   25   , mSettingsPath( settingsRoot )
 
   39     const QList< QAction * > actions = 
object->findChildren< QAction * >();
 
   40     const auto constActions = actions;
 
   41     for ( QAction *a : constActions )
 
   43       registerAction( a, a->shortcut().toString( QKeySequence::NativeText ) );
 
   48     const auto constChildren = 
object->children();
 
   49     for ( QObject *child : constChildren )
 
   51       if ( QAction *a = qobject_cast<QAction *>( child ) )
 
   53         registerAction( a, a->shortcut().toString( QKeySequence::NativeText ) );
 
   63     const QList< QShortcut * > shortcuts = 
object->findChildren< QShortcut * >();
 
   64     const auto constShortcuts = shortcuts;
 
   65     for ( QShortcut *s : constShortcuts )
 
   72     const auto constChildren = 
object->children();
 
   73     for ( QObject *child : constChildren )
 
   75       if ( QShortcut *s = qobject_cast<QShortcut *>( child ) )
 
   85   if ( mActions.contains( action ) )
 
   91     QgsLogger::warning( QStringLiteral( 
"Duplicate shortcut registered: %1" ).arg( action->text() ) );
 
   94   mActions.insert( action, defaultSequence );
 
   95   connect( action, &QObject::destroyed, 
this, &QgsShortcutsManager::actionDestroyed );
 
   97   QString actionText = action->text();
 
   98   actionText.remove( 
'&' ); 
 
  102   const QString sequence = settings.
value( mSettingsPath + actionText, defaultSequence ).toString();
 
  104   action->setShortcut( sequence );
 
  105   if ( !action->toolTip().isEmpty() )
 
  107     const QStringList parts = action->toolTip().split( 
'\n' );
 
  108     QString formatted = QStringLiteral( 
"<b>%1</b>" ).arg( parts.at( 0 ) );
 
  109     if ( parts.count() > 1 )
 
  111       for ( 
int i = 1; i < parts.count(); ++i )
 
  112         formatted += QStringLiteral( 
"<p>%1</p>" ).arg( parts.at( i ) );
 
  115     action->setToolTip( formatted );
 
  116     updateActionToolTip( action, sequence );
 
  127     QgsLogger::warning( QStringLiteral( 
"Duplicate shortcut registered: %1" ).arg( shortcut->objectName() ) );
 
  130   mShortcuts.insert( shortcut, defaultSequence );
 
  131   connect( shortcut, &QObject::destroyed, 
this, &QgsShortcutsManager::shortcutDestroyed );
 
  133   const QString shortcutName = shortcut->objectName();
 
  137   const QString keySequence = settings.
value( mSettingsPath + shortcutName, defaultSequence ).toString();
 
  139   shortcut->setKey( keySequence );
 
  146   if ( !mActions.contains( action ) )
 
  149   mActions.remove( action );
 
  155   if ( !mShortcuts.contains( shortcut ) )
 
  158   mShortcuts.remove( shortcut );
 
  164   return mActions.keys();
 
  169   return mShortcuts.keys();
 
  174   QList< QObject * > list;
 
  175   ActionsHash::const_iterator actionIt = mActions.constBegin();
 
  176   for ( ; actionIt != mActions.constEnd(); ++actionIt )
 
  178     list << actionIt.key();
 
  180   ShortcutsHash::const_iterator shortcutIt = mShortcuts.constBegin();
 
  181   for ( ; shortcutIt != mShortcuts.constEnd(); ++shortcutIt )
 
  183     list << shortcutIt.key();
 
  190   if ( QAction *action = qobject_cast< QAction * >( 
object ) )
 
  192   else if ( QShortcut *shortcut = qobject_cast< QShortcut * >( 
object ) )
 
  200   return mActions.value( action, QString() );
 
  205   return mShortcuts.value( shortcut, QString() );
 
  220   if ( QAction *action = qobject_cast< QAction * >( 
object ) )
 
  222   else if ( QShortcut *shortcut = qobject_cast< QShortcut * >( 
object ) )
 
  230   action->setShortcut( sequence );
 
  231   this->updateActionToolTip( action, sequence );
 
  233   QString actionText = action->text();
 
  234   actionText.remove( 
'&' ); 
 
  238   settings.
setValue( mSettingsPath + actionText, sequence );
 
  244   shortcut->setKey( sequence );
 
  246   const QString shortcutText = shortcut->objectName();
 
  250   settings.
setValue( mSettingsPath + shortcutText, sequence );
 
  266   if ( sequence.isEmpty() )
 
  269   for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
 
  271     if ( it.key()->shortcut() == sequence )
 
  280   if ( sequence.isEmpty() )
 
  283   for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
 
  285     if ( it.key()->key() == sequence )
 
  294   for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
 
  296     if ( it.key()->text() == name )
 
  305   for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
 
  307     if ( it.key()->objectName() == name )
 
  314 void QgsShortcutsManager::actionDestroyed()
 
  316   mActions.remove( qobject_cast<QAction *>( sender() ) );
 
  319 void QgsShortcutsManager::shortcutDestroyed()
 
  321   mShortcuts.remove( qobject_cast<QShortcut *>( sender() ) );
 
  324 void QgsShortcutsManager::updateActionToolTip( QAction *action, 
const QString &sequence )
 
  326   QString current = action->toolTip();
 
  328   const QRegularExpression rx( QStringLiteral( 
"\\(.*\\)" ) );
 
  329   current.replace( rx, QString() );
 
  331   if ( !sequence.isEmpty() )
 
  333     action->setToolTip( current + 
" (" + sequence + 
")" );
 
  337     action->setToolTip( current );
 
static void warning(const QString &msg)
Goes to qWarning.
This class is a composition of two QSettings instances:
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.
QList< QObject * > listAll() const
Returns a list of both actions and shortcuts in the manager.
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.
bool unregisterShortcut(QShortcut *shortcut)
Removes a shortcut from the manager.
bool registerShortcut(QShortcut *shortcut, const QString &defaultSequence=QString())
Registers a QShortcut with the manager so the shortcut can be configured in GUI.
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())
Registers an action with the manager so the shortcut can be configured in GUI.
void registerAllChildren(QObject *object, bool recursive=false)
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.
void registerAllChildShortcuts(QObject *object, bool recursive=false)
Automatically registers all QShortcuts which are children of the passed object.
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.
QList< QAction * > listActions() const
Returns a list of all actions in the manager.
QAction * actionForSequence(const QKeySequence &sequence) const
Returns the action which is associated for a shortcut sequence, or nullptr if no action is associated...
void registerAllChildActions(QObject *object, bool recursive=false)
Automatically registers all QActions which are children of the passed object.
bool unregisterAction(QAction *action)
Removes an action from the manager.
QObject * objectForSequence(const QKeySequence &sequence) const
Returns the object (QAction or QShortcut) matching the specified key sequence,.