X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=blobdiff_plain;f=pv%2Futil.cpp;h=192a2e06ba1571766bbab637e1135d1980b5904a;hp=a54bdcaf8f957e8df5b462b5594ef2842afae3e5;hb=60d9b99a32e551cffd2b537d3e157d578a761c9b;hpb=62974f456595f3f6b9804a8f0fb993b4766d61e7 diff --git a/pv/util.cpp b/pv/util.cpp index a54bdcaf..192a2e06 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -18,12 +18,14 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include "util.h" +#include "util.hpp" #include #include +#include + #include #include @@ -32,40 +34,162 @@ using namespace Qt; namespace pv { namespace util { -static const QString SIPrefixes[9] = - {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"}; -const int FirstSIPrefixPower = -15; +static const QString SIPrefixes[17] = + {"y", "z", "a", "f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G", + "T", "P", "E", "Z", "Y"}; +const int FirstSIPrefix = 8; +const int FirstSIPrefixPower = -(FirstSIPrefix * 3); +const double MinTimeDelta = 1e-15; // Anything below 1 fs can be considered zero -QString format_time(double t, unsigned int prefix, +static QString format_si_value(double v, QString unit, int prefix, unsigned int precision, bool sign) { - assert(prefix < countof(SIPrefixes)); + if (prefix < 0) { + int exp = -FirstSIPrefixPower; + + prefix = 0; + while ((fabs(v) * pow(10.0, exp)) > 999.0 && + prefix < (int)(countof(SIPrefixes) - 1)) { + prefix++; + exp -= 3; + } + } + + assert(prefix >= 0); + assert(prefix < (int)countof(SIPrefixes)); const double multiplier = pow(10.0, (int)- prefix * 3 - FirstSIPrefixPower); QString s; QTextStream ts(&s); - if (sign) { + if (sign) ts << forcesign; - } - ts << fixed << qSetRealNumberPrecision(precision) - << (t * multiplier) << SIPrefixes[prefix] << "s"; + ts << fixed << qSetRealNumberPrecision(precision) << + (v * multiplier) << " " << SIPrefixes[prefix] << unit; return s; } -QString format_second(double second) +QString format_si_value(const Timestamp& v, QString unit, int prefix, + unsigned int precision, bool sign) +{ + return format_si_value(v.convert_to(), unit, prefix, precision, sign); +} + +static QString pad_number(unsigned int number, int length) +{ + return QString("%1").arg(number, length, 10, QChar('0')); +} + +static QString format_time_in_full(double t, signed precision) { - unsigned int i = 0; - int exp = - FirstSIPrefixPower; + const unsigned int whole_seconds = abs((int) t); + const unsigned int days = whole_seconds / (60 * 60 * 24); + const unsigned int hours = (whole_seconds / (60 * 60)) % 24; + const unsigned int minutes = (whole_seconds / 60) % 60; + const unsigned int seconds = whole_seconds % 60; + + QString s; + QTextStream ts(&s); + + if (t >= 0) + ts << "+"; + else + ts << "-"; + + bool use_padding = false; + + if (days) { + ts << days << ":"; + use_padding = true; + } + + if (hours || days) { + ts << pad_number(hours, use_padding ? 2 : 0) << ":"; + use_padding = true; + } + + if (minutes || hours || days) { + ts << pad_number(minutes, use_padding ? 2 : 0); + use_padding = true; + + // We're not showing any seconds with a negative precision + if (precision >= 0) + ts << ":"; + } + + // precision < 0: Use DD:HH:MM format + // precision = 0: Use DD:HH:MM:SS format + // precision > 0: Use DD:HH:MM:SS.mmm nnn ppp fff format + if (precision >= 0) { + ts << pad_number(seconds, use_padding ? 2 : 0); + + const double fraction = fabs(t) - whole_seconds; + + if (precision > 0 && precision < 1000) { + QString fs = QString("%1").arg(fraction, -(2 + precision), 'f', + precision, QChar('0')); + + ts << "."; + + // Copy all digits, inserting spaces as unit separators + for (int i = 1; i <= precision; i++) { + // Start at index 2 to skip the "0." at the beginning + ts << fs[1 + i]; - while ((second * pow(10.0, exp)) > 999.0 && i < countof(SIPrefixes) - 1) { - i++; - exp -= 3; + if ((i > 0) && (i % 3 == 0)) + ts << " "; + } + } } - return format_time(second, i, 0, false); + return s; +} + +static QString format_time_with_si(double t, QString unit, int prefix, + unsigned int precision) +{ + // The precision is always given without taking the prefix into account + // so we need to deduct the number of decimals the prefix might imply + const int prefix_order = + -(prefix * 3 + pv::util::FirstSIPrefixPower); + + const unsigned int relative_prec = + (prefix >= pv::util::FirstSIPrefix) ? precision : + std::max((int)(precision - prefix_order), 0); + + return format_si_value(t, unit, prefix, relative_prec); +} + +static QString format_time(double t, int prefix, TimeUnit unit, unsigned int precision) +{ + // Make 0 appear as 0, not random +0 or -0 + if (fabs(t) < MinTimeDelta) + return "0"; + + // If we have to use samples then we have no alternative formats + if (unit == Samples) + return format_time_with_si(t, "sa", prefix, precision); + + // View in "normal" range -> medium precision, medium step size + // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds + // View zoomed way in -> high precision (>3), low step size (<1s) + // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds + if (abs(t) < 60) + return format_time_with_si(t, "s", prefix, precision); + else + return format_time_in_full(t, precision); +} + +QString format_time(const Timestamp& t, int prefix, TimeUnit unit, unsigned int precision) +{ + return format_time(t.convert_to(), prefix, unit, precision); +} + +QString format_second(const Timestamp& second) +{ + return format_si_value(second.convert_to(), "s", -1, 0, false); } } // namespace util