Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow exporting either all or only user-defined keyboard shortcuts (r…
…efs #40995)
  • Loading branch information
alexbruy committed Sep 5, 2021
1 parent 98a902b commit fa5f088
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
60 changes: 48 additions & 12 deletions src/gui/qgsconfigureshortcutsdialog.cpp
Expand Up @@ -28,14 +28,27 @@
#include <QDomDocument>
#include <QFileDialog>
#include <QTextStream>
#include <QDebug>
#include <QMenu>
#include <QAction>

QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget *parent, QgsShortcutsManager *manager )
: QDialog( parent )
, mManager( manager )
{
setupUi( this );
QgsGui::enableAutoGeometryRestore( this );

mSaveMenu = new QMenu( this );
mSaveUserShortcuts = new QAction( tr( "Save User Shortcuts…" ), this );
mSaveMenu->addAction( mSaveUserShortcuts );
connect( mSaveUserShortcuts, &QAction::triggered, this, [this] { saveShortcuts( false ); } );

mSaveAllShortcuts = new QAction( tr( "Save All Shportcuts…" ), this );
mSaveMenu->addAction( mSaveAllShortcuts );
connect( mSaveAllShortcuts, &QAction::triggered, this, [this] { saveShortcuts(); } );

btnSaveShortcuts->setMenu( mSaveMenu );

connect( mLeFilter, &QgsFilterLineEdit::textChanged, this, &QgsConfigureShortcutsDialog::mLeFilter_textChanged );

if ( !mManager )
Expand All @@ -45,7 +58,6 @@ QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget *parent, QgsSh
connect( btnChangeShortcut, &QAbstractButton::clicked, this, &QgsConfigureShortcutsDialog::changeShortcut );
connect( btnResetShortcut, &QAbstractButton::clicked, this, &QgsConfigureShortcutsDialog::resetShortcut );
connect( btnSetNoShortcut, &QAbstractButton::clicked, this, &QgsConfigureShortcutsDialog::setNoShortcut );
connect( btnSaveShortcuts, &QAbstractButton::clicked, this, &QgsConfigureShortcutsDialog::saveShortcuts );
connect( btnLoadShortcuts, &QAbstractButton::clicked, this, &QgsConfigureShortcutsDialog::loadShortcuts );

connect( treeActions, &QTreeWidget::currentItemChanged,
Expand Down Expand Up @@ -107,7 +119,7 @@ void QgsConfigureShortcutsDialog::populateActions()
actionChanged( treeActions->currentItem(), nullptr );
}

void QgsConfigureShortcutsDialog::saveShortcuts()
void QgsConfigureShortcutsDialog::saveShortcuts( bool saveAll )
{
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save Shortcuts" ), QDir::homePath(),
tr( "XML file" ) + " (*.xml);;" + tr( "All files" ) + " (*)" );
Expand Down Expand Up @@ -139,18 +151,42 @@ void QgsConfigureShortcutsDialog::saveShortcuts()
root.setAttribute( QStringLiteral( "locale" ), settings.value( QgsApplication::settingsLocaleUserLocale.key(), "en_US" ).toString() );
doc.appendChild( root );

settings.beginGroup( mManager->settingsPath() );
QStringList keys = settings.childKeys();
const QList<QObject *> objects = mManager->listAll();
for ( QObject *obj : objects )
{
QString actionText;
QString actionShortcut;
QKeySequence sequence;

QString actionText;
QString actionShortcut;
if ( QAction *action = qobject_cast< QAction * >( obj ) )
{
actionText = action->text().remove( '&' );
actionShortcut = action->shortcut().toString( QKeySequence::NativeText );
sequence = mManager->defaultKeySequence( action );
}
else if ( QShortcut *shortcut = qobject_cast< QShortcut * >( obj ) )
{
actionText = shortcut->whatsThis();
actionShortcut = shortcut->key().toString( QKeySequence::NativeText );
sequence = mManager->defaultKeySequence( shortcut );
}
else
{
continue;
}

for ( int i = 0; i < keys.count(); ++i )
{
actionText = keys[ i ];
actionShortcut = settings.value( actionText, "" ).toString();
if ( actionText.isEmpty() || actionShortcut.isEmpty() )
{
continue;
}

// skip unchanged shortcuts if only user-definied were requested
if ( !saveAll && sequence == QKeySequence( actionShortcut ) )
{
continue;
}

QDomElement el = doc.createElement( QStringLiteral( "act" ) );
QDomElement el = doc.createElement( QStringLiteral( "action" ) );
el.setAttribute( QStringLiteral( "name" ), actionText );
el.setAttribute( QStringLiteral( "shortcut" ), actionShortcut );
root.appendChild( el );
Expand Down
7 changes: 6 additions & 1 deletion src/gui/qgsconfigureshortcutsdialog.h
Expand Up @@ -55,7 +55,6 @@ class GUI_EXPORT QgsConfigureShortcutsDialog : public QDialog, private Ui::QgsCo
void changeShortcut();
void resetShortcut();
void setNoShortcut();
void saveShortcuts();
void loadShortcuts();
void mLeFilter_textChanged( const QString &text );

Expand All @@ -78,11 +77,17 @@ class GUI_EXPORT QgsConfigureShortcutsDialog : public QDialog, private Ui::QgsCo
//! Returns the currently selected QShortcut, or NULLPTR if no shortcut selected
QShortcut *currentShortcut();

//! Saves custom or all shortcuts to XML file
void saveShortcuts( bool saveAll = true );

void setGettingShortcut( bool getting );
void setCurrentActionShortcut( const QKeySequence &s );
void updateShortcutText();

QgsShortcutsManager *mManager = nullptr;
QMenu *mSaveMenu = nullptr;
QAction *mSaveUserShortcuts = nullptr;
QAction *mSaveAllShortcuts = nullptr;

bool mGettingShortcut = false;
int mModifiers = 0, mKey = 0;
Expand Down

0 comments on commit fa5f088

Please sign in to comment.