begin integration with bitcoin upstream

This commit is contained in:
Wladimir J. van der Laan
2011-05-14 10:31:46 +02:00
parent 4d1bb15e31
commit 1f2e0df865
38 changed files with 3489 additions and 35 deletions

19
gui/src/aboutdialog.cpp Normal file
View File

@@ -0,0 +1,19 @@
#include "aboutdialog.h"
#include "ui_aboutdialog.h"
AboutDialog::AboutDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AboutDialog)
{
ui->setupUi(this);
}
AboutDialog::~AboutDialog()
{
delete ui;
}
void AboutDialog::on_buttonBox_accepted()
{
close();
}

View File

@@ -0,0 +1,132 @@
#include "addressbookdialog.h"
#include "ui_addressbookdialog.h"
#include "addresstablemodel.h"
#include "editaddressdialog.h"
#include <QSortFilterProxyModel>
#include <QDebug>
AddressBookDialog::AddressBookDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddressBookDialog),
model(0)
{
ui->setupUi(this);
model = new AddressTableModel(this);
setModel(model);
}
AddressBookDialog::~AddressBookDialog()
{
delete ui;
}
void AddressBookDialog::setModel(AddressTableModel *model)
{
/* Receive filter */
QSortFilterProxyModel *receive_model = new QSortFilterProxyModel(this);
receive_model->setSourceModel(model);
receive_model->setDynamicSortFilter(true);
receive_model->setFilterRole(Qt::UserRole);
receive_model->setFilterKeyColumn(AddressTableModel::Type);
receive_model->setFilterFixedString(AddressTableModel::Receive);
ui->receiveTableView->setModel(receive_model);
/* Send filter */
QSortFilterProxyModel *send_model = new QSortFilterProxyModel(this);
send_model->setSourceModel(model);
send_model->setDynamicSortFilter(true);
send_model->setFilterRole(Qt::UserRole);
send_model->setFilterKeyColumn(AddressTableModel::Type);
send_model->setFilterFixedString(AddressTableModel::Send);
ui->sendTableView->setModel(send_model);
/* Set column widths */
ui->receiveTableView->horizontalHeader()->resizeSection(
AddressTableModel::Address, 320);
ui->receiveTableView->horizontalHeader()->setResizeMode(
AddressTableModel::Label, QHeaderView::Stretch);
ui->sendTableView->horizontalHeader()->resizeSection(
AddressTableModel::Address, 320);
ui->sendTableView->horizontalHeader()->setResizeMode(
AddressTableModel::Label, QHeaderView::Stretch);
/* Hide "Type" column in both views as it is only used for filtering */
ui->receiveTableView->setColumnHidden(AddressTableModel::Type, true);
ui->sendTableView->setColumnHidden(AddressTableModel::Type, true);
}
void AddressBookDialog::setTab(int tab)
{
ui->tabWidget->setCurrentIndex(tab);
}
QTableView *AddressBookDialog::getCurrentTable()
{
switch(ui->tabWidget->currentIndex())
{
case SendingTab:
return ui->sendTableView;
case ReceivingTab:
return ui->receiveTableView;
default:
return 0;
}
}
void AddressBookDialog::on_copyToClipboard_clicked()
{
/* Copy currently selected address to clipboard */
}
void AddressBookDialog::on_editButton_clicked()
{
/* Double click should trigger edit button */
EditAddressDialog dlg;
dlg.exec();
}
void AddressBookDialog::on_newAddressButton_clicked()
{
EditAddressDialog dlg;
dlg.exec();
}
void AddressBookDialog::on_tabWidget_currentChanged(int index)
{
switch(index)
{
case SendingTab:
ui->deleteButton->show();
break;
case ReceivingTab:
ui->deleteButton->hide();
break;
}
}
void AddressBookDialog::on_deleteButton_clicked()
{
QTableView *table = getCurrentTable();
QModelIndexList indexes = table->selectionModel()->selectedRows();
foreach (QModelIndex index, indexes) {
table->model()->removeRow(index.row());
}
}
void AddressBookDialog::on_buttonBox_accepted()
{
QTableView *table = getCurrentTable();
QModelIndexList indexes = table->selectionModel()->selectedRows(AddressTableModel::Address);
foreach (QModelIndex index, indexes) {
QVariant address = table->model()->data(index);
returnValue = address.toString();
}
accept();
}

