Skip to content

Commit

Permalink
opt: extract GroupBackup structure to store the old group info (#1883)
Browse files Browse the repository at this point in the history
* opt: backup group info

* opt: remove updateNames condtion

* opt: rename variable origCfg to a more friendly name

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
xiaoyifang and autofix-ci[bot] authored Nov 1, 2024
1 parent 8a92cbf commit 6b33178
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 40 deletions.
7 changes: 7 additions & 0 deletions src/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,13 @@ enum class ToolbarsIconSize : std::uint8_t {
Large = 2,
};

struct GroupBackup
{
Group dictionaryOrder;
Group inactiveDictionaries;
Groups groups;
};

struct Class
{
Paths paths;
Expand Down
44 changes: 5 additions & 39 deletions src/ui/editdictionaries.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ EditDictionaries::EditDictionaries( QWidget * parent,
dictionaries( dictionaries_ ),
groupInstances( groupInstances_ ),
dictNetMgr( dictNetMgr_ ),
origCfg( cfg ),
sources( this, cfg ),
orderAndProps( new OrderAndProps( this, cfg.dictionaryOrder, cfg.inactiveDictionaries, dictionaries ) ),
groups( new Groups( this, dictionaries, cfg.groups, orderAndProps->getCurrentDictionaryOrder() ) ),
Expand All @@ -31,9 +30,9 @@ EditDictionaries::EditDictionaries( QWidget * parent,
// would like to preserve them if no edits were done. To that end, we save
// the initial group readings so that if no edits were really done, we won't
// be changing groups.
origCfg.groups = groups->getGroups();
origCfg.dictionaryOrder = orderAndProps->getCurrentDictionaryOrder();
origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();
origGroups.groups = groups->getGroups();
origGroups.dictionaryOrder = orderAndProps->getCurrentDictionaryOrder();
origGroups.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();

ui.setupUi( this );

Expand Down Expand Up @@ -94,8 +93,8 @@ void EditDictionaries::save( bool rebuildGroups )
acceptChangedSources( rebuildGroups );
}

if ( origCfg.groups != newGroups || origCfg.dictionaryOrder != newOrder
|| origCfg.inactiveDictionaries != newInactive ) {
if ( origGroups.groups != newGroups || origGroups.dictionaryOrder != newOrder
|| origGroups.inactiveDictionaries != newInactive ) {
groupsChanged = true;
cfg.groups = newGroups;
cfg.dictionaryOrder = newOrder;
Expand Down Expand Up @@ -210,29 +209,8 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )

loadDictionaries( this, cfg, dictionaries, dictNetMgr );

// If no changes to groups were made, update the original data
const bool noGroupEdits = ( origCfg.groups == savedGroups );

if ( noGroupEdits ) {
savedGroups = cfg.groups;
}

Instances::updateNames( savedGroups, dictionaries );

const bool noOrderEdits = ( origCfg.dictionaryOrder == savedOrder );

if ( noOrderEdits ) {
savedOrder = cfg.dictionaryOrder;
}

Instances::updateNames( savedOrder, dictionaries );

const bool noInactiveEdits = ( origCfg.inactiveDictionaries == savedInactive );

if ( noInactiveEdits ) {
savedInactive = cfg.inactiveDictionaries;
}

Instances::updateNames( savedInactive, dictionaries );

if ( rebuildGroups ) {
Expand All @@ -246,18 +224,6 @@ void EditDictionaries::acceptChangedSources( bool rebuildGroups )
ui.tabs->insertTab( 2, groups, 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();
}

if ( noOrderEdits ) {
origCfg.dictionaryOrder = orderAndProps->getCurrentDictionaryOrder();
}

if ( noInactiveEdits ) {
origCfg.inactiveDictionaries = orderAndProps->getCurrentInactiveDictionaries();
}
}
ui.tabs->setUpdatesEnabled( true );
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui/editdictionaries.hh
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ private:
QNetworkAccessManager & dictNetMgr;

// Backed up to decide later if something was changed or not
Config::Class origCfg;
Config::GroupBackup origGroups;

Ui::EditDictionaries ui;
Sources sources;
Expand Down

0 comments on commit 6b33178

Please sign in to comment.