MCPcopy Create free account
hub / github.com/FreeCAD/FreeCAD / formatValue

Method formatValue

src/Mod/TechDraw/App/DimensionFormatter.cpp:49–164  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

47}
48
49std::string DimensionFormatter::formatValue(const qreal value,
50 const QString& qFormatSpec,
51 const Format partial,
52 const bool isDim) const
53{
54 bool distanceMeasure{true};
55 const bool angularMeasure =
56 m_dimension->Type.isValue("Angle") || m_dimension->Type.isValue("Angle3Pt");
57 const bool areaMeasure = m_dimension->Type.isValue("Area");
58
59 Base::Unit unit{Base::Unit::Length};
60 if (angularMeasure) {
61 unit = Base::Unit::Angle;
62 distanceMeasure = false;
63 }
64 else if (areaMeasure) {
65 unit = Base::Unit::Area;
66 distanceMeasure = false;
67 }
68
69 Base::Quantity asQuantity {value, unit};
70
71 QStringList qsl = getPrefixSuffixSpec(qFormatSpec);
72 const std::string formatPrefix = qsl[0].toStdString();
73 const std::string formatSuffix = qsl[1].toStdString();
74 QString formatSpecifier = qsl[2];
75
76 // this handles mm to inch/km/parsec etc and decimal positions but
77 // won't give more than Global_Decimals precision
78 std::string basicString = formatPrefix + asQuantity.getUserString() + formatSuffix;
79
80 if (isMultiValueSchema() || partial == Format::UNALTERED) {
81 return basicString; // Don't even try to use Alt Decimals or hide units
82 }
83
84 if (formatSpecifier.isEmpty()) {
85 Base::Console().warning("Warning - no numeric format in Format Spec %s - %s\n",
86 qPrintable(qFormatSpec),
87 m_dimension->getNameInDocument());
88 return qFormatSpec.toStdString();
89 }
90
91 // for older TD drawings the formatSpecifier "%g" was used, but the number of decimals was
92 // nevertheless limited. To keep old drawings, we limit the number of decimals too
93 // if the TD preferences option to use the global decimal number is set
94 // the formatSpecifier can have a prefix and/or suffix
95 if (m_dimension->useDecimals()
96 && formatSpecifier.contains(QStringLiteral("%g"), Qt::CaseInsensitive)) {
97 const int globalPrecision = Base::UnitsApi::getDecimals();
98 // change formatSpecifier to e.g. "%.2f"
99 const QString newSpecifier =
100 QString::fromStdString("%." + std::to_string(globalPrecision) + "f");
101 formatSpecifier.replace(QStringLiteral("%g"), newSpecifier, Qt::CaseInsensitive);
102 }
103
104 double factor{1.0};
105 std::string unitText{""};
106 asQuantity.getUserString(factor, unitText);

Callers

nothing calls this directly

Calls 14

toStdStringMethod · 0.80
useDecimalsMethod · 0.80
haveToleranceMethod · 0.80
to_stringFunction · 0.50
isValueMethod · 0.45
getUserStringMethod · 0.45
isEmptyMethod · 0.45
warningMethod · 0.45
getNameInDocumentMethod · 0.45
containsMethod · 0.45
replaceMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected