lbrycrd/gui/src/bitcoingui.cpp

410 lines
12 KiB
C++
Raw Normal View History

2011-05-07 22:13:39 +02:00
/*
2011-05-10 19:03:10 +02:00
* Qt4 bitcoin GUI.
*
2011-05-07 22:13:39 +02:00
* W.J. van der Laan 2011
*/
2011-05-12 14:49:42 +02:00
#include "bitcoingui.h"
#include "transactiontablemodel.h"
2011-05-12 14:44:52 +02:00
#include "addressbookdialog.h"
2011-05-12 14:49:42 +02:00
#include "sendcoinsdialog.h"
#include "optionsdialog.h"
#include "aboutdialog.h"
#include "clientmodel.h"
#include "guiutil.h"
#include "editaddressdialog.h"
#include "optionsmodel.h"
#include "transactiondescdialog.h"
2011-05-07 22:13:39 +02:00
#include "main.h"
2011-05-07 22:13:39 +02:00
#include <QApplication>
#include <QMainWindow>
#include <QMenuBar>
#include <QMenu>
#include <QIcon>
2011-05-10 19:03:10 +02:00
#include <QTabWidget>
2011-05-07 22:13:39 +02:00
#include <QVBoxLayout>
#include <QWidget>
#include <QToolBar>
#include <QStatusBar>
#include <QLabel>
#include <QTableView>
#include <QLineEdit>
#include <QPushButton>
2011-05-08 22:23:31 +02:00
#include <QHeaderView>
2011-05-09 20:44:46 +02:00
#include <QLocale>
2011-05-10 19:03:10 +02:00
#include <QSortFilterProxyModel>
2011-05-12 20:16:42 +02:00
#include <QClipboard>
2011-05-30 20:20:12 +02:00
#include <QMessageBox>
2011-05-10 19:03:10 +02:00
#include <QDebug>
2011-05-07 22:13:39 +02:00
#include <iostream>
BitcoinGUI::BitcoinGUI(QWidget *parent):
2011-05-12 20:16:42 +02:00
QMainWindow(parent), trayIcon(0)
2011-05-07 22:13:39 +02:00
{
resize(850, 550);
2011-05-10 13:32:56 +02:00
setWindowTitle(tr("Bitcoin"));
2011-05-09 20:44:46 +02:00
setWindowIcon(QIcon(":icons/bitcoin"));
2011-05-12 20:16:42 +02:00
createActions();
2011-05-10 19:03:10 +02:00
// Menus
2011-05-07 22:13:39 +02:00
QMenu *file = menuBar()->addMenu("&File");
file->addAction(sendcoins);
file->addSeparator();
file->addAction(quit);
QMenu *settings = menuBar()->addMenu("&Settings");
settings->addAction(receivingAddresses);
2011-05-10 19:03:10 +02:00
settings->addAction(options);
2011-05-07 22:13:39 +02:00
QMenu *help = menuBar()->addMenu("&Help");
help->addAction(about);
// Toolbar
2011-05-07 22:13:39 +02:00
QToolBar *toolbar = addToolBar("Main toolbar");
toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
toolbar->addAction(sendcoins);
toolbar->addAction(addressbook);
// Address: <address>: New... : Paste to clipboard
2011-05-07 22:13:39 +02:00
QHBoxLayout *hbox_address = new QHBoxLayout();
2011-05-08 16:30:10 +02:00
hbox_address->addWidget(new QLabel(tr("Your Bitcoin Address:")));
2011-05-12 20:16:42 +02:00
address = new QLineEdit();
address->setReadOnly(true);
address->setFont(GUIUtil::bitcoinAddressFont());
2011-05-12 20:16:42 +02:00
hbox_address->addWidget(address);
2011-05-07 22:13:39 +02:00
2011-05-10 19:03:10 +02:00
QPushButton *button_new = new QPushButton(tr("&New..."));
2011-05-08 16:30:10 +02:00
QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
2011-05-07 22:13:39 +02:00
hbox_address->addWidget(button_new);
hbox_address->addWidget(button_clipboard);
// Balance: <balance>
2011-05-07 22:13:39 +02:00
QHBoxLayout *hbox_balance = new QHBoxLayout();
2011-05-09 20:44:46 +02:00
hbox_balance->addWidget(new QLabel(tr("Balance:")));
2011-05-07 22:13:39 +02:00
hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
2011-05-09 20:44:46 +02:00
labelBalance = new QLabel();
2011-06-01 20:08:21 +02:00
labelBalance->setFont(QFont("Monospace"));
2011-05-12 20:16:42 +02:00
hbox_balance->addWidget(labelBalance);
2011-05-07 22:13:39 +02:00
hbox_balance->addStretch(1);
QVBoxLayout *vbox = new QVBoxLayout();
vbox->addLayout(hbox_address);
vbox->addLayout(hbox_balance);
2011-05-12 20:16:42 +02:00
vbox->addWidget(createTabs());
2011-05-08 22:23:31 +02:00
2011-05-12 20:16:42 +02:00
QWidget *centralwidget = new QWidget(this);
centralwidget->setLayout(vbox);
setCentralWidget(centralwidget);
// Create status bar
2011-05-12 20:16:42 +02:00
statusBar();
labelConnections = new QLabel();
labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelConnections->setMinimumWidth(130);
2011-05-12 20:16:42 +02:00
labelBlocks = new QLabel();
labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelBlocks->setMinimumWidth(130);
2011-05-12 20:16:42 +02:00
labelTransactions = new QLabel();
labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
labelTransactions->setMinimumWidth(130);
2011-05-12 20:16:42 +02:00
statusBar()->addPermanentWidget(labelConnections);
statusBar()->addPermanentWidget(labelBlocks);
statusBar()->addPermanentWidget(labelTransactions);
2011-05-12 20:16:42 +02:00
// Action bindings
2011-05-12 20:16:42 +02:00
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);
receivingAddresses = new QAction(QIcon(":/icons/receiving-addresses"), tr("Your &Receiving Addresses..."), this);
2011-05-12 20:16:42 +02:00
options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
2011-05-12 20:16:42 +02:00
connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
2011-05-12 20:16:42 +02:00
connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
2011-05-12 20:16:42 +02:00
}
void BitcoinGUI::setModel(ClientModel *model)
{
this->model = model;
// Keep up to date with client
setBalance(model->getBalance());
2011-05-28 20:32:19 +02:00
connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
setNumConnections(model->getNumConnections());
connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
setNumTransactions(model->getNumTransactions());
connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
setNumBlocks(model->getNumBlocks());
connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
setAddress(model->getAddress());
connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
2011-05-30 20:20:12 +02:00
// Report errors from network/worker thread
connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
2011-06-05 16:03:29 +02:00
// Put transaction list in tabs
setTabsModel(model->getTransactionTableModel());
}
2011-05-12 20:16:42 +02:00
void BitcoinGUI::createTrayIcon()
{
QMenu *trayIconMenu = new QMenu(this);
trayIconMenu->addAction(openBitcoin);
2011-05-12 20:16:42 +02:00
trayIconMenu->addAction(sendcoins);
trayIconMenu->addAction(options);
trayIconMenu->addSeparator();
trayIconMenu->addAction(quit);
trayIcon = new QSystemTrayIcon(this);
trayIcon->setContextMenu(trayIconMenu);
trayIcon->setIcon(QIcon(":/icons/toolbar"));
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
2011-05-12 20:16:42 +02:00
trayIcon->show();
}
void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
{
if(reason == QSystemTrayIcon::DoubleClick)
{
// Doubleclick on system tray icon triggers "open bitcoin"
openBitcoin->trigger();
}
}
2011-05-12 20:16:42 +02:00
QWidget *BitcoinGUI::createTabs()
{
2011-06-05 16:03:29 +02:00
QStringList tab_labels;
2011-05-10 19:03:10 +02:00
tab_labels << tr("All transactions")
<< tr("Sent/Received")
<< tr("Sent")
<< tr("Received");
2011-06-05 16:03:29 +02:00
QTabWidget *tabs = new QTabWidget(this);
2011-05-10 19:03:10 +02:00
for(int i = 0; i < tab_labels.size(); ++i)
2011-06-05 16:03:29 +02:00
{
QTableView *view = new QTableView(this);
tabs->addTab(view, tab_labels.at(i));
connect(view, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
2011-06-05 16:03:29 +02:00
transactionViews.append(view);
}
return tabs;
}
void BitcoinGUI::setTabsModel(QAbstractItemModel *transaction_model)
{
QStringList tab_filters;
tab_filters << "^."
<< "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
<< "^["+TransactionTableModel::Sent+"]"
<< "^["+TransactionTableModel::Received+"]";
for(int i = 0; i < transactionViews.size(); ++i)
2011-05-10 19:03:10 +02:00
{
QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
proxy_model->setSourceModel(transaction_model);
proxy_model->setDynamicSortFilter(true);
proxy_model->setFilterRole(TransactionTableModel::TypeRole);
2011-05-10 19:03:10 +02:00
proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
2011-05-28 20:32:19 +02:00
proxy_model->setSortRole(Qt::EditRole);
2011-05-10 19:03:10 +02:00
2011-06-05 16:03:29 +02:00
QTableView *transaction_table = transactionViews.at(i);
2011-05-10 19:03:10 +02:00
transaction_table->setModel(proxy_model);
transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
2011-05-27 08:20:23 +02:00
transaction_table->setSortingEnabled(true);
2011-05-28 20:32:19 +02:00
transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
2011-05-10 19:03:10 +02:00
transaction_table->verticalHeader()->hide();
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Status, 120);
2011-05-10 19:03:10 +02:00
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Date, 120);
2011-05-10 19:03:10 +02:00
transaction_table->horizontalHeader()->setResizeMode(
TransactionTableModel::Description, QHeaderView::Stretch);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Debit, 79);
transaction_table->horizontalHeader()->resizeSection(
TransactionTableModel::Credit, 79);
}
}
void BitcoinGUI::sendcoinsClicked()
{
SendCoinsDialog dlg;
2011-05-30 20:20:12 +02:00
dlg.setModel(model);
dlg.exec();
2011-05-10 19:03:10 +02:00
}
void BitcoinGUI::addressbookClicked()
{
AddressBookDialog dlg;
dlg.setModel(model->getAddressTableModel());
2011-05-12 14:44:52 +02:00
dlg.setTab(AddressBookDialog::SendingTab);
2011-06-03 15:16:11 +02:00
dlg.exec();
}
void BitcoinGUI::receivingAddressesClicked()
{
AddressBookDialog dlg;
dlg.setModel(model->getAddressTableModel());
2011-05-12 14:44:52 +02:00
dlg.setTab(AddressBookDialog::ReceivingTab);
dlg.exec();
2011-05-10 19:03:10 +02:00
}
void BitcoinGUI::optionsClicked()
{
OptionsDialog dlg;
dlg.setModel(model->getOptionsModel());
dlg.exec();
2011-05-10 19:03:10 +02:00
}
void BitcoinGUI::aboutClicked()
2011-05-10 19:03:10 +02:00
{
AboutDialog dlg;
dlg.exec();
2011-05-10 19:03:10 +02:00
}
void BitcoinGUI::newAddressClicked()
{
EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
dlg.setModel(model->getAddressTableModel());
if(dlg.exec())
{
QString newAddress = dlg.saveCurrentRow();
// Set returned address as new default addres
if(!newAddress.isEmpty())
{
model->setAddress(newAddress);
}
}
2011-05-07 22:13:39 +02:00
}
2011-05-10 19:03:10 +02:00
void BitcoinGUI::copyClipboardClicked()
2011-05-07 22:13:39 +02:00
{
// Copy text in address to clipboard
2011-05-12 20:16:42 +02:00
QApplication::clipboard()->setText(address->text());
2011-05-07 22:13:39 +02:00
}
void BitcoinGUI::setBalance(qint64 balance)
{
labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
}
2011-05-13 08:30:20 +02:00
void BitcoinGUI::setAddress(const QString &addr)
{
address->setText(addr);
}
void BitcoinGUI::setNumConnections(int count)
{
labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections(s)", "", count));
}
void BitcoinGUI::setNumBlocks(int count)
{
labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
}
void BitcoinGUI::setNumTransactions(int count)
{
labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
}
2011-05-30 20:20:12 +02:00
void BitcoinGUI::error(const QString &title, const QString &message)
{
// Report errors from network/worker thread
2011-06-05 16:03:29 +02:00
if(trayIcon->supportsMessages())
{
// Show as "balloon" message if possible
trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
2011-06-07 18:59:01 +02:00
}
else
{
2011-06-05 16:03:29 +02:00
// Fall back to old fashioned popup dialog if not
QMessageBox::critical(this, title,
message,
QMessageBox::Ok, QMessageBox::Ok);
}
2011-05-30 20:20:12 +02:00
}
void BitcoinGUI::changeEvent(QEvent *e)
{
if (e->type() == QEvent::WindowStateChange)
{
if(model->getOptionsModel()->getMinimizeToTray())
{
if (isMinimized())
{
hide();
e->ignore();
2011-06-07 18:59:01 +02:00
}
else
{
e->accept();
}
}
}
QMainWindow::changeEvent(e);
}
void BitcoinGUI::closeEvent(QCloseEvent *event)
{
if(!model->getOptionsModel()->getMinimizeToTray() &&
!model->getOptionsModel()->getMinimizeOnClose())
{
qApp->quit();
}
QMainWindow::closeEvent(event);
}
2011-06-05 17:36:52 +02:00
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(QString::fromStdString(FormatMoney(nFeeRequired)));
QMessageBox::StandardButton retval = QMessageBox::question(
this, tr("Sending..."), strMessage,
QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
*payFee = (retval == QMessageBox::Yes);
}
void BitcoinGUI::transactionDetails(const QModelIndex& idx)
{
/* A transaction is doubleclicked */
TransactionDescDialog dlg(idx);
dlg.exec();
}