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 "editdictionaries.hh"
#include "dict/loaddictionaries.hh" #include "dict/loaddictionaries.hh"
#include "help.hh" #include "help.hh"
#include <QTabWidget>
#include <QMessageBox> #include <QMessageBox>
using std::vector; using std::vector;
@ -48,12 +49,9 @@ EditDictionaries::EditDictionaries( QWidget * parent,
connect( &sources, &Sources::rescan, this, &EditDictionaries::rescanSources ); 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(), connect( orderAndProps, &OrderAndProps::showDictionaryHeadwords, this, &EditDictionaries::showDictionaryHeadwords );
&OrderAndProps::showDictionaryHeadwords,
this,
&EditDictionaries::showDictionaryHeadwords );
helpAction.setShortcut( QKeySequence( "F1" ) ); helpAction.setShortcut( QKeySequence( "F1" ) );
helpAction.setShortcutContext( Qt::WidgetWithChildrenShortcut ); helpAction.setShortcutContext( Qt::WidgetWithChildrenShortcut );
@ -70,6 +68,7 @@ EditDictionaries::EditDictionaries( QWidget * parent,
addAction( &helpAction ); addAction( &helpAction );
connect( ui.tabs, &QTabWidget::currentChanged, this, &EditDictionaries::currentChanged );
} }
void EditDictionaries::editGroup( unsigned id ) void EditDictionaries::editGroup( unsigned id )
@ -89,16 +88,15 @@ void EditDictionaries::editGroup( unsigned id )
void EditDictionaries::save( bool rebuildGroups ) void EditDictionaries::save( bool rebuildGroups )
{ {
Config::Groups newGroups = groups->getGroups(); const Config::Groups newGroups = groups->getGroups();
Config::Group newOrder = orderAndProps->getCurrentDictionaryOrder(); const Config::Group newOrder = orderAndProps->getCurrentDictionaryOrder();
Config::Group newInactive = orderAndProps->getCurrentInactiveDictionaries(); const Config::Group newInactive = orderAndProps->getCurrentInactiveDictionaries();
if( isSourcesChanged() ) if ( isSourcesChanged() )
acceptChangedSources( rebuildGroups ); acceptChangedSources( rebuildGroups );
if ( origCfg.groups != newGroups || origCfg.dictionaryOrder != newOrder || if ( origCfg.groups != newGroups || origCfg.dictionaryOrder != newOrder
origCfg.inactiveDictionaries != newInactive ) || origCfg.inactiveDictionaries != newInactive ) {
{
groupsChanged = true; groupsChanged = true;
cfg.groups = newGroups; cfg.groups = newGroups;
cfg.dictionaryOrder = newOrder; cfg.dictionaryOrder = newOrder;
@ -112,49 +110,49 @@ void EditDictionaries::accept()
QDialog::accept(); QDialog::accept();
} }
void EditDictionaries::on_tabs_currentChanged( int index ) void EditDictionaries::currentChanged( int index )
{ {
if ( index == -1 || !isVisible() ) if ( index == -1 || !isVisible() )
return; // Sent upon the construction/destruction return; // Sent upon the construction/destruction
qDebug() << ui.tabs->currentWidget()->objectName(); 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 // We're switching away from the Sources tab -- if its contents were
// changed, we need to either apply or reject now. // changed, we need to either apply or reject now.
if ( isSourcesChanged() ) { if ( isSourcesChanged() ) {
ui.tabs->setCurrentIndex( 0 );
QMessageBox question( QMessageBox::Question, QMessageBox question( QMessageBox::Question,
tr( "Sources changed" ), tr( "Sources changed" ),
tr( "Some sources were changed. Would you like to accept the changes?" ), tr( "Some sources were changed. Would you like to accept the changes?" ),
QMessageBox::NoButton, QMessageBox::NoButton,
this ); 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.addButton( tr( "Cancel" ), QMessageBox::RejectRole );
question.exec(); question.exec();
if ( question.clickedButton() == accept ) //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 ) {
acceptChangedSources( true ); disconnect( ui.tabs, &QTabWidget::currentChanged, this, &EditDictionaries::currentChanged );
acceptChangedSources( true );
ui.tabs->setCurrentIndex( index ); ui.tabs->setCurrentIndex( index );
connect( ui.tabs, &QTabWidget::currentChanged, this, &EditDictionaries::currentChanged );
} }
else { else {
// Prevent tab from switching // Prevent tab from switching
return; // return;
} }
} }
} }
else if ( lastDictionaryOrderTab ) { else if ( lastTabName == "OrderAndProps" ) {
// When switching from the dictionary order, we need to propagate any // When switching from the dictionary order, we need to propagate any
// changes to the groups. // changes to the groups.
groups->updateDictionaryOrder( orderAndProps->getCurrentDictionaryOrder() ); groups->updateDictionaryOrder( orderAndProps->getCurrentDictionaryOrder() );
} }
lastDictionaryOrderTab = ui.tabs->currentWidget()->objectName() == "OrderAndProps"; lastTabName = ui.tabs->currentWidget()->objectName();
} }
void EditDictionaries::rescanSources() void EditDictionaries::rescanSources()
@ -204,9 +202,9 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
cfg.programs = sources.getPrograms(); cfg.programs = sources.getPrograms();
cfg.voiceEngines = sources.getVoiceEngines(); cfg.voiceEngines = sources.getVoiceEngines();
groupInstances.clear(); // Those hold pointers to dictionaries, we need to
// free them.
ui.tabs->setUpdatesEnabled( false ); ui.tabs->setUpdatesEnabled( false );
// Those hold pointers to dictionaries, we need to free them.
groupInstances.clear();
groups.clear(); groups.clear();
orderAndProps.clear(); orderAndProps.clear();
@ -214,21 +212,21 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
loadDictionaries( this, true, cfg, dictionaries, dictNetMgr ); loadDictionaries( this, true, cfg, dictionaries, dictNetMgr );
// If no changes to groups were made, update the original data // If no changes to groups were made, update the original data
bool noGroupEdits = ( origCfg.groups == savedGroups ); const bool noGroupEdits = ( origCfg.groups == savedGroups );
if ( noGroupEdits ) if ( noGroupEdits )
savedGroups = cfg.groups; savedGroups = cfg.groups;
Instances::updateNames( savedGroups, dictionaries ); Instances::updateNames( savedGroups, dictionaries );
bool noOrderEdits = ( origCfg.dictionaryOrder == savedOrder ); const bool noOrderEdits = ( origCfg.dictionaryOrder == savedOrder );
if ( noOrderEdits ) if ( noOrderEdits )
savedOrder = cfg.dictionaryOrder; savedOrder = cfg.dictionaryOrder;
Instances::updateNames( savedOrder, dictionaries ); Instances::updateNames( savedOrder, dictionaries );
bool noInactiveEdits = ( origCfg.inactiveDictionaries == savedInactive ); const bool noInactiveEdits = ( origCfg.inactiveDictionaries == savedInactive );
if ( noInactiveEdits ) if ( noInactiveEdits )
savedInactive = cfg.inactiveDictionaries; savedInactive = cfg.inactiveDictionaries;
@ -238,13 +236,14 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
if ( rebuildGroups ) { if ( rebuildGroups ) {
ui.tabs->removeTab( 1 ); ui.tabs->removeTab( 1 );
ui.tabs->removeTab( 1 ); ui.tabs->removeTab( 1 );
orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries ); orderAndProps = new OrderAndProps( this, savedOrder, savedInactive, dictionaries );
groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() ); groups = new Groups( this, dictionaries, savedGroups, orderAndProps->getCurrentDictionaryOrder() );
ui.tabs->insertTab( 1, orderAndProps.get(), QIcon( ":/icons/book.svg" ), tr( "&Dictionaries" ) ); ui.tabs->insertTab( 1, orderAndProps.get(), QIcon( ":/icons/book.svg" ), tr( "&Dictionaries" ) );
ui.tabs->insertTab( 2, groups.get(), QIcon( ":/icons/bookcase.svg" ), tr( "&Groups" ) );
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 ) if ( noGroupEdits )
origCfg.groups = groups->getGroups(); origCfg.groups = groups->getGroups();
@ -256,5 +255,8 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries(); origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();
} }
ui.tabs->setUpdatesEnabled( true ); 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 Instances::Groups & groupInstances, // We only clear those on rescan
QNetworkAccessManager & dictNetMgr ); QNetworkAccessManager & dictNetMgr );
~EditDictionaries() = default; ~EditDictionaries();
/// Instructs the dialog to position itself on editing the given group. /// Instructs the dialog to position itself on editing the given group.
void editGroup( unsigned id ); void editGroup( unsigned id );
@ -45,7 +45,7 @@ protected:
private slots: private slots:
void on_tabs_currentChanged( int index ); void currentChanged( int index );
void buttonBoxClicked( QAbstractButton * button ); void buttonBoxClicked( QAbstractButton * button );
@ -84,7 +84,7 @@ private:
bool dictionariesChanged; bool dictionariesChanged;
bool groupsChanged; bool groupsChanged;
bool lastDictionaryOrderTab = false; QString lastTabName;
QAction helpAction; QAction helpAction;
}; };