From 9213dace6b4e26b9fa31a467f2e5d8c63537d876 Mon Sep 17 00:00:00 2001 From: donovaly Date: Mon, 21 Dec 2020 01:27:41 +0100 Subject: [PATCH] [TD] fix unit conversion for dimensions as reported here: https://forum.freecadweb.org/viewtopic.php?f=35&t=53473 my commit 957ce2f38787 introduced a regression because now units are no longer converted when the unit is displayed. This PR fixes this. --- src/Mod/TechDraw/App/DrawViewDimension.cpp | 25 ++++++++-------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawViewDimension.cpp b/src/Mod/TechDraw/App/DrawViewDimension.cpp index cea5225be9..ac0e18714c 100644 --- a/src/Mod/TechDraw/App/DrawViewDimension.cpp +++ b/src/Mod/TechDraw/App/DrawViewDimension.cpp @@ -614,6 +614,7 @@ std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int QString qUserStringUnits; QString formattedValue; bool angularMeasure = false; + QLocale loc; Base::Quantity asQuantity; asQuantity.setValue(value); @@ -686,25 +687,17 @@ std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int if ((pos = rxUnits.indexIn(qUserString, 0)) != -1) { qUserStringUnits = rxUnits.cap(0); // entire capture - non numerics at end of qUserString } + + // get value in the base unit with default decimals + // for the conversion we use the same method as in DlgUnitsCalculator::valueChanged + // get the conversion factor for the unit + double convertValue = Base::Quantity::parse(QString::fromLatin1("1") + QString::fromStdString(BaseLengthUnit)).getValue(); + // the result is now just val / convertValue because val is always in the base unit + double userVal = asQuantity.getValue() / convertValue; - // we can have 2 possible results: - // - the value in the base unit but without displayed unit - // - the value + unit (not necessarily the base unit!) + // we reformat the value // the user can overwrite the decimal settings, so we must in every case use the formatSpecifier // the default is: if useDecimals(), then formatSpecifier = global decimals, otherwise it is %.2f - QLocale loc; - double userVal; - if (showUnits() || (Type.isValue("Angle")) || (Type.isValue("Angle3Pt"))) { - userVal = asQuantity.getValue(); - } else { - // get value in the base unit with default decimals - // for the conversion we use the same method as in DlgUnitsCalculator::valueChanged - // get the conversion factor for the unit - double convertValue = Base::Quantity::parse(QString::fromLatin1("1") + QString::fromStdString(BaseLengthUnit)).getValue(); - // the result is now just val / convertValue because val is always in the base unit - userVal = asQuantity.getValue() / convertValue; - } - // we reformat the value #if QT_VERSION >= 0x050000 formattedValue = QString::asprintf(Base::Tools::toStdString(formatSpecifier).c_str(), userVal); #else