fix: code smells

refactor the disabledType string build
This commit is contained in:
YiFang Xiao 2023-06-01 19:52:16 +08:00
parent 4b8c87a30d
commit 002f7a02f7
2 changed files with 28 additions and 88 deletions

View file

@ -11,9 +11,9 @@
#include <QWebEngineSettings> #include <QWebEngineSettings>
Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ): Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
QDialog( parent ), prevInterfaceLanguage( 0 ) QDialog( parent ),
, cfg( cfg_ ) cfg( cfg_ ),
, helpAction( this ) helpAction( this )
{ {
Config::Preferences const & p = cfg_.preferences; Config::Preferences const & p = cfg_.preferences;
ui.setupUi( this ); ui.setupUi( this );
@ -342,6 +342,16 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
ui.maxDictionarySize->setValue( p.fts.maxDictionarySize ); ui.maxDictionarySize->setValue( p.fts.maxDictionarySize );
} }
void Preferences::buildDisabledTypes(QString & disabledTypes, bool is_checked, QString name )
{
if( !is_checked )
{
if ( !disabledTypes.isEmpty() )
disabledTypes += ',';
disabledTypes += name ;
}
}
Config::Preferences Preferences::getPreferences() Config::Preferences Preferences::getPreferences()
{ {
Config::Preferences p; Config::Preferences p;
@ -463,89 +473,18 @@ Config::Preferences Preferences::getPreferences()
p.fts.enabled = ui.ftsGroupBox->isChecked(); p.fts.enabled = ui.ftsGroupBox->isChecked();
p.fts.maxDictionarySize = ui.maxDictionarySize->value(); p.fts.maxDictionarySize = ui.maxDictionarySize->value();
if( !ui.allowAard->isChecked() ) buildDisabledTypes( p.fts.disabledTypes, ui.allowAard->isChecked(), "AARD" );
{ buildDisabledTypes( p.fts.disabledTypes, ui.allowBGL->isChecked(), "BGL" );
if( !p.fts.disabledTypes.isEmpty() ) buildDisabledTypes( p.fts.disabledTypes, ui.allowDictD->isChecked(), "DICTD" );
p.fts.disabledTypes += ','; buildDisabledTypes( p.fts.disabledTypes, ui.allowDSL->isChecked(), "DSL" );
p.fts.disabledTypes += "AARD"; buildDisabledTypes( p.fts.disabledTypes, ui.allowMDict->isChecked(), "MDICT" );
} buildDisabledTypes( p.fts.disabledTypes, ui.allowSDict->isChecked(), "SDICT" );
buildDisabledTypes( p.fts.disabledTypes, ui.allowSlob->isChecked(), "SLOB" );
if( !ui.allowBGL->isChecked() ) buildDisabledTypes( p.fts.disabledTypes, ui.allowStardict->isChecked(), "STARDICT" );
{ buildDisabledTypes( p.fts.disabledTypes, ui.allowXDXF->isChecked(), "XDXF" );
if( !p.fts.disabledTypes.isEmpty() ) buildDisabledTypes( p.fts.disabledTypes, ui.allowZim->isChecked(), "ZIM" );
p.fts.disabledTypes += ','; buildDisabledTypes( p.fts.disabledTypes, ui.allowEpwing->isChecked(), "EPWING" );
p.fts.disabledTypes += "BGL"; buildDisabledTypes( p.fts.disabledTypes, ui.allowGls->isChecked(), "GLS" );
}
if( !ui.allowDictD->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "DICTD";
}
if( !ui.allowDSL->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "DSL";
}
if( !ui.allowMDict->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "MDICT";
}
if( !ui.allowSDict->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "SDICT";
}
if( !ui.allowSlob->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "SLOB";
}
if( !ui.allowStardict->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "STARDICT";
}
if( !ui.allowXDXF->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "XDXF";
}
if( !ui.allowZim->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "ZIM";
}
if( !ui.allowEpwing->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "EPWING";
}
if( !ui.allowGls->isChecked() )
{
if( !p.fts.disabledTypes.isEmpty() )
p.fts.disabledTypes += ',';
p.fts.disabledTypes += "GLS";
}
return p; return p;
} }

View file

@ -11,7 +11,7 @@ class Preferences: public QDialog
{ {
Q_OBJECT Q_OBJECT
int prevInterfaceLanguage; int prevInterfaceLanguage = 0;
QString prevWebFontFamily; QString prevWebFontFamily;
@ -21,7 +21,8 @@ class Preferences: public QDialog
public: public:
Preferences( QWidget * parent, Config::Class & cfg_ ); Preferences( QWidget * parent, Config::Class & cfg_ );
~Preferences() = default; void buildDisabledTypes( QString & disabledTypes, bool is_checked, QString name );
~Preferences() override = default;
Config::Preferences getPreferences(); Config::Preferences getPreferences();