2009-09-21 17:50:03 +00:00
|
|
|
#include "dictionarybar.hh"
|
|
|
|
#include <QAction>
|
2009-09-23 18:44:38 +00:00
|
|
|
#include <QApplication>
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
DictionaryBar::DictionaryBar( QWidget * parent,
|
|
|
|
Config::MutedDictionaries & mutedDictionaries_,
|
|
|
|
Config::Events & events ):
|
|
|
|
QToolBar( tr( "Dictionary Bar" ), parent ),
|
|
|
|
mutedDictionaries( mutedDictionaries_ ),
|
|
|
|
configEvents( events )
|
|
|
|
{
|
|
|
|
setObjectName( "dictionaryBar" );
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
connect( &events, SIGNAL( mutedDictionariesChanged() ),
|
|
|
|
this, SLOT( mutedDictionariesChanged() ) );
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
connect( this, SIGNAL(actionTriggered(QAction*)),
|
|
|
|
this, SLOT(actionWasTriggered(QAction*)) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryBar::setDictionaries( vector< sptr< Dictionary::Class > >
|
|
|
|
const & dictionaries )
|
|
|
|
{
|
|
|
|
setUpdatesEnabled( false );
|
|
|
|
|
|
|
|
clear();
|
2009-09-23 18:44:38 +00:00
|
|
|
dictActions.clear();
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
bool use14x21 = false;
|
|
|
|
|
|
|
|
for( unsigned x = 0; x < dictionaries.size(); ++x )
|
|
|
|
{
|
|
|
|
QIcon icon = dictionaries[ x ]->getNativeIcon();
|
|
|
|
|
|
|
|
QAction * action = addAction( icon,
|
|
|
|
QString::fromUtf8(
|
|
|
|
dictionaries[ x ]->getName().c_str() ) );
|
|
|
|
|
|
|
|
QString id = QString::fromStdString( dictionaries[ x ]->getId() );
|
|
|
|
|
|
|
|
action->setData( id );
|
|
|
|
|
|
|
|
action->setCheckable( true );
|
|
|
|
|
|
|
|
action->setChecked( !mutedDictionaries.contains( id ) );
|
|
|
|
|
|
|
|
QList< QSize > sizes = icon.availableSizes();
|
|
|
|
|
|
|
|
for( QList< QSize >::iterator i = sizes.begin(); i != sizes.end();
|
|
|
|
++i )
|
|
|
|
if ( i->width() == 14 && i->height() == 21 )
|
|
|
|
use14x21 = true;
|
2009-09-23 18:44:38 +00:00
|
|
|
|
|
|
|
dictActions.append( action );
|
2009-09-21 17:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setIconSize( use14x21 ? QSize( 14, 21 ) : QSize( 21, 21 ) );
|
|
|
|
|
|
|
|
setUpdatesEnabled( true );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
void DictionaryBar::mutedDictionariesChanged()
|
2009-09-21 17:50:03 +00:00
|
|
|
{
|
2009-10-08 22:19:40 +00:00
|
|
|
// printf( "Muted dictionaries changed\n" );
|
2009-09-21 17:50:03 +00:00
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
// Update actions
|
|
|
|
|
|
|
|
setUpdatesEnabled( false );
|
|
|
|
|
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
|
|
|
bool isUnmuted = !mutedDictionaries.contains( (*i)->data().toString() );
|
|
|
|
|
|
|
|
if ( isUnmuted != (*i)->isChecked() )
|
|
|
|
(*i)->setChecked( isUnmuted );
|
|
|
|
}
|
|
|
|
|
|
|
|
setUpdatesEnabled( true );
|
2009-09-21 17:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryBar::actionWasTriggered( QAction * action )
|
|
|
|
{
|
|
|
|
QString id = action->data().toString();
|
|
|
|
|
|
|
|
if ( id.isEmpty() )
|
|
|
|
return; // Some weird action, not our button
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
if ( QApplication::keyboardModifiers() &
|
|
|
|
( Qt::ControlModifier | Qt::ShiftModifier ) )
|
|
|
|
{
|
|
|
|
// Solo mode -- either use the dictionary exclusively, or toggle
|
|
|
|
// back all dictionaries if we do that already.
|
|
|
|
|
|
|
|
// Are we solo already?
|
|
|
|
|
|
|
|
bool isSolo = true;
|
|
|
|
|
|
|
|
// For solo, all dictionaries must be unchecked, since we're handling
|
|
|
|
// the result of the dictionary being (un)checked, and in case we were
|
|
|
|
// in solo, now we would end up with no dictionaries being checked at all.
|
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
|
|
|
if ( (*i)->isChecked() )
|
|
|
|
{
|
|
|
|
isSolo = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isSolo )
|
|
|
|
{
|
|
|
|
// Toggle back all the dictionaries
|
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
mutedDictionaries.remove( (*i)->data().toString() );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Make dictionary solo
|
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
|
|
|
QString dictId = (*i)->data().toString();
|
|
|
|
|
|
|
|
if ( dictId == id )
|
|
|
|
mutedDictionaries.remove( dictId );
|
|
|
|
else
|
|
|
|
mutedDictionaries.insert( dictId );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
configEvents.signalMutedDictionariesChanged();
|
|
|
|
}
|
2009-09-21 17:50:03 +00:00
|
|
|
else
|
2009-09-23 18:44:38 +00:00
|
|
|
{
|
|
|
|
// Normal mode
|
|
|
|
|
|
|
|
if ( action->isChecked() )
|
|
|
|
{
|
|
|
|
// Unmute the dictionary
|
|
|
|
|
|
|
|
if ( mutedDictionaries.contains( id ) )
|
|
|
|
{
|
|
|
|
mutedDictionaries.remove( id );
|
|
|
|
configEvents.signalMutedDictionariesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Mute the dictionary
|
|
|
|
|
|
|
|
if ( !mutedDictionaries.contains( id ) )
|
|
|
|
{
|
|
|
|
mutedDictionaries.insert( id );
|
|
|
|
configEvents.signalMutedDictionariesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 17:50:03 +00:00
|
|
|
}
|