21 #include <QRegularExpression> 
   22 #include <QWidgetAction> 
   26   , mSettingsPath( settingsRoot )
 
   40     const QList< QAction * > actions = 
object->findChildren< QAction * >();
 
   41     for ( QAction *a : actions )
 
   43       registerAction( a, a->shortcut().toString( QKeySequence::NativeText ) );
 
   48     const QList< QObject *> children = 
object->children();
 
   49     for ( QObject *child : children )
 
   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 ( qobject_cast< QWidgetAction * >( action ) )
 
   88   if ( mActions.contains( action ) )
 
   93   if ( action->text().isEmpty() )
 
   95     QgsLogger::warning( QStringLiteral( 
"Action has no text set: %1" ).arg( action->objectName() ) );
 
   99     QgsLogger::warning( QStringLiteral( 
"Duplicate shortcut registered: %1" ).arg( action->text() ) );
 
  102   mActions.insert( action, defaultSequence );
 
  103   connect( action, &QObject::destroyed, 
this, &QgsShortcutsManager::actionDestroyed );
 
  105   QString actionText = action->text();
 
  106   actionText.remove( 
'&' ); 
 
  110   const QString sequence = settings.
value( mSettingsPath + actionText, defaultSequence ).toString();
 
  112   action->setShortcut( sequence );
 
  113   if ( !action->toolTip().isEmpty() )
 
  115     const QStringList parts = action->toolTip().split( 
'\n' );
 
  116     QString formatted = QStringLiteral( 
"<b>%1</b>" ).arg( parts.at( 0 ) );
 
  117     if ( parts.count() > 1 )
 
  119       for ( 
int i = 1; i < parts.count(); ++i )
 
  120         formatted += QStringLiteral( 
"<p>%1</p>" ).arg( parts.at( i ) );
 
  123     action->setToolTip( formatted );
 
  124     updateActionToolTip( action, sequence );
 
  134   if ( shortcut->objectName().isEmpty() )
 
  135     QgsLogger::warning( QStringLiteral( 
"Shortcut has no object name set: %1" ).arg( shortcut->key().toString() ) );
 
  137     QgsLogger::warning( QStringLiteral( 
"Duplicate shortcut registered: %1" ).arg( shortcut->objectName() ) );
 
  140   mShortcuts.insert( shortcut, defaultSequence );
 
  141   connect( shortcut, &QObject::destroyed, 
this, &QgsShortcutsManager::shortcutDestroyed );
 
  143   const QString shortcutName = shortcut->objectName();
 
  147   const QString keySequence = settings.
value( mSettingsPath + shortcutName, defaultSequence ).toString();
 
  149   shortcut->setKey( keySequence );
 
  156   if ( !mActions.contains( action ) )
 
  159   mActions.remove( action );
 
  165   if ( !mShortcuts.contains( shortcut ) )
 
  168   mShortcuts.remove( shortcut );
 
  174   return mActions.keys();
 
  179   return mShortcuts.keys();
 
  184   QList< QObject * > list;
 
  185   ActionsHash::const_iterator actionIt = mActions.constBegin();
 
  186   for ( ; actionIt != mActions.constEnd(); ++actionIt )
 
  188     list << actionIt.key();
 
  190   ShortcutsHash::const_iterator shortcutIt = mShortcuts.constBegin();
 
  191   for ( ; shortcutIt != mShortcuts.constEnd(); ++shortcutIt )
 
  193     list << shortcutIt.key();
 
  200   if ( QAction *action = qobject_cast< QAction * >( 
object ) )
 
  202   else if ( QShortcut *shortcut = qobject_cast< QShortcut * >( 
object ) )
 
  210   return mActions.value( action, QString() );
 
  215   return mShortcuts.value( shortcut, QString() );
 
  230   if ( QAction *action = qobject_cast< QAction * >( 
object ) )
 
  232   else if ( QShortcut *shortcut = qobject_cast< QShortcut * >( 
object ) )
 
  240   action->setShortcut( sequence );
 
  241   this->updateActionToolTip( action, sequence );
 
  243   QString actionText = action->text();
 
  244   actionText.remove( 
'&' ); 
 
  248   settings.
setValue( mSettingsPath + actionText, sequence );
 
  254   shortcut->setKey( sequence );
 
  256   const QString shortcutText = shortcut->objectName();
 
  260   settings.
setValue( mSettingsPath + shortcutText, sequence );
 
  276   if ( sequence.isEmpty() )
 
  279   for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
 
  281     if ( it.key()->shortcut() == sequence )
 
  290   if ( sequence.isEmpty() )
 
  293   for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
 
  295     if ( it.key()->key() == sequence )
 
  304   for ( ActionsHash::const_iterator it = mActions.constBegin(); it != mActions.constEnd(); ++it )
 
  306     if ( it.key()->text() == name )
 
  315   for ( ShortcutsHash::const_iterator it = mShortcuts.constBegin(); it != mShortcuts.constEnd(); ++it )
 
  317     if ( it.key()->objectName() == name )
 
  324 void QgsShortcutsManager::actionDestroyed()
 
  326   mActions.remove( qobject_cast<QAction *>( sender() ) );
 
  329 void QgsShortcutsManager::shortcutDestroyed()
 
  331   mShortcuts.remove( qobject_cast<QShortcut *>( sender() ) );
 
  334 void QgsShortcutsManager::updateActionToolTip( QAction *action, 
const QString &sequence )
 
  336   QString current = action->toolTip();
 
  338   const QRegularExpression rx( QStringLiteral( 
"\\(.*\\)" ) );
 
  339   current.replace( rx, QString() );
 
  341   if ( !sequence.isEmpty() )
 
  343     action->setToolTip( current + 
" (" + sequence + 
")" );
 
  347     action->setToolTip( current );