Skip to content

Commit

Permalink
[TD] Add new non-standard format specifier '%w' that truncates traili…
Browse files Browse the repository at this point in the history
…ng zeros.
  • Loading branch information
aapo-aapo authored and WandererFan committed Jan 21, 2022
1 parent 83839b6 commit 253817e
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/Mod/TechDraw/App/DrawViewDimension.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 253817e

Please sign in to comment.