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

Method lengthToDisplayFormat

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

convert value to display format %0.[digits]f. Units are displayed if preference "ShowUnits" is true, or if the unit schema in effect uses multiple units (ex. Ft/In). Digits parameter is ignored for multi-unit schemata TODO:: if the user string is delivered in 1.23e45 format, this might not work correctly.

Source from the content-addressed store, hash-verified

853// TODO:: if the user string is delivered in 1.23e45 format, this might not work
854// correctly.
855std::string SketcherGui::lengthToDisplayFormat(double value, int digits)
856{
857 Base::Quantity asQuantity;
858 asQuantity.setValue(value);
859 asQuantity.setUnit(Base::Unit::Length);
860 std::string userString = asQuantity.getUserString();
861 if (Base::UnitsApi::isMultiUnitLength() || (!hideUnits() && useSystemDecimals())) {
862 // just return the user string
863 return userString;
864 }
865
866 // find the unit of measure
867 double factor = 1.0;
868 std::string unitString;
869 std::string translate = Base::UnitsApi::schemaTranslate(asQuantity, factor, unitString);
870 std::string unitPart = " " + unitString;
871
872 // get the numeric part of the user string
873 QRegularExpression rxNoUnits(QStringLiteral("(.*) \\D*$")); // text before space + any non
874 // digits at end of string
875 QRegularExpressionMatch match = rxNoUnits.match(QString::fromStdString(userString));
876 if (!match.hasMatch()) {
877 // no units in userString?
878 return userString;
879 }
880 QString matched = match.captured(1); // matched is the numeric part of user string
881 auto smatched = matched.toStdString();
882 int dpPos = matched.indexOf(QLocale().decimalPoint());
883 if (dpPos < 0) {
884 // no decimal separator (ie an integer), return all the digits
885 if (!hideUnits()) {
886 smatched.append(unitPart);
887 }
888 return smatched;
889 }
890
891 // real number
892 if (useSystemDecimals() && hideUnits()) {
893 // return just the numeric part of the user string
894 return smatched;
895 }
896
897 // real number and not using system decimals
898 int requiredLength = dpPos + digits + 1;
899 if (requiredLength > matched.size()) {
900 // just take the whole thing
901 requiredLength = matched.size();
902 }
903 auto numericPart = matched.left(requiredLength).toStdString();
904 if (!hideUnits()) {
905 numericPart.append(unitPart);
906 }
907 return numericPart;
908}
909
910// convert value to display format %0.[digits]f. Units are always displayed for
911// angles - 123.456° or 12°34'56". Digits parameter is ignored for multi-unit

Callers

nothing calls this directly

Calls 9

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

Tested by

no test coverage detected