View File

@@ -0,0 +1,57 @@
#include "addresstablemodel.h"
const QString AddressTableModel::Send = "S";
const QString AddressTableModel::Receive = "R";
AddressTableModel::AddressTableModel(QObject *parent) :
QAbstractTableModel(parent)
{
}
int AddressTableModel::rowCount(const QModelIndex &parent) const
{
return 5;
}
int AddressTableModel::columnCount(const QModelIndex &parent) const
{
return 3;
}
QVariant AddressTableModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DisplayRole)
{
/* index.row(), index.column() */
/* Return QString */
if(index.column() == Address)
return "1PC9aZC4hNX2rmmrt7uHTfYAS3hRbph4UN" + QString::number(index.row());
else
return "Description";
} else if (role == Qt::UserRole)
{
switch(index.row() % 2)
{
case 0: return Send;
case 1: return Receive;
}
}
return QVariant();
}
QVariant AddressTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
return QVariant();
}
Qt::ItemFlags AddressTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index);
}

24
gui/src/bitcoin.cpp Normal file
View File

@@ -0,0 +1,24 @@
/*
* W.J. van der Laan 2011
*/
#include "bitcoingui.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
BitcoinGUI window;
window.setBalance(1234.567890);
window.setNumConnections(4);
window.setNumTransactions(4);
window.setNumBlocks(33);
window.setAddress("123456789");
window.show();
/* Depending on settings: QApplication::setQuitOnLastWindowClosed(false); */
return app.exec();
}

View File

@@ -0,0 +1,8 @@
#include "bitcoinaddressvalidator.h"
const QString BitcoinAddressValidator::valid_chars = "123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
BitcoinAddressValidator::BitcoinAddressValidator(QObject *parent) :
QRegExpValidator(QRegExp("^["+valid_chars+"]+"), parent)
{
}

275
gui/src/bitcoingui.cpp Normal file
View File

