]> sigrok.org Git - pulseview.git/blobdiff - pv/util.cpp
Trace: Removed coloured_bg state
[pulseview.git] / pv / util.cpp
index 78b32af7920c5de0caf354fe8a99c5066ed0ec24..8f2d7d2544cd8e30301a28c56f6537269dbe759a 100644 (file)
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
+ * along with this program; if not, see <http://www.gnu.org/licenses/>.
  */
 
 #include "util.hpp"
 
 #include <extdef.h>
 
-#include <assert.h>
-
 #include <algorithm>
+#include <cassert>
 #include <sstream>
 
-#include <QTextStream>
 #include <QDebug>
+#include <QTextStream>
+
+using std::fixed;
+using std::max;
+using std::ostringstream;
+using std::setfill;
+using std::setprecision;
+using std::showpos;
+using std::string;
 
 using namespace Qt;
 
 namespace pv {
 namespace util {
 
-static const QString SIPrefixes[17] = {
-       "y", "z",
-       "a", "f", "p",
-       "n", QChar(0x03BC), "m",
-       "",
-       "k", "M", "G",
-       "T", "P", "E",
-       "Z", "Y"};
-const int EmptySIPrefix = 8;
-const int FirstSIPrefixPower = -(EmptySIPrefix * 3);
-const double MinTimeDelta = 1e-15; // Anything below 1 fs can be considered zero
+static QTextStream& operator<<(QTextStream& stream, SIPrefix prefix)
+{
+       switch (prefix) {
+       case SIPrefix::yocto: return stream << 'y';
+       case SIPrefix::zepto: return stream << 'z';
+       case SIPrefix::atto:  return stream << 'a';
+       case SIPrefix::femto: return stream << 'f';
+       case SIPrefix::pico:  return stream << 'p';
+       case SIPrefix::nano:  return stream << 'n';
+       case SIPrefix::micro: return stream << QChar(0x03BC);
+       case SIPrefix::milli: return stream << 'm';
+       case SIPrefix::kilo:  return stream << 'k';
+       case SIPrefix::mega:  return stream << 'M';
+       case SIPrefix::giga:  return stream << 'G';
+       case SIPrefix::tera:  return stream << 'T';
+       case SIPrefix::peta:  return stream << 'P';
+       case SIPrefix::exa:   return stream << 'E';
+       case SIPrefix::zetta: return stream << 'Z';
+       case SIPrefix::yotta: return stream << 'Y';
+
+       default: return stream;
+       }
+}
+
+int exponent(SIPrefix prefix)
+{
+       return 3 * (static_cast<int>(prefix) - static_cast<int>(SIPrefix::none));
+}
+
+static SIPrefix successor(SIPrefix prefix)
+{
+       assert(prefix != SIPrefix::yotta);
+       return static_cast<SIPrefix>(static_cast<int>(prefix) + 1);
+}
 
 // Insert the timestamp value into the stream in fixed-point notation
 // (and honor the precision)
@@ -61,24 +90,18 @@ static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
 
        int precision = stream.realNumberPrecision();
 
-       std::ostringstream ss;
-       ss << std::fixed;
+       ostringstream ss;
+       ss << fixed;
 
-       if (stream.numberFlags() & QTextStream::ForceSign) {
-               ss << std::showpos;
-       }
+       if (stream.numberFlags() & QTextStream::ForceSign)
+               ss << showpos;
 
-       if (0 == precision) {
-               ss
-                       << std::setprecision(1)
-                       << round(t);
-       } else {
-               ss
-                       << std::setprecision(precision)
-                       << t;
-       }
+       if (0 == precision)
+               ss << setprecision(1) << round(t);
+       else
+               ss << setprecision(precision) << t;
 
-       std::string str(ss.str());
+       string str(ss.str());
        if (0 == precision) {
                // remove the separator and the unwanted decimal place
                str.resize(str.size() - 2);
@@ -87,160 +110,119 @@ static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
        return stream << QString::fromStdString(str);
 }
 
-QString format_si_value(const Timestamp& v, QString unit, int prefix,
-       unsigned int precision, bool sign)
+QString format_time_si(const Timestamp& v, SIPrefix prefix,
+       unsigned int precision, QString unit, bool sign)
 {
-       if (prefix < 0) {
+       if (prefix == SIPrefix::unspecified) {
                // No prefix given, calculate it
 
                if (v.is_zero()) {
-                       prefix = EmptySIPrefix;
+                       prefix = SIPrefix::none;
                } else {
-                       int exp = -FirstSIPrefixPower;
-
-                       prefix = 0;
-                       while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
-                               prefix < (int)(countof(SIPrefixes) - 1)) {
-                               prefix++;
+                       int exp = exponent(SIPrefix::yotta);
+                       prefix = SIPrefix::yocto;
+                       while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
+                                       prefix < SIPrefix::yotta) {
+                               prefix = successor(prefix);
                                exp -= 3;
                        }
                }
        }
 
-       assert(prefix >= 0);
-       assert(prefix < (int)countof(SIPrefixes));
+       assert(prefix >= SIPrefix::yocto);
+       assert(prefix <= SIPrefix::yotta);
 
-       const Timestamp multiplier = pow(Timestamp(10),
-               (int)- prefix * 3 - FirstSIPrefixPower);
+       const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix));
 
        QString s;
        QTextStream ts(&s);
        if (sign && !v.is_zero())
                ts << forcesign;
-       ts
-               << qSetRealNumberPrecision(precision)
-               << (v * multiplier)
-               << ' '
-               << SIPrefixes[prefix]
-               << unit;
+       ts << qSetRealNumberPrecision(precision) << (v * multiplier) << ' '
+               << prefix << unit;
 
        return s;
 }
 
+QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix,
+       unsigned precision, QString unit, bool sign)
+{
+       // 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 = -exponent(prefix);
+
+       const unsigned int relative_prec =
+               (prefix >= SIPrefix::none) ? precision :
+               max((int)(precision - prefix_order), 0);
+
+       return format_time_si(t, prefix, relative_prec, unit, sign);
+}
+
+// Helper for 'format_time_minutes()'.
 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)
+QString format_time_minutes(const Timestamp& t, signed precision, bool sign)
 {
-       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;
+       const Timestamp whole_seconds = floor(abs(t));
+       const Timestamp days = floor(whole_seconds / (60 * 60 * 24));
+       const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>();
+       const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
+       const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
 
        QString s;
        QTextStream ts(&s);
 
-       if (t >= 0)
-               ts << "+";
-       else
+       if (t < 0)
                ts << "-";
+       else if (sign)
+               ts << "+";
 
        bool use_padding = false;
 
+       // DD
        if (days) {
-               ts << days << ":";
+               ts << days.str().c_str() << ":";
                use_padding = true;
        }
 
+       // HH
        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;
+       // MM
+       ts << pad_number(minutes, use_padding ? 2 : 0);
 
-               // We're not showing any seconds with a negative precision
-               if (precision >= 0)
-                        ts << ":";
-       }
+       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);
+       // SS
+       ts << pad_number(seconds, 2);
 
-               const double fraction = fabs(t) - whole_seconds;
+       if (precision) {
+               ts << ".";
 
-               if (precision > 0 && precision < 1000) {
-                       QString fs = QString("%1").arg(fraction, -(2 + precision), 'f',
-                               precision, QChar('0'));
+               const Timestamp fraction = fabs(t) - whole_seconds;
 
-                       ts << ".";
+               ostringstream ss;
+               ss << fixed << setprecision(precision) << setfill('0') << fraction;
+               string fs = ss.str();
 
-                       // 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];
+               // 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.at(1 + i);
 
-                               if ((i > 0) && (i % 3 == 0))
-                                       ts << " ";
-                       }
+                       if ((i > 0) && (i % 3 == 0) && (i != precision))
+                               ts << " ";
                }
        }
 
        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 >= EmptySIPrefix) ? 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<double>(), prefix, unit, precision);
-}
-
-QString format_second(const Timestamp& second)
-{
-       return format_si_value(second, "s", -1, 0, false);
-}
-
 } // namespace util
 } // namespace pv