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>
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2012-11-28 19:32:37 +00:00
|
|
|
#include "fsencoding.hh"
|
2013-06-11 18:31:01 +00:00
|
|
|
#include <QDebug>
|
2009-09-21 17:50:03 +00:00
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
DictionaryBar::DictionaryBar( QWidget * parent,
|
2013-06-11 18:31:01 +00:00
|
|
|
Config::Events & events, QString const & _editDictionaryCommand, unsigned short const & maxDictionaryRefsInContextMenu_ ):
|
2012-12-30 09:29:19 +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 ),
|
2013-06-11 18:31:01 +00:00
|
|
|
maxDictionaryRefsInContextMenu(maxDictionaryRefsInContextMenu_),
|
2012-12-30 14:08:54 +00:00
|
|
|
timerId( 0 )
|
2009-09-21 17:50:03 +00:00
|
|
|
{
|
|
|
|
setObjectName( "dictionaryBar" );
|
|
|
|
|
2013-06-11 18:31:01 +00:00
|
|
|
maxDictionaryRefsAction = new QAction( QIcon(":/icons/expand_opt.png"), tr( "Extended menu with all dictionaries..." ), this );
|
|
|
|
|
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*)) );
|
2012-12-30 14:08:54 +00:00
|
|
|
|
|
|
|
installEventFilter( this );
|
2009-09-21 17:50:03 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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 )
|
|
|
|
{
|
2022-08-10 14:13:19 +00:00
|
|
|
setIconSize( QSize( extent, extent ) );
|
2012-11-07 15:18:56 +00:00
|
|
|
}
|
2009-09-21 17:50:03 +00:00
|
|
|
|
2009-10-12 13:58:32 +00:00
|
|
|
void DictionaryBar::contextMenuEvent( QContextMenuEvent * event )
|
2013-06-11 18:31:01 +00:00
|
|
|
{
|
|
|
|
showContextMenu( event );
|
|
|
|
}
|
|
|
|
|
|
|
|
void DictionaryBar::showContextMenu( QContextMenuEvent * event, bool extended )
|
2009-10-12 13:58:32 +00:00
|
|
|
{
|
|
|
|
QMenu menu( this );
|
|
|
|
|
2012-09-27 13:00:40 +00:00
|
|
|
QAction * editAction =
|
2022-01-24 14:23:38 +00:00
|
|
|
menu.addAction( QIcon( ":/icons/bookcase.svg" ), tr( "Edit this group" ) );
|
2012-09-27 13:00:40 +00:00
|
|
|
|
|
|
|
QAction * infoAction = NULL;
|
2014-03-01 13:11:14 +00:00
|
|
|
QAction * headwordsAction = NULL;
|
2012-11-29 17:12:54 +00:00
|
|
|
QAction * editDictAction = NULL;
|
2013-06-09 13:31:57 +00:00
|
|
|
QAction * openDictFolderAction = NULL;
|
2012-11-29 17:12:54 +00:00
|
|
|
QString dictFilename;
|
|
|
|
|
2012-09-27 13:00:40 +00:00
|
|
|
QAction * dictAction = actionAt( event->x(), event->y() );
|
|
|
|
if( dictAction )
|
2012-11-29 17:12:54 +00:00
|
|
|
{
|
2013-06-09 13:31:57 +00:00
|
|
|
Dictionary::Class *pDict = NULL;
|
|
|
|
QString id = dictAction->data().toString();
|
|
|
|
for( unsigned i = 0; i < allDictionaries.size(); i++ )
|
|
|
|
{
|
|
|
|
if( id.compare( allDictionaries[ i ]->getId().c_str() ) == 0 )
|
|
|
|
{
|
|
|
|
pDict = allDictionaries[ i ].get();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-09-27 13:00:40 +00:00
|
|
|
|
2013-06-09 13:31:57 +00:00
|
|
|
if( pDict )
|
2012-11-29 17:12:54 +00:00
|
|
|
{
|
2013-06-09 13:31:57 +00:00
|
|
|
infoAction = menu.addAction( tr( "Dictionary info" ) );
|
|
|
|
|
|
|
|
if( pDict->isLocalDictionary() )
|
2012-11-29 17:12:54 +00:00
|
|
|
{
|
2014-03-01 13:11:14 +00:00
|
|
|
if( pDict->getWordCount() > 0 )
|
|
|
|
headwordsAction = menu.addAction( tr( "Dictionary headwords" ) );
|
|
|
|
|
2013-06-09 13:31:57 +00:00
|
|
|
openDictFolderAction = menu.addAction( tr( "Open dictionary folder" ) );
|
|
|
|
|
|
|
|
if( !editDictionaryCommand.isEmpty() )
|
2012-11-29 17:12:54 +00:00
|
|
|
{
|
2013-06-09 13:31:57 +00:00
|
|
|
if( !pDict->getMainFilename().isEmpty() )
|
2013-06-11 18:59:33 +00:00
|
|
|
{
|
|
|
|
dictFilename = pDict->getMainFilename();
|
|
|
|
editDictAction = menu.addAction( tr( "Edit dictionary" ) );
|
|
|
|
}
|
2012-11-29 17:12:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-28 19:32:37 +00:00
|
|
|
|
2012-09-27 13:00:40 +00:00
|
|
|
if ( !dictActions.empty() )
|
|
|
|
menu.addSeparator();
|
|
|
|
|
2013-06-11 18:31:01 +00:00
|
|
|
unsigned refsAdded = 0;
|
|
|
|
|
2009-10-12 13:58:32 +00:00
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
2013-06-11 18:31:01 +00:00
|
|
|
|
|
|
|
// Enough! Or the menu would become too large.
|
|
|
|
if ( refsAdded++ >= maxDictionaryRefsInContextMenu && !extended )
|
|
|
|
{
|
|
|
|
menu.addSeparator();
|
|
|
|
menu.addAction( maxDictionaryRefsAction );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-10-12 13:58:32 +00:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2014-03-01 13:11:14 +00:00
|
|
|
if( result && result == headwordsAction )
|
|
|
|
{
|
|
|
|
QString id = dictAction->data().toString();
|
|
|
|
emit showDictionaryHeadwords( id );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-09 13:31:57 +00:00
|
|
|
if( result && result == openDictFolderAction )
|
|
|
|
{
|
|
|
|
QString id = dictAction->data().toString();
|
|
|
|
emit openDictionaryFolder( id );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-28 19:32:37 +00:00
|
|
|
if( result && result == editDictAction )
|
|
|
|
{
|
2012-11-29 17:12:54 +00:00
|
|
|
QString command( editDictionaryCommand );
|
|
|
|
command.replace( "%GDDICT%", "\"" + dictFilename + "\"" );
|
2022-01-08 13:16:22 +00:00
|
|
|
if( !QProcess::startDetached( command ,QStringList()) )
|
2012-11-29 17:12:54 +00:00
|
|
|
QApplication::beep();
|
2012-11-28 19:32:37 +00:00
|
|
|
}
|
|
|
|
|
2013-06-11 18:31:01 +00:00
|
|
|
if( result && result == maxDictionaryRefsAction )
|
|
|
|
{
|
|
|
|
showContextMenu( event, true );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2022-01-15 07:29:20 +00:00
|
|
|
//GD_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 )
|
|
|
|
{
|
2018-02-08 15:01:39 +00:00
|
|
|
if ( !isVisible() )
|
|
|
|
return;
|
|
|
|
|
2012-09-19 12:44:36 +00:00
|
|
|
for( QList< QAction * >::iterator i = dictActions.begin();
|
|
|
|
i != dictActions.end(); ++i )
|
|
|
|
{
|
|
|
|
QString dictId = (*i)->data().toString();
|
|
|
|
if ( dictId == id )
|
|
|
|
{
|
|
|
|
(*i)->activate( QAction::Trigger );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-12-30 14:08:54 +00:00
|
|
|
|
|
|
|
bool DictionaryBar::eventFilter( QObject * obj, QEvent * ev )
|
|
|
|
{
|
|
|
|
if(obj == this && !isFloating() )
|
|
|
|
{
|
|
|
|
QPoint pt = parentWidget()->mapFromGlobal( QCursor::pos() );
|
|
|
|
switch( ev->type() )
|
|
|
|
{
|
|
|
|
case QEvent::Leave : if( geometry().contains( pt ) )
|
|
|
|
{
|
2012-12-31 07:54:51 +00:00
|
|
|
if( timerId )
|
|
|
|
killTimer( timerId );
|
2012-12-30 14:08:54 +00:00
|
|
|
timerId = startTimer( 500 );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QEvent::Enter : if( timerId != 0)
|
|
|
|
{
|
|
|
|
killTimer( timerId );
|
|
|
|
timerId = 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case QEvent::Timer: if( static_cast< QTimerEvent * >( ev )->timerId() == timerId )
|
|
|
|
{
|
|
|
|
if( !geometry().contains( pt ) )
|
|
|
|
{
|
|
|
|
killTimer( timerId );
|
|
|
|
timerId = 0;
|
|
|
|
QEvent event( QEvent::Leave );
|
|
|
|
QApplication::sendEvent( this, &event );
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|