Call external editor for dictionaries

This commit is contained in:
Abs62 2012-11-28 23:32:37 +04:00
parent 3616665f1f
commit e6457a1e27
6 changed files with 95 additions and 8 deletions

View file

@ -740,6 +740,9 @@ Class load() throw( exError )
if ( !root.namedItem( "historyExportPath" ).isNull() ) if ( !root.namedItem( "historyExportPath" ).isNull() )
c.historyExportPath = root.namedItem( "historyExportPath" ).toElement().text(); c.historyExportPath = root.namedItem( "historyExportPath" ).toElement().text();
if ( !root.namedItem( "editDictionaryCommandLine" ).isNull() )
c.editDictionaryCommandLine = root.namedItem( "editDictionaryCommandLine" ).toElement().text();
return c; return c;
} }
@ -1360,6 +1363,11 @@ void save( Class const & c ) throw( exError )
opt.appendChild( dd.createTextNode( c.historyExportPath ) ); opt.appendChild( dd.createTextNode( c.historyExportPath ) );
root.appendChild( opt ); root.appendChild( opt );
} }
opt = dd.createElement( "editDictionaryCommandLine" );
opt.appendChild( dd.createTextNode( c.editDictionaryCommandLine ) );
root.appendChild( opt );
} }
QByteArray result( dd.toByteArray() ); QByteArray result( dd.toByteArray() );

View file

@ -400,6 +400,8 @@ struct Class
unsigned short maxDictionaryRefsInContextMenu; unsigned short maxDictionaryRefsInContextMenu;
QString editDictionaryCommandLine; // Command line to call external editor for dictionary
Class(): lastMainGroupId( 0 ), lastPopupGroupId( 0 ), Class(): lastMainGroupId( 0 ), lastPopupGroupId( 0 ),
pinPopupWindow( false ), showingDictBarNames( false ), pinPopupWindow( false ), showingDictBarNames( false ),
usingSmallIconsInToolbars( false ), maxDictionaryRefsInContextMenu( 20 ) usingSmallIconsInToolbars( false ), maxDictionaryRefsInContextMenu( 20 )

View file

@ -3,15 +3,18 @@
#include <QApplication> #include <QApplication>
#include <QMenu> #include <QMenu>
#include <QContextMenuEvent> #include <QContextMenuEvent>
#include <QProcess>
#include "dprintf.hh" #include "dprintf.hh"
#include "fsencoding.hh"
using std::vector; using std::vector;
DictionaryBar::DictionaryBar( QWidget * parent, DictionaryBar::DictionaryBar( QWidget * parent,
Config::Events & events ): Config::Events & events, QString const & _editDictionaryCommand ):
QToolBar( tr( "Dictionary Bar" ), parent ), QToolBar( tr( "Dictionary Bar" ), parent ),
mutedDictionaries( 0 ), mutedDictionaries( 0 ),
configEvents( events ), configEvents( events ),
editDictionaryCommand( _editDictionaryCommand ),
use14x21( false ) use14x21( false )
{ {
setObjectName( "dictionaryBar" ); setObjectName( "dictionaryBar" );
@ -42,6 +45,9 @@ void DictionaryBar::setDictionaries( vector< sptr< Dictionary::Class > >
{ {
setUpdatesEnabled( false ); setUpdatesEnabled( false );
allDictionaries.clear();
allDictionaries = dictionaries;
clear(); clear();
dictActions.clear(); dictActions.clear();
@ -98,6 +104,10 @@ void DictionaryBar::contextMenuEvent( QContextMenuEvent * event )
if( dictAction ) if( dictAction )
infoAction = menu.addAction( tr( "Dictionary info" ) ); infoAction = menu.addAction( tr( "Dictionary info" ) );
QAction * editDictAction = NULL;
if( !editDictionaryCommand.isEmpty() )
editDictAction = menu.addAction( tr( "Edit dictionary" ) );
if ( !dictActions.empty() ) if ( !dictActions.empty() )
menu.addSeparator(); menu.addSeparator();
@ -126,6 +136,29 @@ void DictionaryBar::contextMenuEvent( QContextMenuEvent * event )
return; return;
} }
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;
}
}
}
if ( result == editAction ) if ( result == editAction )
emit editGroupRequested(); emit editGroupRequested();
else else

View file

