Anoncoin  0.9.4
P2P Digital Currency
anoncoinunits.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "anoncoinunits.h"
7 
8 #include <QStringList>
9 
11  QAbstractListModel(parent),
12  unitlist(availableUnits())
13 {
14 }
15 
16 QList<AnoncoinUnits::Unit> AnoncoinUnits::availableUnits()
17 {
18  QList<AnoncoinUnits::Unit> unitlist;
19  unitlist.append(ANC);
20  unitlist.append(mANC);
21  unitlist.append(uANC);
22  return unitlist;
23 }
24 
25 bool AnoncoinUnits::valid(int unit)
26 {
27  switch(unit)
28  {
29  case ANC:
30  case mANC:
31  case uANC:
32  return true;
33  default:
34  return false;
35  }
36 }
37 
38 QString AnoncoinUnits::name(int unit)
39 {
40  switch(unit)
41  {
42  case ANC: return QString("ANC");
43  case mANC: return QString("mANC");
44  case uANC: return QString::fromUtf8("μANC");
45  default: return QString("???");
46  }
47 }
48 
49 QString AnoncoinUnits::description(int unit)
50 {
51  switch(unit)
52  {
53  case ANC: return QString("Anoncoins");
54  case mANC: return QString("Milli-Anoncoins (1 / 1,000)");
55  case uANC: return QString("Micro-Anoncoins (1 / 1,000,000)");
56  default: return QString("???");
57  }
58 }
59 
60 qint64 AnoncoinUnits::factor(int unit)
61 {
62  switch(unit)
63  {
64  case ANC: return 100000000;
65  case mANC: return 100000;
66  case uANC: return 100;
67  default: return 100000000;
68  }
69 }
70 
71 qint64 AnoncoinUnits::maxAmount(int unit)
72 {
73  switch(unit)
74  {
75  case ANC: return Q_INT64_C(21000000);
76  case mANC: return Q_INT64_C(21000000000);
77  case uANC: return Q_INT64_C(21000000000000);
78  default: return 0;
79  }
80 }
81 
83 {
84  switch(unit)
85  {
86  case ANC: return 8; // 21,000,000 (# digits, without commas)
87  case mANC: return 11; // 21,000,000,000
88  case uANC: return 14; // 21,000,000,000,000
89  default: return 0;
90  }
91 }
92 
94 {
95  switch(unit)
96  {
97  case ANC: return 8;
98  case mANC: return 5;
99  case uANC: return 2;
100  default: return 0;
101  }
102 }
103 
104 QString AnoncoinUnits::format(int unit, qint64 n, bool fPlus)
105 {
106  // Note: not using straight sprintf here because we do NOT want
107  // localized number formatting.
108  if(!valid(unit))
109  return QString(); // Refuse to format invalid unit
110  qint64 coin = factor(unit);
111  int num_decimals = decimals(unit);
112  qint64 n_abs = (n > 0 ? n : -n);
113  qint64 quotient = n_abs / coin;
114  qint64 remainder = n_abs % coin;
115  QString quotient_str = QString::number(quotient);
116  QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
117 
118  // Right-trim excess zeros after the decimal point
119  int nTrim = 0;
120  for (int i = remainder_str.size()-1; i>=2 && (remainder_str.at(i) == '0'); --i)
121  ++nTrim;
122  remainder_str.chop(nTrim);
123 
124  if (n < 0)
125  quotient_str.insert(0, '-');
126  else if (fPlus && n > 0)
127  quotient_str.insert(0, '+');
128  return quotient_str + QString(".") + remainder_str;
129 }
130 
131 QString AnoncoinUnits::formatWithUnit(int unit, qint64 amount, bool plussign)
132 {
133  return format(unit, amount, plussign) + QString(" ") + name(unit);
134 }
135 
136 bool AnoncoinUnits::parse(int unit, const QString &value, qint64 *val_out)
137 {
138  if(!valid(unit) || value.isEmpty())
139  return false; // Refuse to parse invalid unit or empty string
140  int num_decimals = decimals(unit);
141  QStringList parts = value.split(".");
142 
143  if(parts.size() > 2)
144  {
145  return false; // More than one dot
146  }
147  QString whole = parts[0];
148  QString decimals;
149 
150  if(parts.size() > 1)
151  {
152  decimals = parts[1];
153  }
154  if(decimals.size() > num_decimals)
155  {
156  return false; // Exceeds max precision
157  }
158  bool ok = false;
159  QString str = whole + decimals.leftJustified(num_decimals, '0');
160 
161  if(str.size() > 18)
162  {
163  return false; // Longer numbers will exceed 63 bits
164  }
165  qint64 retvalue = str.toLongLong(&ok);
166  if(val_out)
167  {
168  *val_out = retvalue;
169  }
170  return ok;
171 }
172 
174 {
175  QString amountTitle = QObject::tr("Amount");
176  if (AnoncoinUnits::valid(unit))
177  {
178  amountTitle += " ("+AnoncoinUnits::name(unit) + ")";
179  }
180  return amountTitle;
181 }
182 
183 int AnoncoinUnits::rowCount(const QModelIndex &parent) const
184 {
185  Q_UNUSED(parent);
186  return unitlist.size();
187 }
188 
189 QVariant AnoncoinUnits::data(const QModelIndex &index, int role) const
190 {
191  int row = index.row();
192  if(row >= 0 && row < unitlist.size())
193  {
194  Unit unit = unitlist.at(row);
195  switch(role)
196  {
197  case Qt::EditRole:
198  case Qt::DisplayRole:
199  return QVariant(name(unit));
200  case Qt::ToolTipRole:
201  return QVariant(description(unit));
202  case UnitRole:
203  return QVariant(static_cast<int>(unit));
204  }
205  }
206  return QVariant();
207 }
static QList< Unit > availableUnits()
Get list of units, for drop-down box.
static QString formatWithUnit(int unit, qint64 amount, bool plussign=false)
Format as string (with unit)
Unit
Anoncoin units.
Definition: anoncoinunits.h:25
static QString name(int unit)
Short name.
AnoncoinUnits(QObject *parent)
static bool parse(int unit, const QString &value, qint64 *val_out)
Parse string to coin amount.
QVariant data(const QModelIndex &index, int role) const
static qint64 maxAmount(int unit)
Max amount per unit.
static QString getAmountColumnTitle(int unit)
Gets title for amount column including current display unit if optionsModel reference available */...
static qint64 factor(int unit)
Number of Satoshis (1e-8) per unit.
int rowCount(const QModelIndex &parent) const
static QString format(int unit, qint64 amount, bool plussign=false)
Format as string.
static QString description(int unit)
Longer description.
static int amountDigits(int unit)
Number of amount digits (to represent max number of coins)
static bool valid(int unit)
Is unit ID valid?
Unit identifier.
Definition: anoncoinunits.h:67
QList< AnoncoinUnits::Unit > unitlist
Definition: anoncoinunits.h:74
static int decimals(int unit)
Number of decimals left.