fix: editDictionaries tab interaction

This commit is contained in:
YiFang Xiao 2023-06-23 09:23:36 +08:00
parent aa94af4382
commit 2359352781
2 changed files with 54 additions and 52 deletions

View file

@ -4,6 +4,7 @@
#include "editdictionaries.hh"
#include "dict/loaddictionaries.hh"
#include "help.hh"
#include <QTabWidget>
#include <QMessageBox>
using std::vector;
@ -48,12 +49,9 @@ EditDictionaries::EditDictionaries( QWidget * parent,
connect( &sources, &Sources::rescan, this, &EditDictionaries::rescanSources );
connect( groups.get(), &Groups::showDictionaryInfo, this, &EditDictionaries::showDictionaryInfo );
connect( groups, &Groups::showDictionaryInfo, this, &EditDictionaries::showDictionaryInfo );
connect( orderAndProps.data(),
&OrderAndProps::showDictionaryHeadwords,
this,
&EditDictionaries::showDictionaryHeadwords );
connect( orderAndProps, &OrderAndProps::showDictionaryHeadwords, this, &EditDictionaries::showDictionaryHeadwords );
helpAction.setShortcut( QKeySequence( "F1" ) );
helpAction.setShortcutContext( Qt::WidgetWithChildrenShortcut );
@ -70,6 +68,7 @@ EditDictionaries::EditDictionaries( QWidget * parent,
addAction( &helpAction );
connect( ui.tabs, &QTabWidget::currentChanged, this, &EditDictionaries::currentChanged );
}
void EditDictionaries::editGroup( unsigned id )
@ -89,16 +88,15 @@ void EditDictionaries::editGroup( unsigned id )
void EditDictionaries::save( bool rebuildGroups )
{
Config::Groups newGroups = groups->getGroups();
Config::Group newOrder = orderAndProps->getCurrentDictionaryOrder();
Config::Group newInactive = orderAndProps->getCurrentInactiveDictionaries();
const Config::Groups newGroups = groups->getGroups();
const Config::Group newOrder = orderAndProps->getCurrentDictionaryOrder();
const Config::Group newInactive = orderAndProps->getCurrentInactiveDictionaries();
if ( isSourcesChanged() )
acceptChangedSources( rebuildGroups );
if ( origCfg.groups != newGroups || origCfg.dictionaryOrder != newOrder ||
origCfg.inactiveDictionaries != newInactive )
{
if ( origCfg.groups != newGroups || origCfg.dictionaryOrder != newOrder
|| origCfg.inactiveDictionaries != newInactive ) {
groupsChanged = true;
cfg.groups = newGroups;
cfg.dictionaryOrder = newOrder;
@ -112,49 +110,49 @@ void EditDictionaries::accept()
QDialog::accept();
}
void EditDictionaries::on_tabs_currentChanged( int index )
void EditDictionaries::currentChanged( int index )
{
if ( index == -1 || !isVisible() )
return; // Sent upon the construction/destruction
qDebug() << ui.tabs->currentWidget()->objectName();
if ( ui.tabs->currentWidget()->objectName() == "Sources" ) {
if ( lastTabName.isEmpty() || lastTabName == "Sources" ) {
// 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 );
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 );
const QPushButton * accept = question.addButton( tr( "Accept" ), QMessageBox::AcceptRole );
question.addButton( tr( "Cancel" ), QMessageBox::RejectRole );
question.exec();
if ( question.clickedButton() == accept )
{
acceptChangedSources( true );
//When accept the changes ,the second and third tab will be recreated. which means the current Index tab will be changed.
if ( question.clickedButton() == accept ) {
disconnect( ui.tabs, &QTabWidget::currentChanged, this, &EditDictionaries::currentChanged );
acceptChangedSources( true );
ui.tabs->setCurrentIndex( index );
connect( ui.tabs, &QTabWidget::currentChanged, this, &EditDictionaries::currentChanged );
}
else {
// Prevent tab from switching
return;
// return;
}
}
}
else if ( lastDictionaryOrderTab ) {
else if ( lastTabName == "OrderAndProps" ) {
// When switching from the dictionary order, we need to propagate any
// changes to the groups.
groups->updateDictionaryOrder( orderAndProps->getCurrentDictionaryOrder() );
}
lastDictionaryOrderTab = ui.tabs->currentWidget()->objectName() == "OrderAndProps";
lastTabName = ui.tabs->currentWidget()->objectName();
}
void EditDictionaries::rescanSources()
@ -204,9 +202,9 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
cfg.programs = sources.getPrograms();
cfg.voiceEngines = sources.getVoiceEngines();
groupInstances.clear(); // Those hold pointers to dictionaries, we need to
// free them.
ui.tabs->setUpdatesEnabled( false );
// Those hold pointers to dictionaries, we need to free them.
groupInstances.clear();
groups.clear();
orderAndProps.clear();
@ -214,21 +212,21 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
// If no changes to groups were made, update the original data
bool noGroupEdits = ( origCfg.groups == savedGroups );
const bool noGroupEdits = ( origCfg.groups == savedGroups );
if ( noGroupEdits )
savedGroups = cfg.groups;
Instances::updateNames( savedGroups, dictionaries );
bool noOrderEdits = ( origCfg.dictionaryOrder == savedOrder );
const bool noOrderEdits = ( origCfg.dictionaryOrder == savedOrder );
if ( noOrderEdits )
savedOrder = cfg.dictionaryOrder;
Instances::updateNames( savedOrder, dictionaries );
bool noInactiveEdits = ( origCfg.inactiveDictionaries == savedInactive );
const bool noInactiveEdits = ( origCfg.inactiveDictionaries == savedInactive );
if ( noInactiveEdits )
savedInactive = cfg.inactiveDictionaries;
@ -238,13 +236,14 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
if ( rebuildGroups ) {
ui.tabs->removeTab( 1 );
ui.tabs->removeTab( 1 );
orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries );
groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() );
ui.tabs->insertTab( 1, orderAndProps.get(), QIcon( ":/icons/book.svg" ), tr( "&Dictionaries" ) );
ui.tabs->insertTab( 2, groups.get(), QIcon( ":/icons/bookcase.svg" ), tr( "&Groups" ) );
connect( groups, &Groups::showDictionaryInfo, this, &EditDictionaries::showDictionaryInfo );
connect( orderAndProps, &OrderAndProps::showDictionaryHeadwords, this, &EditDictionaries::showDictionaryHeadwords );
if ( noGroupEdits )
origCfg.groups = groups->getGroups();
@ -256,5 +255,8 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();
}
ui.tabs->setUpdatesEnabled( true );
}
EditDictionaries::~EditDictionaries()
{
disconnect( ui.tabs, &QTabWidget::currentChanged, this, &EditDictionaries::currentChanged );
}

View file

@ -26,7 +26,7 @@ public:
Instances::Groups & groupInstances, // We only clear those on rescan
QNetworkAccessManager & dictNetMgr );
~EditDictionaries() = default;
~EditDictionaries();
/// Instructs the dialog to position itself on editing the given group.
void editGroup( unsigned id );
@ -45,7 +45,7 @@ protected:
private slots:
void on_tabs_currentChanged( int index );
void currentChanged( int index );
void buttonBoxClicked( QAbstractButton * button );
@ -84,7 +84,7 @@ private:
bool dictionariesChanged;
bool groupsChanged;
bool lastDictionaryOrderTab = false;
QString lastTabName;
QAction helpAction;
};