| 47 | } |
| 48 | |
| 49 | std::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); |
nothing calls this directly
no test coverage detected