Refactoring. Return value or default.

--HG--
branch : feature
This commit is contained in:
Roman Telezhynskyi 2019-03-29 19:50:17 +02:00
parent 248b0e3eeb
commit 290676a2a6
2 changed files with 38 additions and 99 deletions

View file

@ -181,17 +181,7 @@ void VSettings::SetOpenGLRender(bool value)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal VSettings::GetLayoutPaperHeight() const qreal VSettings::GetLayoutPaperHeight() const
{ {
const qreal def = UnitConvertor(1189/*A0*/, Unit::Mm, Unit::Px); return ValueOrDef<qreal>(*settingLayoutPaperHeight, 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;
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -203,17 +193,7 @@ void VSettings::SetLayoutPaperHeight(qreal value)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal VSettings::GetLayoutPaperWidth() const qreal VSettings::GetLayoutPaperWidth() const
{ {
const qreal def = UnitConvertor(841/*A0*/, Unit::Mm, Unit::Px); return ValueOrDef<qreal>(*settingLayoutPaperWidth, 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;
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -225,17 +205,7 @@ void VSettings::SetLayoutPaperWidth(qreal value)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal VSettings::GetLayoutWidth() const qreal VSettings::GetLayoutWidth() const
{ {
const qreal def = GetDefLayoutWidth(); return ValueOrDef<qreal>(*settingLayoutWidth, GetDefLayoutWidth());
bool ok = false;
const qreal lWidth = value(*settingLayoutWidth, def).toDouble(&ok);
if (ok)
{
return lWidth;
}
else
{
return def;
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -253,10 +223,7 @@ void VSettings::SetLayoutWidth(qreal value)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
int VSettings::GetNestingTime() const int VSettings::GetNestingTime() const
{ {
const int def = GetDefNestingTime(); return ValueOrDef<int>(*settingNestingTime, GetDefNestingTime());
bool ok = false;
const int time = value(*settingNestingTime, def).toInt(&ok);
return ok ? time : def;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -268,10 +235,7 @@ void VSettings::SetNestingTime(int value)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
qreal VSettings::GetEfficiencyCoefficient() const qreal VSettings::GetEfficiencyCoefficient() const
{ {
const qreal def = GetDefEfficiencyCoefficient(); return ValueOrDef<qreal>(*settingEfficiencyCoefficient, GetDefEfficiencyCoefficient());
bool ok = false;
const qreal coefficient = value(*settingEfficiencyCoefficient, def).toDouble(&ok);
return ok ? coefficient : def;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -283,12 +247,7 @@ void VSettings::SetEfficiencyCoefficient(qreal value)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
QMarginsF VSettings::GetFields(const QMarginsF &def) const QMarginsF VSettings::GetFields(const QMarginsF &def) const
{ {
const QVariant val = value(*settingFields, QVariant::fromValue(def)); return ValueOrDef<QMarginsF>(*settingFields, def);
if (val.canConvert<QMarginsF>())
{
return val.value<QMarginsF>();
}
return def;
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -300,24 +259,7 @@ void VSettings::SetFields(const QMarginsF &value)
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
Cases VSettings::GetLayoutGroup() const Cases VSettings::GetLayoutGroup() const
{ {
const Cases def = GetDefLayoutGroup(); return ValueOrDef<Cases>(*settingLayoutSorting, GetDefLayoutGroup());
bool ok = false;
const int g = value(*settingLayoutSorting, static_cast<int>(def)).toInt(&ok);
if (ok)
{
if (g >= static_cast<int>(Cases::UnknownCase))
{
return def;
}
else
{
return static_cast<Cases>(g);
}
}
else
{
return def;
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -512,15 +454,7 @@ void VSettings::SetRememberPatternMaterials(bool value)
QMarginsF VSettings::GetTiledPDFMargins(const Unit &unit) const QMarginsF VSettings::GetTiledPDFMargins(const Unit &unit) const
{ {
// default value is 10mm. We save the margins in mm in the setting. // default value is 10mm. We save the margins in mm in the setting.
const QMarginsF def = QMarginsF(10, 10, 10, 10); return UnitConvertor(ValueOrDef<QMarginsF>(*settingTiledPDFMargins, QMarginsF(10, 10, 10, 10)), Unit::Mm, unit);
const QVariant val = value(*settingTiledPDFMargins, QVariant::fromValue(def));
if (val.canConvert<QMarginsF>())
{
return UnitConvertor(val.value<QMarginsF>(), Unit::Mm, unit);
}
return UnitConvertor(def, Unit::Mm, unit);
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -544,17 +478,7 @@ void VSettings::SetTiledPDFMargins(const QMarginsF &value, const Unit &unit)
*/ */
qreal VSettings::GetTiledPDFPaperHeight(const Unit &unit) const qreal VSettings::GetTiledPDFPaperHeight(const Unit &unit) const
{ {
const qreal def = 297 /*A4*/; return UnitConvertor(ValueOrDef<qreal>(*settingTiledPDFPaperHeight, 297 /*A4*/), Unit::Mm, unit);
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);
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -577,18 +501,7 @@ void VSettings::SetTiledPDFPaperHeight(qreal value, const Unit &unit)
*/ */
qreal VSettings::GetTiledPDFPaperWidth(const Unit &unit) const qreal VSettings::GetTiledPDFPaperWidth(const Unit &unit) const
{ {
return UnitConvertor(ValueOrDef<qreal>(*settingTiledPDFPaperWidth, 210 /*A4*/), Unit::Mm, unit);
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);
}
} }
//--------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------
@ -692,8 +605,7 @@ T VSettings::GetCachedValue(T &cache, const QString &setting, T defValue, T valu
{ {
if (cache < 0) if (cache < 0)
{ {
const QVariant val = value(setting, defValue); cache = qBound(valueMin, ValueOrDef(setting, defValue), valueMax);
cache = val.canConvert<T>() ? qBound(valueMin, val.value<T>(), valueMax) : defValue;
} }
return cache; return cache;

View file

@ -179,6 +179,33 @@ private:
template <typename T> template <typename T>
T GetCachedValue(T &cache, const QString &setting, T defValue, T valueMin, T valueMax) const; T GetCachedValue(T &cache, const QString &setting, T defValue, T valueMin, T valueMax) const;
template <class T>
T ValueOrDef(const QString &setting, const T &defValue) const;
}; };
//---------------------------------------------------------------------------------------------------------------------
template <class T>
inline T VSettings::ValueOrDef(const QString &setting, const T &defValue) const
{
const QVariant val = value(setting, QVariant::fromValue(defValue));
return val.canConvert<T>() ? val.value<T>() : defValue;
}
//---------------------------------------------------------------------------------------------------------------------
template <>
inline Cases VSettings::ValueOrDef<Cases>(const QString &setting, const Cases &defValue) const
{
const QVariant val = value(setting, QVariant::fromValue(static_cast<int>(defValue)));
const int g = val.canConvert<int>() ? val.value<int>() : static_cast<int>(defValue);
if (g < static_cast<int>(Cases::CaseThreeGroup) || g >= static_cast<int>(Cases::UnknownCase))
{
return defValue;
}
else
{
return static_cast<Cases>(g);
}
}
#endif // VSETTINGS_H #endif // VSETTINGS_H