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