Skip to content

Commit

Permalink
[FEATURE] Loading and saving of shortcuts.
Browse files Browse the repository at this point in the history
Contributed by Alexander Bruy - thanks!


git-svn-id: http://svn.osgeo.org/qgis/trunk@12004 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder committed Nov 8, 2009
1 parent 5e1e77b commit 3b2de2d
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 10 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -38,3 +38,4 @@ Germán Carrillo
Milena Nowotarska
Anita Graser
Richard Duivenvoorde
Alexander Bruy
133 changes: 132 additions & 1 deletion src/app/qgsconfigureshortcutsdialog.cpp
Expand Up @@ -23,6 +23,11 @@
#include <QKeySequence>
#include <QMessageBox>

#include <QDomDocument>
#include <QFileDialog>
#include <QTextStream>
#include <QSettings>

QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget* parent )
: QDialog( parent ), mGettingShortcut( false )
{
Expand All @@ -31,6 +36,8 @@ QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget* parent )
connect( btnChangeShortcut, SIGNAL( clicked() ), this, SLOT( changeShortcut() ) );
connect( btnResetShortcut, SIGNAL( clicked() ), this, SLOT( resetShortcut() ) );
connect( btnSetNoShortcut, SIGNAL( clicked() ), this, SLOT( setNoShortcut() ) );
connect( btnSaveShortcuts, SIGNAL( clicked() ), this, SLOT( saveShortcuts() ) );
connect( btnLoadShortcuts, SIGNAL( clicked() ), this, SLOT( loadShortcuts() ) );

connect( treeActions, SIGNAL( currentItemChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ),
this, SLOT( actionChanged( QTreeWidgetItem*, QTreeWidgetItem* ) ) );
Expand Down Expand Up @@ -64,6 +71,131 @@ void QgsConfigureShortcutsDialog::populateActions()
actionChanged( treeActions->currentItem(), NULL );
}

void QgsConfigureShortcutsDialog::saveShortcuts()
{
QString fileName = QFileDialog::getSaveFileName( this, tr( "Save shortcuts" ), ".", tr( "XML file (*.xml);; All files (*.*)" ) );

if ( fileName.isEmpty() )
return;

QFile file( fileName );
if ( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
{
QMessageBox::warning( this, tr( "Saving shortcuts" ),
tr( "Cannot write file %1:\n%2." )
.arg( fileName )
.arg( file.errorString() ) );
return;
}

QSettings settings;

QDomDocument doc( "shortcuts" );
QDomElement root = doc.createElement( "qgsshortcuts" );
root.setAttribute( "version", "1.0" );
root.setAttribute( "locale", settings.value( "locale/userLocale", "en_US" ).toString() );
doc.appendChild(root);

settings.beginGroup( "/shortcuts/" );
QStringList keys = settings.childKeys();

QString actionText;
QString actionShortcut;

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

QDomElement el = doc.createElement( "act" );
el.setAttribute( "name", actionText );
el.setAttribute( "shortcut", actionShortcut );
root.appendChild( el );
}

QTextStream out( &file );
doc.save(out, 4);
}

