Sketcher: refactor EditModeConstraintCoinManager::getPresentationString

This commit is contained in:
bofdahof
2025-01-22 09:37:00 +10:00
committed by Ladislav Michl
parent aef1082983
commit 30fd8d6d60

View File

@@ -2100,100 +2100,77 @@ void EditModeConstraintCoinManager::rebuildConstraintNodes(
QString EditModeConstraintCoinManager::getPresentationString(const Constraint* constraint)
{
std::string nameStr; // name parameter string
QString valueStr; // dimensional value string
std::string unitStr; // the actual unit string
std::string baseUnitStr; // the expected base unit string
double factor; // unit scaling factor, currently not used
Base::UnitSystem unitSys; // current unit system
if (!constraint->isActive) {
return QStringLiteral(" ");
}
// Get the current name parameter string of the constraint
nameStr = constraint->Name;
/**
* Hide units if
* - user has requested it,
* - is being displayed in the base units, -and-
* - the schema being used has a clear base unit in the first place.
*
* Remove unit string if expected unit string matches actual unit string
* Example code from: Mod/TechDraw/App/DrawViewDimension.cpp:372
*
* Hide the default length unit
*/
auto fixValueStr = [&](const QString& valueStr, const auto& unitStr) -> std::optional<QString> {
if (!constraintParameters.bHideUnits || constraint->Type == Sketcher::Angle) {
return std::nullopt;
}
const auto baseUnitStr {Base::UnitsApi::getBasicLengthUnit()};
if (baseUnitStr.empty() || baseUnitStr != unitStr) {
return std::nullopt;
}
// trailing space or non-dig
const QRegularExpression rxUnits {QString::fromUtf8(" \\D*$")};
auto vStr = valueStr;
vStr.remove(rxUnits);
return {vStr};
};
// Get the current value string including units
valueStr =
QString::fromStdString(constraint->getPresentationValue().getUserString(factor, unitStr));
double factor {};
std::string unitStr; // the actual unit string
const auto constrPresValue {constraint->getPresentationValue().getUserString(factor, unitStr)};
auto valueStr = QString::fromStdString(constrPresValue);
// Hide units if user has requested it, is being displayed in the base
// units, and the schema being used has a clear base unit in the first
// place. Otherwise, display units.
if (constraintParameters.bHideUnits && constraint->Type != Sketcher::Angle) {
// Only hide the default length unit. Right now there is not an easy way
// to get that from the Unit system so we have to manually add it here.
// Hopefully this can be added in the future so this code won't have to
// be updated if a new units schema is added.
unitSys = Base::UnitsApi::getSchema();
// If this is a supported unit system then define what the base unit is.
switch (unitSys) {
case Base::UnitSystem::SI1:
case Base::UnitSystem::MmMin:
baseUnitStr = "mm";
break;
case Base::UnitSystem::SI2:
baseUnitStr = "m";
break;
case Base::UnitSystem::ImperialDecimal:
baseUnitStr = "in";
break;
case Base::UnitSystem::Centimeters:
baseUnitStr = "cm";
break;
default:
// Nothing to do
break;
}
if (!baseUnitStr.empty()) {
// expected unit string matches actual unit string. remove.
if (baseUnitStr.compare(unitStr) == 0) {
// Example code from: Mod/TechDraw/App/DrawViewDimension.cpp:372
QRegularExpression rxUnits(
QStringLiteral(" \\D*$")); // space + any non digits at end of string
valueStr.remove(rxUnits); // getUserString(defaultDecimals) without units
}
}
auto fixedValueStr = fixValueStr(valueStr, unitStr).value_or(valueStr);
switch (constraint->Type) {
case Sketcher::Diameter:
fixedValueStr.prepend(QChar(0x2300));
break;
case Sketcher::Radius:
fixedValueStr.prepend(QLatin1Char('R'));
break;
default:
break;
}
if (constraint->Type == Sketcher::Diameter) {
valueStr.prepend(QChar(216)); // Diameter sign
}
else if (constraint->Type == Sketcher::Radius) {
valueStr.prepend(QChar(82)); // Capital letter R
if (!constraintParameters.bShowDimensionalName || constraint->Name.empty()) {
return fixedValueStr;
}
/**
Create the representation string from the user defined format string
Format options are:
%N - the constraint name parameter
%V - the value of the dimensional constraint, including any unit characters
*/
if (constraintParameters.bShowDimensionalName && !nameStr.empty()) {
QString presentationStr;
if (constraintParameters.sDimensionalStringFormat.contains(QLatin1String("%V"))
|| constraintParameters.sDimensionalStringFormat.contains(QLatin1String("%N"))) {
presentationStr = constraintParameters.sDimensionalStringFormat;
presentationStr.replace(QLatin1String("%N"), QString::fromStdString(nameStr));
presentationStr.replace(QLatin1String("%V"), valueStr);
}
else {
// user defined format string does not contain any valid parameter, using default format
// "%N = %V"
presentationStr = QString::fromStdString(nameStr) + QStringLiteral(" = ") + valueStr;
}
* Create the representation string from the user defined format string
* Format options are:
* %N - the constraint name parameter
* %V - the value of the dimensional constraint, including any unit characters
*/
auto sDimFmt {constraintParameters.sDimensionalStringFormat};
if (!sDimFmt.contains(QLatin1String("%V"))
&& !sDimFmt.contains(QLatin1String("%N"))) { // using default format "%N = %V"
return presentationStr;
return QString::fromStdString(constraint->Name) + QString::fromLatin1(" = ") + valueStr;
}
return valueStr;
sDimFmt.replace(QLatin1String("%N"), QString::fromStdString(constraint->Name));
sDimFmt.replace(QLatin1String("%V"), fixedValueStr);
return sDimFmt;
}
std::set<int> EditModeConstraintCoinManager::detectPreselectionConstr(const SoPickedPoint* Point,