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