2015-12-13 17:58:29 +01:00
|
|
|
// Copyright (c) 2011-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2013-11-04 16:20:43 +01:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2011-07-25 21:35:45 +02:00
|
|
|
#include "bitcoinunits.h"
|
|
|
|
|
2014-11-18 22:03:02 +01:00
|
|
|
#include "primitives/transaction.h"
|
2014-07-18 16:31:13 +02:00
|
|
|
|
2011-07-25 21:35:45 +02:00
|
|
|
#include <QStringList>
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
BitcoinUnits::BitcoinUnits(QObject *parent):
|
|
|
|
QAbstractListModel(parent),
|
|
|
|
unitlist(availableUnits())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<BitcoinUnits::Unit> BitcoinUnits::availableUnits()
|
|
|
|
{
|
|
|
|
QList<BitcoinUnits::Unit> unitlist;
|
|
|
|
unitlist.append(BTC);
|
|
|
|
unitlist.append(mBTC);
|
|
|
|
unitlist.append(uBTC);
|
|
|
|
return unitlist;
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:11:28 +02:00
|
|
|
bool BitcoinUnits::valid(int unit)
|
|
|
|
{
|
|
|
|
switch(unit)
|
|
|
|
{
|
|
|
|
case BTC:
|
|
|
|
case mBTC:
|
|
|
|
case uBTC:
|
|
|
|
return true;
|
|
|
|
default:
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
QString BitcoinUnits::name(int unit)
|
2011-07-25 21:35:45 +02:00
|
|
|
{
|
|
|
|
switch(unit)
|
|
|
|
{
|
2011-07-25 22:28:12 +02:00
|
|
|
case BTC: return QString("BTC");
|
|
|
|
case mBTC: return QString("mBTC");
|
|
|
|
case uBTC: return QString::fromUtf8("μBTC");
|
|
|
|
default: return QString("???");
|
2011-07-25 21:35:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
QString BitcoinUnits::description(int unit)
|
2011-07-25 21:35:45 +02:00
|
|
|
{
|
|
|
|
switch(unit)
|
|
|
|
{
|
2011-07-26 13:08:34 +02:00
|
|
|
case BTC: return QString("Bitcoins");
|
2014-05-10 00:50:09 +02:00
|
|
|
case mBTC: return QString("Milli-Bitcoins (1 / 1" THIN_SP_UTF8 "000)");
|
|
|
|
case uBTC: return QString("Micro-Bitcoins (1 / 1" THIN_SP_UTF8 "000" THIN_SP_UTF8 "000)");
|
2011-07-25 22:28:12 +02:00
|
|
|
default: return QString("???");
|
2011-07-25 21:35:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
qint64 BitcoinUnits::factor(int unit)
|
2011-07-25 21:35:45 +02:00
|
|
|
{
|
|
|
|
switch(unit)
|
|
|
|
{
|
|
|
|
case BTC: return 100000000;
|
|
|
|
case mBTC: return 100000;
|
|
|
|
case uBTC: return 100;
|
|
|
|
default: return 100000000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
int BitcoinUnits::decimals(int unit)
|
2011-07-25 21:35:45 +02:00
|
|
|
{
|
|
|
|
switch(unit)
|
|
|
|
{
|
|
|
|
case BTC: return 8;
|
|
|
|
case mBTC: return 5;
|
|
|
|
case uBTC: return 2;
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
QString BitcoinUnits::format(int unit, const CAmount& nIn, bool fPlus, SeparatorStyle separators)
|
2011-07-25 21:35:45 +02:00
|
|
|
{
|
|
|
|
// Note: not using straight sprintf here because we do NOT want
|
|
|
|
// localized number formatting.
|
2011-07-26 13:11:28 +02:00
|
|
|
if(!valid(unit))
|
|
|
|
return QString(); // Refuse to format invalid unit
|
2014-04-23 00:46:19 +02:00
|
|
|
qint64 n = (qint64)nIn;
|
2011-07-25 21:35:45 +02:00
|
|
|
qint64 coin = factor(unit);
|
|
|
|
int num_decimals = decimals(unit);
|
|
|
|
qint64 n_abs = (n > 0 ? n : -n);
|
|
|
|
qint64 quotient = n_abs / coin;
|
|
|
|
qint64 remainder = n_abs % coin;
|
|
|
|
QString quotient_str = QString::number(quotient);
|
|
|
|
QString remainder_str = QString::number(remainder).rightJustified(num_decimals, '0');
|
|
|
|
|
2014-11-28 06:13:44 +01:00
|
|
|
// Use SI-style thin space separators as these are locale independent and can't be
|
|
|
|
// confused with the decimal marker.
|
2014-05-10 00:50:09 +02:00
|
|
|
QChar thin_sp(THIN_SP_CP);
|
|
|
|
int q_size = quotient_str.size();
|
|
|
|
if (separators == separatorAlways || (separators == separatorStandard && q_size > 4))
|
|
|
|
for (int i = 3; i < q_size; i += 3)
|
|
|
|
quotient_str.insert(q_size - i, thin_sp);
|
|
|
|
|
2011-07-25 21:35:45 +02:00
|
|
|
if (n < 0)
|
|
|
|
quotient_str.insert(0, '-');
|
|
|
|
else if (fPlus && n > 0)
|
|
|
|
quotient_str.insert(0, '+');
|
|
|
|
return quotient_str + QString(".") + remainder_str;
|
|
|
|
}
|
|
|
|
|
2014-07-07 23:27:09 +02:00
|
|
|
|
2014-07-07 23:28:11 +02:00
|
|
|
// 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.
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
QString BitcoinUnits::formatWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
|
2011-07-25 21:35:45 +02:00
|
|
|
{
|
2014-07-07 23:27:09 +02:00
|
|
|
return format(unit, amount, plussign, separators) + QString(" ") + name(unit);
|
2011-07-25 21:35:45 +02:00
|
|
|
}
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
QString BitcoinUnits::formatHtmlWithUnit(int unit, const CAmount& amount, bool plussign, SeparatorStyle separators)
|
2014-05-10 00:50:09 +02:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
bool BitcoinUnits::parse(int unit, const QString &value, CAmount *val_out)
|
2011-07-25 21:35:45 +02:00
|
|
|
{
|
2011-07-27 20:49:14 +02:00
|
|
|
if(!valid(unit) || value.isEmpty())
|
|
|
|
return false; // Refuse to parse invalid unit or empty string
|
2011-07-25 21:35:45 +02:00
|
|
|
int num_decimals = decimals(unit);
|
2014-05-10 00:50:09 +02:00
|
|
|
|
|
|
|
// Ignore spaces and thin spaces when parsing
|
|
|
|
QStringList parts = removeSpaces(value).split(".");
|
2011-07-27 20:49:14 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2011-07-25 21:35:45 +02:00
|
|
|
bool ok = false;
|
2011-07-27 20:49:14 +02:00
|
|
|
QString str = whole + decimals.leftJustified(num_decimals, '0');
|
2011-07-25 21:35:45 +02:00
|
|
|
|
2011-07-27 20:49:14 +02:00
|
|
|
if(str.size() > 18)
|
|
|
|
{
|
|
|
|
return false; // Longer numbers will exceed 63 bits
|
|
|
|
}
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount retvalue(str.toLongLong(&ok));
|
2011-07-25 21:35:45 +02:00
|
|
|
if(val_out)
|
|
|
|
{
|
|
|
|
*val_out = retvalue;
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
2011-07-26 13:08:34 +02:00
|
|
|
|
2014-06-07 08:20:22 +02:00
|
|
|
QString BitcoinUnits::getAmountColumnTitle(int unit)
|
|
|
|
{
|
|
|
|
QString amountTitle = QObject::tr("Amount");
|
|
|
|
if (BitcoinUnits::valid(unit))
|
|
|
|
{
|
|
|
|
amountTitle += " ("+BitcoinUnits::name(unit) + ")";
|
|
|
|
}
|
|
|
|
return amountTitle;
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
int BitcoinUnits::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
Q_UNUSED(parent);
|
|
|
|
return unitlist.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant BitcoinUnits::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(name(unit));
|
|
|
|
case Qt::ToolTipRole:
|
|
|
|
return QVariant(description(unit));
|
|
|
|
case UnitRole:
|
|
|
|
return QVariant(static_cast<int>(unit));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
2014-07-18 16:31:13 +02:00
|
|
|
|
2014-04-23 00:46:19 +02:00
|
|
|
CAmount BitcoinUnits::maxMoney()
|
2014-07-18 16:31:13 +02:00
|
|
|
{
|
|
|
|
return MAX_MONEY;
|
|
|
|
}
|