Skip to content
Permalink
Browse files

[FEATURE] Loading and saving of shortcuts.

Contributed by Alexander Bruy - thanks!


git-svn-id: http://svn.osgeo.org/qgis/trunk@12004 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
wonder
wonder committed Nov 8, 2009
1 parent 5e1e77b commit 3b2de2db3765214bd3af86e1daa22be7cf870e7a
@@ -38,3 +38,4 @@ Germán Carrillo
Milena Nowotarska
Anita Graser
Richard Duivenvoorde
Alexander Bruy
@@ -23,6 +23,11 @@
#include <QKeySequence>
#include <QMessageBox>

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

QgsConfigureShortcutsDialog::QgsConfigureShortcutsDialog( QWidget* parent )
: QDialog( parent ), mGettingShortcut( false )
{
@@ -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* ) ) );
@@ -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()
{
@@ -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
@@ -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 );

@@ -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;
}
@@ -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();

@@ -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/>

0 comments on commit 3b2de2d

Please sign in to comment.
You can’t perform that action at this time.