]>
Commit | Line | Data |
---|---|---|
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 | ||
30 | using namespace Qt; | |
31 | ||
32 | namespace pv { | |
33 | namespace util { | |
34 | ||
6f1f59c8 JH |
35 | static const QString SIPrefixes[17] = |
36 | {"y", "z", "a", "f", "p", "n", QChar(0x03BC), "m", "", "k", "M", "G", | |
37 | "T", "P", "E", "Z", "Y"}; | |
38 | const int FirstSIPrefixPower = -24; | |
f0c9f81c | 39 | |
4bc10a91 | 40 | QString 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 | ||
96b6316a | 70 | QString format_time(double t, int prefix, TimeUnit unit, |
4bc10a91 | 71 | unsigned int precision, bool sign) |
62974f45 | 72 | { |
96b6316a SA |
73 | if (unit == TimeUnit::Time) |
74 | return format_si_value(t, "s", prefix, precision, sign); | |
75 | else | |
76 | return format_si_value(t, "sa", prefix, precision, sign); | |
4bc10a91 | 77 | } |
62974f45 | 78 | |
4bc10a91 JH |
79 | QString format_second(double second) |
80 | { | |
81 | return format_si_value(second, "s", -1, 0, false); | |
62974f45 JS |
82 | } |
83 | ||
f0c9f81c JS |
84 | } // namespace util |
85 | } // namespace pv |