2009-09-21 17:50:03 +00:00
|
|
|
#include "dictionarybar.hh"
|
|
|
|
#include <QAction>
|
2009-09-23 18:44:38 +00:00
|
|
|
#include <QApplication>
|
2009-10-12 13:58:32 +00:00
|
|
|
#include <QMenu>
|
|
|
|
#include <QContextMenuEvent>
|
2012-11-28 19:32:37 +00:00
|
|
|
#include <QProcess>
|
2011-06-19 18:50:11 +00:00
|
|
|
#include "dprintf.hh"
|
2012-11-28 19:32:37 +00:00
|
|
|
#include "fsencoding.hh"
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
DictionaryBar::DictionaryBar( QWidget * parent,
|
2012-11-28 19:32:37 +00:00
|
|
|
Config::Events & events, QString const & _editDictionaryCommand ):
|
2009-09-21 17:50:03 +00:00
|
|
|
QToolBar( tr( "Dictionary Bar" ), parent ),
|
2012-09-26 13:13:47 +00:00
|
|
|
mutedDictionaries( 0 ),
|
2012-11-07 15:18:56 +00:00
|
|
|
configEvents( events ),
|
2012-11-28 19:32:37 +00:00
|
|
|
editDictionaryCommand( _editDictionaryCommand ),
|
2012-11-07 15:18:56 +00:00
|
|
|
use14x21( false )
|
2009-09-21 17:50:03 +00:00
|
|
|
{
|
|
|
|
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*)) );
|
|
|
|
}
|
|
|
|
|
2009-10-12 13:09:36 +00:00
|
|
|
static QString elideDictName( QString const & name )
|
|
|
|
{
|
|
|
|
// Some names are way too long -- we insert an ellipsis in the middle of those
|
|
|
|
|
|
|
|
int const maxSize = 33;
|
|
|
|
|
|
|
|
if ( name.size() <= maxSize )
|
|
|
|
return name;
|
|
|
|
|
|
|
|
int const pieceSize = maxSize / 2 - 1;
|
|
|
|
|
|
|
|
return name.left( pieceSize ) + QChar( 0x2026 ) + name.right( pieceSize );
|
|
|
|
}
|
|
|
|
|
2009-09-21 17:50:03 +00:00
|
|
|
void DictionaryBar::setDictionaries( vector< sptr< Dictionary::Class > >
|
|
|
|
const & dictionaries )
|
|
|
|
{
|
|
|
|
setUpdatesEnabled( false );
|
|
|
|
|
2012-11-28 19:32:37 +00:00
|
|
|
allDictionaries.clear();
|
|
|
|
allDictionaries = dictionaries;
|
|
|
|
|
2009-09-21 17:50:03 +00:00
|
|
|
clear();
|
2009-09-23 18:44:38 +00:00
|
|
|
dictActions.clear();
|
2009-09-21 17:50:03 +00:00
|
|
|
|
2012-11-07 15:18:56 +00:00
|
|
|
use14x21 = false;
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
for( unsigned x = 0; x < dictionaries.size(); ++x )
|
|
|
|
{
|
|
|
|
QIcon icon = dictionaries[ x ]->getNativeIcon();
|
|
|
|
|
2009-10-12 13:09:36 +00:00
|
|
|
QString dictName = QString::fromUtf8( dictionaries[ x ]->
|
|
|
|
getName().c_str() );
|
|
|
|
|
|
|
|
QAction * action = addAction( icon, elideDictName( dictName ) );
|
|
|
|
|
|
|
|
action->setToolTip( dictName ); // Tooltip need not be shortened
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
QString id = QString::fromStdString( dictionaries[ x ]->getId() );
|
|
|
|
|
|
|
|
action->setData( id );
|
|
|
|
|
|
|
|
action->setCheckable( true );
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
action->setChecked( mutedDictionaries ? !mutedDictionaries->contains( id ) : true );
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-11-07 15:18:56 +00:00
|
|
|
setDictionaryIconSize( 21 );
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
setUpdatesEnabled( true );
|
|
|
|
}
|
|
|
|
|
2012-11-07 15:18:56 +00:00
|
|
|
void DictionaryBar::setDictionaryIconSize( int extent )
|
|
|
|
{
|
|
|
|
setIconSize( QSize( use14x21 ? extent*2/3 : extent, extent ) );
|
|
|
|
}
|
2009-09-21 17:50:03 +00:00
|
|
|
|
2009-10-12 13:58:32 +00:00
|
|
|
void DictionaryBar::contextMenuEvent( QContextMenuEvent * event )
|
|
|
|
{
|
|
|
|
QMenu menu( this );
|
|
|
|
|
2012-09-27 13:00:40 +00:00
|
|
|
QAction * editAction =
|
|
|
|
menu.addAction( QIcon( ":/icons/bookcase.png" ), tr( "Edit this group" ) );
|
|
|
|
|
|
|
|
QAction * infoAction = NULL;
|
|
|
|
QAction * dictAction = actionAt( event->x(), event->y() );
|
|
|
|
if( dictAction )
|
|
|
|
infoAction = menu.addAction( tr( "Dictionary info" ) );
|
|
|
|
|
2012-11-28 19:32:37 +00:00
|
|
|
QAction * editDictAction = NULL;
|
|
|
|
if( !editDictionaryCommand.isEmpty() )
|
|
|
|
editDictAction = menu.addAction( tr( "Edit dictionary" ) );
|
|
|
|
|
2012-09-27 13:00:40 +00:00
|
|
|
if ( !dictActions.empty() )
|
|
|
|
menu.addSeparator();
|
|
|
|
|
2009-10-12 13:58:32 +00:00
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
|
|
|
// We need new action, since the one we have has text elided
|
|
|
|
QAction * action = menu.addAction( (*i)->icon(), (*i)->toolTip() );
|
|
|
|
|
|
|
|
action->setCheckable( true );
|
|
|
|
action->setChecked( (*i)->isChecked() );
|
|
|
|
action->setData( QVariant::fromValue( (void *)*i ) );
|
2011-06-12 16:37:45 +00:00
|
|
|
// Force "icon in menu" on all platforms, for
|
|
|
|
// usability reasons.
|
|
|
|
action->setIconVisibleInMenu( true );
|
2009-10-12 13:58:32 +00:00
|
|
|
}
|
2010-05-08 14:01:59 +00:00
|
|
|
|
2012-09-25 13:13:35 +00:00
|
|
|
connect( this, SIGNAL( closePopupMenu() ), &menu, SLOT( close() ) );
|
|
|
|
|
2009-10-12 13:58:32 +00:00
|
|
|
QAction * result = menu.exec( event->globalPos() );
|
|
|
|
|
2012-09-25 13:13:35 +00:00
|
|
|
if( result && result == infoAction )
|
|
|
|
{
|
|
|
|
QString id = dictAction->data().toString();
|
|
|
|
emit showDictionaryInfo( id );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:32:37 +00:00
|
|
|
if( result && result == editDictAction )
|
|
|
|
{
|
|
|
|
QString id = dictAction->data().toString();
|
|
|
|
for( unsigned i = 0; i < allDictionaries.size(); i++ )
|
|
|
|
{
|
|
|
|
if( id.compare( allDictionaries[ i ]->getId().c_str() ) == 0 )
|
|
|
|
{
|
|
|
|
QString command( editDictionaryCommand );
|
|
|
|
QString dictName = FsEncoding::decode( allDictionaries[ i ]->getDictionaryFilenames().at( 0 ).c_str() );
|
|
|
|
|
|
|
|
if( dictName.endsWith( ".ifo" ) ) // Stardict dictionary
|
|
|
|
dictName = FsEncoding::decode( allDictionaries[ i ]->getDictionaryFilenames().at( 2 ).c_str() );
|
|
|
|
|
|
|
|
command.replace( "%GDDICT%", "\"" + dictName + "\"" );
|
|
|
|
|
|
|
|
if( !QProcess::startDetached( command ) )
|
|
|
|
QApplication::beep();
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-08 14:01:59 +00:00
|
|
|
if ( result == editAction )
|
|
|
|
emit editGroupRequested();
|
|
|
|
else
|
2009-10-12 13:58:32 +00:00
|
|
|
if ( result && result->data().value< void * >() )
|
|
|
|
( ( QAction * )( result->data().value< void * >() ) )->trigger();
|
|
|
|
|
|
|
|
event->accept();
|
|
|
|
}
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
void DictionaryBar::mutedDictionariesChanged()
|
2009-09-21 17:50:03 +00:00
|
|
|
{
|
2011-06-19 18:50:11 +00:00
|
|
|
//DPRINTF( "Muted dictionaries changed\n" );
|
2009-09-21 17:50:03 +00:00
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
if( !mutedDictionaries )
|
|
|
|
return;
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
// Update actions
|
|
|
|
|
|
|
|
setUpdatesEnabled( false );
|
|
|
|
|
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
2012-09-26 13:13:47 +00:00
|
|
|
bool isUnmuted = !mutedDictionaries->contains( (*i)->data().toString() );
|
2009-09-23 18:44:38 +00:00
|
|
|
|
|
|
|
if ( isUnmuted != (*i)->isChecked() )
|
|
|
|
(*i)->setChecked( isUnmuted );
|
|
|
|
}
|
|
|
|
|
|
|
|
setUpdatesEnabled( true );
|
2009-09-21 17:50:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryBar::actionWasTriggered( QAction * action )
|
|
|
|
{
|
2012-09-26 13:13:47 +00:00
|
|
|
if( !mutedDictionaries )
|
|
|
|
return;
|
|
|
|
|
2009-09-21 17:50:03 +00:00
|
|
|
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 )
|
|
|
|
{
|
2012-09-27 14:28:05 +00:00
|
|
|
// Restore or clear all the dictionaries
|
2012-09-28 12:31:46 +00:00
|
|
|
if ( QApplication::keyboardModifiers() & Qt::ShiftModifier )
|
|
|
|
*mutedDictionaries = storedMutedSet;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
mutedDictionaries->remove( (*i)->data().toString() );
|
|
|
|
}
|
2012-09-27 14:28:05 +00:00
|
|
|
storedMutedSet.clear();
|
2009-09-23 18:44:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-27 14:28:05 +00:00
|
|
|
// Save dictionaries state
|
|
|
|
storedMutedSet = *mutedDictionaries;
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
// Make dictionary solo
|
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
|
|
|
QString dictId = (*i)->data().toString();
|
|
|
|
|
|
|
|
if ( dictId == id )
|
2012-09-26 13:13:47 +00:00
|
|
|
mutedDictionaries->remove( dictId );
|
2009-09-23 18:44:38 +00:00
|
|
|
else
|
2012-09-26 13:13:47 +00:00
|
|
|
mutedDictionaries->insert( dictId );
|
2009-09-23 18:44:38 +00:00
|
|
|
}
|
2012-09-28 12:31:46 +00:00
|
|
|
}
|
2009-09-23 18:44:38 +00:00
|
|
|
configEvents.signalMutedDictionariesChanged();
|
|
|
|
}
|
2009-09-21 17:50:03 +00:00
|
|
|
else
|
2009-09-23 18:44:38 +00:00
|
|
|
{
|
|
|
|
// Normal mode
|
|
|
|
|
2012-09-27 14:28:05 +00:00
|
|
|
storedMutedSet.clear();
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
if ( action->isChecked() )
|
|
|
|
{
|
|
|
|
// Unmute the dictionary
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
if ( mutedDictionaries->contains( id ) )
|
2009-09-23 18:44:38 +00:00
|
|
|
{
|
2012-09-26 13:13:47 +00:00
|
|
|
mutedDictionaries->remove( id );
|
2009-09-23 18:44:38 +00:00
|
|
|
configEvents.signalMutedDictionariesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Mute the dictionary
|
|
|
|
|
2012-09-26 13:13:47 +00:00
|
|
|
if ( !mutedDictionaries->contains( id ) )
|
2009-09-23 18:44:38 +00:00
|
|
|
{
|
2012-09-26 13:13:47 +00:00
|
|
|
mutedDictionaries->insert( id );
|
2009-09-23 18:44:38 +00:00
|
|
|
configEvents.signalMutedDictionariesChanged();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-21 17:50:03 +00:00
|
|
|
}
|
2009-10-12 13:58:32 +00:00
|
|
|
|
2012-09-19 12:44:36 +00:00
|
|
|
void DictionaryBar::dictsPaneClicked( const QString & id )
|
|
|
|
{
|
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
|
|
|
QString dictId = (*i)->data().toString();
|
|
|
|
if ( dictId == id )
|
|
|
|
{
|
|
|
|
(*i)->activate( QAction::Trigger );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|