Put the time format function into a separate file.
authorJens Steinhauser <jens.steinhauser@gmail.com>
Thu, 22 May 2014 20:03:22 +0000 (22:03 +0200)
committerJoel Holdsworth <joel@airwebreathe.org.uk>
Fri, 23 May 2014 20:07:36 +0000 (21:07 +0100)
CMakeLists.txt
pv/util.cpp [new file with mode: 0644]
pv/util.h [new file with mode: 0644]
pv/view/cursor.cpp
pv/view/cursorpair.cpp
pv/view/ruler.cpp
pv/view/ruler.h

index 50dde49032ca2ff898757f847abc465f3bc575cf..a6a6c406a00b6765d56c0db992c74529f9afd1b0 100644 (file)
@@ -121,6 +121,7 @@ set(pulseview_SOURCES
        pv/mainwindow.cpp
        pv/sigsession.cpp
        pv/storesession.cpp
+       pv/util.cpp
        pv/data/analog.cpp
        pv/data/analogsnapshot.cpp
        pv/data/logic.cpp
diff --git a/pv/util.cpp b/pv/util.cpp
new file mode 100644 (file)
index 0000000..28e0dfd
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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
+ */
+
+#include "util.h"
+
+#include <extdef.h>
+
+#include <assert.h>
+
+#include <QTextStream>
+#include <QDebug>
+
+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;
+
+QString format_time(double t, unsigned int prefix,
+       unsigned int precision, bool sign)
+{
+       assert(prefix < countof(SIPrefixes));
+
+       const double multiplier = pow(10.0,
+               (int)- prefix * 3 - FirstSIPrefixPower);
+
+       QString s;
+       QTextStream ts(&s);
+       if (sign) {
+               ts << forcesign;
+       }
+       ts << fixed << qSetRealNumberPrecision(precision)
+               << (t  * multiplier) << SIPrefixes[prefix] << "s";
+
+       return s;
+}
+
+} // namespace util
+} // namespace pv
diff --git a/pv/util.h b/pv/util.h
new file mode 100644 (file)
index 0000000..1af3ce9
--- /dev/null
+++ b/pv/util.h
@@ -0,0 +1,50 @@
+/*
+ * This file is part of the PulseView project.
+ *
+ * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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
+ */
+
+#ifndef PULSEVIEW_UTIL_H
+#define PULSEVIEW_UTIL_H
+
+#include <math.h>
+
+#include <QString>
+
+namespace pv {
+namespace util {
+
+extern const int FirstSIPrefixPower;
+
+/**
+ * Formats a given time value with the specified SI prefix.
+ * @param t The time value in seconds to format.
+ * @param prefix The number of the prefix, from 0 for 'femto' up to
+ *               8 for 'giga'.
+ * @parma precision The number of digits after the decimal separator.
+ * @param sign Whether or not to add a sign also for positive numbers.
+ *
+ * @return The formated value.
+ */
+QString format_time(
+       double t, unsigned int prefix,
+       unsigned precision = 0, bool sign = true);
+
+} // namespace util
+} // namespace pv
+
+#endif // PULSEVIEW_UTIL_H
index ca22eefdf1f65d44c145f84c0649733b6b9e3e84..95134b775a1d3daaeaa13f3ff3ce0f51fe245441 100644 (file)
@@ -20,8 +20,8 @@
 
 #include "cursor.h"
 
-#include "ruler.h"
 #include "view.h"
+#include "pv/util.h"
 
 #include <QBrush>
 #include <QPainter>
@@ -139,13 +139,13 @@ void Cursor::paint_label(QPainter &p, const QRect &rect,
 
        p.setPen(TextColour);
        p.drawText(r, Qt::AlignCenter | Qt::AlignVCenter,
-               Ruler::format_time(_time, prefix, 2));
+               pv::util::format_time(_time, prefix, 2));
 }
 
 void Cursor::compute_text_size(QPainter &p, unsigned int prefix)
 {
        _text_size = p.boundingRect(QRectF(), 0,
-               Ruler::format_time(_time, prefix, 2)).size();
+               pv::util::format_time(_time, prefix, 2)).size();
 }
 
 shared_ptr<Cursor> Cursor::get_other_cursor() const
index ed8829d25befdcc4f0b6f790a2f62bc9b4922bc7..f73567e3bf750e08e384c2864ee9fadf0db2ad13 100644 (file)
@@ -20,8 +20,8 @@
 
 #include "cursorpair.h"
 
