]> sigrok.org Git - pulseview.git/blame - pv/util.cpp
AnalogSignal: Make sure the trace is redrawn when changing vdiv count
[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
2ad82c2e 90 if (stream.numberFlags() & QTextStream::ForceSign)
f52be90d 91 ss << std::showpos;
f52be90d 92
2ad82c2e
UH
93 if (0 == precision)
94 ss << std::setprecision(1) << round(t);
95 else
96 ss << std::setprecision(precision) << t;
f52be90d
JS
97
98 std::string str(ss.str());
99 if (0 == precision) {
100 // remove the separator and the unwanted decimal place
101 str.resize(str.size() - 2);
102 }
103
104 return stream << QString::fromStdString(str);
105}
106
3ccf0f7f
JS
107QString format_time_si(
108 const Timestamp& v,
109 SIPrefix prefix,
110 unsigned int precision,
111 QString unit,
112 bool sign)
f0c9f81c 113{
d001f416 114 if (prefix == SIPrefix::unspecified) {
f52be90d
JS
115 // No prefix given, calculate it
116
117 if (v.is_zero()) {
d001f416 118 prefix = SIPrefix::none;
f52be90d 119 } else {
d001f416
JS
120 int exp = exponent(SIPrefix::yotta);
121 prefix = SIPrefix::yocto;
122 while ((fabs(v) * pow(Timestamp(10), exp)) > 999 &&
123 prefix < SIPrefix::yotta) {
124 prefix = successor(prefix);
f52be90d
JS
125 exp -= 3;
126 }
4bc10a91
JH
127 }
128 }
129
d001f416
JS
130 assert(prefix >= SIPrefix::yocto);
131 assert(prefix <= SIPrefix::yotta);
f0c9f81c 132
d001f416 133 const Timestamp multiplier = pow(Timestamp(10), -exponent(prefix));
f0c9f81c
JS
134
135 QString s;
136 QTextStream ts(&s);
f52be90d 137 if (sign && !v.is_zero())
f0c9f81c 138 ts << forcesign;
f52be90d
JS
139 ts
140 << qSetRealNumberPrecision(precision)
141 << (v * multiplier)
142 << ' '
d001f416 143 << prefix
f52be90d 144 << unit;
f0c9f81c
JS
145
146 return s;
147}
148
3ccf0f7f
JS
149QString format_time_si_adjusted(
150 const Timestamp& t,
151 SIPrefix prefix,
152 unsigned precision,
153 QString unit,
154 bool sign)
155{
156 // The precision is always given without taking the prefix into account
157 // so we need to deduct the number of decimals the prefix might imply
158 const int prefix_order = -exponent(prefix);
159
160 const unsigned int relative_prec =
161 (prefix >= SIPrefix::none) ? precision :
162 std::max((int)(precision - prefix_order), 0);
163
164 return format_time_si(t, prefix, relative_prec, unit, sign);
165}
166
167
168// Helper for 'format_time_minutes()'.
ac981988
SA
169static QString pad_number(unsigned int number, int length)
170{
171 return QString("%1").arg(number, length, 10, QChar('0'));
172}
173
3ccf0f7f 174QString format_time_minutes(const Timestamp& t, signed precision, bool sign)
ac981988 175{
af95045e
JS
176 const Timestamp whole_seconds = floor(abs(t));
177 const Timestamp days = floor(whole_seconds / (60 * 60 * 24));
178 const unsigned int hours = fmod(whole_seconds / (60 * 60), 24).convert_to<uint>();
179 const unsigned int minutes = fmod(whole_seconds / 60, 60).convert_to<uint>();
180 const unsigned int seconds = fmod(whole_seconds, 60).convert_to<uint>();
ac981988
SA
181
182 QString s;
183 QTextStream ts(&s);
184
3ccf0f7f 185 if (t < 0)
ac981988 186 ts << "-";
3ccf0f7f
JS
187 else if (sign)
188 ts << "+";
ac981988
SA
189
190 bool use_padding = false;
191
3ccf0f7f 192 // DD
ac981988 193 if (days) {
af95045e 194 ts << days.str().c_str() << ":";
ac981988
SA
195 use_padding = true;
196 }
197
3ccf0f7f 198 // HH
ac981988
SA
199 if (hours || days) {
200 ts << pad_number(hours, use_padding ? 2 : 0) << ":";
201 use_padding = true;
202 }
203
3ccf0f7f
JS
204 // MM
205 ts << pad_number(minutes, use_padding ? 2 : 0);
ac981988 206
3ccf0f7f 207 ts << ":";
ac981988 208
3ccf0f7f
JS
209 // SS
210 ts << pad_number(seconds, 2);
ac981988 211
3ccf0f7f
JS
212 if (precision) {
213 ts << ".";
ac981988 214
3ccf0f7f 215 const Timestamp fraction = fabs(t) - whole_seconds;
ac981988 216
3ccf0f7f
JS
217 std::ostringstream ss;
218 ss
219 << std::fixed
220 << std::setprecision(precision)
221 << std::setfill('0')
222 << fraction;
223 std::string fs = ss.str();
ac981988 224
3ccf0f7f
JS
225 // Copy all digits, inserting spaces as unit separators
226 for (int i = 1; i <= precision; i++) {
227 // Start at index 2 to skip the "0." at the beginning
228 ts << fs.at(1 + i);
ac981988 229
3ccf0f7f
JS
230 if ((i > 0) && (i % 3 == 0) && (i != precision))
231 ts << " ";
ac981988
SA
232 }
233 }
234
235 return s;
236}
237
f0c9f81c
JS
238} // namespace util
239} // namespace pv