@@ -0,0 +1,275 @@
/*
* Qt4 bitcoin GUI.
*
* W.J. van der Laan 2011
*/
#include "bitcoingui.h"
#include "transactiontablemodel.h"
#include "addressbookdialog.h"
#include "sendcoinsdialog.h"
#include "optionsdialog.h"
#include "aboutdialog.h"
#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QIcon>
#include <QTabWidget>
#include <QVBoxLayout>
#include <QWidget>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>
#include <QTableView>
#include <QLineEdit>
#include <QPushButton>
#include <QHeaderView>
#include <QLocale>
#include <QSortFilterProxyModel>
#include <QClipboard>
#include <QDebug>
#include <iostream>
BitcoinGUI::BitcoinGUI(QWidget *parent):
QMainWindow(parent), trayIcon(0)
{
resize(850, 550);
setWindowTitle(tr("Bitcoin"));
setWindowIcon(QIcon(":icons/bitcoin"));
createActions();
/* Menus */
QMenu *file = menuBar()->addMenu("&File");
file->addAction(sendcoins);
file->addSeparator();
file->addAction(quit);
QMenu *settings = menuBar()->addMenu("&Settings");
settings->addAction(receiving_addresses);
settings->addAction(options);
QMenu *help = menuBar()->addMenu("&Help");
help->addAction(about);
/* Toolbar */
QToolBar *toolbar = addToolBar("Main toolbar");
toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolbar->addAction(sendcoins);
toolbar->addAction(addressbook);
/* Address: <address>: New... : Paste to clipboard */
QHBoxLayout *hbox_address = new QHBoxLayout();
hbox_address->addWidget(new QLabel(tr("Your Bitcoin Address:")));
address = new QLineEdit();
address->setReadOnly(true);
hbox_address->addWidget(address);
QPushButton *button_new = new QPushButton(tr("&New..."));
QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
hbox_address->addWidget(button_new);
hbox_address->addWidget(button_clipboard);
/* Balance: <balance> */
QHBoxLayout *hbox_balance = new QHBoxLayout();
hbox_balance->addWidget(new QLabel(tr("Balance:")));
hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
labelBalance = new QLabel();
labelBalance->setFont(QFont("Teletype"));
hbox_balance->addWidget(labelBalance);
hbox_balance->addStretch(1);
QVBoxLayout *vbox = new QVBoxLayout();
vbox->addLayout(hbox_address);
vbox->addLayout(hbox_balance);
transaction_model = new TransactionTableModel(this);
vbox->addWidget(createTabs());
QWidget *centralwidget = new QWidget(this);
centralwidget->setLayout(vbox);
setCentralWidget(centralwidget);
/* Create status bar */
statusBar();
labelConnections = new QLabel();
labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelConnections->setMinimumWidth(130);
labelBlocks = new QLabel();
labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelBlocks->setMinimumWidth(130);
labelTransactions = new QLabel();
labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelTransactions->setMinimumWidth(130);
statusBar()->addPermanentWidget(labelConnections);
statusBar()->addPermanentWidget(labelBlocks);
statusBar()->addPermanentWidget(labelTransactions);
/* Action bindings */
connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
createTrayIcon();
}
void BitcoinGUI::createActions()
{
quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
receiving_addresses = new QAction(QIcon(":/icons/receiving-addresses"), tr("Your &Receiving Addresses..."), this);
options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
openBitCoin = new QAction(QIcon(":/icons/bitcoin"), "Open Bitcoin", this);
connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
connect(receiving_addresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
}
void BitcoinGUI::createTrayIcon()
{
QMenu *trayIconMenu = new QMenu(this);
trayIconMenu->addAction(openBitCoin);
trayIconMenu->addAction(sendcoins);
trayIconMenu->addAction(options);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quit);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QIcon(":/icons/toolbar"));
trayIcon->show();
}
QWidget *BitcoinGUI::createTabs()
{
QStringList tab_filters, tab_labels;
tab_filters << "^."
<< "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
<< "^["+TransactionTableModel::Sent+"]"
<< "^["+TransactionTableModel::Received+"]";
tab_labels << tr("All transactions")
<< tr("Sent/Received")
<< tr("Sent")
<< tr("Received");
QTabWidget *tabs = new QTabWidget(this);
for(int i = 0; i < tab_labels.size(); ++i)
{
QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
proxy_model->setSourceModel(transaction_model);
proxy_model->setDynamicSortFilter(true);
proxy_model->setFilterRole(Qt::UserRole);
proxy_model->setFilterKeyColumn(TransactionTableModel::Type);
proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
QTableView *transaction_table = new QTableView(this);
transaction_table->setModel(proxy_model);
transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
transaction_table->verticalHeader()->hide();
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Status, 112);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Date, 112);
transaction_table->horizontalHeader()->setResizeMode(
TransactionTableModel::Description, QHeaderView::Stretch);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Debit, 79);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Credit, 79);
transaction_table->setColumnHidden(TransactionTableModel::Type, true);
tabs->addTab(transaction_table, tab_labels.at(i));
}
return tabs;
}
void BitcoinGUI::sendcoinsClicked()
{
qDebug() << "Send coins clicked";
SendCoinsDialog dlg;
dlg.exec();
}
void BitcoinGUI::addressbookClicked()
{
qDebug() << "Address book clicked";
AddressBookDialog dlg;
dlg.setTab(AddressBookDialog::SendingTab);
dlg.exec();
}
void BitcoinGUI::receivingAddressesClicked()
{
qDebug() << "Receiving addresses clicked";
AddressBookDialog dlg;
dlg.setTab(AddressBookDialog::ReceivingTab);
dlg.exec();
}
void BitcoinGUI::optionsClicked()
{
qDebug() << "Options clicked";
OptionsDialog dlg;
dlg.exec();
}
void BitcoinGUI::aboutClicked()
{
qDebug() << "About clicked";
AboutDialog dlg;
dlg.exec();
}
void BitcoinGUI::newAddressClicked()
{
qDebug() << "New address clicked";
/* TODO: generate new address */
}
void BitcoinGUI::copyClipboardClicked()
{
qDebug() << "Copy to clipboard";
/* Copy text in address to clipboard */
QApplication::clipboard()->setText(address->text());
}
void BitcoinGUI::setBalance(double balance)
{
labelBalance->setText(QLocale::system().toString(balance, 8));
}
void BitcoinGUI::setAddress(const QString &addr)
{
address->setText(addr);
}
void BitcoinGUI::setNumConnections(int count)
{
labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections"));
}
void BitcoinGUI::setNumBlocks(int count)
{
labelBlocks->setText(QLocale::system().toString(count)+" "+tr("blocks"));
}
void BitcoinGUI::setNumTransactions(int count)
{
labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transactions"));
}

