]>
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 | |
efdec55a | 17 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
f0c9f81c JS |
18 | */ |
19 | ||
2acdb232 | 20 | #include "util.hpp" |
f0c9f81c JS |
21 | |
22 | #include <extdef.h> | |
23 | ||
eb8269e3 | 24 | #include <cassert> |
ac981988 | 25 | #include <algorithm> |
f52be90d | 26 | #include <sstream> |
ac981988 | 27 | |
f0c9f81c JS |
28 | #include <QTextStream> |
29 | #include <QDebug> | |
30 | ||
31 | using namespace Qt; | |
32 | ||
33 | namespace pv { | |
34 | namespace util { | |
35 | ||
d001f416 JS |
36 | static QTextStream& operator<<(QTextStream& stream, SIPrefix prefix) |
37 | { | |
38 | switch (prefix) { | |
39 | case SIPrefix::yocto: return stream << 'y'; | |
40 | case SIPrefix::zepto: return stream << 'z'; | |
41 | case SIPrefix::atto: return stream << 'a'; | |
42 | case SIPrefix::femto: return stream << 'f'; | |
43 | case SIPrefix::pico: return stream << 'p'; | |
44 | case SIPrefix::nano: return stream << 'n'; | |
45 | case SIPrefix::micro: return stream << QChar(0x03BC); | |
46 | case SIPrefix::milli: return stream << 'm'; | |
47 | case SIPrefix::kilo: return stream << 'k'; | |
48 | case SIPrefix::mega: return stream << 'M'; | |
49 | case SIPrefix::giga: return stream << 'G'; | |
50 | case SIPrefix::tera: return stream << 'T'; | |
51 | case SIPrefix::peta: return stream << 'P'; | |
52 | case SIPrefix::exa: return stream << 'E'; | |
53 | case SIPrefix::zetta: return stream << 'Z'; | |
54 | case SIPrefix::yotta: return stream << 'Y'; | |
55 | ||
56 | default: return stream; | |
57 | } | |
58 | } | |
59 | ||
60 | int exponent(SIPrefix prefix) | |
61 | { | |
62 | return 3 * (static_cast<int>(prefix) - static_cast<int>(SIPrefix::none)); | |
63 | } | |
64 | ||
65 | static SIPrefix successor(SIPrefix prefix) | |
66 | { | |
67 | assert(prefix != SIPrefix::yotta); | |
68 | return static_cast<SIPrefix>(static_cast<int>(prefix) + 1); | |
69 | } | |
f0c9f81c | 70 | |
f52be90d JS |
71 | // Insert the timestamp value into the stream in fixed-point notation |
72 | // (and honor the precision) | |
73 | static QTextStream& operator<<(QTextStream& stream, const Timestamp& t) | |
74 | { | |
75 | // The multiprecision types already have a function and a stream insertion | |
76 | // operator to convert them to a string, however these functions abuse a | |
77 | // precision value of zero to print all available decimal places instead of | |
78 | // none, and the boost authors refuse to fix this because they don't want | |
79 | // to break buggy code that relies on this bug. | |
80 | // (https://svn.boost.org/trac/boost/ticket/10103) | |
81 | // Therefore we have to work around the case where precision is zero. | |
82 | ||
83 | int precision = stream.realNumberPrecision(); | |
84 | ||
85 | std::ostringstream ss; | |
86 | ss << std::fixed; | |
87 | ||
2ad82c2e | 88 | if (stream.numberFlags() & QTextStream::ForceSign) |
f52be90d | 89 | ss << std::showpos; |
f52be90d | 90 | |
2ad82c2e UH |
91 | if (0 == precision) |
92 | ss << std::setprecision(1) << round(t); | |
93 | else | |
94 | ss << std::setprecision(precision) << t; | |
f52be90d JS |
95 | |
96 | std::string str(ss.str()); | |
97 | if (0 == precision) { | |
98 | // remove the separator and the unwanted decimal place | |
99 | str.resize(str.size() - 2); | |
100 | } | |
101 | ||
102 | return stream << QString::fromStdString(str); | |
103 | } | |
104 | ||
3ccf0f7f JS |
105 | QString format_time_si( |
106 | const Timestamp& v, | |
107 | SIPrefix prefix, | |
108 | unsigned int precision, | |
109 | QString unit, | |
110 | bool sign) | |
f0c9f81c | 111 | { |
d001f416 | 112 | if (prefix == SIPrefix::unspecified) { |
f52be90d JS |
113 | // No prefix given, calculate it |
114 | ||
115 | if (v.is_zero()) { | |
d001f416 | 116 | prefix = SIPrefix::none; |
f52be90d | 117 | } else { |
d001f416 JS |
118 | int exp = exponent(SIPrefix::yotta); |
119 | prefix = SIPrefix::yocto; | |
120 | while ((fabs(v) * pow(Timestamp(10), exp)) > 999 && | |
121 | prefix < SIPrefix::yotta) { | |
122 | prefix = successor(prefix); | |
f52be90d JS |
123 | exp -= 3; |
124 | } | |
4bc10a91 JH |
125 | } |
126 | } | |
127 | ||
d001f416 JS |
128 | assert(prefix >= SIPrefix::yocto); |
129 | assert(prefix <= SIPrefix::yotta); | |
f0c9f81c | 130 | |
d001f416 | 131 | const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix)); |
f0c9f81c JS |
132 | |
133 | QString s; | |
134 | QTextStream ts(&s); | |
f52be90d | 135 | if (sign && !v.is_zero()) |
f0c9f81c | 136 | ts << forcesign; |
f52be90d JS |
137 | ts |
138 | << qSetRealNumberPrecision(precision) | |
139 | << (v * multiplier) | |
140 | << ' ' | |
d001f416 | 141 | << prefix |
f52be90d | 142 | << unit; |
f0c9f81c JS |
143 | |
144 | return s; | |
145 | } | |
146 | ||
3ccf0f7f JS |
147 | QString format_time_si_adjusted( |
148 | const Timestamp& t, | |
149 | SIPrefix prefix, | |
150 | unsigned precision, | |
151 | QString unit, | |
152 | bool sign) | |
153 | { | |
154 | // The precision is always given without taking the prefix into account | |
155 | // so we need to deduct the number of decimals the prefix might imply | |
156 | const int prefix_order = -exponent(prefix); | |
157 | ||
158 | const unsigned int relative_prec = | |
159 | (prefix >= SIPrefix::none) ? precision : | |
160 | std::max((int)(precision - prefix_order), 0); | |
161 | ||
162 | return format_time_si(t, prefix, relative_prec, unit, sign); | |
163 | } | |
164 | ||
165 | ||
166 | // Helper for 'format_time_minutes()'. | |
ac981988 SA |
167 | static QString pad_number(unsigned int number, int length) |
168 | { | |
169 | return QString("%1").arg(number, length, 10, QChar('0')); | |
170 | } | |
171 | ||
3ccf0f7f | 172 | QString format_time_minutes(const Timestamp& t, signed precision, bool sign) |
ac981988 | 173 | { |
af95045e JS |
174 | const Timestamp whole_seconds = floor(abs(t)); |
175 | const Timestamp days = floor(whole_seconds / (60 * 60 * 24)); | |
176 | const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>(); | |
177 | const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>(); | |
178 | const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>(); | |
ac981988 SA |
179 | |
180 | QString s; | |
181 | QTextStream ts(&s); | |
182 | ||
3ccf0f7f | 183 | if (t < 0) |
ac981988 | 184 | ts << "-"; |
3ccf0f7f JS |
185 | else if (sign) |
186 | ts << "+"; | |
ac981988 SA |
187 | |
188 | bool use_padding = false; | |
189 | ||
3ccf0f7f | 190 | // DD |
ac981988 | 191 | if (days) { |
af95045e | 192 | ts << days.str().c_str() << ":"; |
ac981988 SA |
193 | use_padding = true; |
194 | } | |
195 | ||
3ccf0f7f | 196 | // HH |
ac981988 SA |
197 | if (hours || days) { |
198 | ts << pad_number(hours, use_padding ? 2 : 0) << ":"; | |
199 | use_padding = true; | |
200 | } | |
201 | ||
3ccf0f7f JS |
202 | // MM |
203 | ts << pad_number(minutes, use_padding ? 2 : 0); | |
ac981988 | 204 | |
3ccf0f7f | 205 | ts << ":"; |
ac981988 | 206 | |
3ccf0f7f JS |
207 | // SS |
208 | ts << pad_number(seconds, 2); | |
ac981988 | 209 | |
3ccf0f7f JS |
210 | if (precision) { |
211 | ts << "."; | |
ac981988 | 212 | |
3ccf0f7f | 213 | const Timestamp fraction = fabs(t) - whole_seconds; |
ac981988 | 214 | |
3ccf0f7f JS |
215 | std::ostringstream ss; |
216 | ss | |
217 | << std::fixed | |
218 | << std::setprecision(precision) | |
219 | << std::setfill('0') | |
220 | << fraction; | |
221 | std::string fs = ss.str(); | |
ac981988 | 222 | |
3ccf0f7f JS |
223 | // Copy all digits, inserting spaces as unit separators |
224 | for (int i = 1; i <= precision; i++) { | |
225 | // Start at index 2 to skip the "0." at the beginning | |
226 | ts << fs.at(1 + i); | |
ac981988 | 227 | |
3ccf0f7f JS |
228 | if ((i > 0) && (i % 3 == 0) && (i != precision)) |
229 | ts << " "; | |
ac981988 SA |
230 | } |
231 | } | |
232 | ||
233 | return s; | |
234 | } | |
235 | ||
f0c9f81c JS |
236 | } // namespace util |
237 | } // namespace pv |