Browse Source

preparations for multiple unit (uBTC, mBTC, BTC) support, fix amount entry issue

miguelfreitas
Wladimir J. van der Laan 14 years ago
parent
commit
e285ffcd05
  1. 6
      bitcoin-qt.pro
  2. 12
      src/qt/bitcoinamountfield.cpp
  3. 7
      src/qt/bitcoingui.cpp
  4. 96
      src/qt/bitcoinunits.cpp
  5. 34
      src/qt/bitcoinunits.h
  6. 10
      src/qt/guiutil.cpp
  7. 6
      src/qt/guiutil.h
  8. 64
      src/qt/optionsdialog.cpp
  9. 9
      src/qt/optionsdialog.h
  10. 6
      src/qt/overviewpage.cpp
  11. 6
      src/qt/sendcoinsdialog.cpp
  12. 3
      src/qt/sendcoinsentry.cpp
  13. 3
      src/qt/transactiontablemodel.cpp
  14. 4
      src/qt/transactionview.cpp

6
bitcoin-qt.pro

@ -87,7 +87,8 @@ HEADERS += src/qt/bitcoingui.h \ @@ -87,7 +87,8 @@ HEADERS += src/qt/bitcoingui.h \
src/qt/qtwin.h \
src/crypter.h \
src/qt/sendcoinsentry.h \
src/qt/qvalidatedlineedit.h
src/qt/qvalidatedlineedit.h \
src/qt/bitcoinunits.h
SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
src/qt/transactiontablemodel.cpp \
src/qt/addresstablemodel.cpp \
@ -129,7 +130,8 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \ @@ -129,7 +130,8 @@ SOURCES += src/qt/bitcoin.cpp src/qt/bitcoingui.cpp \
src/qt/qtwin.cpp \
src/crypter.cpp \
src/qt/sendcoinsentry.cpp \
src/qt/qvalidatedlineedit.cpp
src/qt/qvalidatedlineedit.cpp \
src/qt/bitcoinunits.cpp
RESOURCES += \
src/qt/bitcoin.qrc

12
src/qt/bitcoinamountfield.cpp

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
#include "bitcoinamountfield.h"
#include "qvalidatedlineedit.h"
#include "bitcoinunits.h"
#include <QLabel>
#include <QLineEdit>
@ -11,7 +12,7 @@ BitcoinAmountField::BitcoinAmountField(QWidget *parent): @@ -11,7 +12,7 @@ BitcoinAmountField::BitcoinAmountField(QWidget *parent):
QWidget(parent), amount(0), decimals(0)
{
amount = new QValidatedLineEdit(this);
amount->setValidator(new QRegExpValidator(QRegExp("[0-9]?"), this));
amount->setValidator(new QRegExpValidator(QRegExp("[0-9]*"), this));
amount->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
amount->installEventFilter(this);
amount->setMaximumWidth(100);
@ -26,7 +27,7 @@ BitcoinAmountField::BitcoinAmountField(QWidget *parent): @@ -26,7 +27,7 @@ BitcoinAmountField::BitcoinAmountField(QWidget *parent):
layout->addWidget(amount);
layout->addWidget(new QLabel(QString(".")));
layout->addWidget(decimals);
layout->addWidget(new QLabel(QString(" BTC")));
layout->addWidget(new QLabel(QString(" ") + BitcoinUnits::name(BitcoinUnits::BTC)));
layout->addStretch(1);
layout->setContentsMargins(0,0,0,0);
@ -69,6 +70,13 @@ bool BitcoinAmountField::validate() @@ -69,6 +70,13 @@ bool BitcoinAmountField::validate()
decimals->setValid(false);
valid = false;
}
if(!BitcoinUnits::parse(BitcoinUnits::BTC, text(), 0))
{
amount->setValid(false);
decimals->setValid(false);
valid = false;
}
return valid;
}

7
src/qt/bitcoingui.cpp

