2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-02-01 00:08:08 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "groupcombobox.hh"
|
2010-07-05 14:13:29 +00:00
|
|
|
#include <QEvent>
|
|
|
|
#include <QShortcutEvent>
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2009-05-10 11:49:14 +00:00
|
|
|
GroupComboBox::GroupComboBox( QWidget * parent ): QComboBox( parent ),
|
2012-10-01 12:43:00 +00:00
|
|
|
popupAction( this ),
|
|
|
|
selectNextAction( this ),
|
|
|
|
selectPreviousAction( this )
|
2009-02-01 00:08:08 +00:00
|
|
|
{
|
|
|
|
setSizeAdjustPolicy( AdjustToContents );
|
2009-05-10 11:49:14 +00:00
|
|
|
setToolTip( tr( "Choose a Group (Alt+G)" ) );
|
|
|
|
|
|
|
|
popupAction.setShortcut( QKeySequence( "Alt+G" ) );
|
|
|
|
connect( &popupAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( popupGroups() ) );
|
|
|
|
|
|
|
|
addAction( &popupAction );
|
2012-10-01 12:43:00 +00:00
|
|
|
|
|
|
|
selectNextAction.setShortcut( QKeySequence( "Alt+PgDown" ) );
|
|
|
|
connect( &selectNextAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( selectNextGroup() ) );
|
|
|
|
addAction( &selectNextAction );
|
|
|
|
|
|
|
|
selectPreviousAction.setShortcut( QKeySequence( "Alt+PgUp" ) );
|
|
|
|
connect( &selectPreviousAction, SIGNAL( triggered() ),
|
|
|
|
this, SLOT( selectPreviousGroup() ) );
|
|
|
|
addAction( &selectPreviousAction );
|
2017-07-20 14:46:48 +00:00
|
|
|
|
|
|
|
setMaxVisibleItems( 30 );
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GroupComboBox::fill( Instances::Groups const & groups )
|
|
|
|
{
|
2009-04-10 12:48:40 +00:00
|
|
|
unsigned prevId = 0;
|
|
|
|
|
|
|
|
if ( count() )
|
|
|
|
prevId = itemData( currentIndex() ).toUInt();
|
2009-02-01 00:08:08 +00:00
|
|
|
|
|
|
|
clear();
|
|
|
|
|
2010-07-05 14:13:29 +00:00
|
|
|
for( QMap< int, int >::iterator i = shortcuts.begin(); i != shortcuts.end(); ++i )
|
|
|
|
releaseShortcut( i.key() );
|
|
|
|
|
|
|
|
shortcuts.clear();
|
|
|
|
|
2009-02-01 00:08:08 +00:00
|
|
|
for( unsigned x = 0; x < groups.size(); ++x )
|
|
|
|
{
|
2009-10-21 19:28:09 +00:00
|
|
|
addItem( groups[ x ].makeIcon(),
|
|
|
|
groups[ x ].name, groups[ x ].id );
|
2009-02-01 00:08:08 +00:00
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
if ( prevId == groups[ x ].id )
|
2009-02-01 00:08:08 +00:00
|
|
|
setCurrentIndex( x );
|
2010-07-05 14:13:29 +00:00
|
|
|
|
|
|
|
// Create a shortcut
|
|
|
|
|
|
|
|
if ( !groups[ x ].shortcut.isEmpty() )
|
|
|
|
{
|
|
|
|
int id = grabShortcut( groups[ x ].shortcut );
|
|
|
|
setShortcutEnabled( id );
|
|
|
|
|
|
|
|
shortcuts.insert( id, x );
|
|
|
|
}
|
2009-02-01 00:08:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 14:13:29 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:00:23 +00:00
|
|
|
QList< QAction * > GroupComboBox::getExternActions()
|
|
|
|
{
|
|
|
|
QList< QAction * > list;
|
|
|
|
list.append( &popupAction );
|
|
|
|
list.append( &selectNextAction );
|
|
|
|
list.append( &selectPreviousAction );
|
|
|
|
return list;
|
|
|
|
}
|
2010-07-05 14:13:29 +00:00
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
void GroupComboBox::setCurrentGroup( unsigned id )
|
2009-02-06 17:04:11 +00:00
|
|
|
{
|
|
|
|
for( int x = 0; x < count(); ++x )
|
|
|
|
{
|
2009-04-10 12:48:40 +00:00
|
|
|
if ( itemData( x ).toUInt() == id )
|
2009-02-06 17:04:11 +00:00
|
|
|
{
|
|
|
|
setCurrentIndex( x );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-10 12:48:40 +00:00
|
|
|
unsigned GroupComboBox::getCurrentGroup() const
|
|
|
|
{
|
|
|
|
if ( !count() )
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return itemData( currentIndex() ).toUInt();
|
|
|
|
}
|
|
|
|
|
2009-05-10 11:49:14 +00:00
|
|
|
void GroupComboBox::popupGroups()
|
|
|
|
{
|
|
|
|
showPopup();
|
|
|
|
}
|
2012-10-01 12:43:00 +00:00
|
|
|
|
|
|
|
void GroupComboBox::selectNextGroup()
|
|
|
|
{
|
|
|
|
if( count() <= 1 )
|
|
|
|
return;
|
|
|
|
int n = currentIndex() + 1;
|
|
|
|
if( n >= count() )
|
|
|
|
n = 0;
|
|
|
|
setCurrentIndex( n );
|
|
|
|
}
|
|
|
|
|
|
|
|
void GroupComboBox::selectPreviousGroup()
|
|
|
|
{
|
|
|
|
if( count() <= 1 )
|
|
|
|
return;
|
|
|
|
int n = currentIndex() - 1;
|
|
|
|
if( n < 0 )
|
|
|
|
n = count() - 1;
|
|
|
|
setCurrentIndex( n );
|
|
|
|
}
|