QString UnitSystem::displayAmount( double amount, Unit* units, int precision, Unit::unitScale scale ) { // If the precision is not specified, we take the default one if( precision < 0) { precision = this->precision; } // Special cases. Make sure the unit isn't null and that we're // dealing with volume. if( units == 0 || units->getUnitType() != _type) return QString("%L1").arg(amount, fieldWidth, format, precision); // We really shouldn't ever reference something that could be null until // after we have verified it isn't. double SIAmount = units->toSI( amount ); double absSIAmount = qAbs(SIAmount); Unit* last = 0; // Don't loop if the 'without' key is defined if ( scaleToUnit().contains(Unit::scaleWithout) ) scale = Unit::scaleWithout; // If a specific scale is provided, just use that and don't loop. if ( scaleToUnit().contains(scale) ) { Unit* bob = scaleToUnit().value(scale); return QString("%L1 %2").arg(bob->fromSI(SIAmount), fieldWidth, format, precision).arg(bob->getUnitName()); } // scaleToUnit() is a QMap which means we loop in the order in which the // items were inserted. Order counts, and this map has to be // created from smallest to largest scale (e.g., mg, g, kg). QMap<Unit::unitScale, Unit*>::const_iterator it; for( it = scaleToUnit().begin(); it != scaleToUnit().end(); ++it) { Unit* bob = it.value(); double boundary = bob->boundary(); // This is a nice bit of work, if I may say so myself. If we've been // through the loop at least once already, and the boundary condition is // met, use the Unit* from the last loop. if ( last && absSIAmount < bob->toSI(boundary) ) return QString("%L1 %2").arg(last->fromSI(SIAmount), fieldWidth, format, precision).arg(last->getUnitName()); // If we get all the way through the map, this will be the largest unit // available last = bob; } // If we get here, use the largest unit available if( last ) return QString("%L1 %2").arg(last->fromSI(SIAmount), fieldWidth, format, precision).arg(last->getUnitName()); else return QString("nounit"); // Should never happen, so be obvious if it does }
double UnitSystem::amountDisplay( double amount, Unit* units, Unit::unitScale scale ) { // Special cases. Make sure the unit isn't null and that we're // dealing with volume. if( units == 0 || units->getUnitType() != _type) return amount; double SIAmount = units->toSI( amount ); double absSIAmount = qAbs(SIAmount); Unit* last = 0; // Short circuit if the 'without' key is defined if ( scaleToUnit().contains(Unit::scaleWithout) ) scale = Unit::scaleWithout; if ( scaleToUnit().contains(scale) ) { Unit* bob = scaleToUnit().value(scale); return bob->fromSI(SIAmount); } QMap<Unit::unitScale, Unit*>::const_iterator it; for( it = scaleToUnit().begin(); it != scaleToUnit().end(); ++it) { Unit* bob = it.value(); double boundary = bob->boundary(); if ( last && absSIAmount < bob->toSI(boundary) ) return last->fromSI(SIAmount); last = bob; } // If we get here, use the largest unit available if( last ) return last->fromSI(SIAmount); else return -42.42; // Should never happen, so be obvious if it does }