[TD]fix unit display to standards

This commit is contained in:
Wanderer Fan
2022-03-12 13:01:08 -05:00
committed by WandererFan
parent a22608b7b5
commit 244caf39cd
3 changed files with 120 additions and 68 deletions

View File

@@ -772,7 +772,10 @@ std::string DrawViewDimension::getBaseLengthUnit(Base::UnitSystem system)
}
}
std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int partial)
std::string DrawViewDimension::formatValue(qreal value,
QString qFormatSpec,
int partial,
bool isDim)
{
std::string result;
@@ -853,8 +856,8 @@ std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int
// qUserString is the value + unit with default decimals, so extract the unit
// we cannot just use unit.getString() because this would convert '°' to 'deg'
QRegExp rxUnits(QString::fromUtf8(" \\D*$")); // space + any non digits at end of string
int pos = 0;
if ((pos = rxUnits.indexIn(qUserString, 0)) != -1) {
int pos = rxUnits.indexIn(qUserString, 0);
if (pos != -1) {
qUserStringUnits = rxUnits.cap(0); // entire capture - non numerics at end of qUserString
}
@@ -916,20 +919,48 @@ std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int
result = formattedValue.toStdString();
if (partial == 0) {
if (partial == 0) { //full text for multi-value schemas
result = Base::Tools::toStdString(formatPrefix) +
Base::Tools::toStdString(qMultiValueStr) +
Base::Tools::toStdString(formatSuffix) +
Base::Tools::toStdString(qUserStringUnits);
}
else if (partial == 1) { // prefix number suffix
result = Base::Tools::toStdString(formatPrefix) +
result +
Base::Tools::toStdString(formatSuffix);
else if (partial == 1) { // prefix number[unit] suffix
// remove space between dimension and ° (U+00B0)
// other units need 1 space for readability
if ( angularMeasure &&
!qUserStringUnits.contains(QString::fromLatin1("deg")) ) {
QRegExp space(QString::fromUtf8("\\s"));
qUserStringUnits.remove(space);
}
if (angularMeasure) {
//always insert unit after value
result = Base::Tools::toStdString(formatPrefix) +
result +
Base::Tools::toStdString(qUserStringUnits) +
Base::Tools::toStdString(formatSuffix);
} else if (showUnits()){
if (isDim and haveTolerance()) {
//unit will be included in tolerance so don't repeat it here
result = Base::Tools::toStdString(formatPrefix) +
result +
Base::Tools::toStdString(formatSuffix);
} else {
//no tolerance, so we need to include unit
result = Base::Tools::toStdString(formatPrefix) +
result +
Base::Tools::toStdString(qUserStringUnits) +
Base::Tools::toStdString(formatSuffix);
}
} else {
result = Base::Tools::toStdString(formatPrefix) +
result +
Base::Tools::toStdString(formatSuffix);
}
}
else if (partial == 2) { // just the unit
if (angularMeasure) {
// remove space between dimension and unit if unit is not "deg"
// remove leading space from unit if unit is not "deg"
if ( !qUserStringUnits.contains(QString::fromLatin1("deg")) ) {
QRegExp space(QString::fromUtf8("\\s"));
qUserStringUnits.remove(space);
@@ -945,6 +976,19 @@ std::string DrawViewDimension::formatValue(qreal value, QString qFormatSpec, int
return result;
}
bool DrawViewDimension::haveTolerance(void)
{
bool result = false;
//if a numeric tolerance is specified AND
//tolerances are NOT arbitrary
if ((!DrawUtil::fpCompare(OverTolerance.getValue(), 0.0) ||
!DrawUtil::fpCompare(UnderTolerance.getValue(), 0.0)) &&
!ArbitraryTolerances.getValue()){
result = true;
}
return result;
}
std::string DrawViewDimension::getFormattedToleranceValue(int partial)
{
QString FormatSpec = QString::fromUtf8(FormatSpecOverTolerance.getStrValue().data());
@@ -953,11 +997,15 @@ std::string DrawViewDimension::getFormattedToleranceValue(int partial)
if (ArbitraryTolerances.getValue())
ToleranceString = FormatSpec;
else
ToleranceString = QString::fromUtf8(formatValue(OverTolerance.getValue(), FormatSpec, partial).c_str());
ToleranceString = QString::fromUtf8(formatValue(OverTolerance.getValue(),
FormatSpec,
partial,
false).c_str());
return ToleranceString.toStdString();
}
//get over and under tolerances
std::pair<std::string, std::string> DrawViewDimension::getFormattedToleranceValues(int partial)
{
QString underFormatSpec = QString::fromUtf8(FormatSpecUnderTolerance.getStrValue().data());
@@ -970,16 +1018,28 @@ std::pair<std::string, std::string> DrawViewDimension::getFormattedToleranceValu
overTolerance = overFormatSpec;
} else {
if (DrawUtil::fpCompare(UnderTolerance.getValue(), 0.0)) {
underTolerance = QString::fromUtf8(formatValue(UnderTolerance.getValue(), QString::fromUtf8("%.0f"), partial).c_str());
underTolerance = QString::fromUtf8(formatValue(UnderTolerance.getValue(),
QString::fromUtf8("%.0f"),
partial,
false).c_str());
}
else {
underTolerance = QString::fromUtf8(formatValue(UnderTolerance.getValue(), underFormatSpec, partial).c_str());
underTolerance = QString::fromUtf8(formatValue(UnderTolerance.getValue(),
underFormatSpec,
partial,
false).c_str());
}
if (DrawUtil::fpCompare(OverTolerance.getValue(), 0.0)) {
overTolerance = QString::fromUtf8(formatValue(OverTolerance.getValue(), QString::fromUtf8("%.0f"), partial).c_str());
overTolerance = QString::fromUtf8(formatValue(OverTolerance.getValue(),
QString::fromUtf8("%.0f"),
partial,
false).c_str());
}
else {
overTolerance = QString::fromUtf8(formatValue(OverTolerance.getValue(), overFormatSpec, partial).c_str());
overTolerance = QString::fromUtf8(formatValue(OverTolerance.getValue(),
overFormatSpec,
partial,
false).c_str());
}
}
@@ -989,8 +1049,6 @@ std::pair<std::string, std::string> DrawViewDimension::getFormattedToleranceValu
return tolerances;
}
//partial = 0 full text for multi-value schemas
//partial = 1 value only
//partial = 2 unit only
std::string DrawViewDimension::getFormattedDimensionValue(int partial)
{
@@ -1001,37 +1059,50 @@ std::string DrawViewDimension::getFormattedDimensionValue(int partial)
return FormatSpec.getStrValue();
}
// if there is an equal over-/undertolerance and not theoretically exact, add the tolerance to dimension
if (EqualTolerance.getValue() && !TheoreticalExact.getValue() &&
(!DrawUtil::fpCompare(OverTolerance.getValue(), 0.0) || ArbitraryTolerances.getValue())) {
QString labelText = QString::fromUtf8(formatValue(getDimValue(), qFormatSpec, 1).c_str()); //just the number pref/spec/suf
QString unitText = QString::fromUtf8(formatValue(getDimValue(), qFormatSpec, 2).c_str()); //just the unit
if (Arbitrary.getValue()) {
return FormatSpec.getStrValue();
}
// if there is an equal over-/undertolerance (so only 1 tolerance to show with +/-) and
// not theoretically exact (which has no tolerance), and
// tolerance has been specified, ie
// (OverTolerance != 0.0 (so a tolerance has been specified) or
// ArbitraryTolerances are specified)
// concatenate the tolerance to dimension
if (EqualTolerance.getValue() &&
!TheoreticalExact.getValue() &&
(!DrawUtil::fpCompare(OverTolerance.getValue(), 0.0) || ArbitraryTolerances.getValue())) {
QString labelText = QString::fromUtf8(formatValue(getDimValue(),
qFormatSpec,
1,
true).c_str()); //just the number pref/spec[unit]/suf
QString unitText = QString::fromUtf8(formatValue(getDimValue(),
qFormatSpec,
2,
false).c_str()); //just the unit
QString tolerance = QString::fromStdString(getFormattedToleranceValue(1).c_str());
QString result;
if (Arbitrary.getValue()) {
labelText = QString::fromStdString(FormatSpec.getStrValue());
unitText = QString();
}
// tolerance might start with a plus sign that we don't want, so cut it off
if (tolerance.at(0) == QChar::fromLatin1('+'))
tolerance.remove(0, 1);
if ((Type.isValue("Angle")) || (Type.isValue("Angle3Pt"))) {
result = labelText + unitText + QString::fromUtf8(" \xC2\xB1 ") + tolerance;
} else {
// add the tolerance to the dimension using the ± sign
result = labelText + QString::fromUtf8(" \xC2\xB1 ") + tolerance;
}
// note plus sign is not at pos = 0!
QRegExp plus(QString::fromUtf8("^\\s*\\+"));
tolerance.remove(plus);
result = labelText +
QString::fromUtf8(" \xC2\xB1 ") + // +/- symbol
tolerance;
if (partial == 2) {
result = unitText;
}
return result.toStdString();
}
if (Arbitrary.getValue()) {
return FormatSpec.getStrValue();
}
return formatValue(getDimValue(), qFormatSpec, partial);
//tolerance not specified, so just format dimension value?
std::string formattedValue = formatValue(getDimValue(), qFormatSpec, partial, true);
return formattedValue;
}
QStringList DrawViewDimension::getPrefixSuffixSpec(QString fSpec)
@@ -1045,7 +1116,6 @@ QStringList DrawViewDimension::getPrefixSuffixSpec(QString fSpec)
int pos = 0;
if ((pos = rxFormat.indexIn(fSpec, 0)) != -1) {
match = rxFormat.cap(0); //entire capture of rx
// formatted = QString::asprintf(Base::Tools::toStdString(match).c_str(),value);
formatPrefix = fSpec.left(pos);
result.append(formatPrefix);
formatSuffix = fSpec.right(fSpec.size() - pos - match.size());
@@ -1471,6 +1541,7 @@ bool DrawViewDimension::has3DReferences(void) const
return (References3D.getSize() > 0);
}
//has arbitrary or nonzero tolerance
bool DrawViewDimension::hasOverUnderTolerance(void) const
{
bool result = false;