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