-#include "ruler.h"
 #include "view.h"
+#include "pv/util.h"
 
 #include <algorithm>
 
@@ -99,7 +99,7 @@ void CursorPair::draw_markers(QPainter &p,
 
                p.setPen(Cursor::TextColour);
                p.drawText(text_rect, Qt::AlignCenter | Qt::AlignVCenter,
-                       Ruler::format_time(_second->time() - _first->time(), prefix, 2));
+                       pv::util::format_time(_second->time() - _first->time(), prefix, 2));
        }
 
        // Paint the cursor markers
@@ -137,7 +137,7 @@ void CursorPair::compute_text_size(QPainter &p, unsigned int prefix)
        assert(_first);
        assert(_second);
 
-       _text_size = p.boundingRect(QRectF(), 0, Ruler::format_time(
+       _text_size = p.boundingRect(QRectF(), 0, pv::util::format_time(
                _second->time() - _first->time(), prefix, 2)).size();
 }
 
index aec3de979cf5171062df6777e78a42aaf4c74b03..9e29ec294b76786ed3e3e22b7a824be056c71720 100644 (file)
 #include "cursor.h"
 #include "view.h"
 #include "viewport.h"
+#include "pv/util.h"
 
 #include <extdef.h>
 
-#include <assert.h>
-#include <math.h>
-#include <limits.h>
-
 #include <QApplication>
 #include <QMouseEvent>
 #include <QPainter>
@@ -47,10 +44,6 @@ const int Ruler::RulerHeight = 30;
 const int Ruler::MinorTickSubdivision = 4;
 const int Ruler::ScaleUnits[3] = {1, 2, 5};
 
-const QString Ruler::SIPrefixes[9] =
-       {"f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G"};
-const int Ruler::FirstSIPrefixPower = -15;
-
 const int Ruler::HoverArrowSize = 5;
 
 Ruler::Ruler(View &parent) :
@@ -71,19 +64,6 @@ void Ruler::clear_selection()
        update();
 }
 
-QString Ruler::format_time(double t, unsigned int prefix,
-       unsigned int precision)
-{
-       const double multiplier = pow(10.0,
-               (int)- prefix * 3 - FirstSIPrefixPower);
-
-       QString s;
-       QTextStream ts(&s);
-       ts.setRealNumberPrecision(precision);
-       ts << fixed << forcesign << (t  * multiplier) <<
-               SIPrefixes[prefix] << "s";
-       return s;
-}
 
 QSize Ruler::sizeHint() const
 {
@@ -120,12 +100,10 @@ void Ruler::paintEvent(QPaintEvent*)
                        tick_period = order_decimal * ScaleUnits[unit++];
                } while (tick_period < min_period && unit < countof(ScaleUnits));
 
-               prefix = (order - FirstSIPrefixPower) / 3;
-               assert(prefix < countof(SIPrefixes));
-
+               prefix = (order - pv::util::FirstSIPrefixPower) / 3;
 
                typical_width = p.boundingRect(0, 0, INT_MAX, INT_MAX,
-                       AlignLeft | AlignTop, format_time(_view.offset(),
+                       AlignLeft | AlignTop, pv::util::format_time(_view.offset(),
                        prefix)).width() + MinValueSpacing;
 
                min_width += SpacingIncrement;
@@ -163,7 +141,7 @@ void Ruler::paintEvent(QPaintEvent*)
                        // Draw a major tick
                        p.drawText(x, ValueMargin, 0, text_height,
                                AlignCenter | AlignTop | TextDontClip,
-                               format_time(t, prefix));
+                               pv::util::format_time(t, prefix));
                        p.drawLine(QPointF(x, major_tick_y1),
                                QPointF(x, tick_y2));
                }
index dc4e7bb73139530cf870dacb5f06010e7c432050..c48f25a0bb8a826dbcd8f4234ce805a001115845 100644 (file)
@@ -40,9 +40,6 @@ private:
        static const int MinorTickSubdivision;
        static const int ScaleUnits[3];
 
-       static const QString SIPrefixes[9];
-       static const int FirstSIPrefixPower;
-
        static const int HoverArrowSize;
 
 public:
@@ -50,9 +47,6 @@ public:
 
        void clear_selection();
 
-       static QString format_time(double t, unsigned int prefix,
-               unsigned precision = 0);
-
 public:
        QSize sizeHint() const;