From: Jens Steinhauser Date: Thu, 22 May 2014 20:03:23 +0000 (+0200) Subject: SamplingBar: Use nicer time format in the tooltip. X-Git-Tag: pulseview-0.3.0~617 X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=62974f456595f3f6b9804a8f0fb993b4766d61e7;p=pulseview.git SamplingBar: Use nicer time format in the tooltip. --- diff --git a/pv/toolbars/samplingbar.cpp b/pv/toolbars/samplingbar.cpp index 05c730bf..085e0430 100644 --- a/pv/toolbars/samplingbar.cpp +++ b/pv/toolbars/samplingbar.cpp @@ -35,6 +35,7 @@ #include #include #include +#include using boost::shared_ptr; using std::map; @@ -456,16 +457,9 @@ bool SamplingBar::eventFilter(QObject *watched, QEvent *event) if ((watched == &_sample_count || watched == &_sample_rate) && (event->type() == QEvent::ToolTip)) { double sec = (double)_sample_count.value() / _sample_rate.value(); - - QString str; - QTextStream(&str) - << tr("Total sampling time: ") - << fixed - << qSetRealNumberPrecision(1) - << sec - << "s"; - QHelpEvent *help_event = static_cast(event); + + QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec)); QToolTip::showText(help_event->globalPos(), str); return true; diff --git a/pv/util.cpp b/pv/util.cpp index 28e0dfde..a54bdcaf 100644 --- a/pv/util.cpp +++ b/pv/util.cpp @@ -55,5 +55,18 @@ QString format_time(double t, unsigned int prefix, return s; } +QString format_second(double second) +{ + unsigned int i = 0; + int exp = - FirstSIPrefixPower; + + while ((second * pow(10.0, exp)) > 999.0 && i < countof(SIPrefixes) - 1) { + i++; + exp -= 3; + } + + return format_time(second, i, 0, false); +} + } // namespace util } // namespace pv diff --git a/pv/util.h b/pv/util.h index 1af3ce93..572bed31 100644 --- a/pv/util.h +++ b/pv/util.h @@ -44,6 +44,15 @@ QString format_time( double t, unsigned int prefix, unsigned precision = 0, bool sign = true); +/** + * Formats a given time value with a SI prefix so that the + * value is between 1 and 999. + * @param second The time value in seconds to format. + * + * @return The formated value. + */ +QString format_second(double second); + } // namespace util } // namespace pv