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