You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

starwelsunits.cpp 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // Copyright (c) 2011-2016 The Starwels developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "starwelsunits.h"
  5. #include "primitives/transaction.h"
  6. #include <QStringList>
  7. StarwelsUnits::StarwelsUnits(QObject *parent):
  8. QAbstractListModel(parent),
  9. unitlist(availableUnits())
  10. {
  11. }
  12. QList<StarwelsUnits::Unit> StarwelsUnits::availableUnits()
  13. {
  14. QList<StarwelsUnits::Unit> unitlist;
  15. unitlist.append(MAI);
  16. unitlist.append(mMAI);
  17. unitlist.append(uMAI);
  18. return unitlist;
  19. }
  20. bool StarwelsUnits::valid(int unit)
  21. {
  22. switch(unit)
  23. {
  24. case MAI:
  25. case mMAI:
  26. case uMAI:
  27. return true;
  28. default:
  29. return false;
  30. }
  31. }
  32. QString StarwelsUnits::name(int unit)
  33. {
  34. switch(unit)
  35. {
  36. case MAI: return QString("MAI");
  37. case mMAI: return QString("mMAI");
  38. case uMAI: return QString::fromUtf8("μMAI");
  39. default: return QString("???");
  40. }
  41. }
  42. QString StarwelsUnits::description(int unit)
  43. {
  44. switch(unit)
  45. {
  46. case MAI: return QString("Starwelss");
  47. case mMAI: return QString("Milli-Starwelss (1 / 1" THIN_SP_UTF8 "000)");
  48. case uMAI: return QString("Micro-Starwelss (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
  49. default: return QString("???");
  50. }
  51. }
  52. qint64 StarwelsUnits::factor(int unit)
  53. {
  54. switch(unit)
  55. {
  56. case MAI: return 100000000;
  57. case mMAI: return 100000;
  58. case uMAI: return 100;
  59. default: return 100000000;
  60. }
  61. }
  62. int StarwelsUnits::decimals(int unit)
  63. {
  64. switch(unit)
  65. {
  66. case MAI: return 8;
  67. case mMAI: return 5;
  68. case uMAI: return 2;
  69. default: return 0;
  70. }
  71. }
  72. QString StarwelsUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators)
  73. {
  74. // Note: not using straight sprintf here because we do NOT want
  75. // localized number formatting.
  76. if(!valid(unit))
  77. return QString(); // Refuse to format invalid unit
  78. qint64 n = (qint64)nIn;
  79. qint64 coin = factor(unit);
  80. int num_decimals = decimals(unit);
  81. qint64 n_abs = (n > 0 ? n : -n);
  82. qint64 quotient = n_abs / coin;
  83. qint64 remainder = n_abs % coin;
  84. QString quotient_str = QString::number(quotient);
  85. QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
  86. // Use SI-style thin space separators as these are locale independent and can't be
  87. // confused with the decimal marker.
  88. QChar thin_sp(THIN_SP_CP);
  89. int q_size = quotient_str.size();
  90. if (separators == separatorAlways || (separators == separatorStandard && q_size > 4))
  91. for (int i = 3; i < q_size; i += 3)
  92. quotient_str.insert(q_size - i, thin_sp);
  93. if (n < 0)
  94. quotient_str.insert(0, '-');
  95. else if (fPlus && n > 0)
  96. quotient_str.insert(0, '+');
  97. return quotient_str + QString(".") + remainder_str;
  98. }
  99. // NOTE: Using formatWithUnit in an HTML context risks wrapping
  100. // quantities at the thousands separator. More subtly, it also results
  101. // in a standard space rather than a thin space, due to a bug in Qt's
  102. // XML whitespace canonicalisation
  103. //
  104. // Please take care to use formatHtmlWithUnit instead, when
  105. // appropriate.
  106. QString StarwelsUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
  107. {
  108. return format(unit, amount, plussign, separators) + QString(" ") + name(unit);
  109. }
  110. QString StarwelsUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
  111. {
  112. QString str(formatWithUnit(unit, amount, plussign, separators));
  113. str.replace(QChar(THIN_SP_CP), QString(THIN_SP_HTML));
  114. return QString("<span style='white-space: nowrap;'>%1</span>").arg(str);
  115. }
  116. bool StarwelsUnits::parse(int unit, const QString &value, CAmount *val_out)
  117. {
  118. if(!valid(unit) || value.isEmpty())
  119. return false; // Refuse to parse invalid unit or empty string
  120. int num_decimals = decimals(unit);
  121. // Ignore spaces and thin spaces when parsing
  122. QStringList parts = removeSpaces(value).split(".");
  123. if(parts.size() > 2)
  124. {
  125. return false; // More than one dot
  126. }
  127. QString whole = parts[0];
  128. QString decimals;
  129. if(parts.size() > 1)
  130. {
  131. decimals = parts[1];
  132. }
  133. if(decimals.size() > num_decimals)
  134. {
  135. return false; // Exceeds max precision
  136. }
  137. bool ok = false;
  138. QString str = whole + decimals.leftJustified(num_decimals, '0');
  139. if(str.size() > 18)
  140. {
  141. return false; // Longer numbers will exceed 63 bits
  142. }
  143. CAmount retvalue(str.toLongLong(&ok));
  144. if(val_out)
  145. {
  146. *val_out = retvalue;
  147. }
  148. return ok;
  149. }
  150. QString StarwelsUnits::getAmountColumnTitle(int unit)
  151. {
  152. QString amountTitle = QObject::tr("Amount");
  153. if (StarwelsUnits::valid(unit))
  154. {
  155. amountTitle += " ("+StarwelsUnits::name(unit) + ")";
  156. }
  157. return amountTitle;
  158. }
  159. int StarwelsUnits::rowCount(const QModelIndex &parent) const
  160. {
  161. Q_UNUSED(parent);
  162. return unitlist.size();
  163. }
  164. QVariant StarwelsUnits::data(const QModelIndex &index, int role) const
  165. {
  166. int row = index.row();
  167. if(row >= 0 && row < unitlist.size())
  168. {
  169. Unit unit = unitlist.at(row);
  170. switch(role)
  171. {
  172. case Qt::EditRole:
  173. case Qt::DisplayRole:
  174. return QVariant(name(unit));
  175. case Qt::ToolTipRole:
  176. return QVariant(description(unit));
  177. case UnitRole:
  178. return QVariant(static_cast<int>(unit));
  179. }
  180. }
  181. return QVariant();
  182. }
  183. CAmount StarwelsUnits::maxMoney()
  184. {
  185. return MAX_MONEY;
  186. }