]>
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 | ||
7a01bd36 JH |
20 | #ifndef PULSEVIEW_UTIL_HPP |
21 | #define PULSEVIEW_UTIL_HPP | |
f0c9f81c | 22 | |
d9e71737 | 23 | #include <cmath> |
34f4a40b GS |
24 | #include <string> |
25 | #include <vector> | |
f0c9f81c | 26 | |
2b68d17c | 27 | #ifndef Q_MOC_RUN |
60d9b99a | 28 | #include <boost/multiprecision/cpp_dec_float.hpp> |
2b68d17c | 29 | #endif |
60d9b99a | 30 | |
806d3e1e | 31 | #include <QMetaType> |
f0c9f81c JS |
32 | #include <QString> |
33 | ||
34f4a40b GS |
34 | using std::string; |
35 | using std::vector; | |
36 | ||
f0c9f81c JS |
37 | namespace pv { |
38 | namespace util { | |
39 | ||
d001f416 | 40 | enum class TimeUnit { |
96b6316a SA |
41 | Time = 1, |
42 | Samples = 2 | |
43 | }; | |
44 | ||
d001f416 JS |
45 | enum class SIPrefix { |
46 | unspecified = -1, | |
47 | yocto, zepto, | |
48 | atto, femto, pico, | |
49 | nano, micro, milli, | |
50 | none, | |
51 | kilo, mega, giga, | |
52 | tera, peta, exa, | |
53 | zetta, yotta | |
54 | }; | |
55 | ||
56 | /// Returns the exponent that corresponds to a given prefix. | |
57 | int exponent(SIPrefix prefix); | |
58 | ||
60d9b99a JS |
59 | /// Timestamp type providing yoctosecond resolution. |
60 | typedef boost::multiprecision::number< | |
61 | boost::multiprecision::cpp_dec_float<24>, | |
62 | boost::multiprecision::et_off> Timestamp; | |
63 | ||
f0c9f81c | 64 | /** |
3ccf0f7f JS |
65 | * Formats a given timestamp with the specified SI prefix. |
66 | * | |
67 | * If 'prefix' is left 'unspecified', the function chooses a prefix so that | |
68 | * the value in front of the decimal point is between 1 and 999. | |
69 | * | |
70 | * The default value "s" for the unit argument makes the most sense when | |
71 | * formatting time values, but a different value can be given if the function | |
72 | * is reused to format a value of another quantity. | |
73 | * | |
74 | * @param t The value to format. | |
75 | * @param prefix The SI prefix to use. | |
4bc10a91 | 76 | * @param precision The number of digits after the decimal separator. |
3ccf0f7f | 77 | * @param unit The unit of quantity. |
4bc10a91 JH |
78 | * @param sign Whether or not to add a sign also for positive numbers. |
79 | * | |
39ccf9c3 | 80 | * @return The formatted value. |
4bc10a91 | 81 | */ |
c063290a UH |
82 | QString format_time_si(const Timestamp& v, |
83 | SIPrefix prefix = SIPrefix::unspecified, unsigned precision = 0, | |
84 | QString unit = "s", bool sign = true); | |
4bc10a91 | 85 | |
0cbadf1c SA |
86 | /** |
87 | * Formats a given value into a representation using SI units. | |
88 | * | |
89 | * If 'prefix' is left 'unspecified', the function chooses a prefix so that | |
90 | * the value in front of the decimal point is between 1 and 999. | |
91 | * | |
92 | * @param value The value to format. | |
93 | * @param prefix The SI prefix to use. | |
94 | * @param precision The number of digits after the decimal separator. | |
95 | * @param unit The unit of quantity. | |
96 | * @param sign Whether or not to add a sign also for positive numbers. | |
97 | * | |
98 | * @return The formatted value. | |
99 | */ | |
100 | QString format_value_si(double v, | |
101 | SIPrefix prefix = SIPrefix::unspecified, unsigned precision = 0, | |
102 | QString unit = "", bool sign = true); | |
103 | ||
4bc10a91 | 104 | /** |
39ccf9c3 | 105 | * Wrapper around 'format_time_si()' that interprets the given 'precision' |
3ccf0f7f | 106 | * value as the number of decimal places if the timestamp would be formatted |
39ccf9c3 | 107 | * without a SI prefix (using 'SIPrefix::none') and adjusts the precision to |
3ccf0f7f JS |
108 | * match the given 'prefix' |
109 | * | |
110 | * @param t The value to format. | |
111 | * @param prefix The SI prefix to use. | |
112 | * @param precision The number of digits after the decimal separator if the | |
113 | * 'prefix' would be 'SIPrefix::none', see above for more information. | |
4bc10a91 | 114 | * @param unit The unit of quantity. |
3ccf0f7f | 115 | * @param sign Whether or not to add a sign also for positive numbers. |
f0c9f81c | 116 | * |
39ccf9c3 | 117 | * @return The formatted value. |
f0c9f81c | 118 | */ |
c063290a UH |
119 | QString format_time_si_adjusted(const Timestamp& t, SIPrefix prefix, |
120 | unsigned precision = 0, QString unit = "s", bool sign = true); | |
f0c9f81c | 121 | |
62974f45 | 122 | /** |
3ccf0f7f JS |
123 | * Formats the given timestamp using "[+-]DD:HH:MM:SS.mmm uuu nnn ppp..." format. |
124 | * | |
125 | * "DD" and "HH" are left out if they would be "00" (but if "DD" is generated, | |
126 | * "HH" is also always generated. The "MM:SS" part is always produced, the | |
127 | * number of subsecond digits can be influenced using the 'precision' parameter. | |
128 | * | |
129 | * @param t The value to format. | |
130 | * @param precision The number of digits after the decimal separator. | |
131 | * @param sign Whether or not to add a sign also for positive numbers. | |
62974f45 | 132 | * |
39ccf9c3 | 133 | * @return The formatted value. |
62974f45 | 134 | */ |
c063290a | 135 | QString format_time_minutes(const Timestamp& t, signed precision = 0, |
3ccf0f7f | 136 | bool sign = true); |
62974f45 | 137 | |
34f4a40b GS |
138 | vector<string> split_string(string text, string separator); |
139 | ||
f0c9f81c JS |
140 | } // namespace util |
141 | } // namespace pv | |
142 | ||
806d3e1e JS |
143 | Q_DECLARE_METATYPE(pv::util::Timestamp) |
144 | ||
7a01bd36 | 145 | #endif // PULSEVIEW_UTIL_HPP |