From 244caf39cd0dd78e88fc14d9d361dec8f8580a4b Mon Sep 17 00:00:00 2001 From: Wanderer Fan Date: Sat, 12 Mar 2022 13:01:08 -0500 Subject: [PATCH] [TD]fix unit display to standards --- src/Mod/TechDraw/App/DrawViewDimension.cpp | 147 +++++++++++++++------ src/Mod/TechDraw/App/DrawViewDimension.h | 4 +- src/Mod/TechDraw/Gui/QGIViewDimension.cpp | 37 ++---- 3 files changed, 120 insertions(+), 68 deletions(-) diff --git a/src/Mod/TechDraw/App/DrawViewDimension.cpp b/src/Mod/TechDraw/App/DrawViewDimension.cpp index 7ee2f4fa38..a6717e55c3 100644 --- a/src/Mod/TechDraw/App/DrawViewDimension.cpp +++ b/src/Mod/TechDraw/App/DrawViewDimension.cpp @@ -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 DrawViewDimension::getFormattedToleranceValues(int partial) { QString underFormatSpec = QString::fromUtf8(FormatSpecUnderTolerance.getStrValue().data()); @@ -970,16 +1018,28 @@ std::pair 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 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; diff --git a/src/Mod/TechDraw/App/DrawViewDimension.h b/src/Mod/TechDraw/App/DrawViewDimension.h index 5765e0b2b1..9ee8c04dd6 100644 --- a/src/Mod/TechDraw/App/DrawViewDimension.h +++ b/src/Mod/TechDraw/App/DrawViewDimension.h @@ -145,7 +145,9 @@ public: virtual std::string getFormattedToleranceValue(int partial); virtual std::pair getFormattedToleranceValues(int partial = 0); virtual std::string getFormattedDimensionValue(int partial = 0); - virtual std::string formatValue(qreal value, QString qFormatSpec, int partial = 0); + virtual std::string formatValue(qreal value, QString qFormatSpec, int partial = 0, bool isDim = true); + + virtual bool haveTolerance(void); virtual double getDimValue(); QStringList getPrefixSuffixSpec(QString fSpec); diff --git a/src/Mod/TechDraw/Gui/QGIViewDimension.cpp b/src/Mod/TechDraw/Gui/QGIViewDimension.cpp index a1eb2d7123..bec2fe6862 100644 --- a/src/Mod/TechDraw/Gui/QGIViewDimension.cpp +++ b/src/Mod/TechDraw/Gui/QGIViewDimension.cpp @@ -359,7 +359,7 @@ void QGIDatumLabel::setToleranceString() std::pair labelTexts, unitTexts; if (dim->ArbitraryTolerances.getValue()) { - labelTexts = dim->getFormattedToleranceValues(1); //just the number pref/spec/suf + labelTexts = dim->getFormattedToleranceValues(1); //copy tolerance spec unitTexts.first = ""; unitTexts.second = ""; } else { @@ -368,13 +368,13 @@ void QGIDatumLabel::setToleranceString() unitTexts.first = ""; unitTexts.second = ""; } else { - labelTexts = dim->getFormattedToleranceValues(1); //just the number pref/spec/suf + labelTexts = dim->getFormattedToleranceValues(1); // prefix value [unit] postfix unitTexts = dim->getFormattedToleranceValues(2); //just the unit } } - m_tolTextUnder->setPlainText(QString::fromUtf8(labelTexts.first.c_str()) + QString::fromUtf8(unitTexts.first.c_str())); - m_tolTextOver->setPlainText(QString::fromUtf8(labelTexts.second.c_str()) + QString::fromUtf8(unitTexts.second.c_str())); + m_tolTextUnder->setPlainText(QString::fromUtf8(labelTexts.first.c_str())); + m_tolTextOver->setPlainText(QString::fromUtf8(labelTexts.second.c_str())); return; } @@ -580,10 +580,6 @@ void QGIViewDimension::setNormalColorAll() aHead2->setFillColor(qc); } - -//special handling to prevent unwanted repositioning -//clicking on the dimension, but outside the label, should do nothing to position -//label will get clicks before QGIVDim void QGIViewDimension::mouseReleaseEvent(QGraphicsSceneMouseEvent * event) { // Base::Console().Message("QGIVDim::mouseReleaseEvent() - %s\n",getViewName()); @@ -633,27 +629,11 @@ void QGIViewDimension::updateDim() return; } - QString labelText; - QString unitText; - if ( (dim->Arbitrary.getValue() && !dim->EqualTolerance.getValue()) - || (dim->Arbitrary.getValue() && dim->TheoreticalExact.getValue()) ) { - labelText = QString::fromUtf8(dim->getFormattedDimensionValue(1).c_str()); //just the number pref/spec/suf - } else { - if (dim->isMultiValueSchema()) { - labelText = QString::fromUtf8(dim->getFormattedDimensionValue(0).c_str()); //don't format multis - } else { - labelText = QString::fromUtf8(dim->getFormattedDimensionValue(1).c_str()); //just the number pref/spec/suf - if (dim->EqualTolerance.getValue()) { - if (dim->ArbitraryTolerances.getValue()) { - unitText = QString(); - } else { - unitText = QString::fromUtf8(dim->getFormattedToleranceValue(2).c_str()); //just the unit - } - } else { - unitText = QString::fromUtf8(dim->getFormattedDimensionValue(2).c_str()); //just the unit - } - } + QString labelText= QString::fromUtf8(dim->getFormattedDimensionValue(1).c_str()); // pre value [unit] post + if (dim->isMultiValueSchema()) { + labelText = QString::fromUtf8(dim->getFormattedDimensionValue(0).c_str()); //don't format multis } + QFont font = datumLabel->getFont(); font.setFamily(QString::fromUtf8(vp->Font.getValue())); font.setPixelSize(calculateFontPixelSize(vp->Fontsize.getValue())); @@ -662,7 +642,6 @@ void QGIViewDimension::updateDim() prepareGeometryChange(); datumLabel->setDimString(labelText); datumLabel->setToleranceString(); - datumLabel->setUnitString(unitText); datumLabel->setPosFromCenter(datumLabel->X(),datumLabel->Y()); datumLabel->setFramed(dim->TheoreticalExact.getValue());