diff options
author | Wladimir J. van der Laan <laanwj@gmail.com> | 2011-07-25 21:35:45 +0200 |
---|---|---|
committer | Wladimir J. van der Laan <laanwj@gmail.com> | 2011-07-25 21:38:54 +0200 |
commit | e285ffcd052a42a6e870f093e7663671a2a3b147 (patch) | |
tree | d9c5237ba0d2c6617d6202cbf1fda666a0b33d78 /src/qt | |
parent | bbae0fc9efa6eb7155d679a1cc3eeb451d594d14 (diff) |
preparations for multiple unit (uBTC, mBTC, BTC) support, fix amount entry issue
Diffstat (limited to 'src/qt')
-rw-r--r-- | src/qt/bitcoinamountfield.cpp | 12 | ||||
-rw-r--r-- | src/qt/bitcoingui.cpp | 7 | ||||
-rw-r--r-- | src/qt/bitcoinunits.cpp | 96 | ||||
-rw-r--r-- | src/qt/bitcoinunits.h | 34 | ||||
-rw-r--r-- | src/qt/guiutil.cpp | 10 | ||||
-rw-r--r-- | src/qt/guiutil.h | 6 | ||||
-rw-r--r-- | src/qt/optionsdialog.cpp | 64 | ||||
-rw-r--r-- | src/qt/optionsdialog.h | 9 | ||||
-rw-r--r-- | src/qt/overviewpage.cpp | 6 | ||||
-rw-r--r-- | src/qt/sendcoinsdialog.cpp | 6 | ||||
-rw-r--r-- | src/qt/sendcoinsentry.cpp | 3 | ||||
-rw-r--r-- | src/qt/transactiontablemodel.cpp | 3 | ||||
-rw-r--r-- | src/qt/transactionview.cpp | 4 |
13 files changed, 216 insertions, 44 deletions
diff --git a/src/qt/bitcoinamountfield.cpp b/src/qt/bitcoinamountfield.cpp index b312b9792e..330a7bfdef 100644 --- a/src/qt/bitcoinamountfield.cpp +++ b/src/qt/bitcoinamountfield.cpp @@ -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): 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): 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() decimals->setValid(false); valid = false; } + if(!BitcoinUnits::parse(BitcoinUnits::BTC, text(), 0)) + { + amount->setValid(false); + decimals->setValid(false); + valid = false; + } + return valid; } diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 938d030d25..bd80e42d03 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -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) 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 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); diff --git a/src/qt/bitcoinunits.cpp b/src/qt/bitcoinunits.cpp new file mode 100644 index 0000000000..ff30563f97 --- /dev/null +++ b/src/qt/bitcoinunits.cpp @@ -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; +} diff --git a/src/qt/bitcoinunits.h b/src/qt/bitcoinunits.h new file mode 100644 index 0000000000..fa85755ad4 --- /dev/null +++ b/src/qt/bitcoinunits.h @@ -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 diff --git a/src/qt/guiutil.cpp b/src/qt/guiutil.cpp index 31b28024df..308a6ba9bc 100644 --- a/src/qt/guiutil.cpp +++ b/src/qt/guiutil.cpp @@ -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)); -} diff --git a/src/qt/guiutil.h b/src/qt/guiutil.h index 58f8979511..26a1a03712 100644 --- a/src/qt/guiutil.h +++ b/src/qt/guiutil.h @@ -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 diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index 3697b9fe41..4f3a82d319 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -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: }; +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): 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): 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) 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) 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) +{ +} diff --git a/src/qt/optionsdialog.h b/src/qt/optionsdialog.h index 07e85297d5..d5238a3664 100644 --- a/src/qt/optionsdialog.h +++ b/src/qt/optionsdialog.h @@ -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: 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: 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(); }; diff --git a/src/qt/overviewpage.cpp b/src/qt/overviewpage.cpp index d991f0d7ba..9515117c21 100644 --- a/src/qt/overviewpage.cpp +++ b/src/qt/overviewpage.cpp @@ -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() 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) diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp index 38a0a65520..58f9422b27 100644 --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -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() 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() 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: diff --git a/src/qt/sendcoinsentry.cpp b/src/qt/sendcoinsentry.cpp index 2d4fe9b123..9fc041111f 100644 --- a/src/qt/sendcoinsentry.cpp +++ b/src/qt/sendcoinsentry.cpp @@ -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() 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; } diff --git a/src/qt/transactiontablemodel.cpp b/src/qt/transactiontablemodel.cpp index 7062ceecbd..2b8fe0b471 100644 --- a/src/qt/transactiontablemodel.cpp +++ b/src/qt/transactiontablemodel.cpp @@ -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) 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) diff --git a/src/qt/transactionview.cpp b/src/qt/transactionview.cpp index 50291fea42..f88b6fc123 100644 --- a/src/qt/transactionview.cpp +++ b/src/qt/transactionview.cpp @@ -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) 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); } |