@ -11,13 +11,13 @@ @@ -11,13 +11,13 @@
#include "aboutdialog.h"
#include "clientmodel.h"
#include "walletmodel.h"
#include "guiutil.h"
#include "editaddressdialog.h"
#include "optionsmodel.h"
#include "transactiondescdialog.h"
#include "addresstablemodel.h"
#include "transactionview.h"
#include "overviewpage.h"
#include "bitcoinunits.h"
#include <QApplication>
#include <QMainWindow>
@ -436,7 +436,8 @@ void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee) @@ -436,7 +436,8 @@ void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
QString strMessage =
tr("This transaction is over the size limit. You can still send it for a fee of %1, "
"which goes to the nodes that process your transaction and helps to support the network. "
"Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
"Do you want to pay the fee?").arg(
BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
QMessageBox::StandardButton retval = QMessageBox::question(
this, tr("Sending..."), strMessage,
QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
@ -462,7 +463,7 @@ void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int @@ -462,7 +463,7 @@ void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int
trayIcon->showMessage((amount)<0 ? tr("Sent transaction") :
tr("Incoming transaction"),
tr("Date: ") + date + "\n" +
tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
tr("Amount: ") + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, amount, true) + "\n" +
tr("Type: ") + type + "\n" +
tr("Address: ") + address + "\n",
QSystemTrayIcon::Information);

96
src/qt/bitcoinunits.cpp

@ -0,0 +1,96 @@ @@ -0,0 +1,96 @@
#include "bitcoinunits.h"
#include <QStringList>
QString BitcoinUnits::name(BitcoinUnits::Unit unit)
{
switch(unit)
{
case BTC: return "BTC";
case mBTC: return "mBTC";
case uBTC: return "uBTC";
default: return "???";
}
}
QString BitcoinUnits::description(BitcoinUnits::Unit unit)
{
switch(unit)
{
case BTC: return "Bitcoin";
case mBTC: return "Milli-bitcoin";
case uBTC: return "Micro-bitcoin";
default: return "???";
}
}
qint64 BitcoinUnits::factor(BitcoinUnits::Unit unit)
{
switch(unit)
{
case BTC: return 100000000;
case mBTC: return 100000;
case uBTC: return 100;
default: return 100000000;
}
}
int BitcoinUnits::decimals(BitcoinUnits::Unit unit)
{
switch(unit)
{
case BTC: return 8;
case mBTC: return 5;
case uBTC: return 2;
default: return 0;
}
}
QString BitcoinUnits::format(BitcoinUnits::Unit unit, qint64 n, bool fPlus)
{
// Note: not using straight sprintf here because we do NOT want
// localized number formatting.
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');
// Right-trim excess 0's after the decimal point
int nTrim = 0;
for (int i = remainder_str.size()-1; i>=2 && (remainder_str.at(i) == '0'); --i)
++nTrim;
remainder_str.chop(nTrim);
if (n < 0)
quotient_str.insert(0, '-');
else if (fPlus && n > 0)
quotient_str.insert(0, '+');
return quotient_str + QString(".") + remainder_str;
}
QString BitcoinUnits::formatWithUnit(BitcoinUnits::Unit unit, qint64 amount, bool plussign)
{
return format(unit, amount, plussign) + QString(" ") + name(unit);
}
bool BitcoinUnits::parse(BitcoinUnits::Unit unit, const QString &value, qint64 *val_out)
{
int num_decimals = decimals(unit);
QStringList parts = value.split(".");
if(parts.size() != 2 || parts.at(1).size() > num_decimals)
return false; // Max num decimals
bool ok = false;
QString str = parts[0] + parts[1].leftJustified(num_decimals, '0');
if(str.size()>18)
return false; // Bounds check
qint64 retvalue = str.toLongLong(&ok);
if(val_out)
{
*val_out = retvalue;
}
return ok;
}

34
src/qt/bitcoinunits.h

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
#ifndef BITCOINUNITS_H
#define BITCOINUNITS_H
#include <QString>
// Bitcoin unit definitions
class BitcoinUnits
{
public:
enum Unit
{
BTC,
mBTC,
uBTC
};
// Short name
static QString name(Unit unit);
// Longer description
static QString description(Unit unit);
// Number of satoshis / unit
static qint64 factor(Unit unit);
// Number of decimals left
static int decimals(Unit unit);
// Format as string
static QString format(Unit unit, qint64 amount, bool plussign=false);
// Format as string (with unit)
static QString formatWithUnit(Unit unit, qint64 amount, bool plussign=false);
// Parse string to coin amount
static bool parse(Unit unit, const QString &value, qint64 *val_out);
};
#endif // BITCOINUNITS_H

10
src/qt/guiutil.cpp

@ -37,13 +37,3 @@ void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent) @@ -37,13 +37,3 @@ void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent)
widget->setValidator(amountValidator);
widget->setAlignment(Qt::AlignRight|Qt::AlignVCenter);
}
bool GUIUtil::parseMoney(const QString &amount, qint64 *val_out)
{
return ParseMoney(amount.toStdString(), *val_out);
}
QString GUIUtil::formatMoney(qint64 amount, bool plussign)
{
return QString::fromStdString(FormatMoney(amount, plussign));
}

