2013-11-04 16:20:43 +01:00
|
|
|
// Copyright (c) 2011-2013 The Bitcoin developers
|
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2011-07-25 21:35:45 +02:00
|
|
|
#include "bitcoinunits.h"
|
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-05 17:14:43 +02:00
|
|
|
QString BitcoinUnits::id(int unit)
|
|
|
|
{
|
|
|
|
switch(unit)
|
|
|
|
{
|
|
|
|
case BTC: return QString("btc");
|
|
|
|
case mBTC: return QString("mbtc");
|
|
|
|
case uBTC: return QString("ubtc");
|
|
|
|
default: return QString("???");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-22 00:19:18 +02:00
|
|
|
qint64 BitcoinUnits::maxAmount(int unit)
|
|
|
|
{
|
|
|
|
switch(unit)
|
|
|
|
{
|
|
|
|
case BTC: return Q_INT64_C(21000000);
|
|
|
|
case mBTC: return Q_INT64_C(21000000000);
|
|
|
|
case uBTC: return Q_INT64_C(21000000000000);
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
int BitcoinUnits::amountDigits(int unit)
|
|
|
|
{
|
|
|
|
switch(unit)
|
|
|
|
{
|
2011-07-26 13:11:28 +02:00
|
|
|
case BTC: return 8; // 21,000,000 (# digits, without commas)
|
2011-07-26 13:08:34 +02:00
|
|
|
case mBTC: return 11; // 21,000,000,000
|
|
|
|
case uBTC: return 14; // 21,000,000,000,000
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-10 00:50:09 +02:00
|
|
|
QString BitcoinUnits::format(int unit, qint64 n, bool fPlus, SeparatorStyle separators, bool fAlign)
|
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
|
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-05-10 00:50:09 +02:00
|
|
|
// Use SI-stule separators as these are locale indendent and can't be
|
|
|
|
// confused with the decimal marker. Rule is to use a thin space every
|
|
|
|
// three digits on *both* sides of the decimal point - but only if there
|
|
|
|
// are five or more digits
|
|
|
|
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);
|
|
|
|
|
|
|
|
int r_size = remainder_str.size();
|
|
|
|
if (separators == separatorAlways || (separators == separatorStandard && r_size > 4))
|
|
|
|
for (int i = 3, adj = 0; i < r_size ; i += 3, adj++)
|
|
|
|
remainder_str.insert(i + adj, 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-05-10 00:50:09 +02:00
|
|
|
QString BitcoinUnits::formatWithUnit(int unit, qint64 amount, bool plussign, SeparatorStyle separators, bool fAlign)
|
2011-07-25 21:35:45 +02:00
|
|
|
{
|
2014-05-10 00:50:09 +02:00
|
|
|
return format(unit, amount, plussign, separators, fAlign) + QString(" ") + name(unit);
|
2011-07-25 21:35:45 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 00:50:09 +02:00
|
|
|
QString BitcoinUnits::formatHtmlWithUnit(int unit, qint64 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-07-26 13:08:34 +02:00
|
|
|
bool BitcoinUnits::parse(int unit, const QString &value, qint64 *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
|
|
|
|
}
|
2011-07-25 21:35:45 +02:00
|
|
|
qint64 retvalue = str.toLongLong(&ok);
|
|
|
|
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();
|
|
|
|
}
|