void QgsConfigureShortcutsDialog::loadShortcuts()
{
QString fileName = QFileDialog::getOpenFileName( this, tr( "Load shortcuts" ), ".", tr( "XML file (*.xml);; All files (*.*)" ) );

if ( fileName.isEmpty() )
{
return;
}

QFile file( fileName );
if ( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
{
QMessageBox::warning( this, tr( "Loading shortcuts" ),
tr( "Cannot read file %1:\n%2." )
.arg( fileName )
.arg( file.errorString() ) );
return;
}

QDomDocument doc;
QString errorStr;
int errorLine;
int errorColumn;

if ( !doc.setContent( &file, true, &errorStr, &errorLine, &errorColumn ) )
{
QMessageBox::information( this, tr( "Loading shortcuts" ),
tr( "Parse error at line %1, column %2:\n%3" )
.arg( errorLine )
.arg( errorColumn )
.arg( errorStr ) );
return;
}

QDomElement root = doc.documentElement();
if ( root.tagName() != "qgsshortcuts" )
{
QMessageBox::information( this, tr( "Loading shortcuts" ),
tr( "The file is not an shortcuts exchange file.") );
return;
}

QSettings settings;
QString currentLocale;

bool localeOverrideFlag = settings.value( "locale/overrideFlag", false ).toBool();
if ( localeOverrideFlag )
{
currentLocale = settings.value( "locale/userLocale", "en_US" ).toString();
}
else // use QGIS locale
{
currentLocale = QLocale::system().name();
}

if ( root.attribute( "locale" ) != currentLocale )
{
QMessageBox::information( this, tr( "Loading shortcuts" ),
tr( "The file contains shortcuts created with different locale, so you can't use it.") );
return;
}

QAction* action;
QString actionName;
QString actionShortcut;

QDomElement child = root.firstChildElement();
while ( !child.isNull() )
{
actionName = child.attribute( "name" );
actionShortcut = child.attribute( "shortcut" );
action = QgsShortcutsManager::instance()->actionByName( actionName );
QgsShortcutsManager::instance()->setActionShortcut( action, actionShortcut );
child = child.nextSiblingElement();
}

treeActions->clear();
populateActions();
}

void QgsConfigureShortcutsDialog::changeShortcut()
{
Expand Down Expand Up @@ -197,7 +329,6 @@ void QgsConfigureShortcutsDialog::keyReleaseEvent( QKeyEvent * event )
}
}


void QgsConfigureShortcutsDialog::updateShortcutText()
{
// update text of the button so that user can see what has typed already
Expand Down
2 changes: 2 additions & 0 deletions src/app/qgsconfigureshortcutsdialog.h
Expand Up @@ -43,6 +43,8 @@ class QgsConfigureShortcutsDialog : public QDialog, private Ui::QgsConfigureShor
void changeShortcut();
void resetShortcut();
void setNoShortcut();
void saveShortcuts();
void loadShortcuts();

void actionChanged( QTreeWidgetItem* current, QTreeWidgetItem* previous );

Expand Down
11 changes: 11 additions & 0 deletions src/app/qgsshortcutsmanager.cpp
Expand Up @@ -92,3 +92,14 @@ QAction* QgsShortcutsManager::actionForShortcut( QKeySequence s )

return NULL;
}

QAction* QgsShortcutsManager::actionByName( QString name )
{
for ( ActionsHash::iterator it = mActions.begin(); it != mActions.end(); ++it )
{
if ( it.key()->text() == name )
return it.key();
}

return NULL;
}
3 changes: 3 additions & 0 deletions src/app/qgsshortcutsmanager.h
Expand Up @@ -49,6 +49,9 @@ class QgsShortcutsManager
//! return action which is associated for the shortcut, NULL if no action is associated
QAction* actionForShortcut( QKeySequence s );

// return action by it's name. NULL if nothing found
QAction* actionByName( QString name );

protected:
QgsShortcutsManager();

Expand Down
52 changes: 43 additions & 9 deletions src/ui/qgsconfigureshortcutsdialog.ui
Expand Up @@ -47,41 +47,75 @@
<property name="checkable">
<bool>true</bool>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnSetNoShortcut">
<property name="text">
<string>Set none</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnResetShortcut">
<property name="text">
<string>Set default</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="btnLoadShortcuts">
<property name="text">
<string>Load...</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="btnSaveShortcuts">
<property name="text">
<string>Save...</string>
</property>
<property name="autoDefault">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<tabstops>
<tabstop>treeActions</tabstop>
<tabstop>btnChangeShortcut</tabstop>
<tabstop>btnSetNoShortcut</tabstop>
<tabstop>btnResetShortcut</tabstop>
<tabstop>btnLoadShortcuts</tabstop>
<tabstop>btnSaveShortcuts</tabstop>
<tabstop>buttonBox</tabstop>
</tabstops>
<resources/>
Expand Down

0 comments on commit 3b2de2d

Please sign in to comment.