From 290676a2a648ad6cf463ea5c3d04f56a3906b9d4 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Fri, 29 Mar 2019 19:50:17 +0200 Subject: [PATCH] Refactoring. Return value or default. --HG-- branch : feature --- src/libs/vmisc/vsettings.cpp | 110 ++++------------------------------- src/libs/vmisc/vsettings.h | 27 +++++++++ 2 files changed, 38 insertions(+), 99 deletions(-) diff --git a/src/libs/vmisc/vsettings.cpp b/src/libs/vmisc/vsettings.cpp index 5dcd9bfb9..ff756d342 100644 --- a/src/libs/vmisc/vsettings.cpp +++ b/src/libs/vmisc/vsettings.cpp @@ -181,17 +181,7 @@ void VSettings::SetOpenGLRender(bool value) //--------------------------------------------------------------------------------------------------------------------- qreal VSettings::GetLayoutPaperHeight() const { - const qreal def = UnitConvertor(1189/*A0*/, Unit::Mm, Unit::Px); - bool ok = false; - const qreal height = value(*settingLayoutPaperHeight, def).toDouble(&ok); - if (ok) - { - return height; - } - else - { - return def; - } + return ValueOrDef(*settingLayoutPaperHeight, UnitConvertor(1189/*A0*/, Unit::Mm, Unit::Px)); } //--------------------------------------------------------------------------------------------------------------------- @@ -203,17 +193,7 @@ void VSettings::SetLayoutPaperHeight(qreal value) //--------------------------------------------------------------------------------------------------------------------- qreal VSettings::GetLayoutPaperWidth() const { - const qreal def = UnitConvertor(841/*A0*/, Unit::Mm, Unit::Px); - bool ok = false; - const qreal width = value(*settingLayoutPaperWidth, def).toDouble(&ok); - if (ok) - { - return width; - } - else - { - return def; - } + return ValueOrDef(*settingLayoutPaperWidth, UnitConvertor(841/*A0*/, Unit::Mm, Unit::Px)); } //--------------------------------------------------------------------------------------------------------------------- @@ -225,17 +205,7 @@ void VSettings::SetLayoutPaperWidth(qreal value) //--------------------------------------------------------------------------------------------------------------------- qreal VSettings::GetLayoutWidth() const { - const qreal def = GetDefLayoutWidth(); - bool ok = false; - const qreal lWidth = value(*settingLayoutWidth, def).toDouble(&ok); - if (ok) - { - return lWidth; - } - else - { - return def; - } + return ValueOrDef(*settingLayoutWidth, GetDefLayoutWidth()); } //--------------------------------------------------------------------------------------------------------------------- @@ -253,10 +223,7 @@ void VSettings::SetLayoutWidth(qreal value) //--------------------------------------------------------------------------------------------------------------------- int VSettings::GetNestingTime() const { - const int def = GetDefNestingTime(); - bool ok = false; - const int time = value(*settingNestingTime, def).toInt(&ok); - return ok ? time : def; + return ValueOrDef(*settingNestingTime, GetDefNestingTime()); } //--------------------------------------------------------------------------------------------------------------------- @@ -268,10 +235,7 @@ void VSettings::SetNestingTime(int value) //--------------------------------------------------------------------------------------------------------------------- qreal VSettings::GetEfficiencyCoefficient() const { - const qreal def = GetDefEfficiencyCoefficient(); - bool ok = false; - const qreal coefficient = value(*settingEfficiencyCoefficient, def).toDouble(&ok); - return ok ? coefficient : def; + return ValueOrDef(*settingEfficiencyCoefficient, GetDefEfficiencyCoefficient()); } //--------------------------------------------------------------------------------------------------------------------- @@ -283,12 +247,7 @@ void VSettings::SetEfficiencyCoefficient(qreal value) //--------------------------------------------------------------------------------------------------------------------- QMarginsF VSettings::GetFields(const QMarginsF &def) const { - const QVariant val = value(*settingFields, QVariant::fromValue(def)); - if (val.canConvert()) - { - return val.value(); - } - return def; + return ValueOrDef(*settingFields, def); } //--------------------------------------------------------------------------------------------------------------------- @@ -300,24 +259,7 @@ void VSettings::SetFields(const QMarginsF &value) //--------------------------------------------------------------------------------------------------------------------- Cases VSettings::GetLayoutGroup() const { - const Cases def = GetDefLayoutGroup(); - bool ok = false; - const int g = value(*settingLayoutSorting, static_cast(def)).toInt(&ok); - if (ok) - { - if (g >= static_cast(Cases::UnknownCase)) - { - return def; - } - else - { - return static_cast(g); - } - } - else - { - return def; - } + return ValueOrDef(*settingLayoutSorting, GetDefLayoutGroup()); } //--------------------------------------------------------------------------------------------------------------------- @@ -512,15 +454,7 @@ void VSettings::SetRememberPatternMaterials(bool value) QMarginsF VSettings::GetTiledPDFMargins(const Unit &unit) const { // default value is 10mm. We save the margins in mm in the setting. - const QMarginsF def = QMarginsF(10, 10, 10, 10); - - const QVariant val = value(*settingTiledPDFMargins, QVariant::fromValue(def)); - - if (val.canConvert()) - { - return UnitConvertor(val.value(), Unit::Mm, unit); - } - return UnitConvertor(def, Unit::Mm, unit); + return UnitConvertor(ValueOrDef(*settingTiledPDFMargins, QMarginsF(10, 10, 10, 10)), Unit::Mm, unit); } //--------------------------------------------------------------------------------------------------------------------- @@ -544,17 +478,7 @@ void VSettings::SetTiledPDFMargins(const QMarginsF &value, const Unit &unit) */ qreal VSettings::GetTiledPDFPaperHeight(const Unit &unit) const { - const qreal def = 297 /*A4*/; - bool ok = false; - const qreal height = value(*settingTiledPDFPaperHeight, def).toDouble(&ok); - if (ok) - { - return UnitConvertor(height, Unit::Mm, unit); - } - else - { - return UnitConvertor(def, Unit::Mm, unit); - } + return UnitConvertor(ValueOrDef(*settingTiledPDFPaperHeight, 297 /*A4*/), Unit::Mm, unit); } //--------------------------------------------------------------------------------------------------------------------- @@ -577,18 +501,7 @@ void VSettings::SetTiledPDFPaperHeight(qreal value, const Unit &unit) */ qreal VSettings::GetTiledPDFPaperWidth(const Unit &unit) const { - - const qreal def = 210 /*A4*/; - bool ok = false; - const qreal width = value(*settingTiledPDFPaperWidth, def).toDouble(&ok); - if (ok) - { - return UnitConvertor(width, Unit::Mm, unit); - } - else - { - return UnitConvertor(def, Unit::Mm, unit); - } + return UnitConvertor(ValueOrDef(*settingTiledPDFPaperWidth, 210 /*A4*/), Unit::Mm, unit); } //--------------------------------------------------------------------------------------------------------------------- @@ -692,8 +605,7 @@ T VSettings::GetCachedValue(T &cache, const QString &setting, T defValue, T valu { if (cache < 0) { - const QVariant val = value(setting, defValue); - cache = val.canConvert() ? qBound(valueMin, val.value(), valueMax) : defValue; + cache = qBound(valueMin, ValueOrDef(setting, defValue), valueMax); } return cache; diff --git a/src/libs/vmisc/vsettings.h b/src/libs/vmisc/vsettings.h index 51fa22134..a9741a8b7 100644 --- a/src/libs/vmisc/vsettings.h +++ b/src/libs/vmisc/vsettings.h @@ -179,6 +179,33 @@ private: template T GetCachedValue(T &cache, const QString &setting, T defValue, T valueMin, T valueMax) const; + + template + T ValueOrDef(const QString &setting, const T &defValue) const; }; +//--------------------------------------------------------------------------------------------------------------------- +template +inline T VSettings::ValueOrDef(const QString &setting, const T &defValue) const +{ + const QVariant val = value(setting, QVariant::fromValue(defValue)); + return val.canConvert() ? val.value() : defValue; +} + +//--------------------------------------------------------------------------------------------------------------------- +template <> +inline Cases VSettings::ValueOrDef(const QString &setting, const Cases &defValue) const +{ + const QVariant val = value(setting, QVariant::fromValue(static_cast(defValue))); + const int g = val.canConvert() ? val.value() : static_cast(defValue); + if (g < static_cast(Cases::CaseThreeGroup) || g >= static_cast(Cases::UnknownCase)) + { + return defValue; + } + else + { + return static_cast(g); + } +} + #endif // VSETTINGS_H