]> sigrok.org Git - pulseview.git/blame - pv/util.cpp
SignalData: Renamed get_max_sample_count
[pulseview.git] / pv / util.cpp
CommitLineData
f0c9f81c
JS
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
2acdb232 21#include "util.hpp"
f0c9f81c
JS
22
23#include <extdef.h>
24
25#include <assert.h>
26
27#include <QTextStream>
28#include <QDebug>
29
30using namespace Qt;
31
32namespace pv {
33namespace util {
34
6f1f59c8
JH
35static const QString SIPrefixes[17] =
36 {"y", "z", "a", "f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G",
37 "T", "P", "E", "Z", "Y"};
38const int FirstSIPrefixPower = -24;
f0c9f81c 39
4bc10a91 40QString format_si_value(double v, QString unit, int prefix,
f0c9f81c
JS
41 unsigned int precision, bool sign)
42{
4bc10a91
JH
43 if (prefix < 0) {
44 int exp = -FirstSIPrefixPower;
45
46 prefix = 0;
47 while ((fabs(v) * pow(10.0, exp)) > 999.0 &&
48 prefix < (int)(countof(SIPrefixes) - 1)) {
49 prefix++;
50 exp -= 3;
51 }
52 }
53
54 assert(prefix >= 0);
0ba3971c 55 assert(prefix < (int)countof(SIPrefixes));
f0c9f81c
JS
56
57 const double multiplier = pow(10.0,
58 (int)- prefix * 3 - FirstSIPrefixPower);
59
60 QString s;
61 QTextStream ts(&s);
4bc10a91 62 if (sign)
f0c9f81c 63 ts << forcesign;
e113edc6
JH
64 ts << fixed << qSetRealNumberPrecision(precision) <<
65 (v * multiplier) << " " << SIPrefixes[prefix] << unit;
f0c9f81c
JS
66
67 return s;
68}
69
4bc10a91
JH
70QString format_time(double t, int prefix,
71 unsigned int precision, bool sign)
62974f45 72{
4bc10a91
JH
73 return format_si_value(t, "s", prefix, precision, sign);
74}
62974f45 75
4bc10a91
JH
76QString format_second(double second)
77{
78 return format_si_value(second, "s", -1, 0, false);
62974f45
JS
79}
80
f0c9f81c
JS
81} // namespace util
82} // namespace pv