View File

@@ -0,0 +1,14 @@
#include "editaddressdialog.h"
#include "ui_editaddressdialog.h"
EditAddressDialog::EditAddressDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::EditAddressDialog)
{
ui->setupUi(this);
}
EditAddressDialog::~EditAddressDialog()
{
delete ui;
}

View File

@@ -0,0 +1,67 @@
#include "mainoptionspage.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QLabel>
#include <QLineEdit>
MainOptionsPage::MainOptionsPage(QWidget *parent):
QWidget(parent)
{
QVBoxLayout *layout = new QVBoxLayout();
QCheckBox *bitcoin_at_startup = new QCheckBox(tr("&Start Bitcoin on window system startup"));
layout->addWidget(bitcoin_at_startup);
QCheckBox *minimize_to_tray = new QCheckBox(tr("&Minimize to the tray instead of the taskbar"));
layout->addWidget(minimize_to_tray);
QCheckBox *map_port_upnp = new QCheckBox(tr("Map port using &UPnP"));
layout->addWidget(map_port_upnp);
QCheckBox *minimize_on_close = new QCheckBox(tr("M&inimize on close"));
layout->addWidget(minimize_on_close);
QCheckBox *connect_socks4 = new QCheckBox(tr("&Connect through socks4 proxy:"));
layout->addWidget(connect_socks4);
QHBoxLayout *proxy_hbox = new QHBoxLayout();
proxy_hbox->addSpacing(18);
QLabel *proxy_ip_label = new QLabel(tr("Proxy &IP: "));
proxy_hbox->addWidget(proxy_ip_label);
QLineEdit *proxy_ip = new QLineEdit();
proxy_ip->setMaximumWidth(140);
proxy_ip_label->setBuddy(proxy_ip);
proxy_hbox->addWidget(proxy_ip);
QLabel *proxy_port_label = new QLabel(tr("&Port: "));
proxy_hbox->addWidget(proxy_port_label);
QLineEdit *proxy_port = new QLineEdit();
proxy_port->setMaximumWidth(55);
proxy_port_label->setBuddy(proxy_port);
proxy_hbox->addWidget(proxy_port);
proxy_hbox->addStretch(1);
layout->addLayout(proxy_hbox);
QLabel *fee_help = new QLabel(tr("Optional transaction fee per KB that helps make sure your transactions are processed quickly. Most transactions are 1KB. Fee 0.01 recommended."));
fee_help->setWordWrap(true);
layout->addWidget(fee_help);
QHBoxLayout *fee_hbox = new QHBoxLayout();
fee_hbox->addSpacing(18);
QLabel *fee_label = new QLabel(tr("Pay transaction &fee"));
fee_hbox->addWidget(fee_label);
QLineEdit *fee_edit = new QLineEdit();
fee_edit->setMaximumWidth(70);
fee_label->setBuddy(fee_edit);
fee_hbox->addWidget(fee_edit);
fee_hbox->addStretch(1);
layout->addLayout(fee_hbox);
layout->addStretch(1); /* Extra space at bottom */
setLayout(layout);
}

57
gui/src/optionsdialog.cpp Normal file
View File

