Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

bitcoinunits.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2015 The Bitcoin Core developers
2 // Copyright (c) 2014-2019 The Dash Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include <qt/bitcoinunits.h>
7 #include <chainparams.h>
9 
10 #include <QSettings>
11 #include <QStringList>
12 
13 BitcoinUnits::BitcoinUnits(QObject *parent):
14  QAbstractListModel(parent),
15  unitlist(availableUnits())
16 {
17 }
18 
19 QList<BitcoinUnits::Unit> BitcoinUnits::availableUnits()
20 {
21  QList<BitcoinUnits::Unit> unitlist;
22  unitlist.append(DASH);
23  unitlist.append(mDASH);
24  unitlist.append(uDASH);
25  unitlist.append(duffs);
26  return unitlist;
27 }
28 
29 bool BitcoinUnits::valid(int unit)
30 {
31  switch(unit)
32  {
33  case DASH:
34  case mDASH:
35  case uDASH:
36  case duffs:
37  return true;
38  default:
39  return false;
40  }
41 }
42 
43 QString BitcoinUnits::name(int unit)
44 {
45  if(Params().NetworkIDString() == CBaseChainParams::MAIN)
46  {
47  switch(unit)
48  {
49  case DASH: return QString("DASH");
50  case mDASH: return QString("mDASH");
51  case uDASH: return QString::fromUtf8("μDASH");
52  case duffs: return QString("duffs");
53  default: return QString("???");
54  }
55  }
56  else
57  {
58  switch(unit)
59  {
60  case DASH: return QString("tDASH");
61  case mDASH: return QString("mtDASH");
62  case uDASH: return QString::fromUtf8("μtDASH");
63  case duffs: return QString("tduffs");
64  default: return QString("???");
65  }
66  }
67 }
68 
69 QString BitcoinUnits::description(int unit)
70 {
71  if(Params().NetworkIDString() == CBaseChainParams::MAIN)
72  {
73  switch(unit)
74  {
75  case DASH: return QString("Dash");
76  case mDASH: return QString("Milli-Dash (1 / 1" THIN_SP_UTF8 "000)");
77  case uDASH: return QString("Micro-Dash (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
78  case duffs: return QString("Ten Nano-Dash (1 / 100" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
79  default: return QString("???");
80  }
81  }
82  else
83  {
84  switch(unit)
85  {
86  case DASH: return QString("TestDashs");
87  case mDASH: return QString("Milli-TestDash (1 / 1" THIN_SP_UTF8 "000)");
88  case uDASH: return QString("Micro-TestDash (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
89  case duffs: return QString("Ten Nano-TestDash (1 / 100" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
90  default: return QString("???");
91  }
92  }
93 }
94 
95 qint64 BitcoinUnits::factor(int unit)
96 {
97  switch(unit)
98  {
99  case DASH: return 100000000;
100  case mDASH: return 100000;
101  case uDASH: return 100;
102  case duffs: return 1;
103  default: return 100000000;
104  }
105 }
106 
108 {
109  switch(unit)
110  {
111  case DASH: return 8;
112  case mDASH: return 5;
113  case uDASH: return 2;
114  case duffs: return 0;
115  default: return 0;
116  }
117 }
118 
119 QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators)
120 {
121  // Note: not using straight sprintf here because we do NOT want
122  // localized number formatting.
123  if(!valid(unit))
124  return QString(); // Refuse to format invalid unit
125  qint64 n = (qint64)nIn;
126  qint64 coin = factor(unit);
127  int num_decimals = decimals(unit);
128  qint64 n_abs = (n > 0 ? n : -n);
129  qint64 quotient = n_abs / coin;
130  qint64 remainder = n_abs % coin;
131  QString quotient_str = QString::number(quotient);
132  QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
133 
134  // Use SI-style thin space separators as these are locale independent and can't be
135  // confused with the decimal marker.
136  QChar thin_sp(THIN_SP_CP);
137  int q_size = quotient_str.size();
138  if (separators == separatorAlways || (separators == separatorStandard && q_size > 4))
139  for (int i = 3; i < q_size; i += 3)
140  quotient_str.insert(q_size - i, thin_sp);
141 
142  if (n < 0)
143  quotient_str.insert(0, '-');
144  else if (fPlus && n > 0)
145  quotient_str.insert(0, '+');
146 
147  if (num_decimals <= 0)
148  return quotient_str;
149 
150  return quotient_str + QString(".") + remainder_str;
151 }
152 
153 
154 // NOTE: Using formatWithUnit in an HTML context risks wrapping
155 // quantities at the thousands separator. More subtly, it also results
156 // in a standard space rather than a thin space, due to a bug in Qt's
157 // XML whitespace canonicalisation
158 //
159 // Please take care to use formatHtmlWithUnit instead, when
160 // appropriate.
161 
162 QString BitcoinUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
163 {
164  return format(unit, amount, plussign, separators) + QString(" ") + name(unit);
165 }
166 
167 QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
168 {
169  QString str(formatWithUnit(unit, amount, plussign, separators));
170  str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
171  return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
172 }
173 
174 QString BitcoinUnits::floorWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
175 {
176  QSettings settings;
177  int digits = settings.value("digits").toInt();
178 
179  QString result = format(unit, amount, plussign, separators);
180  if(decimals(unit) > digits) result.chop(decimals(unit) - digits);
181 
182  return result + QString(" ") + name(unit);
183 }
184 
185 QString BitcoinUnits::floorHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
186 {
187  QString str(floorWithUnit(unit, amount, plussign, separators));
188  str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
189  return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
190 }
191 
192 bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out)
193 {
194  if(!valid(unit) || value.isEmpty())
195  return false; // Refuse to parse invalid unit or empty string
196  int num_decimals = decimals(unit);
197 
198  // Ignore spaces and thin spaces when parsing
199  QStringList parts = removeSpaces(value).split(".");
200 
201  if(parts.size() > 2)
202  {
203  return false; // More than one dot
204  }
205  QString whole = parts[0];
206  QString decimals;
207 
208  if(parts.size() > 1)
209  {
210  decimals = parts[1];
211  }
212  if(decimals.size() > num_decimals)
213  {
214  return false; // Exceeds max precision
215  }
216  bool ok = false;
217  QString str = whole + decimals.leftJustified(num_decimals, '0');
218 
219  if(str.size() > 18)
220  {
221  return false; // Longer numbers will exceed 63 bits
222  }
223  CAmount retvalue(str.toLongLong(&ok));
224  if(val_out)
225  {
226  *val_out = retvalue;
227  }
228  return ok;
229 }
230 
232 {
233  QString amountTitle = QObject::tr("Amount");
234  if (BitcoinUnits::valid(unit))
235  {
236  amountTitle += " ("+BitcoinUnits::name(unit) + ")";
237  }
238  return amountTitle;
239 }
240 
241 int BitcoinUnits::rowCount(const QModelIndex &parent) const
242 {
243  Q_UNUSED(parent);
244  return unitlist.size();
245 }
246 
247 QVariant BitcoinUnits::data(const QModelIndex &index, int role) const
248 {
249  return data(index.row(), role);
250 }
251 
252 QVariant BitcoinUnits::data(const int &row, int role) const
253 {
254  if(row >= 0 && row < unitlist.size())
255  {
256  Unit unit = unitlist.at(row);
257  switch(role)
258  {
259  case Qt::EditRole:
260  case Qt::DisplayRole:
261  return QVariant(name(unit));
262  case Qt::ToolTipRole:
263  return QVariant(description(unit));
264  case UnitRole:
265  return QVariant(static_cast<int>(unit));
266  }
267  }
268  return QVariant();
269 }
270 
272 {
273  return MAX_MONEY;
274 }
QList< BitcoinUnits::Unit > unitlist
Definition: bitcoinunits.h:131
Unit
Dash units.
Definition: bitcoinunits.h:58
static const CAmount MAX_MONEY
No amount larger than this (in satoshi) is valid.
Definition: amount.h:26
static QString formatHtmlWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as HTML string (with unit)
static bool valid(int unit)
Is unit ID valid?
#define THIN_SP_HTML
Definition: bitcoinunits.h:43
static bool parse(int unit, const QString &value, CAmount *val_out)
Parse string to coin amount.
static QString getAmountColumnTitle(int unit)
Gets title for amount column including current display unit if optionsModel reference available */...
static const std::string MAIN
BIP70 chain name strings (main, test or regtest)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
BitcoinUnits(QObject *parent)
static QString description(int unit)
Longer description.
#define THIN_SP_UTF8
Definition: bitcoinunits.h:42
int rowCount(const QModelIndex &parent) const
static qint64 factor(int unit)
Number of Satoshis (1e-8) per unit.
Unit identifier.
Definition: bitcoinunits.h:110
static QString name(int unit)
Short name.
static QList< Unit > availableUnits()
Get list of units, for drop-down box.
static QString floorWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string (with unit) but floor value up to "digits" settings.
const CChainParams & Params()
Return the currently selected parameters.
QVariant data(const QModelIndex &index, int role) const
static QString removeSpaces(QString text)
Definition: bitcoinunits.h:117
static QString floorHtmlWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
static QString formatWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string (with unit)
#define THIN_SP_CP
Definition: bitcoinunits.h:41
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string.
static CAmount maxMoney()
Return maximum number of base units (Satoshis)
static int decimals(int unit)
Number of decimals left.
Released under the MIT license