6
src/qt/guiutil.h

@ -20,12 +20,6 @@ public: @@ -20,12 +20,6 @@ public:
static void setupAddressWidget(QLineEdit *widget, QWidget *parent);
static void setupAmountWidget(QLineEdit *widget, QWidget *parent);
// Convenience wrapper around ParseMoney that takes QString
static bool parseMoney(const QString &amount, qint64 *val_out);
// Convenience wrapper around FormatMoney that returns QString
static QString formatMoney(qint64 amount, bool plussign=false);
};
#endif // GUIUTIL_H

64
src/qt/optionsdialog.cpp

@ -17,8 +17,9 @@ @@ -17,8 +17,9 @@
#include <QDoubleValidator>
#include <QRegExpValidator>
#include <QDialogButtonBox>
#include <QDebug>
/* First (currently only) page of options */
/* First page of options */
class MainOptionsPage : public QWidget
{
public:
@ -41,9 +42,23 @@ public slots: @@ -41,9 +42,23 @@ public slots:
};
class DisplayOptionsPage : public QWidget
{
public:
explicit DisplayOptionsPage(QWidget *parent=0);
void setMapper(MonitoredDataMapper *mapper);
private:
QLineEdit *unit;
signals:
public slots:
};
OptionsDialog::OptionsDialog(QWidget *parent):
QDialog(parent), contents_widget(0), pages_widget(0),
main_options_page(0), model(0)
model(0), main_page(0), display_page(0)
{
contents_widget = new QListWidget();
contents_widget->setMaximumWidth(128);
@ -53,8 +68,13 @@ OptionsDialog::OptionsDialog(QWidget *parent): @@ -53,8 +68,13 @@ OptionsDialog::OptionsDialog(QWidget *parent):
QListWidgetItem *item_main = new QListWidgetItem(tr("Main"));
contents_widget->addItem(item_main);
main_options_page = new MainOptionsPage(this);
pages_widget->addWidget(main_options_page);
main_page = new MainOptionsPage(this);
pages_widget->addWidget(main_page);
QListWidgetItem *item_display = new QListWidgetItem(tr("Display"));
//contents_widget->addItem(item_display);
display_page = new DisplayOptionsPage(this);
pages_widget->addWidget(display_page);
contents_widget->setCurrentRow(0);
@ -83,6 +103,8 @@ OptionsDialog::OptionsDialog(QWidget *parent): @@ -83,6 +103,8 @@ OptionsDialog::OptionsDialog(QWidget *parent):
connect(mapper, SIGNAL(currentIndexChanged(int)), this, SLOT(disableApply()));
/* Event bindings */
qDebug() << "setup";
connect(contents_widget, SIGNAL(currentRowChanged(int)), this, SLOT(changePage(int)));
connect(buttonbox->button(QDialogButtonBox::Ok), SIGNAL(clicked()), this, SLOT(okClicked()));
connect(buttonbox->button(QDialogButtonBox::Cancel), SIGNAL(clicked()), this, SLOT(cancelClicked()));
connect(buttonbox->button(QDialogButtonBox::Apply), SIGNAL(clicked()), this, SLOT(applyClicked()));
@ -93,18 +115,16 @@ void OptionsDialog::setModel(OptionsModel *model) @@ -93,18 +115,16 @@ void OptionsDialog::setModel(OptionsModel *model)
this->model = model;
mapper->setModel(model);
main_options_page->setMapper(mapper);
main_page->setMapper(mapper);
display_page->setMapper(mapper);
mapper->toFirst();
}
void OptionsDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
void OptionsDialog::changePage(int index)
{
Q_UNUSED(previous);
if(current)
{
pages_widget->setCurrentIndex(contents_widget->row(current));
}
qDebug() << "page" << index;
pages_widget->setCurrentIndex(index);
}
void OptionsDialog::okClicked()
@ -224,3 +244,25 @@ void MainOptionsPage::setMapper(MonitoredDataMapper *mapper) @@ -224,3 +244,25 @@ void MainOptionsPage::setMapper(MonitoredDataMapper *mapper)
mapper->addMapping(fee_edit, OptionsModel::Fee);
}
DisplayOptionsPage::DisplayOptionsPage(QWidget *parent):
QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout();
QHBoxLayout *unit_hbox = new QHBoxLayout();
unit_hbox->addSpacing(18);
QLabel *unit_label = new QLabel(tr("&Unit: "));
unit_hbox->addWidget(unit_label);
unit = new QLineEdit();
unit_label->setBuddy(unit);
unit_hbox->addWidget(unit);
layout->addLayout(unit_hbox);
layout->addStretch();
setLayout(layout);
}
void DisplayOptionsPage::setMapper(MonitoredDataMapper *mapper)
{
}

