forked from digibyte/digibyte
-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathdigibyteunits.cpp
More file actions
267 lines (234 loc) · 7.58 KB
/
digibyteunits.cpp
File metadata and controls
267 lines (234 loc) · 7.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright (c) 2011-2021 The Bitcoin Core developers
// Copyright (c) 2014-2025 The DigiByte Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
// Source: https://dgbwiki.com/index.php?title=DigiByte#Subunits
#include <qt/digibyteunits.h>
#include <consensus/amount.h>
#include <QStringList>
#include <cassert>
static constexpr auto MAX_DIGITS_DGB = 16;
DigiByteUnits::DigiByteUnits(QObject *parent):
QAbstractListModel(parent),
unitlist(availableUnits())
{
}
QList<DigiByteUnit> DigiByteUnits::availableUnits()
{
QList<DigiByteUnit> unitlist;
unitlist.append(Unit::DGB);
unitlist.append(Unit::mDGB);
unitlist.append(Unit::uDGB);
unitlist.append(Unit::SAT);
return unitlist;
}
QString DigiByteUnits::longName(Unit unit)
{
switch (unit) {
case Unit::DGB: return QString("DGB");
case Unit::mDGB: return QString("mDGB");
case Unit::uDGB: return QString::fromUtf8("µDGB (bits)");
case Unit::SAT: return QString("DigiSatoshi (sat)");
} // no default case, so the compiler can warn about missing cases
assert(false);
}
QString DigiByteUnits::shortName(Unit unit)
{
switch (unit) {
case Unit::DGB: return longName(unit);
case Unit::mDGB: return longName(unit);
case Unit::uDGB: return QString("bits");
case Unit::SAT: return QString("sat");
} // no default case, so the compiler can warn about missing cases
assert(false);
}
QString DigiByteUnits::description(Unit unit)
{
switch (unit) {
case Unit::DGB: return QString("DigiBytes");
case Unit::mDGB: return QString("Milli-DigiBytes (1 / 1" THIN_SP_UTF8 "000)");
case Unit::uDGB: return QString("Micro-DigiBytes (bits) (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
case Unit::SAT: return QString("DigiSatoshi (sat) (1 / 100" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
} // no default case, so the compiler can warn about missing cases
assert(false);
}
qint64 DigiByteUnits::factor(Unit unit)
{
switch (unit) {
case Unit::DGB: return 100'000'000;
case Unit::mDGB: return 100'000;
case Unit::uDGB: return 100;
case Unit::SAT: return 1;
} // no default case, so the compiler can warn about missing cases
assert(false);
}
int DigiByteUnits::decimals(Unit unit)
{
switch (unit) {
case Unit::DGB: return 8;
case Unit::mDGB: return 5;
case Unit::uDGB: return 2;
case Unit::SAT: return 0;
} // no default case, so the compiler can warn about missing cases
assert(false);
}
QString DigiByteUnits::format(Unit unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators, bool justify)
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
qint64 n = (qint64)nIn;
qint64 coin = factor(unit);
int num_decimals = decimals(unit);
qint64 n_abs = (n > 0 ? n : -n);
qint64 quotient = n_abs / coin;
QString quotient_str = QString::number(quotient);
if (justify) {
quotient_str = quotient_str.rightJustified(MAX_DIGITS_DGB - num_decimals, ' ');
}
// Use SI-style thin space separators as these are locale independent and can't be
// confused with the decimal marker.
QChar thin_sp(THIN_SP_CP);
int q_size = quotient_str.size();
if (separators == SeparatorStyle::ALWAYS || (separators == SeparatorStyle::STANDARD && q_size > 4))
for (int i = 3; i < q_size; i += 3)
quotient_str.insert(q_size - i, thin_sp);
if (n < 0)
quotient_str.insert(0, '-');
else if (fPlus && n > 0)
quotient_str.insert(0, '+');
if (num_decimals > 0) {
qint64 remainder = n_abs % coin;
QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
return quotient_str + QString(".") + remainder_str;
} else {
return quotient_str;
}
}
// NOTE: Using formatWithUnit in an HTML context risks wrapping
// quantities at the thousands separator. More subtly, it also results
// in a standard space rather than a thin space, due to a bug in Qt's
// XML whitespace canonicalisation
//
// Please take care to use formatHtmlWithUnit instead, when
// appropriate.
QString DigiByteUnits::formatWithUnit(Unit unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
{
return format(unit, amount, plussign, separators) + QString(" ") + shortName(unit);
}
QString DigiByteUnits::formatHtmlWithUnit(Unit unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
{
QString str(formatWithUnit(unit, amount, plussign, separators));
str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
}
QString DigiByteUnits::formatWithPrivacy(Unit unit, const CAmount& amount, SeparatorStyle separators, bool privacy)
{
assert(amount >= 0);
QString value;
if (privacy) {
value = format(unit, 0, false, separators, true).replace('0', '#');
} else {
value = format(unit, amount, false, separators, true);
}
return value + QString(" ") + shortName(unit);
}
bool DigiByteUnits::parse(Unit unit, const QString& value, CAmount* val_out)
{
if (value.isEmpty()) {
return false; // Refuse to parse invalid unit or empty string
}
int num_decimals = decimals(unit);
// Ignore spaces and thin spaces when parsing
QStringList parts = removeSpaces(value).split(".");
if(parts.size() > 2)
{
return false; // More than one dot
}
QString whole = parts[0];
QString decimals;
if(parts.size() > 1)
{
decimals = parts[1];
}
if(decimals.size() > num_decimals)
{
return false; // Exceeds max precision
}
bool ok = false;
QString str = whole + decimals.leftJustified(num_decimals, '0');
if(str.size() > 18)
{
return false; // Longer numbers will exceed 63 bits
}
CAmount retvalue(str.toLongLong(&ok));
if(val_out)
{
*val_out = retvalue;
}
return ok;
}
QString DigiByteUnits::getAmountColumnTitle(Unit unit)
{
return QObject::tr("Amount") + " (" + shortName(unit) + ")";
}
int DigiByteUnits::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return unitlist.size();
}
QVariant DigiByteUnits::data(const QModelIndex &index, int role) const
{
int row = index.row();
if(row >= 0 && row < unitlist.size())
{
Unit unit = unitlist.at(row);
switch(role)
{
case Qt::EditRole:
case Qt::DisplayRole:
return QVariant(longName(unit));
case Qt::ToolTipRole:
return QVariant(description(unit));
case UnitRole:
return QVariant::fromValue(unit);
}
}
return QVariant();
}
CAmount DigiByteUnits::maxMoney()
{
return MAX_MONEY;
}
namespace {
qint8 ToQint8(DigiByteUnit unit)
{
switch (unit) {
case DigiByteUnit::DGB: return 0;
case DigiByteUnit::mDGB: return 1;
case DigiByteUnit::uDGB: return 2;
case DigiByteUnit::SAT: return 3;
} // no default case, so the compiler can warn about missing cases
assert(false);
}
DigiByteUnit FromQint8(qint8 num)
{
switch (num) {
case 0: return DigiByteUnit::DGB;
case 1: return DigiByteUnit::mDGB;
case 2: return DigiByteUnit::uDGB;
case 3: return DigiByteUnit::SAT;
}
assert(false);
}
} // namespace
QDataStream& operator<<(QDataStream& out, const DigiByteUnit& unit)
{
return out << ToQint8(unit);
}
QDataStream& operator>>(QDataStream& in, DigiByteUnit& unit)
{
qint8 input;
in >> input;
unit = FromQint8(input);
return in;
}