Allow assigning keyboard shortcuts to the groups for fast switching.

This commit is contained in:
Konstantin Isakov 2010-07-05 18:13:29 +04:00
parent 621d842394
commit d9c23a07e5
8 changed files with 86 additions and 3 deletions

View file

@ -73,7 +73,9 @@ HotKey::HotKey( QKeySequence const & seq ):
QKeySequence HotKey::toKeySequence() const QKeySequence HotKey::toKeySequence() const
{ {
return QKeySequence( key1 | modifiers, key2 | modifiers ); int v2 = key2 ? ( key2 | modifiers ): 0;
return QKeySequence( key1 | modifiers, v2 );
} }
Preferences::Preferences(): Preferences::Preferences():
@ -181,6 +183,9 @@ Group loadGroup( QDomElement grp, unsigned * nextId = 0 )
g.name = grp.attribute( "name" ); g.name = grp.attribute( "name" );
g.icon = grp.attribute( "icon" ); g.icon = grp.attribute( "icon" );
if ( !grp.attribute( "shortcut" ).isEmpty() )
g.shortcut = QKeySequence::fromString( grp.attribute( "shortcut" ) );
QDomNodeList dicts = grp.elementsByTagName( "dictionary" ); QDomNodeList dicts = grp.elementsByTagName( "dictionary" );
for( unsigned y = 0; y < dicts.length(); ++y ) for( unsigned y = 0; y < dicts.length(); ++y )
@ -632,6 +637,15 @@ void saveGroup( Group const & data, QDomElement & group )
group.setAttributeNode( icon ); 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 ) for( vector< DictionaryRef >::const_iterator j = data.dictionaries.begin(); j != data.dictionaries.end(); ++j )
{ {
QDomElement dictionary = dd.createElement( "dictionary" ); QDomElement dictionary = dd.createElement( "dictionary" );

View file

@ -74,13 +74,14 @@ struct Group
{ {
unsigned id; unsigned id;
QString name, icon; QString name, icon;
QKeySequence shortcut;
vector< DictionaryRef > dictionaries; vector< DictionaryRef > dictionaries;
Group(): id( 0 ) {} Group(): id( 0 ) {}
bool operator == ( Group const & other ) const bool operator == ( Group const & other ) const
{ return id == other.id && name == other.name && icon == other.icon && { 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 bool operator != ( Group const & other ) const
{ return ! operator == ( other ); } { return ! operator == ( other ); }

View file

@ -54,6 +54,16 @@
</property> </property>
</spacer> </spacer>
</item> </item>
<item>
<widget class="QLabel" name="label_2">
<property name="text">
<string>Shortcut:</string>
</property>
</widget>
</item>
<item>
<widget class="HotKeyEdit" name="shortcut"/>
</item>
</layout> </layout>
</item> </item>
</layout> </layout>
@ -64,6 +74,11 @@
<extends>QListWidget</extends> <extends>QListWidget</extends>
<header>groups_widgets.hh</header> <header>groups_widgets.hh</header>
</customwidget> </customwidget>
<customwidget>
<class>HotKeyEdit</class>
<extends>QLineEdit</extends>
<header>hotkeyedit.hh</header>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>

View file

@ -2,6 +2,8 @@
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */ * Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include "groupcombobox.hh" #include "groupcombobox.hh"
#include <QEvent>
#include <QShortcutEvent>
GroupComboBox::GroupComboBox( QWidget * parent ): QComboBox( parent ), GroupComboBox::GroupComboBox( QWidget * parent ): QComboBox( parent ),
popupAction( this ) popupAction( this )
@ -25,6 +27,11 @@ void GroupComboBox::fill( Instances::Groups const & groups )
clear(); 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 ) for( unsigned x = 0; x < groups.size(); ++x )
{ {
addItem( groups[ x ].makeIcon(), addItem( groups[ x ].makeIcon(),
@ -32,9 +39,40 @@ void GroupComboBox::fill( Instances::Groups const & groups )
if ( prevId == groups[ x ].id ) if ( prevId == groups[ x ].id )
setCurrentIndex( x ); 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 ) void GroupComboBox::setCurrentGroup( unsigned id )
{ {
for( int x = 0; x < count(); ++x ) for( int x = 0; x < count(); ++x )

View file

@ -24,8 +24,15 @@ public:
/// does nothing. /// does nothing.
void setCurrentGroup( unsigned id ); void setCurrentGroup( unsigned id );
/// Returns current group. /// Returns current group.
unsigned getCurrentGroup() const; unsigned getCurrentGroup() const;
protected:
/// We handle shortcut events here.
virtual bool event( QEvent * event );
private slots: private slots:
void popupGroups(); void popupGroups();
@ -33,6 +40,7 @@ private slots:
private: private:
QAction popupAction; QAction popupAction;
QMap< int, int > shortcuts;
}; };
#endif #endif

View file

@ -42,6 +42,8 @@ DictGroupWidget::DictGroupWidget( QWidget * parent,
if ( icons[ x ] == group.icon ) if ( icons[ x ] == group.icon )
ui.groupIcon->setCurrentIndex( x + 1 ); ui.groupIcon->setCurrentIndex( x + 1 );
} }
ui.shortcut->setHotKey( Config::HotKey( group.shortcut ) );
} }
Config::Group DictGroupWidget::makeGroup() const Config::Group DictGroupWidget::makeGroup() const
@ -54,6 +56,8 @@ Config::Group DictGroupWidget::makeGroup() const
g.icon = ui.groupIcon->itemData( ui.groupIcon->currentIndex() ).toString(); g.icon = ui.groupIcon->itemData( ui.groupIcon->currentIndex() ).toString();
g.shortcut = ui.shortcut->getHotKey().toKeySequence();
return g.makeConfigGroup(); return g.makeConfigGroup();
} }

View file

@ -13,7 +13,8 @@ Group::Group( Config::Group const & cfgGroup,
vector< sptr< Dictionary::Class > > const & allDictionaries ): vector< sptr< Dictionary::Class > > const & allDictionaries ):
id( cfgGroup.id ), id( cfgGroup.id ),
name( cfgGroup.name ), name( cfgGroup.name ),
icon( cfgGroup.icon ) icon( cfgGroup.icon ),
shortcut( cfgGroup.shortcut )
{ {
for( unsigned x = 0; x < cfgGroup.dictionaries.size(); ++x ) for( unsigned x = 0; x < cfgGroup.dictionaries.size(); ++x )
{ {
@ -56,6 +57,7 @@ Config::Group Group::makeConfigGroup()
result.id = id; result.id = id;
result.name = name; result.name = name;
result.icon = icon; result.icon = icon;
result.shortcut = shortcut;
for( unsigned x = 0; x < dictionaries.size(); ++x ) for( unsigned x = 0; x < dictionaries.size(); ++x )
result.dictionaries.push_back( result.dictionaries.push_back(

View file

@ -22,6 +22,7 @@ struct Group
{ {
unsigned id; unsigned id;
QString name, icon; QString name, icon;
QKeySequence shortcut;
vector< sptr< Dictionary::Class > > dictionaries; vector< sptr< Dictionary::Class > > dictionaries;
/// Instantiates the given group from its configuration. If some dictionary /// Instantiates the given group from its configuration. If some dictionary