Quantity Constraint::getPresentationValue() const { Quantity quantity; switch (Type) { case Distance: case Radius: case DistanceX: case DistanceY: quantity.setValue(Value); quantity.setUnit(Unit::Length); break; case Angle: quantity.setValue(toDegrees<double>(Value)); quantity.setUnit(Unit::Angle); break; case SnellsLaw: quantity.setValue(Value); break; default: quantity.setValue(Value); break; } QuantityFormat format = quantity.getFormat(); format.option = QuantityFormat::None; format.format = QuantityFormat::Default; format.precision = 6; // QString's default quantity.setFormat(format); return quantity; }
QString UnitsSchemaImperialBuilding::schemaTranslate(const Quantity &quant, double &factor, QString &unitString) { // this schema expresses distances in feet + inches + fractions // ex: 3'- 4 1/4" with proper rounding Unit unit = quant.getUnit(); if (unit == Unit::Length) { unitString = QString::fromLatin1("in"); factor = 25.4; // Total number of inches to format double totalInches = std::abs(quant.getValue())/factor; // minimum denominator (8 for 1/8, 16 for 1/16, etc) int minden; // Outputs int feet; // whole feet int inches; // whole inches int num,den; // numerator and denominator of fractional val std::stringstream output; // output stream // Intermediate values int ntot; // total fractional units int a,b,d; // used to compute greatest common denominator int tmp; // temporary variable for GCD // Get the current user specified minimum denominator minden = quant.getFormat().getDenominator(); // Compute and round the total number of fractional units ntot = (int)std::round(totalInches * (double)minden); // If this is zero, nothing to do but return if( ntot==0 ) return QString::fromLatin1("0"); // Compute the whole number of feet and remaining units feet = (int)std::floor(ntot / (12*minden)); ntot = ntot - 12*minden*feet; // Compute the remaining number of whole inches inches = (int)std::floor(ntot/minden); // Lastly the fractional quantities num = ntot - inches*minden; den = minden; // If numerator is not zero, compute greatest common divisor and reduce // fraction if( num!=0 ) { // initialize a = num; b = den; while (b != 0) { tmp = a % b; a = b; b = tmp; } d = a; num /= d; den /= d; } // Process into string. Start with negative sign if quantity is less // than zero if( quant.getValue() < 0 ) output << "-"; // Print feet if we have any if( feet!=0 ) { output << feet << "'"; // if there is to be trailing numbers, add space if( inches!=0 || num!=0 ) { output << " "; } } // Three cases: // 1. Whole inches, no fraction // 2. Whole inches, fraction // 3. Fraction only if( inches>0 && num==0 ) // case 1. { output << inches << "\""; } else if( inches>0 && num!=0 ) // case 2 { output << inches << "+" << num << "/" << den << "\""; } else if( inches==0 && num!=0 ) // case 3 { output << num << "/" << den << "\""; } // Done! return QString::fromLatin1(output.str().c_str()); } else if (unit == Unit::Area) { unitString = QString::fromLatin1("sqft"); factor = 92903.04; } else if (unit == Unit::Volume) { unitString = QString::fromLatin1("cuft"); factor = 28316846.592; } else if (unit == Unit::Velocity) { unitString = QString::fromLatin1("in/min"); factor = 25.4/60; } else { unitString = quant.getUnit().getString(); factor = 1.0; } return toLocale(quant, factor, unitString); }