9
src/qt/optionsdialog.h

@ -11,6 +11,7 @@ class QPushButton; @@ -11,6 +11,7 @@ class QPushButton;
QT_END_NAMESPACE
class OptionsModel;
class MainOptionsPage;
class DisplayOptionsPage;
class MonitoredDataMapper;
class OptionsDialog : public QDialog
@ -24,7 +25,8 @@ public: @@ -24,7 +25,8 @@ public:
signals:
public slots:
void changePage(QListWidgetItem *current, QListWidgetItem *previous);
void changePage(int index);
private slots:
void okClicked();
void cancelClicked();
@ -34,11 +36,14 @@ private slots: @@ -34,11 +36,14 @@ private slots:
private:
QListWidget *contents_widget;
QStackedWidget *pages_widget;
MainOptionsPage *main_options_page;
OptionsModel *model;
MonitoredDataMapper *mapper;
QPushButton *apply_button;
// Pages
MainOptionsPage *main_page;
DisplayOptionsPage *display_page;
void setupMainPage();
};

6
src/qt/overviewpage.cpp

@ -2,7 +2,7 @@ @@ -2,7 +2,7 @@
#include "ui_overviewpage.h"
#include "walletmodel.h"
#include "guiutil.h"
#include "bitcoinunits.h"
OverviewPage::OverviewPage(QWidget *parent) :
QWidget(parent),
@ -34,8 +34,8 @@ OverviewPage::~OverviewPage() @@ -34,8 +34,8 @@ OverviewPage::~OverviewPage()
void OverviewPage::setBalance(qint64 balance, qint64 unconfirmedBalance)
{
ui->labelBalance->setText(GUIUtil::formatMoney(balance) + QString(" BTC"));
ui->labelUnconfirmed->setText(GUIUtil::formatMoney(unconfirmedBalance) + QString(" BTC"));
ui->labelBalance->setText(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, balance));
ui->labelUnconfirmed->setText(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, unconfirmedBalance));
}
void OverviewPage::setNumTransactions(int count)

