From d9c23a07e5af3a6c8d261fc9b12b6330c4fcc763 Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Mon, 5 Jul 2010 18:13:29 +0400 Subject: [PATCH] Allow assigning keyboard shortcuts to the groups for fast switching. --- config.cc | 16 +++++++++++++++- config.hh | 3 ++- dictgroupwidget.ui | 15 +++++++++++++++ groupcombobox.cc | 38 ++++++++++++++++++++++++++++++++++++++ groupcombobox.hh | 8 ++++++++ groups_widgets.cc | 4 ++++ instances.cc | 4 +++- instances.hh | 1 + 8 files changed, 86 insertions(+), 3 deletions(-) diff --git a/config.cc b/config.cc index b1055333..2d74ec4f 100644 --- a/config.cc +++ b/config.cc @@ -73,7 +73,9 @@ HotKey::HotKey( QKeySequence const & seq ): QKeySequence HotKey::toKeySequence() const { - return QKeySequence( key1 | modifiers, key2 | modifiers ); + int v2 = key2 ? ( key2 | modifiers ): 0; + + return QKeySequence( key1 | modifiers, v2 ); } Preferences::Preferences(): @@ -181,6 +183,9 @@ Group loadGroup( QDomElement grp, unsigned * nextId = 0 ) g.name = grp.attribute( "name" ); g.icon = grp.attribute( "icon" ); + if ( !grp.attribute( "shortcut" ).isEmpty() ) + g.shortcut = QKeySequence::fromString( grp.attribute( "shortcut" ) ); + QDomNodeList dicts = grp.elementsByTagName( "dictionary" ); for( unsigned y = 0; y < dicts.length(); ++y ) @@ -632,6 +637,15 @@ void saveGroup( Group const & data, QDomElement & group ) group.setAttributeNode( icon ); } + if ( !data.shortcut.isEmpty() ) + { + QDomAttr shortcut = dd.createAttribute( "shortcut" ); + + shortcut.setValue( data.shortcut.toString() ); + + group.setAttributeNode( shortcut ); + } + for( vector< DictionaryRef >::const_iterator j = data.dictionaries.begin(); j != data.dictionaries.end(); ++j ) { QDomElement dictionary = dd.createElement( "dictionary" ); diff --git a/config.hh b/config.hh index fec41347..365b80f8 100644 --- a/config.hh +++ b/config.hh @@ -74,13 +74,14 @@ struct Group { unsigned id; QString name, icon; + QKeySequence shortcut; vector< DictionaryRef > dictionaries; Group(): id( 0 ) {} bool operator == ( Group const & other ) const { return id == other.id && name == other.name && icon == other.icon && - dictionaries == other.dictionaries; } + dictionaries == other.dictionaries && shortcut == other.shortcut; } bool operator != ( Group const & other ) const { return ! operator == ( other ); } diff --git a/dictgroupwidget.ui b/dictgroupwidget.ui index f2db4edf..ba89583c 100644 --- a/dictgroupwidget.ui +++ b/dictgroupwidget.ui @@ -54,6 +54,16 @@ + + + + Shortcut: + + + + + + @@ -64,6 +74,11 @@ QListWidget
groups_widgets.hh
+ + HotKeyEdit + QLineEdit +
hotkeyedit.hh
+
diff --git a/groupcombobox.cc b/groupcombobox.cc index 4833c0a4..e4a54523 100644 --- a/groupcombobox.cc +++ b/groupcombobox.cc @@ -2,6 +2,8 @@ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ #include "groupcombobox.hh" +#include +#include GroupComboBox::GroupComboBox( QWidget * parent ): QComboBox( parent ), popupAction( this ) @@ -25,6 +27,11 @@ void GroupComboBox::fill( Instances::Groups const & groups ) clear(); + for( QMap< int, int >::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i ) + releaseShortcut( i.key() ); + + shortcuts.clear(); + for( unsigned x = 0; x < groups.size(); ++x ) { addItem( groups[ x ].makeIcon(), @@ -32,9 +39,40 @@ void GroupComboBox::fill( Instances::Groups const & groups ) if ( prevId == groups[ x ].id ) setCurrentIndex( x ); + + // Create a shortcut + + if ( !groups[ x ].shortcut.isEmpty() ) + { + int id = grabShortcut( groups[ x ].shortcut ); + setShortcutEnabled( id ); + + shortcuts.insert( id, x ); + } } } +bool GroupComboBox::event( QEvent * event ) +{ + if ( event->type() == QEvent::Shortcut ) + { + QShortcutEvent * ev = ( QShortcutEvent * ) event; + + QMap< int, int >::const_iterator i = shortcuts.find( ev->shortcutId() ); + + if ( i != shortcuts.end() ) + { + ev->accept(); + setCurrentIndex( i.value() ); + return true; + } + } + + return QComboBox::event( event ); + +} + + void GroupComboBox::setCurrentGroup( unsigned id ) { for( int x = 0; x < count(); ++x ) diff --git a/groupcombobox.hh b/groupcombobox.hh index 7539b21d..140d66d4 100644 --- a/groupcombobox.hh +++ b/groupcombobox.hh @@ -24,8 +24,15 @@ public: /// does nothing. void setCurrentGroup( unsigned id ); + /// Returns current group. unsigned getCurrentGroup() const; + +protected: + + /// We handle shortcut events here. + virtual bool event( QEvent * event ); + private slots: void popupGroups(); @@ -33,6 +40,7 @@ private slots: private: QAction popupAction; + QMap< int, int > shortcuts; }; #endif diff --git a/groups_widgets.cc b/groups_widgets.cc index 9d573822..e1bd7984 100644 --- a/groups_widgets.cc +++ b/groups_widgets.cc @@ -42,6 +42,8 @@ DictGroupWidget::DictGroupWidget( QWidget * parent, if ( icons[ x ] == group.icon ) ui.groupIcon->setCurrentIndex( x + 1 ); } + + ui.shortcut->setHotKey( Config::HotKey( group.shortcut ) ); } Config::Group DictGroupWidget::makeGroup() const @@ -54,6 +56,8 @@ Config::Group DictGroupWidget::makeGroup() const g.icon = ui.groupIcon->itemData( ui.groupIcon->currentIndex() ).toString(); + g.shortcut = ui.shortcut->getHotKey().toKeySequence(); + return g.makeConfigGroup(); } diff --git a/instances.cc b/instances.cc index 6334596e..5e346114 100644 --- a/instances.cc +++ b/instances.cc @@ -13,7 +13,8 @@ Group::Group( Config::Group const & cfgGroup, vector< sptr< Dictionary::Class > > const & allDictionaries ): id( cfgGroup.id ), name( cfgGroup.name ), - icon( cfgGroup.icon ) + icon( cfgGroup.icon ), + shortcut( cfgGroup.shortcut ) { for( unsigned x = 0; x < cfgGroup.dictionaries.size(); ++x ) { @@ -56,6 +57,7 @@ Config::Group Group::makeConfigGroup() result.id = id; result.name = name; result.icon = icon; + result.shortcut = shortcut; for( unsigned x = 0; x < dictionaries.size(); ++x ) result.dictionaries.push_back( diff --git a/instances.hh b/instances.hh index 0a989eb1..440e18e7 100644 --- a/instances.hh +++ b/instances.hh @@ -22,6 +22,7 @@ struct Group { unsigned id; QString name, icon; + QKeySequence shortcut; vector< sptr< Dictionary::Class > > dictionaries; /// Instantiates the given group from its configuration. If some dictionary