goldendict-ng/src/editdictionaries.cc

124 lines
3.4 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_,
QNetworkAccessManager & dictNetMgr_ ):
QDialog( parent ), cfg( cfg_ ), dictionaries( dictionaries_ ),
dictNetMgr( dictNetMgr_ ),
origCfg( cfg ),
sources( this, cfg.paths, cfg.soundDirs, cfg.hunspell, 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" ) );
}
void EditDictionaries::accept()
{
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
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(), tr( "&Groups" ) );
ui.tabs->setUpdatesEnabled( true );
2009-05-02 21:46:43 +00:00
lastCurrentTab = index;
ui.tabs->setCurrentIndex( index );
}
else
{
// Prevent tab from switching
lastCurrentTab = 0;
return;
}
}
}
lastCurrentTab = index;
}
bool EditDictionaries::isSourcesChanged() const
{
return sources.getPaths() != cfg.paths ||
sources.getSoundDirs() != cfg.soundDirs ||
sources.getHunspell() != cfg.hunspell ||
sources.getMediaWikis() != cfg.mediawikis;
}
void EditDictionaries::acceptChangedSources()
{
if ( isSourcesChanged() )
{
dictionariesChanged = true;
2009-05-02 21:46:43 +00:00
cfg.paths = sources.getPaths();
cfg.soundDirs = sources.getSoundDirs();
cfg.hunspell = sources.getHunspell();
cfg.mediawikis = sources.getMediaWikis();
loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
}
}