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