From: Jens Steinhauser Date: Thu, 22 May 2014 20:03:22 +0000 (+0200) Subject: Put the time format function into a separate file. X-Git-Tag: pulseview-0.3.0~618 X-Git-Url: https://sigrok.org/gitweb/?p=pulseview.git;a=commitdiff_plain;h=f0c9f81c03d2c1d1fa59c357a88d25f6a3128651 Put the time format function into a separate file. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 50dde490..a6a6c406 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 index 00000000..28e0dfde --- /dev/null +++ b/pv/util.cpp @@ -0,0 +1,59 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2012 Joel Holdsworth + * + * 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 + +#include + +#include +#include + +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 index 00000000..1af3ce93 --- /dev/null +++ b/pv/util.h @@ -0,0 +1,50 @@ +/* + * This file is part of the PulseView project. + * + * Copyright (C) 2012 Joel Holdsworth + * + * 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 + +#include + +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 diff --git a/pv/view/cursor.cpp b/pv/view/cursor.cpp index ca22eefd..95134b77 100644 --- a/pv/view/cursor.cpp +++ b/pv/view/cursor.cpp @@ -20,8 +20,8 @@ #include "cursor.h" -#include "ruler.h" #include "view.h" +#include "pv/util.h" #include #include @@ -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::get_other_cursor() const diff --git a/pv/view/cursorpair.cpp b/pv/view/cursorpair.cpp index ed8829d2..f73567e3 100644 --- a/pv/view/cursorpair.cpp +++ b/pv/view/cursorpair.cpp @@ -20,8 +20,8 @@ #include "cursorpair.h" -#include "ruler.h" #include "view.h" +#include "pv/util.h" #include @@ -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(); } diff --git a/pv/view/ruler.cpp b/pv/view/ruler.cpp index aec3de97..9e29ec29 100644 --- a/pv/view/ruler.cpp +++ b/pv/view/ruler.cpp @@ -23,13 +23,10 @@ #include "cursor.h" #include "view.h" #include "viewport.h" +#include "pv/util.h" #include -#include -#include -#include - #include #include #include @@ -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)); } diff --git a/pv/view/ruler.h b/pv/view/ruler.h index dc4e7bb7..c48f25a0 100644 --- a/pv/view/ruler.h +++ b/pv/view/ruler.h @@ -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;