4 * W.J. van der Laan 2011
6 #include "bitcoingui.h"
7 #include "transactiontablemodel.h"
8 #include "addressbookdialog.h"
9 #include "sendcoinsdialog.h"
10 #include "optionsdialog.h"
11 #include "aboutdialog.h"
12 #include "clientmodel.h"
14 #include "editaddressdialog.h"
15 #include "optionsmodel.h"
16 #include "transactiondescdialog.h"
20 #include <QApplication>
21 #include <QMainWindow>
26 #include <QVBoxLayout>
33 #include <QPushButton>
34 #include <QHeaderView>
36 #include <QSortFilterProxyModel>
38 #include <QMessageBox>
39 #include <QProgressBar>
45 BitcoinGUI::BitcoinGUI(QWidget *parent):
46 QMainWindow(parent), trayIcon(0)
49 setWindowTitle(tr("Bitcoin"));
50 setWindowIcon(QIcon(":icons/bitcoin"));
55 QMenu *file = menuBar()->addMenu("&File");
56 file->addAction(sendcoins);
58 file->addAction(quit);
60 QMenu *settings = menuBar()->addMenu("&Settings");
61 settings->addAction(receivingAddresses);
62 settings->addAction(options);
64 QMenu *help = menuBar()->addMenu("&Help");
65 help->addAction(about);
68 QToolBar *toolbar = addToolBar("Main toolbar");
69 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
70 toolbar->addAction(sendcoins);
71 toolbar->addAction(addressbook);
73 // Address: <address>: New... : Paste to clipboard
74 QHBoxLayout *hbox_address = new QHBoxLayout();
75 hbox_address->addWidget(new QLabel(tr("Your Bitcoin address:")));
76 address = new QLineEdit();
77 address->setReadOnly(true);
78 address->setFont(GUIUtil::bitcoinAddressFont());
79 address->setToolTip(tr("Your current default receiving address"));
80 hbox_address->addWidget(address);
82 QPushButton *button_new = new QPushButton(tr("&New..."));
83 button_new->setToolTip(tr("Create new receiving address"));
84 QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
85 button_clipboard->setToolTip(tr("Copy current receiving address to the system clipboard"));
86 hbox_address->addWidget(button_new);
87 hbox_address->addWidget(button_clipboard);
90 QHBoxLayout *hbox_balance = new QHBoxLayout();
91 hbox_balance->addWidget(new QLabel(tr("Balance:")));
92 hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
94 labelBalance = new QLabel();
95 labelBalance->setFont(QFont("Monospace"));
96 labelBalance->setToolTip(tr("Your current balance"));
97 hbox_balance->addWidget(labelBalance);
98 hbox_balance->addStretch(1);
100 QVBoxLayout *vbox = new QVBoxLayout();
101 vbox->addLayout(hbox_address);
102 vbox->addLayout(hbox_balance);
104 vbox->addWidget(createTabs());
106 QWidget *centralwidget = new QWidget(this);
107 centralwidget->setLayout(vbox);
108 setCentralWidget(centralwidget);
113 labelConnections = new QLabel();
114 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
115 labelConnections->setMinimumWidth(150);
116 labelConnections->setToolTip(tr("Number of connections to other clients"));
118 labelBlocks = new QLabel();
119 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
120 labelBlocks->setMinimumWidth(130);
121 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
123 labelTransactions = new QLabel();
124 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
125 labelTransactions->setMinimumWidth(130);
126 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
128 // Progress bar for blocks download
129 progressBarLabel = new QLabel(tr("Downloading initial data..."));
130 progressBarLabel->setVisible(false);
131 progressBar = new QProgressBar();
132 progressBar->setToolTip(tr("Initial block chain download in progress"));
133 progressBar->setVisible(false);
135 statusBar()->addWidget(progressBarLabel);
136 statusBar()->addWidget(progressBar);
137 statusBar()->addPermanentWidget(labelConnections);
138 statusBar()->addPermanentWidget(labelBlocks);
139 statusBar()->addPermanentWidget(labelTransactions);
142 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
143 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
148 void BitcoinGUI::createActions()
150 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
151 quit->setToolTip(tr("Quit application"));
152 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
153 sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
154 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
155 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
156 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
157 about->setToolTip(tr("Show information about Bitcoin"));
158 receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("Your &Receiving Addresses..."), this);
159 receivingAddresses->setToolTip(tr("Show the list of receiving addresses and edit their labels"));
160 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
161 options->setToolTip(tr("Modify configuration options for bitcoin"));
162 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
163 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
165 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
166 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
167 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
168 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
169 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
170 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
171 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
174 void BitcoinGUI::setModel(ClientModel *model)
178 // Keep up to date with client
179 setBalance(model->getBalance());
180 connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
182 setNumConnections(model->getNumConnections());
183 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
185 setNumTransactions(model->getNumTransactions());
186 connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
188 setNumBlocks(model->getNumBlocks());
189 connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
191 setAddress(model->getAddress());
192 connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
194 // Report errors from network/worker thread
195 connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
197 // Put transaction list in tabs
198 setTabsModel(model->getTransactionTableModel());
201 void BitcoinGUI::createTrayIcon()
203 QMenu *trayIconMenu = new QMenu(this);
204 trayIconMenu->addAction(openBitcoin);
205 trayIconMenu->addAction(sendcoins);
206 trayIconMenu->addAction(options);
207 trayIconMenu->addSeparator();
208 trayIconMenu->addAction(quit);
210 trayIcon = new QSystemTrayIcon(this);
211 trayIcon->setContextMenu(trayIconMenu);
212 trayIcon->setIcon(QIcon(":/icons/toolbar"));
213 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
214 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
218 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
220 if(reason == QSystemTrayIcon::DoubleClick)
222 // Doubleclick on system tray icon triggers "open bitcoin"
223 openBitcoin->trigger();
227 QWidget *BitcoinGUI::createTabs()
229 QStringList tab_labels;
230 tab_labels << tr("All transactions")
231 << tr("Sent/Received")
235 QTabWidget *tabs = new QTabWidget(this);
236 for(int i = 0; i < tab_labels.size(); ++i)
238 QTableView *view = new QTableView(this);
239 tabs->addTab(view, tab_labels.at(i));
241 connect(view, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
242 transactionViews.append(view);
248 void BitcoinGUI::setTabsModel(QAbstractItemModel *transaction_model)
250 QStringList tab_filters;
252 << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
253 << "^["+TransactionTableModel::Sent+"]"
254 << "^["+TransactionTableModel::Received+"]";
256 for(int i = 0; i < transactionViews.size(); ++i)
258 QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
259 proxy_model->setSourceModel(transaction_model);
260 proxy_model->setDynamicSortFilter(true);
261 proxy_model->setFilterRole(TransactionTableModel::TypeRole);
262 proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
263 proxy_model->setSortRole(Qt::EditRole);
265 QTableView *transaction_table = transactionViews.at(i);
266 transaction_table->setModel(proxy_model);
267 transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
268 transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
269 transaction_table->setSortingEnabled(true);
270 transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
271 transaction_table->verticalHeader()->hide();
273 transaction_table->horizontalHeader()->resizeSection(
274 TransactionTableModel::Status, 23);
275 transaction_table->horizontalHeader()->resizeSection(
276 TransactionTableModel::Date, 120);
277 transaction_table->horizontalHeader()->setResizeMode(
278 TransactionTableModel::Description, QHeaderView::Stretch);
279 transaction_table->horizontalHeader()->resizeSection(
280 TransactionTableModel::Debit, 79);
281 transaction_table->horizontalHeader()->resizeSection(
282 TransactionTableModel::Credit, 79);
285 connect(transaction_model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
286 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
289 void BitcoinGUI::sendcoinsClicked()
296 void BitcoinGUI::addressbookClicked()
298 AddressBookDialog dlg(AddressBookDialog::ForEditing);
299 dlg.setModel(model->getAddressTableModel());
300 dlg.setTab(AddressBookDialog::SendingTab);
304 void BitcoinGUI::receivingAddressesClicked()
306 AddressBookDialog dlg(AddressBookDialog::ForEditing);
307 dlg.setModel(model->getAddressTableModel());
308 dlg.setTab(AddressBookDialog::ReceivingTab);
312 void BitcoinGUI::optionsClicked()
315 dlg.setModel(model->getOptionsModel());
319 void BitcoinGUI::aboutClicked()
325 void BitcoinGUI::newAddressClicked()
327 EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
328 dlg.setModel(model->getAddressTableModel());
331 QString newAddress = dlg.saveCurrentRow();
332 // Set returned address as new default addres
333 if(!newAddress.isEmpty())
335 model->setAddress(newAddress);
340 void BitcoinGUI::copyClipboardClicked()
342 // Copy text in address to clipboard
343 QApplication::clipboard()->setText(address->text());
346 void BitcoinGUI::setBalance(qint64 balance)
348 labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
351 void BitcoinGUI::setAddress(const QString &addr)
353 address->setText(addr);
356 void BitcoinGUI::setNumConnections(int count)
361 case 0: icon = ":/icons/connect_0"; break;
362 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
363 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
364 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
365 default: icon = ":/icons/connect_4"; break;
367 labelConnections->setTextFormat(Qt::RichText);
368 labelConnections->setText("<img src=\""+icon+"\"> " + QLocale::system().toString(count)+" "+tr("connection(s)", "", count));
371 void BitcoinGUI::setNumBlocks(int count)
373 int total = model->getTotalBlocksEstimate();
376 progressBarLabel->setVisible(true);
377 progressBar->setVisible(true);
378 progressBar->setMaximum(total);
379 progressBar->setValue(count);
383 progressBarLabel->setVisible(false);
384 progressBar->setVisible(false);
387 labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
390 void BitcoinGUI::setNumTransactions(int count)
392 labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
395 void BitcoinGUI::error(const QString &title, const QString &message)
397 // Report errors from network/worker thread
398 if(trayIcon->supportsMessages())
400 // Show as "balloon" message if possible
401 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
405 // Fall back to old fashioned popup dialog if not
406 QMessageBox::critical(this, title,
408 QMessageBox::Ok, QMessageBox::Ok);
412 void BitcoinGUI::changeEvent(QEvent *e)
414 if (e->type() == QEvent::WindowStateChange)
416 if(model->getOptionsModel()->getMinimizeToTray())
429 QMainWindow::changeEvent(e);
432 void BitcoinGUI::closeEvent(QCloseEvent *event)
434 if(!model->getOptionsModel()->getMinimizeToTray() &&
435 !model->getOptionsModel()->getMinimizeOnClose())
439 QMainWindow::closeEvent(event);
442 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
445 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
446 "which goes to the nodes that process your transaction and helps to support the network. "
447 "Do you want to pay the fee?").arg(QString::fromStdString(FormatMoney(nFeeRequired)));
448 QMessageBox::StandardButton retval = QMessageBox::question(
449 this, tr("Sending..."), strMessage,
450 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
451 *payFee = (retval == QMessageBox::Yes);
454 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
456 // A transaction is doubleclicked
457 TransactionDescDialog dlg(idx);
461 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
463 TransactionTableModel *ttm = model->getTransactionTableModel();
464 qint64 credit = ttm->index(start, TransactionTableModel::Credit, parent)
465 .data(Qt::EditRole).toULongLong();
466 qint64 debit = ttm->index(start, TransactionTableModel::Debit, parent)
467 .data(Qt::EditRole).toULongLong();
468 if((credit+debit)>0 && !model->inInitialBlockDownload())
470 // On incoming transaction, make an info balloon
471 // Unless the initial block download is in progress, to prevent balloon-spam
472 QString date = ttm->index(start, TransactionTableModel::Date, parent)
474 QString description = ttm->index(start, TransactionTableModel::Description, parent)
477 trayIcon->showMessage(tr("Incoming transaction"),
478 "Date: " + date + "\n" +
479 "Amount: " + QString::fromStdString(FormatMoney(credit+debit, true)) + "\n" +
481 QSystemTrayIcon::Information);