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