6
src/qt/sendcoinsdialog.cpp

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
#include "sendcoinsdialog.h"
#include "ui_sendcoinsdialog.h"
#include "walletmodel.h"
#include "guiutil.h"
#include "bitcoinunits.h"
#include "addressbookpage.h"
#include "optionsmodel.h"
#include "sendcoinsentry.h"
@ -71,7 +71,7 @@ void SendCoinsDialog::on_sendButton_clicked() @@ -71,7 +71,7 @@ void SendCoinsDialog::on_sendButton_clicked()
QStringList formatted;
foreach(const SendCoinsRecipient &rcp, recipients)
{
formatted.append(tr("%1 BTC to %2 (%3)").arg(GUIUtil::formatMoney(rcp.amount), rcp.label, rcp.address));
formatted.append(tr("%1 to %2 (%3)").arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, rcp.amount), rcp.label, rcp.address));
}
QMessageBox::StandardButton retval = QMessageBox::question(this, tr("Confirm send coins"),
@ -105,7 +105,7 @@ void SendCoinsDialog::on_sendButton_clicked() @@ -105,7 +105,7 @@ void SendCoinsDialog::on_sendButton_clicked()
case WalletModel::AmountWithFeeExceedsBalance:
QMessageBox::warning(this, tr("Send Coins"),
tr("Total exceeds your balance when the %1 transaction fee is included").
arg(GUIUtil::formatMoney(sendstatus.fee)),
arg(BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, sendstatus.fee)),
QMessageBox::Ok, QMessageBox::Ok);
break;
case WalletModel::DuplicateAddress:

3
src/qt/sendcoinsentry.cpp

@ -1,6 +1,7 @@ @@ -1,6 +1,7 @@
#include "sendcoinsentry.h"
#include "ui_sendcoinsentry.h"
#include "guiutil.h"
#include "bitcoinunits.h"
#include "addressbookpage.h"
#include "walletmodel.h"
#include "addresstablemodel.h"
@ -103,7 +104,7 @@ SendCoinsRecipient SendCoinsEntry::getValue() @@ -103,7 +104,7 @@ SendCoinsRecipient SendCoinsEntry::getValue()
rv.address = ui->payTo->text();
rv.label = ui->addAsLabel->text();
GUIUtil::parseMoney(ui->payAmount->text(), &rv.amount);
BitcoinUnits::parse(BitcoinUnits::BTC, ui->payAmount->text(), &rv.amount);
return rv;
}

3
src/qt/transactiontablemodel.cpp

@ -5,6 +5,7 @@ @@ -5,6 +5,7 @@
#include "transactiondesc.h"
#include "walletmodel.h"
#include "addresstablemodel.h"
#include "bitcoinunits.h"
#include "headers.h"
@ -397,7 +398,7 @@ QVariant TransactionTableModel::formatTxToAddress(const TransactionRecord *wtx) @@ -397,7 +398,7 @@ QVariant TransactionTableModel::formatTxToAddress(const TransactionRecord *wtx)
QVariant TransactionTableModel::formatTxAmount(const TransactionRecord *wtx, bool showUnconfirmed) const
{
QString str = QString::fromStdString(FormatMoney(wtx->credit + wtx->debit));
QString str = BitcoinUnits::format(BitcoinUnits::BTC, wtx->credit + wtx->debit);
if(showUnconfirmed)
{
if(!wtx->status.confirmed || wtx->status.maturity != TransactionStatus::Mature)

4
src/qt/transactionview.cpp

@ -5,7 +5,7 @@ @@ -5,7 +5,7 @@
#include "walletmodel.h"
#include "addresstablemodel.h"
#include "transactiontablemodel.h"
#include "guiutil.h"
#include "bitcoinunits.h"
#include "csvmodelwriter.h"
#include "transactiondescdialog.h"
#include "editaddressdialog.h"
@ -227,7 +227,7 @@ void TransactionView::changedPrefix(const QString &prefix) @@ -227,7 +227,7 @@ void TransactionView::changedPrefix(const QString &prefix)
void TransactionView::changedAmount(const QString &amount)
{
qint64 amount_parsed = 0;
if(GUIUtil::parseMoney(amount, &amount_parsed))
if(BitcoinUnits::parse(BitcoinUnits::BTC, amount, &amount_parsed))
{
transactionProxyModel->setMinAmount(amount_parsed);
}

Loading…
Cancel
Save