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