From e457b021421c9065c8677e7fb7d7cae0391bf1f8 Mon Sep 17 00:00:00 2001 From: "Wladimir J. van der Laan" Date: Thu, 2 Jun 2011 15:57:23 +0200 Subject: [PATCH] namespacing, user friendly base58 entry, addressbook work --- gui/forms/editaddressdialog.ui | 46 +++++++++++++++++++-------- gui/include/bitcoinaddressvalidator.h | 2 ++ gui/include/editaddressdialog.h | 11 +++++-- gui/include/guiutil.h | 22 ++++++++++--- gui/src/addressbookdialog.cpp | 16 +++++++--- gui/src/addresstablemodel.cpp | 4 ++- gui/src/bitcoinaddressvalidator.cpp | 37 +++++++++++++++++++++ gui/src/bitcoingui.cpp | 2 ++ gui/src/editaddressdialog.cpp | 5 ++- gui/src/guiutil.cpp | 25 +++++++++++++-- gui/src/sendcoinsdialog.cpp | 11 ++----- gui/src/transactiontablemodel.cpp | 4 +-- 12 files changed, 147 insertions(+), 38 deletions(-) diff --git a/gui/forms/editaddressdialog.ui b/gui/forms/editaddressdialog.ui index 2d8aa880..763a0bb8 100644 --- a/gui/forms/editaddressdialog.ui +++ b/gui/forms/editaddressdialog.ui @@ -6,26 +6,46 @@ 0 0 - 400 - 300 + 458 + 113 - Dialog + Edit Address - - - Qt::Vertical - - - - 20 - 40 - + + + QFormLayout::AllNonFixedFieldsGrow - + + + + &Label + + + labelEdit + + + + + + + &Address + + + addressEdit + + + + + + + + + + diff --git a/gui/include/bitcoinaddressvalidator.h b/gui/include/bitcoinaddressvalidator.h index 8322eef7..c7b2eefc 100644 --- a/gui/include/bitcoinaddressvalidator.h +++ b/gui/include/bitcoinaddressvalidator.h @@ -9,6 +9,8 @@ class BitcoinAddressValidator : public QRegExpValidator public: explicit BitcoinAddressValidator(QObject *parent = 0); + State validate(QString &input, int &pos) const; + static const int MaxAddressLength = 34; signals: diff --git a/gui/include/editaddressdialog.h b/gui/include/editaddressdialog.h index 650ed534..8e4a0388 100644 --- a/gui/include/editaddressdialog.h +++ b/gui/include/editaddressdialog.h @@ -12,8 +12,15 @@ class EditAddressDialog : public QDialog Q_OBJECT public: - explicit EditAddressDialog(QWidget *parent = 0); - ~EditAddressDialog(); + enum Mode { + NewReceivingAddress, + NewSendingAddress, + EditReceivingAddress, + EditSendingAddress + }; + + explicit EditAddressDialog(Mode mode, QWidget *parent = 0); + ~EditAddressDialog(); private: Ui::EditAddressDialog *ui; diff --git a/gui/include/guiutil.h b/gui/include/guiutil.h index eaa81999..748e29bf 100644 --- a/gui/include/guiutil.h +++ b/gui/include/guiutil.h @@ -2,10 +2,24 @@ #define GUIUTIL_H #include -#include -QString DateTimeStr(qint64 nTime); -/* Render bitcoin addresses in monospace font */ -QFont bitcoinAddressFont(); +QT_BEGIN_NAMESPACE +class QFont; +class QLineEdit; +class QWidget; +QT_END_NAMESPACE + +class GUIUtil +{ +public: + static QString DateTimeStr(qint64 nTime); + + /* Render bitcoin addresses in monospace font */ + static QFont bitcoinAddressFont(); + + static void setupAddressWidget(QLineEdit *widget, QWidget *parent); + + static void setupAmountWidget(QLineEdit *widget, QWidget *parent); +}; #endif // GUIUTIL_H diff --git a/gui/src/addressbookdialog.cpp b/gui/src/addressbookdialog.cpp index ba6fdb51..853da585 100644 --- a/gui/src/addressbookdialog.cpp +++ b/gui/src/addressbookdialog.cpp @@ -87,14 +87,20 @@ void AddressBookDialog::on_copyToClipboard_clicked() void AddressBookDialog::on_editButton_clicked() { - /* Double click triggers edit button */ - EditAddressDialog dlg; + /* Double click also triggers edit button */ + EditAddressDialog dlg( + ui->tabWidget->currentIndex() == SendingTab ? + EditAddressDialog::EditSendingAddress : + EditAddressDialog::EditReceivingAddress); dlg.exec(); } void AddressBookDialog::on_newAddressButton_clicked() { - EditAddressDialog dlg; + EditAddressDialog dlg( + ui->tabWidget->currentIndex() == SendingTab ? + EditAddressDialog::NewSendingAddress : + EditAddressDialog::NewReceivingAddress); dlg.exec(); } @@ -103,10 +109,10 @@ void AddressBookDialog::on_tabWidget_currentChanged(int index) switch(index) { case SendingTab: - ui->deleteButton->show(); + ui->deleteButton->setEnabled(true); break; case ReceivingTab: - ui->deleteButton->hide(); + ui->deleteButton->setEnabled(false); break; } } diff --git a/gui/src/addresstablemodel.cpp b/gui/src/addresstablemodel.cpp index 21a90442..c375ca75 100644 --- a/gui/src/addresstablemodel.cpp +++ b/gui/src/addresstablemodel.cpp @@ -2,6 +2,8 @@ #include "guiutil.h" #include "main.h" +#include + const QString AddressTableModel::Send = "S"; const QString AddressTableModel::Receive = "R"; @@ -108,7 +110,7 @@ QVariant AddressTableModel::data(const QModelIndex &index, int role) const { if(index.column() == Address) { - return bitcoinAddressFont(); + return GUIUtil::bitcoinAddressFont(); } } else if (role == TypeRole) { diff --git a/gui/src/bitcoinaddressvalidator.cpp b/gui/src/bitcoinaddressvalidator.cpp index 8e719163..bccf4457 100644 --- a/gui/src/bitcoinaddressvalidator.cpp +++ b/gui/src/bitcoinaddressvalidator.cpp @@ -2,7 +2,44 @@ #include "base58.h" +#include + +/* Base58 characters are: + "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" + + This is: + - All numbers except for '0' + - All uppercase letters except for 'I' and 'O' + - All lowercase letters except for 'l' + + User friendly Base58 input can map + - 'l' and 'I' to '1' + - '0' and 'O' to 'o' +*/ + BitcoinAddressValidator::BitcoinAddressValidator(QObject *parent) : QRegExpValidator(QRegExp(QString("^[")+QString(pszBase58)+QString("]+")), parent) { } + +QValidator::State BitcoinAddressValidator::validate(QString &input, int &pos) const +{ + for(int idx=0; idxaddWidget(new QLabel(tr("Your Bitcoin Address:"))); address = new QLineEdit(); address->setReadOnly(true); + address->setFont(GUIUtil::bitcoinAddressFont()); hbox_address->addWidget(address); QPushButton *button_new = new QPushButton(tr("&New...")); diff --git a/gui/src/editaddressdialog.cpp b/gui/src/editaddressdialog.cpp index bd555979..0699b563 100644 --- a/gui/src/editaddressdialog.cpp +++ b/gui/src/editaddressdialog.cpp @@ -1,11 +1,14 @@ #include "editaddressdialog.h" #include "ui_editaddressdialog.h" +#include "guiutil.h" -EditAddressDialog::EditAddressDialog(QWidget *parent) : +EditAddressDialog::EditAddressDialog(Mode mode, QWidget *parent) : QDialog(parent), ui(new Ui::EditAddressDialog) { ui->setupUi(this); + + GUIUtil::setupAddressWidget(ui->addressEdit, this); } EditAddressDialog::~EditAddressDialog() diff --git a/gui/src/guiutil.cpp b/gui/src/guiutil.cpp index 59b4de30..d01f23d8 100644 --- a/gui/src/guiutil.cpp +++ b/gui/src/guiutil.cpp @@ -1,16 +1,37 @@ #include "guiutil.h" +#include "bitcoinaddressvalidator.h" +#include #include +#include +#include +#include -QString DateTimeStr(qint64 nTime) +QString GUIUtil::DateTimeStr(qint64 nTime) { QDateTime date = QDateTime::fromMSecsSinceEpoch(nTime*1000); return date.date().toString(Qt::SystemLocaleShortDate) + QString(" ") + date.toString("hh:mm"); } -QFont bitcoinAddressFont() +QFont GUIUtil::bitcoinAddressFont() { QFont font("Monospace"); font.setStyleHint(QFont::TypeWriter); return font; } + +void GUIUtil::setupAddressWidget(QLineEdit *widget, QWidget *parent) +{ + widget->setMaxLength(BitcoinAddressValidator::MaxAddressLength); + widget->setValidator(new BitcoinAddressValidator(parent)); + widget->setFont(bitcoinAddressFont()); +} + +void GUIUtil::setupAmountWidget(QLineEdit *widget, QWidget *parent) +{ + QDoubleValidator *amountValidator = new QDoubleValidator(parent); + amountValidator->setDecimals(8); + amountValidator->setBottom(0.0); + widget->setValidator(amountValidator); +} + diff --git a/gui/src/sendcoinsdialog.cpp b/gui/src/sendcoinsdialog.cpp index a6ab6015..b50ed990 100644 --- a/gui/src/sendcoinsdialog.cpp +++ b/gui/src/sendcoinsdialog.cpp @@ -1,9 +1,9 @@ #include "sendcoinsdialog.h" #include "ui_sendcoinsdialog.h" #include "clientmodel.h" +#include "guiutil.h" #include "addressbookdialog.h" -#include "bitcoinaddressvalidator.h" #include "optionsmodel.h" #include @@ -22,13 +22,8 @@ SendCoinsDialog::SendCoinsDialog(QWidget *parent, const QString &address) : { ui->setupUi(this); - /* Set up validators */ - ui->payTo->setMaxLength(BitcoinAddressValidator::MaxAddressLength); - ui->payTo->setValidator(new BitcoinAddressValidator(this)); - QDoubleValidator *amountValidator = new QDoubleValidator(this); - amountValidator->setDecimals(8); - amountValidator->setBottom(0.0); - ui->payAmount->setValidator(amountValidator); + GUIUtil::setupAddressWidget(ui->payTo, this); + GUIUtil::setupAmountWidget(ui->payAmount, this); /* Set initial address if provided */ if(!address.isEmpty()) diff --git a/gui/src/transactiontablemodel.cpp b/gui/src/transactiontablemodel.cpp index c7fb43ed..e23f45dd 100644 --- a/gui/src/transactiontablemodel.cpp +++ b/gui/src/transactiontablemodel.cpp @@ -159,7 +159,7 @@ QVariant TransactionTableModel::formatTxStatus(const TransactionRecord *wtx) con status = tr("Open for %n block(s)","",wtx->status.open_for); break; case TransactionStatus::OpenUntilDate: - status = tr("Open until ") + DateTimeStr(wtx->status.open_for); + status = tr("Open until ") + GUIUtil::DateTimeStr(wtx->status.open_for); break; case TransactionStatus::Offline: status = tr("%1/offline").arg(wtx->status.depth); @@ -179,7 +179,7 @@ QVariant TransactionTableModel::formatTxDate(const TransactionRecord *wtx) const { if(wtx->time) { - return QVariant(DateTimeStr(wtx->time)); + return QVariant(GUIUtil::DateTimeStr(wtx->time)); } else { return QVariant(); }