]> sigrok.org Git - pulseview.git/blobdiff - pv/util.cpp
Build fixes for Qt5 Windows/mingw/MXE support.
[pulseview.git] / pv / util.cpp
index 62f0ee1f67002b33ef2816bef15ccd3679dc4a00..fdd954d5d90824a05fb13b0dd6ad34b6ccf96aad 100644 (file)
@@ -14,8 +14,7 @@
  * 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"
@@ -24,6 +23,9 @@
 
 #include <assert.h>
 
+#include <algorithm>
+#include <sstream>
+
 #include <QTextStream>
 #include <QDebug>
 
@@ -32,40 +34,204 @@ 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 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)
+static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
+{
+       // The multiprecision types already have a function and a stream insertion
+       // operator to convert them to a string, however these functions abuse a
+       // precision value of zero to print all available decimal places instead of
+       // none, and the boost authors refuse to fix this because they don't want
+       // to break buggy code that relies on this bug.
+       // (https://svn.boost.org/trac/boost/ticket/10103)
+       // Therefore we have to work around the case where precision is zero.
+
+       int precision = stream.realNumberPrecision();
+
+       std::ostringstream ss;
+       ss << std::fixed;
+
+       if (stream.numberFlags() & QTextStream::ForceSign)
+               ss << std::showpos;
+
+       if (0 == precision)
+               ss << std::setprecision(1) << round(t);
+       else
+               ss << std::setprecision(precision) << t;
 
-QString format_time(double t, unsigned int prefix,
-       unsigned int precision, bool sign)
+       std::string str(ss.str());
+       if (0 == precision) {
+               // remove the separator and the unwanted decimal place
+               str.resize(str.size() - 2);
+       }
+
+       return stream << QString::fromStdString(str);
+}
+
+QString format_time_si(
+       const Timestamp& v,
+       SIPrefix prefix,
+       unsigned int precision,
+       QString unit,
+       bool sign)
 {
-       assert(prefix < countof(SIPrefixes));
+       if (prefix == SIPrefix::unspecified) {
+               // No prefix given, calculate it
+
+               if (v.is_zero()) {
+                       prefix = SIPrefix::none;
+               } else {
+                       int exp = exponent(SIPrefix::yotta);
+                       prefix = SIPrefix::yocto;
+                       while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
+                                       prefix < SIPrefix::yotta) {
+                               prefix = successor(prefix);
+                               exp -= 3;
+                       }
+               }
+       }
 
-       const double multiplier = pow(10.0,
-               (int)- prefix * 3 - FirstSIPrefixPower);
+       assert(prefix >= SIPrefix::yocto);
+       assert(prefix <= SIPrefix::yotta);
+
+       const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix));
 
        QString s;
        QTextStream ts(&s);
-       if (sign) {
+       if (sign && !v.is_zero())
                ts << forcesign;
-       }
-       ts << fixed << qSetRealNumberPrecision(precision)
-               << (t  * multiplier) << SIPrefixes[prefix] << "s";
+       ts
+               << qSetRealNumberPrecision(precision)
+               << (v * multiplier)
+               << ' '
+               << prefix
+               << unit;
 
        return s;
 }
 
-QString format_second(double second)
+QString format_time_si_adjusted(
+       const Timestamp& t,
+       SIPrefix prefix,
+       unsigned precision,
+       QString unit,
+       bool sign)
 {
-       unsigned int i = 0;
-       int exp = - FirstSIPrefixPower;
+       // 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 :
+               std::max((int)(precision - prefix_order), 0);
 
-       while ((second * pow(10.0, exp)) > 999.0 && i < countof(SIPrefixes) - 1) {
-               i++;
-               exp -= 3;
+       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'));
+}
+
+QString format_time_minutes(const Timestamp& t, signed precision, bool sign)
+{
+       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 (sign)
+               ts << "+";
+
+       bool use_padding = false;
+
+       // DD
+       if (days) {
+               ts << days.str().c_str() << ":";
+               use_padding = true;
        }
 
-       return format_time(second, i, 0, false);
+       // HH
+       if (hours || days) {
+               ts << pad_number(hours, use_padding ? 2 : 0) << ":";
+               use_padding = true;
+       }
+
+       // MM
+       ts << pad_number(minutes, use_padding ? 2 : 0);
+
+       ts << ":";
+
+       // SS
+       ts << pad_number(seconds, 2);
+
+       if (precision) {
+               ts << ".";
+
+               const Timestamp fraction = fabs(t) - whole_seconds;
+
+               std::ostringstream ss;
+               ss
+                       << std::fixed
+                       << std::setprecision(precision)
+                       << std::setfill('0')
+                       << fraction;
+               std::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.at(1 + i);
+
+                       if ((i > 0) && (i % 3 == 0) && (i != precision))
+                               ts << " ";
+               }
+       }
+
+       return s;
 }
 
 } // namespace util