2012-02-20 21:47:14 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-04-30 15:29:03 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
|
|
|
#include "editdictionaries.hh"
|
|
|
|
#include "loaddictionaries.hh"
|
|
|
|
#include <QMessageBox>
|
|
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
EditDictionaries::EditDictionaries( QWidget * parent, Config::Class & cfg_,
|
|
|
|
vector< sptr< Dictionary::Class > > & dictionaries_,
|
2009-05-15 12:15:08 +00:00
|
|
|
Instances::Groups & groupInstances_,
|
2009-04-30 15:29:03 +00:00
|
|
|
QNetworkAccessManager & dictNetMgr_ ):
|
|
|
|
QDialog( parent ), cfg( cfg_ ), dictionaries( dictionaries_ ),
|
2009-05-15 12:15:08 +00:00
|
|
|
groupInstances( groupInstances_ ),
|
2009-04-30 15:29:03 +00:00
|
|
|
dictNetMgr( dictNetMgr_ ),
|
|
|
|
origCfg( cfg ),
|
2009-05-06 14:39:08 +00:00
|
|
|
sources( this, cfg.paths, cfg.soundDirs, cfg.hunspell, cfg.transliteration,
|
2011-05-29 05:08:37 +00:00
|
|
|
cfg.forvo, cfg.mediawikis, cfg.webSites, cfg.programs ),
|
2009-05-18 18:01:50 +00:00
|
|
|
orderAndProps( new OrderAndProps( this, cfg.dictionaryOrder, cfg.inactiveDictionaries,
|
|
|
|
dictionaries ) ),
|
2009-05-23 14:19:57 +00:00
|
|
|
groups( new Groups( this, dictionaries, cfg.groups, orderAndProps->getCurrentDictionaryOrder() ) ),
|
2009-04-30 15:29:03 +00:00
|
|
|
dictionariesChanged( false ),
|
|
|
|
groupsChanged( false ),
|
|
|
|
lastCurrentTab( 0 )
|
|
|
|
{
|
2009-05-18 18:01:50 +00:00
|
|
|
// Some groups may have contained links to non-existnent dictionaries. We
|
|
|
|
// would like to preserve them if no edits were done. To that end, we save
|
|
|
|
// the initial group readings so that if no edits were really done, we won't
|
|
|
|
// be changing groups.
|
|
|
|
origCfg.groups = groups->getGroups();
|
|
|
|
origCfg.dictionaryOrder = orderAndProps->getCurrentDictionaryOrder();
|
|
|
|
origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();
|
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
ui.setupUi( this );
|
|
|
|
|
2009-05-02 21:46:43 +00:00
|
|
|
setWindowIcon( QIcon(":/icons/book.png") );
|
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
ui.tabs->clear();
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
ui.tabs->addTab( &sources, QIcon(":/icons/reload.png"), tr( "&Sources" ) );
|
|
|
|
ui.tabs->addTab( orderAndProps.get(), QIcon(":/icons/book.png"), tr( "&Dictionaries" ) );
|
2009-05-02 21:46:43 +00:00
|
|
|
ui.tabs->addTab( groups.get(), QIcon(":/icons/bookcase.png"), tr( "&Groups" ) );
|
2009-05-05 08:57:37 +00:00
|
|
|
|
2011-06-13 12:30:49 +00:00
|
|
|
connect( ui.buttons, SIGNAL( clicked( QAbstractButton * ) ),
|
|
|
|
this, SLOT( buttonBoxClicked( QAbstractButton * ) ) );
|
|
|
|
|
2009-05-05 08:57:37 +00:00
|
|
|
connect( &sources, SIGNAL( rescan() ), this, SLOT( rescanSources() ) );
|
2009-04-30 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
2010-05-08 14:01:59 +00:00
|
|
|
void EditDictionaries::editGroup( unsigned id )
|
|
|
|
{
|
|
|
|
if ( id == Instances::Group::AllGroupId )
|
|
|
|
ui.tabs->setCurrentIndex( 1 );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ui.tabs->setCurrentIndex( 2 );
|
|
|
|
groups->editGroup( id );
|
|
|
|
}
|
|
|
|
}
|
2009-04-30 15:29:03 +00:00
|
|
|
|
2011-06-13 12:30:49 +00:00
|
|
|
void EditDictionaries::save()
|
2009-04-30 15:29:03 +00:00
|
|
|
{
|
|
|
|
Config::Groups newGroups = groups->getGroups();
|
2009-05-18 18:01:50 +00:00
|
|
|
Config::Group newOrder = orderAndProps->getCurrentDictionaryOrder();
|
|
|
|
Config::Group newInactive = orderAndProps->getCurrentInactiveDictionaries();
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-05-15 12:39:53 +00:00
|
|
|
if ( isSourcesChanged() )
|
|
|
|
acceptChangedSources( false );
|
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
if ( origCfg.groups != newGroups || origCfg.dictionaryOrder != newOrder ||
|
|
|
|
origCfg.inactiveDictionaries != newInactive )
|
2009-04-30 15:29:03 +00:00
|
|
|
{
|
|
|
|
groupsChanged = true;
|
|
|
|
cfg.groups = newGroups;
|
2009-05-18 18:01:50 +00:00
|
|
|
cfg.dictionaryOrder = newOrder;
|
|
|
|
cfg.inactiveDictionaries = newInactive;
|
2009-04-30 15:29:03 +00:00
|
|
|
}
|
2011-06-13 12:30:49 +00:00
|
|
|
}
|
2009-04-30 15:29:03 +00:00
|
|
|
|
2011-06-13 12:30:49 +00:00
|
|
|
void EditDictionaries::accept()
|
|
|
|
{
|
|
|
|
save();
|
2009-04-30 15:29:03 +00:00
|
|
|
QDialog::accept();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditDictionaries::on_tabs_currentChanged( int index )
|
|
|
|
{
|
|
|
|
if ( index == -1 || !isVisible() )
|
|
|
|
return; // Sent upon the construction/destruction
|
|
|
|
|
|
|
|
if ( !lastCurrentTab && index )
|
|
|
|
{
|
|
|
|
// We're switching away from the Sources tab -- if its contents were
|
|
|
|
// changed, we need to either apply or reject now.
|
|
|
|
|
|
|
|
if ( isSourcesChanged() )
|
|
|
|
{
|
|
|
|
ui.tabs->setCurrentIndex( 0 );
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
QMessageBox question( QMessageBox::Question, tr( "Sources changed" ),
|
|
|
|
tr( "Some sources were changed. Would you like to accept the changes?" ),
|
|
|
|
QMessageBox::NoButton, this );
|
|
|
|
|
|
|
|
QPushButton * accept = question.addButton( tr( "Accept" ), QMessageBox::AcceptRole );
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
question.addButton( tr( "Cancel" ), QMessageBox::RejectRole );
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
question.exec();
|
|
|
|
|
|
|
|
if ( question.clickedButton() == accept )
|
|
|
|
{
|
2009-05-15 12:39:53 +00:00
|
|
|
acceptChangedSources( true );
|
2009-05-05 21:58:48 +00:00
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
lastCurrentTab = index;
|
|
|
|
ui.tabs->setCurrentIndex( index );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Prevent tab from switching
|
|
|
|
lastCurrentTab = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-05-18 18:01:50 +00:00
|
|
|
else
|
|
|
|
if ( lastCurrentTab == 1 && index != 1 )
|
|
|
|
{
|
|
|
|
// When switching from the dictionary order, we need to propagate any
|
|
|
|
// changes to the groups.
|
|
|
|
groups->updateDictionaryOrder( orderAndProps->getCurrentDictionaryOrder() );
|
|
|
|
}
|
2009-04-30 15:29:03 +00:00
|
|
|
|
|
|
|
lastCurrentTab = index;
|
|
|
|
}
|
|
|
|
|
2009-05-05 08:57:37 +00:00
|
|
|
void EditDictionaries::rescanSources()
|
|
|
|
{
|
2009-05-15 12:39:53 +00:00
|
|
|
acceptChangedSources( true );
|
2009-05-05 08:57:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-13 12:30:49 +00:00
|
|
|
void EditDictionaries::buttonBoxClicked( QAbstractButton * button )
|
|
|
|
{
|
|
|
|
if (ui.buttons->buttonRole(button) == QDialogButtonBox::ApplyRole) {
|
|
|
|
if ( isSourcesChanged() ) {
|
|
|
|
acceptChangedSources( true );
|
|
|
|
}
|
|
|
|
save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
bool EditDictionaries::isSourcesChanged() const
|
|
|
|
{
|
|
|
|
return sources.getPaths() != cfg.paths ||
|
|
|
|
sources.getSoundDirs() != cfg.soundDirs ||
|
|
|
|
sources.getHunspell() != cfg.hunspell ||
|
2009-05-06 14:39:08 +00:00
|
|
|
sources.getTransliteration() != cfg.transliteration ||
|
2010-06-12 20:16:35 +00:00
|
|
|
sources.getForvo() != cfg.forvo ||
|
2009-05-16 18:04:21 +00:00
|
|
|
sources.getMediaWikis() != cfg.mediawikis ||
|
2011-05-29 05:08:37 +00:00
|
|
|
sources.getWebSites() != cfg.webSites ||
|
|
|
|
sources.getPrograms() != cfg.programs;
|
2009-04-30 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
2009-05-15 12:39:53 +00:00
|
|
|
void EditDictionaries::acceptChangedSources( bool rebuildGroups )
|
2009-04-30 15:29:03 +00:00
|
|
|
{
|
2009-05-05 08:57:37 +00:00
|
|
|
dictionariesChanged = true;
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-05-15 12:39:53 +00:00
|
|
|
Config::Groups savedGroups = groups->getGroups();
|
2009-05-18 18:01:50 +00:00
|
|
|
Config::Group savedOrder = orderAndProps->getCurrentDictionaryOrder();
|
|
|
|
Config::Group savedInactive = orderAndProps->getCurrentInactiveDictionaries();
|
2009-05-15 12:39:53 +00:00
|
|
|
|
2009-05-05 08:57:37 +00:00
|
|
|
cfg.paths = sources.getPaths();
|
|
|
|
cfg.soundDirs = sources.getSoundDirs();
|
|
|
|
cfg.hunspell = sources.getHunspell();
|
2009-05-06 14:39:08 +00:00
|
|
|
cfg.transliteration = sources.getTransliteration();
|
2010-06-12 20:16:35 +00:00
|
|
|
cfg.forvo = sources.getForvo();
|
2009-05-05 08:57:37 +00:00
|
|
|
cfg.mediawikis = sources.getMediaWikis();
|
2009-05-16 18:04:21 +00:00
|
|
|
cfg.webSites = sources.getWebSites();
|
2011-05-29 05:08:37 +00:00
|
|
|
cfg.programs = sources.getPrograms();
|
2009-05-16 18:04:21 +00:00
|
|
|
|
2009-05-15 12:15:08 +00:00
|
|
|
groupInstances.clear(); // Those hold pointers to dictionaries, we need to
|
|
|
|
// free them.
|
2009-05-05 21:58:48 +00:00
|
|
|
|
|
|
|
ui.tabs->setUpdatesEnabled( false );
|
|
|
|
ui.tabs->removeTab( 1 );
|
2009-05-18 18:01:50 +00:00
|
|
|
ui.tabs->removeTab( 1 );
|
2009-05-05 21:58:48 +00:00
|
|
|
groups.reset();
|
2009-05-18 18:01:50 +00:00
|
|
|
orderAndProps.reset();
|
|
|
|
|
2009-05-15 12:39:53 +00:00
|
|
|
loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
|
|
|
|
|
2009-05-18 18:01:50 +00:00
|
|
|
// If no changes to groups were made, update the original data
|
2009-05-23 14:19:57 +00:00
|
|
|
bool noGroupEdits = ( origCfg.groups == savedGroups );
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
if ( noGroupEdits )
|
|
|
|
savedGroups = cfg.groups;
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
Instances::updateNames( savedGroups, dictionaries );
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
bool noOrderEdits = ( origCfg.dictionaryOrder == savedOrder );
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
if ( noOrderEdits )
|
|
|
|
savedOrder = cfg.dictionaryOrder;
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
Instances::updateNames( savedOrder, dictionaries );
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
bool noInactiveEdits = ( origCfg.inactiveDictionaries == savedInactive );
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
if ( noInactiveEdits )
|
|
|
|
savedInactive = cfg.inactiveDictionaries;
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
Instances::updateNames( savedInactive, dictionaries );
|
2009-05-18 18:01:50 +00:00
|
|
|
|
2009-05-15 12:39:53 +00:00
|
|
|
if ( rebuildGroups )
|
|
|
|
{
|
2009-05-18 18:01:50 +00:00
|
|
|
orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries );
|
|
|
|
ui.tabs->insertTab( 1, orderAndProps.get(), QIcon(":/icons/book.png"), tr( "&Dictionaries" ) );
|
|
|
|
|
2009-05-23 14:19:57 +00:00
|
|
|
groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() );
|
2009-05-18 18:01:50 +00:00
|
|
|
ui.tabs->insertTab( 2, groups.get(), QIcon(":/icons/bookcase.png"), tr( "&Groups" ) );
|
|
|
|
|
2009-05-15 12:39:53 +00:00
|
|
|
ui.tabs->setUpdatesEnabled( true );
|
2009-05-23 14:19:57 +00:00
|
|
|
|
|
|
|
if ( noGroupEdits )
|
|
|
|
origCfg.groups = groups->getGroups();
|
|
|
|
|
|
|
|
if ( noOrderEdits )
|
|
|
|
origCfg.dictionaryOrder = orderAndProps->getCurrentDictionaryOrder();
|
|
|
|
|
|
|
|
if ( noInactiveEdits )
|
|
|
|
origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();
|
2009-05-15 12:39:53 +00:00
|
|
|
}
|
2009-05-05 21:58:48 +00:00
|
|
|
}
|
2009-05-15 12:39:53 +00:00
|
|
|
|