@ -4,6 +4,7 @@
#include <QToolBar> #include <QToolBar>
#include <QSize> #include <QSize>
#include <QList> #include <QList>
#include <QString>
#include "dictionary.hh" #include "dictionary.hh"
#include "config.hh" #include "config.hh"
@ -17,7 +18,7 @@ public:
/// Constructs an empty dictionary bar /// Constructs an empty dictionary bar
DictionaryBar( QWidget * parent, DictionaryBar( QWidget * parent,
Config::Events & ); Config::Events &, QString const & _editDictionaryCommand );
/// Sets dictionaries to be displayed in the bar. Their statuses (enabled/ /// Sets dictionaries to be displayed in the bar. Their statuses (enabled/
/// disabled) are taken from the configuration data. /// disabled) are taken from the configuration data.
@ -45,7 +46,8 @@ private:
Config::MutedDictionaries * mutedDictionaries; Config::MutedDictionaries * mutedDictionaries;
Config::Events & configEvents; Config::Events & configEvents;
Config::MutedDictionaries storedMutedSet; Config::MutedDictionaries storedMutedSet;
QString editDictionaryCommand;
std::vector< sptr< Dictionary::Class > > allDictionaries;
/// All the actions we have added to the toolbar /// All the actions we have added to the toolbar
QList< QAction * > dictActions; QList< QAction * > dictActions;

View file

@ -21,6 +21,8 @@
#include <QDebug> #include <QDebug>
#include <QTextStream> #include <QTextStream>
#include "dictinfo.hh" #include "dictinfo.hh"
#include "fsencoding.hh"
#include <QProcess>
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
#include "lionsupport.h" #include "lionsupport.h"
@ -61,7 +63,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
addTab( this ), addTab( this ),
cfg( cfg_ ), cfg( cfg_ ),
history( History::Load(), cfg_.preferences.maxStringsInHistory ), history( History::Load(), cfg_.preferences.maxStringsInHistory ),
dictionaryBar( this, configEvents ), dictionaryBar( this, configEvents, cfg.editDictionaryCommandLine ),
articleMaker( dictionaries, groupInstances, cfg.preferences.displayStyle ), articleMaker( dictionaries, groupInstances, cfg.preferences.displayStyle ),
articleNetMgr( this, dictionaries, articleMaker, articleNetMgr( this, dictionaries, articleMaker,
cfg.preferences.disallowContentFromOtherSites ), cfg.preferences.disallowContentFromOtherSites ),
@ -3048,10 +3050,50 @@ void MainWindow::foundDictsContextMenuRequested( const QPoint &pos )
QListWidgetItem *item = ui.dictsList->itemAt( pos ); QListWidgetItem *item = ui.dictsList->itemAt( pos );
if( item ) if( item )
{ {
scanPopup.get()->blockSignals( true );
QString id = item->data( Qt::UserRole ).toString(); QString id = item->data( Qt::UserRole ).toString();
showDictionaryInfo( id ); if( cfg.editDictionaryCommandLine.isEmpty() )
scanPopup.get()->blockSignals( false ); {
scanPopup.get()->blockSignals( true );
showDictionaryInfo( id );
scanPopup.get()->blockSignals( false );
}
else
{
QMenu menu( ui.dictsList );
QAction * infoAction = menu.addAction( tr( "Dictionary info" ) );
QAction * editAction = menu.addAction( tr( "Edit dictionary" ) );
QAction * result = menu.exec( ui.dictsList->mapToGlobal( pos ) );
if( result && result == infoAction )
{
scanPopup.get()->blockSignals( true );
showDictionaryInfo( id );
scanPopup.get()->blockSignals( false );
}
else
if( result && result == editAction )
{
for( unsigned i = 0; i < dictionaries.size(); i++ )
{
if( id.compare( dictionaries[ i ]->getId().c_str() ) == 0 )
{
QString command( cfg.editDictionaryCommandLine );
QString dictName = FsEncoding::decode( dictionaries[ i ]->getDictionaryFilenames().at( 0 ).c_str() );
if( dictName.endsWith( ".ifo" ) ) // Stardict dictionary
dictName = FsEncoding::decode( dictionaries[ i ]->getDictionaryFilenames().at( 2 ).c_str() );
command.replace( "%GDDICT%", "\"" + dictName + "\"" );
if( !QProcess::startDetached( command ) )
QApplication::beep();
break;
}
}
}
}
} }
} }

View file

@ -42,7 +42,7 @@ ScanPopup::ScanPopup( QWidget * parent,
escapeAction( this ), escapeAction( this ),
switchExpandModeAction( this ), switchExpandModeAction( this ),
wordFinder( this ), wordFinder( this ),
dictionaryBar( this, configEvents ), dictionaryBar( this, configEvents, cfg.editDictionaryCommandLine ),
mouseEnteredOnce( false ), mouseEnteredOnce( false ),
mouseIntercepted( false ), mouseIntercepted( false ),
hideTimer( this ) hideTimer( this )