]>
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 | ||
ac981988 | 24 | #include <algorithm> |
aca9aa83 | 25 | #include <cassert> |
f52be90d | 26 | #include <sstream> |
ac981988 | 27 | |
f0c9f81c | 28 | #include <QDebug> |
aca9aa83 | 29 | #include <QTextStream> |
f0c9f81c | 30 | |
6f925ba9 UH |
31 | using std::fixed; |
32 | using std::max; | |
33 | using std::ostringstream; | |
34 | using std::setfill; | |
35 | using std::setprecision; | |
36 | using std::showpos; | |
37 | using std::string; | |
38 | ||
f0c9f81c JS |
39 | using namespace Qt; |
40 | ||
41 | namespace pv { | |
42 | namespace util { | |
43 | ||
d001f416 JS |
44 | static QTextStream& operator<<(QTextStream& stream, SIPrefix prefix) |
45 | { | |
46 | switch (prefix) { | |
47 | case SIPrefix::yocto: return stream << 'y'; | |
48 | case SIPrefix::zepto: return stream << 'z'; | |
49 | case SIPrefix::atto: return stream << 'a'; | |
50 | case SIPrefix::femto: return stream << 'f'; | |
51 | case SIPrefix::pico: return stream << 'p'; | |
52 | case SIPrefix::nano: return stream << 'n'; | |
53 | case SIPrefix::micro: return stream << QChar(0x03BC); | |
54 | case SIPrefix::milli: return stream << 'm'; | |
55 | case SIPrefix::kilo: return stream << 'k'; | |
56 | case SIPrefix::mega: return stream << 'M'; | |
57 | case SIPrefix::giga: return stream << 'G'; | |
58 | case SIPrefix::tera: return stream << 'T'; | |
59 | case SIPrefix::peta: return stream << 'P'; | |
60 | case SIPrefix::exa: return stream << 'E'; | |
61 | case SIPrefix::zetta: return stream << 'Z'; | |
62 | case SIPrefix::yotta: return stream << 'Y'; | |
63 | ||
64 | default: return stream; | |
65 | } | |
66 | } | |
67 | ||
68 | int exponent(SIPrefix prefix) | |
69 | { | |
70 | return 3 * (static_cast<int>(prefix) - static_cast<int>(SIPrefix::none)); | |
71 | } | |
72 | ||
73 | static SIPrefix successor(SIPrefix prefix) | |
74 | { | |
75 | assert(prefix != SIPrefix::yotta); | |
76 | return static_cast<SIPrefix>(static_cast<int>(prefix) + 1); | |
77 | } | |
f0c9f81c | 78 | |
f52be90d JS |
79 | // Insert the timestamp value into the stream in fixed-point notation |
80 | // (and honor the precision) | |
81 | static QTextStream& operator<<(QTextStream& stream, const Timestamp& t) | |
82 | { | |
83 | // The multiprecision types already have a function and a stream insertion | |
84 | // operator to convert them to a string, however these functions abuse a | |
85 | // precision value of zero to print all available decimal places instead of | |
86 | // none, and the boost authors refuse to fix this because they don't want | |
87 | // to break buggy code that relies on this bug. | |
88 | // (https://svn.boost.org/trac/boost/ticket/10103) | |
89 | // Therefore we have to work around the case where precision is zero. | |
90 | ||
91 | int precision = stream.realNumberPrecision(); | |
92 | ||
6f925ba9 UH |
93 | ostringstream ss; |
94 | ss << fixed; | |
f52be90d | 95 | |
2ad82c2e | 96 | if (stream.numberFlags() & QTextStream::ForceSign) |
6f925ba9 | 97 | ss << showpos; |
f52be90d | 98 | |
2ad82c2e | 99 | if (0 == precision) |
6f925ba9 | 100 | ss << setprecision(1) << round(t); |
2ad82c2e | 101 | else |
6f925ba9 | 102 | ss << setprecision(precision) << t; |
f52be90d | 103 | |
6f925ba9 | 104 | string str(ss.str()); |
f52be90d JS |
105 | if (0 == precision) { |
106 | // remove the separator and the unwanted decimal place | |
107 | str.resize(str.size() - 2); | |
108 | } | |
109 | ||
110 | return stream << QString::fromStdString(str); | |
111 | } | |
112 | ||
0efa7f0c | 113 | SIPrefix determine_value_prefix(double v) |
f0c9f81c | 114 | { |
0efa7f0c SA |
115 | SIPrefix prefix; |
116 | ||
117 | if (v == 0) { | |
118 | prefix = SIPrefix::none; | |
119 | } else { | |
120 | int exp = exponent(SIPrefix::yotta); | |
121 | prefix = SIPrefix::yocto; | |
122 | while ((fabs(v) * pow(10, exp)) > 999 && | |
123 | prefix < SIPrefix::yotta) { | |
124 | prefix = successor(prefix); | |
125 | exp -= 3; | |
4bc10a91 JH |
126 | } |
127 | } | |
128 | ||
0efa7f0c SA |
129 | return prefix; |
130 | } | |
131 | ||
132 | QString format_time_si(const Timestamp& v, SIPrefix prefix, | |
133 | unsigned int precision, QString unit, bool sign) | |
134 | { | |
135 | if (prefix == SIPrefix::unspecified) | |
136 | prefix = determine_value_prefix(v.convert_to<double>()); | |
137 | ||
d001f416 JS |
138 | assert(prefix >= SIPrefix::yocto); |
139 | assert(prefix <= SIPrefix::yotta); | |
f0c9f81c | 140 | |
d001f416 | 141 | const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix)); |
f0c9f81c JS |
142 | |
143 | QString s; | |
144 | QTextStream ts(&s); | |
f52be90d | 145 | if (sign && !v.is_zero()) |
ae726b70 | 146 | ts.setNumberFlags(ts.numberFlags() | QTextStream::ForceSign); |
ef85cfa4 | 147 | ts << qSetRealNumberPrecision(precision) << (v * multiplier); |
66279897 | 148 | ts << ' ' << prefix << unit; |
f0c9f81c JS |
149 | |
150 | return s; | |
151 | } | |
152 | ||
0cbadf1c SA |
153 | QString format_value_si(double v, SIPrefix prefix, unsigned precision, |
154 | QString unit, bool sign) | |
155 | { | |
156 | if (prefix == SIPrefix::unspecified) { | |
0efa7f0c | 157 | prefix = determine_value_prefix(v); |
66279897 SA |
158 | |
159 | const int prefix_order = -exponent(prefix); | |
160 | precision = (prefix >= SIPrefix::none) ? max((int)(precision + prefix_order), 0) : | |
161 | max((int)(precision - prefix_order), 0); | |
0cbadf1c SA |
162 | } |
163 | ||
164 | assert(prefix >= SIPrefix::yocto); | |
165 | assert(prefix <= SIPrefix::yotta); | |
166 | ||
167 | const double multiplier = pow(10.0, -exponent(prefix)); | |
168 | ||
169 | QString s; | |
170 | QTextStream ts(&s); | |
171 | if (sign && (v != 0)) | |
ae726b70 | 172 | ts.setNumberFlags(ts.numberFlags() | QTextStream::ForceSign); |
0cbadf1c SA |
173 | ts.setRealNumberNotation(QTextStream::FixedNotation); |
174 | ts.setRealNumberPrecision(precision); | |
175 | ts << (v * multiplier) << ' ' << prefix << unit; | |
176 | ||
177 | return s; | |
178 | } | |
179 | ||
c063290a UH |
180 | QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix, |
181 | unsigned precision, QString unit, bool sign) | |
3ccf0f7f JS |
182 | { |
183 | // The precision is always given without taking the prefix into account | |
184 | // so we need to deduct the number of decimals the prefix might imply | |
185 | const int prefix_order = -exponent(prefix); | |
186 | ||
187 | const unsigned int relative_prec = | |
188 | (prefix >= SIPrefix::none) ? precision : | |
6f925ba9 | 189 | max((int)(precision - prefix_order), 0); |
3ccf0f7f JS |
190 | |
191 | return format_time_si(t, prefix, relative_prec, unit, sign); | |
192 | } | |
193 | ||
3ccf0f7f | 194 | // Helper for 'format_time_minutes()'. |
ac981988 SA |
195 | static QString pad_number(unsigned int number, int length) |
196 | { | |
197 | return QString("%1").arg(number, length, 10, QChar('0')); | |
198 | } | |
199 | ||
3ccf0f7f | 200 | QString format_time_minutes(const Timestamp& t, signed precision, bool sign) |
ac981988 | 201 | { |
af95045e JS |
202 | const Timestamp whole_seconds = floor(abs(t)); |
203 | const Timestamp days = floor(whole_seconds / (60 * 60 * 24)); | |
204 | const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>(); | |
205 | const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>(); | |
206 | const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>(); | |
ac981988 SA |
207 | |
208 | QString s; | |
209 | QTextStream ts(&s); | |
210 | ||
3ccf0f7f | 211 | if (t < 0) |
ac981988 | 212 | ts << "-"; |
3ccf0f7f JS |
213 | else if (sign) |
214 | ts << "+"; | |
ac981988 SA |
215 | |
216 | bool use_padding = false; | |
217 | ||
3ccf0f7f | 218 | // DD |
ac981988 | 219 | if (days) { |
af95045e | 220 | ts << days.str().c_str() << ":"; |
ac981988 SA |
221 | use_padding = true; |
222 | } | |
223 | ||
3ccf0f7f | 224 | // HH |
ac981988 SA |
225 | if (hours || days) { |
226 | ts << pad_number(hours, use_padding ? 2 : 0) << ":"; | |
227 | use_padding = true; | |
228 | } | |
229 | ||
3ccf0f7f JS |
230 | // MM |
231 | ts << pad_number(minutes, use_padding ? 2 : 0); | |
ac981988 | 232 | |
3ccf0f7f | 233 | ts << ":"; |
ac981988 | 234 | |
3ccf0f7f JS |
235 | // SS |
236 | ts << pad_number(seconds, 2); | |
ac981988 | 237 | |
3ccf0f7f JS |
238 | if (precision) { |
239 | ts << "."; | |
ac981988 | 240 | |
3ccf0f7f | 241 | const Timestamp fraction = fabs(t) - whole_seconds; |
ac981988 | 242 | |
6f925ba9 UH |
243 | ostringstream ss; |
244 | ss << fixed << setprecision(precision) << setfill('0') << fraction; | |
245 | string fs = ss.str(); | |
ac981988 | 246 | |
3ccf0f7f JS |
247 | // Copy all digits, inserting spaces as unit separators |
248 | for (int i = 1; i <= precision; i++) { | |
249 | // Start at index 2 to skip the "0." at the beginning | |
250 | ts << fs.at(1 + i); | |
ac981988 | 251 | |
3ccf0f7f JS |
252 | if ((i > 0) && (i % 3 == 0) && (i != precision)) |
253 | ts << " "; | |
ac981988 SA |
254 | } |
255 | } | |
256 | ||
257 | return s; | |
258 | } | |
259 | ||
34f4a40b | 260 | /** |
3083cda8 | 261 | * Split a string into tokens at occurences of the separator. |
34f4a40b | 262 | * |
3083cda8 UH |
263 | * @param[in] text The input string to split. |
264 | * @param[in] separator The delimiter between tokens. | |
34f4a40b | 265 | * |
3083cda8 | 266 | * @return A vector of broken down tokens. |
34f4a40b GS |
267 | */ |
268 | vector<string> split_string(string text, string separator) | |
269 | { | |
270 | vector<string> result; | |
271 | size_t pos; | |
272 | ||
273 | while ((pos = text.find(separator)) != std::string::npos) { | |
274 | result.push_back(text.substr(0, pos)); | |
275 | text = text.substr(pos + separator.length()); | |
276 | } | |
277 | result.push_back(text); | |
278 | ||
279 | return result; | |
280 | } | |
281 | ||
ae726b70 VO |
282 | /** |
283 | * Return the width of a string in a given font. | |
284 | * | |
285 | * @param[in] metric metrics of the font | |
286 | * @param[in] string the string whose width should be determined | |
287 | * | |
288 | * @return width of the string in pixels | |
289 | */ | |
290 | std::streamsize text_width(const QFontMetrics &metric, const QString &string) | |
291 | { | |
292 | #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0) | |
293 | return metric.horizontalAdvance(string); | |
294 | #else | |
295 | return metric.width(string); | |
296 | #endif | |
297 | } | |
298 | ||
f0c9f81c JS |
299 | } // namespace util |
300 | } // namespace pv |