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

Method angleToDisplayFormat

src/Mod/Sketcher/Gui/Utils.cpp:915–965  ·  view source on GitHub ↗

convert value to display format %0.[digits]f. Units are always displayed for angles - 123.456° or 12°34'56". Digits parameter is ignored for multi-unit schemata. Note small differences between this method and lengthToDisplyFormat TODO:: if the user string is delivered in 1.23e45 format, this might not work correctly.

Source from the content-addressed store, hash-verified

913// TODO:: if the user string is delivered in 1.23e45 format, this might not work
914// correctly.
915std::string SketcherGui::angleToDisplayFormat(double value, int digits)
916{
917 Base::Quantity asQuantity;
918 asQuantity.setValue(value);
919 asQuantity.setUnit(Base::Unit::Angle);
920 QString qUserString = QString::fromStdString(asQuantity.getUserString());
921 if (Base::UnitsApi::isMultiUnitAngle()) {
922 // just return the user string
923 // Coin SbString doesn't handle utf8 well, so we convert to ascii
924 QString schemeMinute = QStringLiteral("\xE2\x80\xB2"); // prime symbol
925 QString schemeSecond = QStringLiteral("\xE2\x80\xB3"); // double prime symbol
926 QString escapeMinute = QStringLiteral("\'"); // substitute ascii single quote
927 QString escapeSecond = QStringLiteral("\""); // substitute ascii double quote
928 QString displayString = qUserString.replace(schemeMinute, escapeMinute);
929 displayString = displayString.replace(schemeSecond, escapeSecond);
930 return displayString.toStdString();
931 }
932
933 // we always use use U+00B0 (°) as the unit of measure for angles in
934 // single unit schema. Will need a change to support rads or grads.
935 std::string unitString = "°";
936 auto decimalSep = QLocale().decimalPoint();
937
938 // get the numeric part of the user string
939 QRegularExpression rxNoUnits(
940 QStringLiteral("(\\d*\\%1?\\d*)(\\D*)$").arg(decimalSep)
941 ); // number + non digits at end of string
942 QRegularExpressionMatch match = rxNoUnits.match(qUserString);
943 if (!match.hasMatch()) {
944 // no units in userString?
945 return qUserString.toStdString();
946 }
947 QString matched = match.captured(1); // matched is the numeric part of user string
948 int dpPos = matched.indexOf(decimalSep);
949 if (dpPos < 0 || useSystemDecimals()) {
950 // just the numeric part of the user string + degree symbol
951 auto angle = matched.toStdString();
952 angle.append(unitString);
953 return angle;
954 }
955
956 // real number and not using system decimals
957 int requiredLength = dpPos + digits + 1;
958 if (requiredLength > matched.size()) {
959 // just take the whole thing
960 requiredLength = matched.size();
961 }
962 auto numericPart = matched.left(requiredLength).toStdString();
963 numericPart.append(unitString);
964 return numericPart;
965}
966
967
968bool SketcherGui::areCollinear(

Callers

nothing calls this directly

Calls 10

toStdStringMethod · 0.80
leftMethod · 0.80
setValueMethod · 0.45
setUnitMethod · 0.45
getUserStringMethod · 0.45
replaceMethod · 0.45
matchMethod · 0.45
indexOfMethod · 0.45
appendMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected