goldendict-ng/src/editdictionaries.cc

143 lines
3.9 KiB
C++
Raw Normal View History

/* This file is (c) 2008-2009 Konstantin Isakov <ikm@users.berlios.de>
* 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_,
Instances::Groups & groupInstances_,
QNetworkAccessManager & dictNetMgr_ ):
QDialog( parent ), cfg( cfg_ ), dictionaries( dictionaries_ ),
groupInstances( groupInstances_ ),
dictNetMgr( dictNetMgr_ ),
origCfg( cfg ),
sources( this, cfg.paths, cfg.soundDirs, cfg.hunspell, cfg.transliteration,
cfg.mediawikis ),
groups( new Groups( this, dictionaries, cfg.groups ) ),
dictionariesChanged( false ),
groupsChanged( false ),
lastCurrentTab( 0 )
{
ui.setupUi( this );
2009-05-02 21:46:43 +00:00
setWindowIcon( QIcon(":/icons/book.png") );
ui.tabs->clear();
2009-05-02 21:46:43 +00:00
ui.tabs->addTab( &sources, QIcon(":/icons/book.png"), tr( "&Sources" ) );
ui.tabs->addTab( groups.get(), QIcon(":/icons/bookcase.png"), tr( "&Groups" ) );
connect( &sources, SIGNAL( rescan() ), this, SLOT( rescanSources() ) );
}
void EditDictionaries::accept()
{
if ( isSourcesChanged() )
acceptChangedSources();
2009-05-02 21:46:43 +00:00
Config::Groups newGroups = groups->getGroups();
2009-05-02 21:46:43 +00:00
if ( origCfg.groups != newGroups )
{
groupsChanged = true;
cfg.groups = newGroups;
}
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
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
question.addButton( tr( "Cancel" ), QMessageBox::RejectRole );
2009-05-02 21:46:43 +00:00
question.exec();
if ( question.clickedButton() == accept )
{
acceptChangedSources();
// Rebuild groups from scratch
2009-05-02 21:46:43 +00:00
rebuildGroups();
lastCurrentTab = index;
ui.tabs->setCurrentIndex( index );
}
else
{
// Prevent tab from switching
lastCurrentTab = 0;
return;
}
}
}
lastCurrentTab = index;
}
void EditDictionaries::rescanSources()
{
acceptChangedSources();
rebuildGroups();
}
bool EditDictionaries::isSourcesChanged() const
{
return sources.getPaths() != cfg.paths ||
sources.getSoundDirs() != cfg.soundDirs ||
sources.getHunspell() != cfg.hunspell ||
sources.getTransliteration() != cfg.transliteration ||
sources.getMediaWikis() != cfg.mediawikis;
}
void EditDictionaries::acceptChangedSources()
{
dictionariesChanged = true;
2009-05-02 21:46:43 +00:00
cfg.paths = sources.getPaths();
cfg.soundDirs = sources.getSoundDirs();
cfg.hunspell = sources.getHunspell();
cfg.transliteration = sources.getTransliteration();
cfg.mediawikis = sources.getMediaWikis();
groupInstances.clear(); // Those hold pointers to dictionaries, we need to
// free them.
loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
}
void EditDictionaries::rebuildGroups()
{
Config::Groups savedGroups = groups->getGroups();
ui.tabs->setUpdatesEnabled( false );
ui.tabs->removeTab( 1 );
groups.reset();
groups = new Groups( this, dictionaries, savedGroups );
ui.tabs->insertTab( 1, groups.get(), QIcon(":/icons/bookcase.png"), tr( "&Groups" ) );
ui.tabs->setUpdatesEnabled( true );
}