From 253817e3e9ce11a5e13469b72a35ef02d62af10e Mon Sep 17 00:00:00 2001 From: Aapo Date: Thu, 20 Jan 2022 17:57:57 +0200 Subject: [PATCH] [TD] Add new non-standard format specifier '%w' that truncates trailing zeros. --- src/Mod/TechDraw/App/DrawViewDimension.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawViewDimension.cpp b/src/Mod/TechDraw/App/DrawViewDimension.cpp index 12092d913b3d..6978d681c120 100644 --- a/src/Mod/TechDraw/App/DrawViewDimension.cpp +++ b/src/Mod/TechDraw/App/DrawViewDimension.cpp @@ -821,7 +821,19 @@ std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int // 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 - formattedValue = QString::asprintf(Base::Tools::toStdString(formatSpecifier).c_str(), userVal); + // Also, handle the new non-standard format-specifier '%w', which has the following rules: works as %f, but no trailing zeros + if (formatSpecifier.contains(QRegExp(QStringLiteral("%.*[wW]")))) { + QString fs = formatSpecifier; + fs.replace(QRegExp(QStringLiteral("%(.*)w")), QStringLiteral("%\\1f")); + fs.replace(QRegExp(QStringLiteral("%(.*)W")), QStringLiteral("%\\1F")); + formattedValue = QString::asprintf(Base::Tools::toStdString(fs).c_str(), userVal); + // First, try to cut trailing zeros, if AFTER decimal dot there are nonzero numbers + // Second, try to cut also decimal dot and zeros, if there are just zeros after it + formattedValue.replace(QRegExp(QStringLiteral("([0-9][0-9]*\\.[0-9]*[1-9])00*$")), QStringLiteral("\\1")); + formattedValue.replace(QRegExp(QStringLiteral("([0-9][0-9]*)\\.0*$")), QStringLiteral("\\1")); + } else { + formattedValue = QString::asprintf(Base::Tools::toStdString(formatSpecifier).c_str(), userVal); + } // if abs(1 - userVal / formattedValue) > 0.1 we know that we make an error greater than 10% // then we need more digits @@ -969,7 +981,7 @@ QStringList DrawViewDimension::getPrefixSuffixSpec(QString fSpec) QString formatPrefix; QString formatSuffix; //find the %x.y tag in FormatSpec - QRegExp rxFormat(QString::fromUtf8("%[+-]?[0-9]*\\.*[0-9]*[aefgAEFG]")); //printf double format spec + QRegExp rxFormat(QStringLiteral("%[+-]?[0-9]*\\.*[0-9]*[aefgwAEFGW]")); //printf double format spec QString match; int pos = 0; if ((pos = rxFormat.indexIn(fSpec, 0)) != -1) {