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"
26 #include "macdockiconhandler.h"
29 #include <QApplication>
30 #include <QMainWindow>
35 #include <QVBoxLayout>
40 #include <QPushButton>
42 #include <QMessageBox>
43 #include <QProgressBar>
44 #include <QStackedWidget>
48 #include <QDragEnterEvent>
53 BitcoinGUI::BitcoinGUI(QWidget *parent):
57 encryptWalletAction(0),
58 changePassphraseAction(0),
63 setWindowTitle(tr("Bitcoin Wallet"));
65 setWindowIcon(QIcon(":icons/bitcoin"));
67 setUnifiedTitleAndToolBarOnMac(true);
68 QApplication::setAttribute(Qt::AA_DontShowIconsInMenus);
73 // Create actions for the toolbar, menu bar and tray/dock icon
76 // Create application menu bar
79 // Create the toolbars
82 // Create the tray icon (or setup the dock icon)
86 overviewPage = new OverviewPage();
88 transactionsPage = new QWidget(this);
89 QVBoxLayout *vbox = new QVBoxLayout();
90 transactionView = new TransactionView(this);
91 vbox->addWidget(transactionView);
92 transactionsPage->setLayout(vbox);
94 addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
96 receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
98 sendCoinsPage = new SendCoinsDialog(this);
100 centralWidget = new QStackedWidget(this);
101 centralWidget->addWidget(overviewPage);
102 centralWidget->addWidget(transactionsPage);
103 centralWidget->addWidget(addressBookPage);
104 centralWidget->addWidget(receiveCoinsPage);
105 centralWidget->addWidget(sendCoinsPage);
106 setCentralWidget(centralWidget);
111 // Status bar notification icons
112 QFrame *frameBlocks = new QFrame();
113 //frameBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
114 frameBlocks->setContentsMargins(0,0,0,0);
115 frameBlocks->setMinimumWidth(56);
116 frameBlocks->setMaximumWidth(56);
117 QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
118 frameBlocksLayout->setContentsMargins(3,0,3,0);
119 frameBlocksLayout->setSpacing(3);
120 labelEncryptionIcon = new QLabel();
121 labelConnectionsIcon = new QLabel();
122 labelBlocksIcon = new QLabel();
123 frameBlocksLayout->addStretch();
124 frameBlocksLayout->addWidget(labelEncryptionIcon);
125 frameBlocksLayout->addStretch();
126 frameBlocksLayout->addWidget(labelConnectionsIcon);
127 frameBlocksLayout->addStretch();
128 frameBlocksLayout->addWidget(labelBlocksIcon);
129 frameBlocksLayout->addStretch();
131 // Progress bar for blocks download
132 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
133 progressBarLabel->setVisible(false);
134 progressBar = new QProgressBar();
135 progressBar->setToolTip(tr("Block chain synchronization in progress"));
136 progressBar->setVisible(false);
138 statusBar()->addWidget(progressBarLabel);
139 statusBar()->addWidget(progressBar);
140 statusBar()->addPermanentWidget(frameBlocks);
142 syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
144 // Clicking on a transaction on the overview page simply sends you to transaction history page
145 connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
147 // Doubleclicking on a transaction on the transaction history page shows details
148 connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
153 BitcoinGUI::~BitcoinGUI()
160 void BitcoinGUI::createActions()
162 QActionGroup *tabGroup = new QActionGroup(this);
164 overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
165 overviewAction->setToolTip(tr("Show general overview of wallet"));
166 overviewAction->setCheckable(true);
167 tabGroup->addAction(overviewAction);
169 historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
170 historyAction->setToolTip(tr("Browse transaction history"));
171 historyAction->setCheckable(true);
172 tabGroup->addAction(historyAction);
174 addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
175 addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
176 addressBookAction->setCheckable(true);
177 tabGroup->addAction(addressBookAction);
179 receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
180 receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
181 receiveCoinsAction->setCheckable(true);
182 tabGroup->addAction(receiveCoinsAction);
184 sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
185 sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
186 sendCoinsAction->setCheckable(true);
187 tabGroup->addAction(sendCoinsAction);
189 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
190 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
191 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
192 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
193 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
195 quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
196 quitAction->setToolTip(tr("Quit application"));
197 quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
198 quitAction->setMenuRole(QAction::QuitRole);
199 aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About %1").arg(qApp->applicationName()), this);
200 aboutAction->setToolTip(tr("Show information about Bitcoin"));
201 aboutAction->setMenuRole(QAction::AboutQtRole);
202 optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
203 optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
204 optionsAction->setMenuRole(QAction::PreferencesRole);
205 openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
206 openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
207 exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
208 exportAction->setToolTip(tr("Export the current view to a file"));
209 encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
210 encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
211 encryptWalletAction->setCheckable(true);
212 changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
213 changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
215 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
216 connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
217 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
218 connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
219 connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
220 connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
223 void BitcoinGUI::createMenuBar()
226 // Create a decoupled menu bar on Mac which stays even if the window is closed
227 appMenuBar = new QMenuBar();
229 // Get the main window's menu bar on other platforms
230 appMenuBar = menuBar();
233 // Configure the menus
234 QMenu *file = appMenuBar->addMenu(tr("&File"));
235 file->addAction(quitAction);
237 QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
238 settings->addAction(encryptWalletAction);
239 settings->addAction(changePassphraseAction);
240 settings->addSeparator();
241 settings->addAction(optionsAction);
243 QMenu *help = appMenuBar->addMenu(tr("&Help"));
244 help->addAction(aboutAction);
247 void BitcoinGUI::createToolBars()
249 QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
250 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
251 toolbar->addAction(overviewAction);
252 toolbar->addAction(sendCoinsAction);
253 toolbar->addAction(receiveCoinsAction);
254 toolbar->addAction(historyAction);
255 toolbar->addAction(addressBookAction);
257 QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
258 toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
259 toolbar2->addAction(exportAction);
262 void BitcoinGUI::setClientModel(ClientModel *clientModel)
264 this->clientModel = clientModel;
266 if(clientModel->isTestNet())
268 QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
269 setWindowTitle(title_testnet);
271 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
273 MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
277 trayIcon->setToolTip(title_testnet);
278 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
282 // Keep up to date with client
283 setNumConnections(clientModel->getNumConnections());
284 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
286 setNumBlocks(clientModel->getNumBlocks());
287 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
289 // Report errors from network/worker thread
290 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
293 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
295 this->walletModel = walletModel;
297 // Report errors from wallet thread
298 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
300 // Put transaction list in tabs
301 transactionView->setModel(walletModel);
303 overviewPage->setModel(walletModel);
304 addressBookPage->setModel(walletModel->getAddressTableModel());
305 receiveCoinsPage->setModel(walletModel->getAddressTableModel());
306 sendCoinsPage->setModel(walletModel);
308 setEncryptionStatus(walletModel->getEncryptionStatus());
309 connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
311 // Balloon popup for new transaction
312 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
313 this, SLOT(incomingTransaction(QModelIndex,int,int)));
315 // Ask for passphrase if needed
316 connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
319 void BitcoinGUI::createTrayIcon()
323 trayIcon = new QSystemTrayIcon(this);
324 trayIconMenu = new QMenu(this);
325 trayIcon->setContextMenu(trayIconMenu);
326 trayIcon->setToolTip("Bitcoin client");
327 trayIcon->setIcon(QIcon(":/icons/toolbar"));
328 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
329 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
332 // Note: On Mac, the dock icon is used to provide the tray's functionality.
333 MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
334 connect(dockIconHandler, SIGNAL(dockIconClicked()), openBitcoinAction, SLOT(trigger()));
335 trayIconMenu = dockIconHandler->dockMenu();
338 // Configuration of the tray icon (or dock icon) icon menu
339 trayIconMenu->addAction(openBitcoinAction);
340 trayIconMenu->addSeparator();
341 trayIconMenu->addAction(receiveCoinsAction);
342 trayIconMenu->addAction(sendCoinsAction);
343 trayIconMenu->addSeparator();
344 trayIconMenu->addAction(optionsAction);
345 #ifndef Q_WS_MAC // This is built-in on Mac
346 trayIconMenu->addSeparator();
347 trayIconMenu->addAction(quitAction);
350 notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
354 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
356 if(reason == QSystemTrayIcon::Trigger)
358 // Click on system tray icon triggers "open bitcoin"
359 openBitcoinAction->trigger();
365 void BitcoinGUI::optionsClicked()
368 dlg.setModel(clientModel->getOptionsModel());
372 void BitcoinGUI::aboutClicked()
375 dlg.setModel(clientModel);
379 void BitcoinGUI::setNumConnections(int count)
384 case 0: icon = ":/icons/connect_0"; break;
385 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
386 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
387 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
388 default: icon = ":/icons/connect_4"; break;
390 labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
391 labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
394 void BitcoinGUI::setNumBlocks(int count)
396 int initTotal = clientModel->getNumBlocksAtStartup();
397 int total = clientModel->getNumBlocksOfPeers();
402 progressBarLabel->setVisible(true);
403 progressBar->setVisible(true);
404 progressBar->setMaximum(total - initTotal);
405 progressBar->setValue(count - initTotal);
406 tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
410 progressBarLabel->setVisible(false);
411 progressBar->setVisible(false);
412 tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
415 QDateTime now = QDateTime::currentDateTime();
416 QDateTime lastBlockDate = clientModel->getLastBlockDate();
417 int secs = lastBlockDate.secsTo(now);
420 // Represent time from last generated block in human readable text
423 text = tr("%n second(s) ago","",secs);
425 else if(secs < 60*60)
427 text = tr("%n minute(s) ago","",secs/60);
429 else if(secs < 24*60*60)
431 text = tr("%n hour(s) ago","",secs/(60*60));
435 text = tr("%n day(s) ago","",secs/(60*60*24));
438 // Set icon state: spinning if catching up, tick otherwise
441 tooltip = tr("Up to date") + QString("\n") + tooltip;
442 labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
446 tooltip = tr("Catching up...") + QString("\n") + tooltip;
447 labelBlocksIcon->setMovie(syncIconMovie);
448 syncIconMovie->start();
451 tooltip += QString("\n");
452 tooltip += tr("Last received block was generated %1.").arg(text);
454 labelBlocksIcon->setToolTip(tooltip);
455 progressBarLabel->setToolTip(tooltip);
456 progressBar->setToolTip(tooltip);
459 void BitcoinGUI::error(const QString &title, const QString &message)
461 // Report errors from network/worker thread
462 notificator->notify(Notificator::Critical, title, message);
465 void BitcoinGUI::changeEvent(QEvent *e)
467 #ifndef Q_WS_MAC // Ignored on Mac
468 if (e->type() == QEvent::WindowStateChange)
470 if (clientModel->getOptionsModel()->getMinimizeToTray())
485 QMainWindow::changeEvent(e);
488 void BitcoinGUI::closeEvent(QCloseEvent *event)
490 #ifndef Q_WS_MAC // Ignored on Mac
491 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
492 !clientModel->getOptionsModel()->getMinimizeOnClose())
497 QMainWindow::closeEvent(event);
500 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
503 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
504 "which goes to the nodes that process your transaction and helps to support the network. "
505 "Do you want to pay the fee?").arg(
506 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
507 QMessageBox::StandardButton retval = QMessageBox::question(
508 this, tr("Sending..."), strMessage,
509 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
510 *payFee = (retval == QMessageBox::Yes);
513 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
515 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
516 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
517 .data(Qt::EditRole).toULongLong();
518 if(!clientModel->inInitialBlockDownload())
520 // On new transaction, make an info balloon
521 // Unless the initial block download is in progress, to prevent balloon-spam
522 QString date = ttm->index(start, TransactionTableModel::Date, parent)
524 QString type = ttm->index(start, TransactionTableModel::Type, parent)
526 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
528 QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
529 TransactionTableModel::ToAddress, parent)
530 .data(Qt::DecorationRole));
532 notificator->notify(Notificator::Information,
533 (amount)<0 ? tr("Sent transaction") :
534 tr("Incoming transaction"),
540 .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
542 .arg(address), icon);
546 void BitcoinGUI::gotoOverviewPage()
549 overviewAction->setChecked(true);
550 centralWidget->setCurrentWidget(overviewPage);
552 exportAction->setEnabled(false);
553 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
556 void BitcoinGUI::gotoHistoryPage()
559 historyAction->setChecked(true);
560 centralWidget->setCurrentWidget(transactionsPage);
562 exportAction->setEnabled(true);
563 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
564 connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
567 void BitcoinGUI::gotoAddressBookPage()
570 addressBookAction->setChecked(true);
571 centralWidget->setCurrentWidget(addressBookPage);
573 exportAction->setEnabled(true);
574 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
575 connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
578 void BitcoinGUI::gotoReceiveCoinsPage()
581 receiveCoinsAction->setChecked(true);
582 centralWidget->setCurrentWidget(receiveCoinsPage);
584 exportAction->setEnabled(true);
585 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
586 connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
589 void BitcoinGUI::gotoSendCoinsPage()
592 sendCoinsAction->setChecked(true);
593 centralWidget->setCurrentWidget(sendCoinsPage);
595 exportAction->setEnabled(false);
596 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
599 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
602 if(event->mimeData()->hasUrls())
603 event->acceptProposedAction();
606 void BitcoinGUI::dropEvent(QDropEvent *event)
608 if(event->mimeData()->hasUrls())
611 QList<QUrl> urls = event->mimeData()->urls();
612 foreach(const QUrl &url, urls)
614 sendCoinsPage->handleURL(&url);
618 event->acceptProposedAction();
621 void BitcoinGUI::setEncryptionStatus(int status)
625 case WalletModel::Unencrypted:
626 labelEncryptionIcon->hide();
627 encryptWalletAction->setChecked(false);
628 changePassphraseAction->setEnabled(false);
629 encryptWalletAction->setEnabled(true);
631 case WalletModel::Unlocked:
632 labelEncryptionIcon->show();
633 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
634 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
635 encryptWalletAction->setChecked(true);
636 changePassphraseAction->setEnabled(true);
637 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
639 case WalletModel::Locked:
640 labelEncryptionIcon->show();
641 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
642 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
643 encryptWalletAction->setChecked(true);
644 changePassphraseAction->setEnabled(true);
645 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
650 void BitcoinGUI::encryptWallet(bool status)
652 AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
653 AskPassphraseDialog::Decrypt, this);
654 dlg.setModel(walletModel);
657 setEncryptionStatus(walletModel->getEncryptionStatus());
660 void BitcoinGUI::changePassphrase()
662 AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
663 dlg.setModel(walletModel);
667 void BitcoinGUI::unlockWallet()
669 // Unlock wallet when requested by wallet model
670 if(walletModel->getEncryptionStatus() == WalletModel::Locked)
672 AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
673 dlg.setModel(walletModel);