]> sigrok.org Git - pulseview.git/blame - pv/util.cpp
Use typesafe enum classes in pv::util
[pulseview.git] / pv / util.cpp
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
2acdb232 21#include "util.hpp"
f0c9f81c
JS
22
23#include <extdef.h>
24
25#include <assert.h>
26
ac981988 27#include <algorithm>
f52be90d 28#include <sstream>
ac981988 29
f0c9f81c
JS
30#include <QTextStream>
31#include <QDebug>
32
33using namespace Qt;
34
35namespace pv {
36namespace util {
37
d001f416
JS
38static QTextStream& operator<<(QTextStream& stream, SIPrefix prefix)
39{
40 switch (prefix) {
41 case SIPrefix::yocto: return stream << 'y';
42 case SIPrefix::zepto: return stream << 'z';
43 case SIPrefix::atto: return stream << 'a';
44 case SIPrefix::femto: return stream << 'f';
45 case SIPrefix::pico: return stream << 'p';
46 case SIPrefix::nano: return stream << 'n';
47 case SIPrefix::micro: return stream << QChar(0x03BC);
48 case SIPrefix::milli: return stream << 'm';
49 case SIPrefix::kilo: return stream << 'k';
50 case SIPrefix::mega: return stream << 'M';
51 case SIPrefix::giga: return stream << 'G';
52 case SIPrefix::tera: return stream << 'T';
53 case SIPrefix::peta: return stream << 'P';
54 case SIPrefix::exa: return stream << 'E';
55 case SIPrefix::zetta: return stream << 'Z';
56 case SIPrefix::yotta: return stream << 'Y';
57
58 default: return stream;
59 }
60}
61
62int exponent(SIPrefix prefix)
63{
64 return 3 * (static_cast<int>(prefix) - static_cast<int>(SIPrefix::none));
65}
66
67static SIPrefix successor(SIPrefix prefix)
68{
69 assert(prefix != SIPrefix::yotta);
70 return static_cast<SIPrefix>(static_cast<int>(prefix) + 1);
71}
f0c9f81c 72
f52be90d
JS
73// Insert the timestamp value into the stream in fixed-point notation
74// (and honor the precision)
75static QTextStream& operator<<(QTextStream& stream, const Timestamp& t)
76{
77 // The multiprecision types already have a function and a stream insertion
78 // operator to convert them to a string, however these functions abuse a
79 // precision value of zero to print all available decimal places instead of
80 // none, and the boost authors refuse to fix this because they don't want
81 // to break buggy code that relies on this bug.
82 // (https://svn.boost.org/trac/boost/ticket/10103)
83 // Therefore we have to work around the case where precision is zero.
84
85 int precision = stream.realNumberPrecision();
86
87 std::ostringstream ss;
88 ss << std::fixed;
89
90 if (stream.numberFlags() & QTextStream::ForceSign) {
91 ss << std::showpos;
92 }
93
94 if (0 == precision) {
95 ss
96 << std::setprecision(1)
97 << round(t);
98 } else {
99 ss
100 << std::setprecision(precision)
101 << t;
102 }
103
104 std::string str(ss.str());
105 if (0 == precision) {
106 // remove the separator and the unwanted decimal place
107 str.resize(str.size() - 2);
108 }
109
110 return stream << QString::fromStdString(str);
111}
112
d001f416 113QString format_si_value(const Timestamp& v, QString unit, SIPrefix prefix,
f0c9f81c
JS
114 unsigned int precision, bool sign)
115{
d001f416 116 if (prefix == SIPrefix::unspecified) {
f52be90d
JS
117 // No prefix given, calculate it
118
119 if (v.is_zero()) {
d001f416 120 prefix = SIPrefix::none;
f52be90d 121 } else {
d001f416
JS
122 int exp = exponent(SIPrefix::yotta);
123 prefix = SIPrefix::yocto;
124 while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
125 prefix < SIPrefix::yotta) {
126 prefix = successor(prefix);
f52be90d
JS
127 exp -= 3;
128 }
4bc10a91
JH
129 }
130 }
131
d001f416
JS
132 assert(prefix >= SIPrefix::yocto);
133 assert(prefix <= SIPrefix::yotta);
f0c9f81c 134
d001f416 135 const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix));
f0c9f81c
JS
136
137 QString s;
138 QTextStream ts(&s);
f52be90d 139 if (sign && !v.is_zero())
f0c9f81c 140 ts << forcesign;
f52be90d
JS
141 ts
142 << qSetRealNumberPrecision(precision)
143 << (v * multiplier)
144 << ' '
d001f416 145 << prefix
f52be90d 146 << unit;
f0c9f81c
JS
147
148 return s;
149}
150
ac981988
SA
151static QString pad_number(unsigned int number, int length)
152{
153 return QString("%1").arg(number, length, 10, QChar('0'));
154}
155
af95045e 156static QString format_time_in_full(const Timestamp& t, signed precision)
ac981988 157{
af95045e
JS
158 const Timestamp whole_seconds = floor(abs(t));
159 const Timestamp days = floor(whole_seconds / (60 * 60 * 24));
160 const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>();
161 const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
162 const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
ac981988
SA
163
164 QString s;
165 QTextStream ts(&s);
166
dae21ebc 167 if (t >= 0)
ac981988 168 ts << "+";
dae21ebc 169 else
ac981988
SA
170 ts << "-";
171
172 bool use_padding = false;
173
174 if (days) {
af95045e 175 ts << days.str().c_str() << ":";
ac981988
SA
176 use_padding = true;
177 }
178
179 if (hours || days) {
180 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
181 use_padding = true;
182 }
183
184 if (minutes || hours || days) {
185 ts << pad_number(minutes, use_padding ? 2 : 0);
186 use_padding = true;
187
188 // We're not showing any seconds with a negative precision
189 if (precision >= 0)
190 ts << ":";
191 }
192
193 // precision < 0: Use DD:HH:MM format
194 // precision = 0: Use DD:HH:MM:SS format
195 // precision > 0: Use DD:HH:MM:SS.mmm nnn ppp fff format
196 if (precision >= 0) {
197 ts << pad_number(seconds, use_padding ? 2 : 0);
198
af95045e 199 const Timestamp fraction = fabs(t) - whole_seconds;
ac981988
SA
200
201 if (precision > 0 && precision < 1000) {
af95045e
JS
202 std::ostringstream ss;
203 ss
204 << std::fixed
205 << std::setprecision(2 + precision)
206 << std::setfill('0')
207 << fraction;
208 std::string fs = ss.str();
ac981988
SA
209
210 ts << ".";
211
212 // Copy all digits, inserting spaces as unit separators
213 for (int i = 1; i <= precision; i++) {
214 // Start at index 2 to skip the "0." at the beginning
215 ts << fs[1 + i];
216
af95045e 217 if ((i > 0) && (i % 3 == 0) && (i != precision))
ac981988
SA
218 ts << " ";
219 }
220 }
221 }
222
223 return s;
224}
225
af95045e 226static QString format_time_with_si(const Timestamp& t, QString unit,
d001f416 227 SIPrefix prefix, unsigned int precision)
6638455f
SA
228{
229 // The precision is always given without taking the prefix into account
230 // so we need to deduct the number of decimals the prefix might imply
d001f416 231 const int prefix_order = -exponent(prefix);
6638455f
SA
232
233 const unsigned int relative_prec =
d001f416 234 (prefix >= SIPrefix::none) ? precision :
6638455f
SA
235 std::max((int)(precision - prefix_order), 0);
236
dae21ebc 237 return format_si_value(t, unit, prefix, relative_prec);
6638455f
SA
238}
239
d001f416 240QString format_time(const Timestamp& t, SIPrefix prefix, TimeUnit unit,
af95045e 241 unsigned int precision)
62974f45 242{
f4ebff5e 243 // Make 0 appear as 0, not random +0 or -0
af95045e 244 if (t.is_zero())
f4ebff5e
SA
245 return "0";
246
ac981988 247 // If we have to use samples then we have no alternative formats
d001f416 248 if (unit == TimeUnit::Samples)
dae21ebc 249 return format_time_with_si(t, "sa", prefix, precision);
ac981988
SA
250
251 // View in "normal" range -> medium precision, medium step size
252 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
253 // View zoomed way in -> high precision (>3), low step size (<1s)
254 // -> HH:MM:SS.mmm... or xxxx (si unit) if less than 60 seconds
6638455f 255 if (abs(t) < 60)
dae21ebc 256 return format_time_with_si(t, "s", prefix, precision);
6638455f 257 else
dae21ebc 258 return format_time_in_full(t, precision);
4bc10a91 259}
62974f45 260
60d9b99a 261QString format_second(const Timestamp& second)
4bc10a91 262{
d001f416 263 return format_si_value(second, "s", SIPrefix::unspecified, 0, false);
62974f45
JS
264}
265
f0c9f81c
JS
266} // namespace util
267} // namespace pv