4 * W.J. van der Laan 2011
6 #include "bitcoingui.h"
7 #include "transactiontablemodel.h"
8 #include "addressbookpage.h"
9 #include "sendcoinsdialog.h"
10 #include "optionsdialog.h"
11 #include "aboutdialog.h"
12 #include "clientmodel.h"
13 #include "walletmodel.h"
14 #include "editaddressdialog.h"
15 #include "optionsmodel.h"
16 #include "transactiondescdialog.h"
17 #include "addresstablemodel.h"
18 #include "transactionview.h"
19 #include "overviewpage.h"
20 #include "bitcoinunits.h"
21 #include "guiconstants.h"
22 #include "askpassphrasedialog.h"
23 #include "notificator.h"
25 #include <QApplication>
26 #include <QMainWindow>
31 #include <QVBoxLayout>
36 #include <QPushButton>
38 #include <QMessageBox>
39 #include <QProgressBar>
40 #include <QStackedWidget>
44 #include <QDragEnterEvent>
49 BitcoinGUI::BitcoinGUI(QWidget *parent):
53 encryptWalletAction(0),
54 changePassphraseAction(0),
59 setWindowTitle(tr("Bitcoin Wallet"));
60 setWindowIcon(QIcon(":icons/bitcoin"));
67 QMenu *file = menuBar()->addMenu(tr("&File"));
68 file->addAction(sendCoinsAction);
69 file->addAction(receiveCoinsAction);
71 file->addAction(quitAction);
73 QMenu *settings = menuBar()->addMenu(tr("&Settings"));
74 settings->addAction(encryptWalletAction);
75 settings->addAction(changePassphraseAction);
76 settings->addSeparator();
77 settings->addAction(optionsAction);
79 QMenu *help = menuBar()->addMenu(tr("&Help"));
80 help->addAction(aboutAction);
83 QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
84 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
85 toolbar->addAction(overviewAction);
86 toolbar->addAction(sendCoinsAction);
87 toolbar->addAction(receiveCoinsAction);
88 toolbar->addAction(historyAction);
89 toolbar->addAction(addressBookAction);
91 QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
92 toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
93 toolbar2->addAction(exportAction);
96 overviewPage = new OverviewPage();
98 transactionsPage = new QWidget(this);
99 QVBoxLayout *vbox = new QVBoxLayout();
100 transactionView = new TransactionView(this);
101 vbox->addWidget(transactionView);
102 transactionsPage->setLayout(vbox);
104 addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
106 receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
108 sendCoinsPage = new SendCoinsDialog(this);
110 centralWidget = new QStackedWidget(this);
111 centralWidget->addWidget(overviewPage);
112 centralWidget->addWidget(transactionsPage);
113 centralWidget->addWidget(addressBookPage);
114 centralWidget->addWidget(receiveCoinsPage);
115 centralWidget->addWidget(sendCoinsPage);
116 setCentralWidget(centralWidget);
121 // Status bar notification icons
122 QFrame *frameBlocks = new QFrame();
123 //frameBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
124 frameBlocks->setContentsMargins(0,0,0,0);
125 frameBlocks->setMinimumWidth(56);
126 frameBlocks->setMaximumWidth(56);
127 QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
128 frameBlocksLayout->setContentsMargins(3,0,3,0);
129 frameBlocksLayout->setSpacing(3);
130 labelEncryptionIcon = new QLabel();
131 labelConnectionsIcon = new QLabel();
132 labelBlocksIcon = new QLabel();
133 frameBlocksLayout->addStretch();
134 frameBlocksLayout->addWidget(labelEncryptionIcon);
135 frameBlocksLayout->addStretch();
136 frameBlocksLayout->addWidget(labelConnectionsIcon);
137 frameBlocksLayout->addStretch();
138 frameBlocksLayout->addWidget(labelBlocksIcon);
139 frameBlocksLayout->addStretch();
141 // Progress bar for blocks download
142 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
143 progressBarLabel->setVisible(false);
144 progressBar = new QProgressBar();
145 progressBar->setToolTip(tr("Block chain synchronization in progress"));
146 progressBar->setVisible(false);
148 statusBar()->addWidget(progressBarLabel);
149 statusBar()->addWidget(progressBar);
150 statusBar()->addPermanentWidget(frameBlocks);
154 syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
156 // Clicking on a transaction on the overview page simply sends you to transaction history page
157 connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
159 // Doubleclicking on a transaction on the transaction history page shows details
160 connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
165 void BitcoinGUI::createActions()
167 QActionGroup *tabGroup = new QActionGroup(this);
169 overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
170 overviewAction->setToolTip(tr("Show general overview of wallet"));
171 overviewAction->setCheckable(true);
172 tabGroup->addAction(overviewAction);
174 historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
175 historyAction->setToolTip(tr("Browse transaction history"));
176 historyAction->setCheckable(true);
177 tabGroup->addAction(historyAction);
179 addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
180 addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
181 addressBookAction->setCheckable(true);
182 tabGroup->addAction(addressBookAction);
184 receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
185 receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
186 receiveCoinsAction->setCheckable(true);
187 tabGroup->addAction(receiveCoinsAction);
189 sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
190 sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
191 sendCoinsAction->setCheckable(true);
192 tabGroup->addAction(sendCoinsAction);
194 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
195 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
196 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
197 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
198 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
200 quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
201 quitAction->setToolTip(tr("Quit application"));
202 aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
203 aboutAction->setToolTip(tr("Show information about Bitcoin"));
204 optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
205 optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
206 openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
207 openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
208 exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
209 exportAction->setToolTip(tr("Export the current view to a file"));
210 encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
211 encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
212 encryptWalletAction->setCheckable(true);
213 changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
214 changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
216 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
217 connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
218 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
219 connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
220 connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
221 connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
224 void BitcoinGUI::setClientModel(ClientModel *clientModel)
226 this->clientModel = clientModel;
228 if(clientModel->isTestNet())
230 QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
231 setWindowTitle(title_testnet);
232 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
235 trayIcon->setToolTip(title_testnet);
236 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
240 // Keep up to date with client
241 setNumConnections(clientModel->getNumConnections());
242 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
244 setNumBlocks(clientModel->getNumBlocks());
245 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
247 // Report errors from network/worker thread
248 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
251 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
253 this->walletModel = walletModel;
255 // Report errors from wallet thread
256 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
258 // Put transaction list in tabs
259 transactionView->setModel(walletModel);
261 overviewPage->setModel(walletModel);
262 addressBookPage->setModel(walletModel->getAddressTableModel());
263 receiveCoinsPage->setModel(walletModel->getAddressTableModel());
264 sendCoinsPage->setModel(walletModel);
266 setEncryptionStatus(walletModel->getEncryptionStatus());
267 connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
269 // Balloon popup for new transaction
270 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
271 this, SLOT(incomingTransaction(QModelIndex,int,int)));
273 // Ask for passphrase if needed
274 connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
277 void BitcoinGUI::createTrayIcon()
279 QMenu *trayIconMenu = new QMenu(this);
280 trayIconMenu->addAction(openBitcoinAction);
281 trayIconMenu->addAction(optionsAction);
282 trayIconMenu->addSeparator();
283 trayIconMenu->addAction(quitAction);
285 trayIcon = new QSystemTrayIcon(this);
286 trayIcon->setContextMenu(trayIconMenu);
287 trayIcon->setToolTip("Bitcoin client");
288 trayIcon->setIcon(QIcon(":/icons/toolbar"));
289 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
290 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
293 notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
296 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
298 if(reason == QSystemTrayIcon::Trigger)
300 // Click on system tray icon triggers "open bitcoin"
301 openBitcoinAction->trigger();
306 void BitcoinGUI::optionsClicked()
309 dlg.setModel(clientModel->getOptionsModel());
313 void BitcoinGUI::aboutClicked()
316 dlg.setModel(clientModel);
320 void BitcoinGUI::setNumConnections(int count)
325 case 0: icon = ":/icons/connect_0"; break;
326 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
327 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
328 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
329 default: icon = ":/icons/connect_4"; break;
331 labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
332 labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
335 void BitcoinGUI::setNumBlocks(int count)
337 int initTotal = clientModel->getNumBlocksAtStartup();
338 int total = clientModel->getNumBlocksOfPeers();
343 progressBarLabel->setVisible(true);
344 progressBar->setVisible(true);
345 progressBar->setMaximum(total - initTotal);
346 progressBar->setValue(count - initTotal);
347 tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
351 progressBarLabel->setVisible(false);
352 progressBar->setVisible(false);
353 tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
356 QDateTime now = QDateTime::currentDateTime();
357 QDateTime lastBlockDate = clientModel->getLastBlockDate();
358 int secs = lastBlockDate.secsTo(now);
361 // Represent time from last generated block in human readable text
364 text = tr("%n second(s) ago","",secs);
366 else if(secs < 60*60)
368 text = tr("%n minute(s) ago","",secs/60);
370 else if(secs < 24*60*60)
372 text = tr("%n hour(s) ago","",secs/(60*60));
376 text = tr("%n day(s) ago","",secs/(60*60*24));
379 // Set icon state: spinning if catching up, tick otherwise
382 tooltip = tr("Up to date") + QString("\n") + tooltip;
383 labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
387 tooltip = tr("Catching up...") + QString("\n") + tooltip;
388 labelBlocksIcon->setMovie(syncIconMovie);
389 syncIconMovie->start();
392 tooltip += QString("\n");
393 tooltip += tr("Last received block was generated %1.").arg(text);
395 labelBlocksIcon->setToolTip(tooltip);
396 progressBarLabel->setToolTip(tooltip);
397 progressBar->setToolTip(tooltip);
400 void BitcoinGUI::error(const QString &title, const QString &message)
402 // Report errors from network/worker thread
403 notificator->notify(Notificator::Critical, title, message);
406 void BitcoinGUI::changeEvent(QEvent *e)
408 if (e->type() == QEvent::WindowStateChange)
410 if(clientModel->getOptionsModel()->getMinimizeToTray())
424 QMainWindow::changeEvent(e);
427 void BitcoinGUI::closeEvent(QCloseEvent *event)
429 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
430 !clientModel->getOptionsModel()->getMinimizeOnClose())
434 QMainWindow::closeEvent(event);
437 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
440 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
441 "which goes to the nodes that process your transaction and helps to support the network. "
442 "Do you want to pay the fee?").arg(
443 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
444 QMessageBox::StandardButton retval = QMessageBox::question(
445 this, tr("Sending..."), strMessage,
446 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
447 *payFee = (retval == QMessageBox::Yes);
450 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
452 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
453 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
454 .data(Qt::EditRole).toULongLong();
455 if(!clientModel->inInitialBlockDownload())
457 // On new transaction, make an info balloon
458 // Unless the initial block download is in progress, to prevent balloon-spam
459 QString date = ttm->index(start, TransactionTableModel::Date, parent)
461 QString type = ttm->index(start, TransactionTableModel::Type, parent)
463 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
465 QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
466 TransactionTableModel::ToAddress, parent)
467 .data(Qt::DecorationRole));
469 notificator->notify(Notificator::Information,
470 (amount)<0 ? tr("Sent transaction") :
471 tr("Incoming transaction"),
477 .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
479 .arg(address), icon);
483 void BitcoinGUI::gotoOverviewPage()
485 overviewAction->setChecked(true);
486 centralWidget->setCurrentWidget(overviewPage);
488 exportAction->setEnabled(false);
489 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
492 void BitcoinGUI::gotoHistoryPage()
494 historyAction->setChecked(true);
495 centralWidget->setCurrentWidget(transactionsPage);
497 exportAction->setEnabled(true);
498 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
499 connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
502 void BitcoinGUI::gotoAddressBookPage()
504 addressBookAction->setChecked(true);
505 centralWidget->setCurrentWidget(addressBookPage);
507 exportAction->setEnabled(true);
508 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
509 connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
512 void BitcoinGUI::gotoReceiveCoinsPage()
514 receiveCoinsAction->setChecked(true);
515 centralWidget->setCurrentWidget(receiveCoinsPage);
517 exportAction->setEnabled(true);
518 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
519 connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
522 void BitcoinGUI::gotoSendCoinsPage()
524 sendCoinsAction->setChecked(true);
525 if(centralWidget->currentWidget() != sendCoinsPage)
527 // Clear the current contents if we arrived from another tab
528 sendCoinsPage->clear();
530 centralWidget->setCurrentWidget(sendCoinsPage);
532 exportAction->setEnabled(false);
533 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
536 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
539 if(event->mimeData()->hasUrls())
540 event->acceptProposedAction();
543 void BitcoinGUI::dropEvent(QDropEvent *event)
545 if(event->mimeData()->hasUrls())
548 QList<QUrl> urls = event->mimeData()->urls();
549 foreach(const QUrl &url, urls)
551 sendCoinsPage->handleURL(&url);
555 event->acceptProposedAction();
558 void BitcoinGUI::setEncryptionStatus(int status)
562 case WalletModel::Unencrypted:
563 labelEncryptionIcon->hide();
564 encryptWalletAction->setChecked(false);
565 changePassphraseAction->setEnabled(false);
566 encryptWalletAction->setEnabled(true);
568 case WalletModel::Unlocked:
569 labelEncryptionIcon->show();
570 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
571 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
572 encryptWalletAction->setChecked(true);
573 changePassphraseAction->setEnabled(true);
574 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
576 case WalletModel::Locked:
577 labelEncryptionIcon->show();
578 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
579 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
580 encryptWalletAction->setChecked(true);
581 changePassphraseAction->setEnabled(true);
582 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
587 void BitcoinGUI::encryptWallet(bool status)
589 AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
590 AskPassphraseDialog::Decrypt, this);
591 dlg.setModel(walletModel);
594 setEncryptionStatus(walletModel->getEncryptionStatus());
597 void BitcoinGUI::changePassphrase()
599 AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
600 dlg.setModel(walletModel);
604 void BitcoinGUI::unlockWallet()
606 // Unlock wallet when requested by wallet model
607 if(walletModel->getEncryptionStatus() == WalletModel::Locked)
609 AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
610 dlg.setModel(walletModel);