@@ -0,0 +1,57 @@
#include "optionsdialog.h"
#include "mainoptionspage.h"
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QPushButton>
#include <QListWidget>
#include <QStackedWidget>
OptionsDialog::OptionsDialog(QWidget *parent) :
QDialog(parent), contents_widget(0), pages_widget(0)
{
contents_widget = new QListWidget();
contents_widget->setMaximumWidth(128);
pages_widget = new QStackedWidget();
pages_widget->setMinimumWidth(300);
QListWidgetItem *item_main = new QListWidgetItem(tr("Main"));
contents_widget->addItem(item_main);
pages_widget->addWidget(new MainOptionsPage(this));
contents_widget->setCurrentRow(0);
QHBoxLayout *main_layout = new QHBoxLayout();
main_layout->addWidget(contents_widget);
main_layout->addWidget(pages_widget, 1);
QVBoxLayout *layout = new QVBoxLayout();
layout->addLayout(main_layout);
QHBoxLayout *buttons = new QHBoxLayout();
buttons->addStretch(1);
QPushButton *ok_button = new QPushButton(tr("OK"));
buttons->addWidget(ok_button);
QPushButton *cancel_button = new QPushButton(tr("Cancel"));
buttons->addWidget(cancel_button);
QPushButton *apply_button = new QPushButton(tr("Apply"));
buttons->addWidget(apply_button);
layout->addLayout(buttons);
setLayout(layout);
setWindowTitle(tr("Options"));
}
void OptionsDialog::changePage(QListWidgetItem *current, QListWidgetItem *previous)
{
Q_UNUSED(previous);
if(current)
{
pages_widget->setCurrentIndex(contents_widget->row(current));
}
}

View File

@@ -0,0 +1,45 @@
#include "sendcoinsdialog.h"
#include "ui_sendcoinsdialog.h"
#include "addressbookdialog.h"
#include "bitcoinaddressvalidator.h"
#include <QApplication>
#include <QClipboard>
SendCoinsDialog::SendCoinsDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SendCoinsDialog)
{
ui->setupUi(this);
ui->payTo->setValidator(new BitcoinAddressValidator(this));
ui->payAmount->setValidator(new QDoubleValidator(this));
}
SendCoinsDialog::~SendCoinsDialog()
{
delete ui;
}
void SendCoinsDialog::on_sendButton_clicked()
{
accept();
}
void SendCoinsDialog::on_pasteButton_clicked()
{
/* Paste text from clipboard into recipient field */
ui->payTo->setText(QApplication::clipboard()->text());
}
void SendCoinsDialog::on_addressBookButton_clicked()
{
AddressBookDialog dlg;
dlg.exec();
ui->payTo->setText(dlg.getReturnValue());
}
void SendCoinsDialog::on_buttonBox_rejected()
{
reject();
}

View File

@@ -0,0 +1,88 @@
#include "transactiontablemodel.h"
#include <QLocale>
const QString TransactionTableModel::Sent = "s";
const QString TransactionTableModel::Received = "r";
const QString TransactionTableModel::Generated = "g";
/* Credit and Debit columns are right-aligned as they contain numbers */
static int column_alignments[] = {
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignLeft|Qt::AlignVCenter,
Qt::AlignRight|Qt::AlignVCenter,
Qt::AlignRight|Qt::AlignVCenter,
Qt::AlignLeft|Qt::AlignVCenter
};
TransactionTableModel::TransactionTableModel(QObject *parent):
QAbstractTableModel(parent)
{
columns << tr("Status") << tr("Date") << tr("Description") << tr("Debit") << tr("Credit") << tr("Type");
}
int TransactionTableModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 5;
}
int TransactionTableModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return columns.length();
}
QVariant TransactionTableModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant();
if(role == Qt::DisplayRole)
{
/* index.row(), index.column() */
/* Return QString */
return QLocale::system().toString(index.row());
} else if (role == Qt::TextAlignmentRole)
{
return column_alignments[index.column()];
} else if (role == Qt::UserRole)
{
/* user role: transaction type for filtering
"s" (sent)
"r" (received)
"g" (generated)
*/
switch(index.row() % 3)
{
case 0: return QString("s");
case 1: return QString("r");
case 2: return QString("o");
}
}
return QVariant();
}
QVariant TransactionTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role == Qt::DisplayRole)
{
if(orientation == Qt::Horizontal)
{
return columns[section];
}
} else if (role == Qt::TextAlignmentRole)
{
return column_alignments[section];
}
return QVariant();
}
Qt::ItemFlags TransactionTableModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsEnabled;
return QAbstractTableModel::flags(index);
}