2009-04-30 15:29:03 +00:00
|
|
|
/* 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") );
|
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
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" ) );
|
2009-05-05 08:57:37 +00:00
|
|
|
|
|
|
|
connect( &sources, SIGNAL( rescan() ), this, SLOT( rescanSources() ) );
|
2009-04-30 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EditDictionaries::accept()
|
|
|
|
{
|
2009-05-05 08:57:37 +00:00
|
|
|
if ( isSourcesChanged() )
|
|
|
|
acceptChangedSources();
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
Config::Groups newGroups = groups->getGroups();
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-04-30 15:29:03 +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
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
acceptChangedSources();
|
|
|
|
|
|
|
|
// Rebuild groups from scratch
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-05-05 21:58:48 +00:00
|
|
|
rebuildGroups();
|
|
|
|
|
2009-04-30 15:29:03 +00:00
|
|
|
lastCurrentTab = index;
|
|
|
|
ui.tabs->setCurrentIndex( index );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Prevent tab from switching
|
|
|
|
lastCurrentTab = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lastCurrentTab = index;
|
|
|
|
}
|
|
|
|
|
2009-05-05 08:57:37 +00:00
|
|
|
void EditDictionaries::rescanSources()
|
|
|
|
{
|
|
|
|
acceptChangedSources();
|
2009-05-05 21:58:48 +00:00
|
|
|
rebuildGroups();
|
2009-05-05 08:57:37 +00:00
|
|
|
}
|
|
|
|
|
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 ||
|
|
|
|
sources.getMediaWikis() != cfg.mediawikis;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditDictionaries::acceptChangedSources()
|
|
|
|
{
|
2009-05-05 08:57:37 +00:00
|
|
|
dictionariesChanged = true;
|
2009-05-02 21:46:43 +00:00
|
|
|
|
2009-05-05 08:57:37 +00:00
|
|
|
cfg.paths = sources.getPaths();
|
|
|
|
cfg.soundDirs = sources.getSoundDirs();
|
|
|
|
cfg.hunspell = sources.getHunspell();
|
|
|
|
cfg.mediawikis = sources.getMediaWikis();
|
2009-04-30 15:29:03 +00:00
|
|
|
|
2009-05-05 08:57:37 +00:00
|
|
|
loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
|
2009-04-30 15:29:03 +00:00
|
|
|
}
|
2009-05-05 21:58:48 +00:00
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|