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 overviewAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_1));
168 tabGroup->addAction(overviewAction);
170 historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
171 historyAction->setToolTip(tr("Browse transaction history"));
172 historyAction->setCheckable(true);
173 historyAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_4));
174 tabGroup->addAction(historyAction);
176 addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
177 addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
178 addressBookAction->setCheckable(true);
179 addressBookAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_5));
180 tabGroup->addAction(addressBookAction);
182 receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
183 receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
184 receiveCoinsAction->setCheckable(true);
185 receiveCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_3));
186 tabGroup->addAction(receiveCoinsAction);
188 sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
189 sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
190 sendCoinsAction->setCheckable(true);
191 sendCoinsAction->setShortcut(QKeySequence(Qt::ALT + Qt::Key_2));
192 tabGroup->addAction(sendCoinsAction);
194 connect(overviewAction, SIGNAL(triggered()), this, SLOT(show()));
195 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
196 connect(historyAction, SIGNAL(triggered()), this, SLOT(show()));
197 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
198 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(show()));
199 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
200 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(show()));
201 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
202 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(show()));
203 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
205 quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
206 quitAction->setToolTip(tr("Quit application"));
207 quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
208 quitAction->setMenuRole(QAction::QuitRole);
209 aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About %1").arg(qApp->applicationName()), this);
210 aboutAction->setToolTip(tr("Show information about Bitcoin"));
211 aboutAction->setMenuRole(QAction::AboutQtRole);
212 optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
213 optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
214 optionsAction->setMenuRole(QAction::PreferencesRole);
215 openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
216 openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
217 exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
218 exportAction->setToolTip(tr("Export the current view to a file"));
219 encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
220 encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
221 encryptWalletAction->setCheckable(true);
222 changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
223 changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
225 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
226 connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
227 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
228 connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
229 connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
230 connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
233 void BitcoinGUI::createMenuBar()
236 // Create a decoupled menu bar on Mac which stays even if the window is closed
237 appMenuBar = new QMenuBar();
239 // Get the main window's menu bar on other platforms
240 appMenuBar = menuBar();
243 // Configure the menus
244 QMenu *file = appMenuBar->addMenu(tr("&File"));
245 file->addAction(quitAction);
247 QMenu *settings = appMenuBar->addMenu(tr("&Settings"));
248 settings->addAction(encryptWalletAction);
249 settings->addAction(changePassphraseAction);
250 settings->addSeparator();
251 settings->addAction(optionsAction);
253 QMenu *help = appMenuBar->addMenu(tr("&Help"));
254 help->addAction(aboutAction);
257 void BitcoinGUI::createToolBars()
259 QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
260 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
261 toolbar->addAction(overviewAction);
262 toolbar->addAction(sendCoinsAction);
263 toolbar->addAction(receiveCoinsAction);
264 toolbar->addAction(historyAction);
265 toolbar->addAction(addressBookAction);
267 QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
268 toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
269 toolbar2->addAction(exportAction);
272 void BitcoinGUI::setClientModel(ClientModel *clientModel)
274 this->clientModel = clientModel;
277 if(clientModel->isTestNet())
279 QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
280 setWindowTitle(title_testnet);
282 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
284 MacDockIconHandler::instance()->setIcon(QIcon(":icons/bitcoin_testnet"));
288 trayIcon->setToolTip(title_testnet);
289 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
293 // Keep up to date with client
294 setNumConnections(clientModel->getNumConnections());
295 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
297 setNumBlocks(clientModel->getNumBlocks());
298 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
300 // Report errors from network/worker thread
301 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
305 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
307 this->walletModel = walletModel;
310 // Report errors from wallet thread
311 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
313 // Put transaction list in tabs
314 transactionView->setModel(walletModel);
316 overviewPage->setModel(walletModel);
317 addressBookPage->setModel(walletModel->getAddressTableModel());
318 receiveCoinsPage->setModel(walletModel->getAddressTableModel());
319 sendCoinsPage->setModel(walletModel);
321 setEncryptionStatus(walletModel->getEncryptionStatus());
322 connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
324 // Balloon popup for new transaction
325 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
326 this, SLOT(incomingTransaction(QModelIndex,int,int)));
328 // Ask for passphrase if needed
329 connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
333 void BitcoinGUI::createTrayIcon()
337 trayIcon = new QSystemTrayIcon(this);
338 trayIconMenu = new QMenu(this);
339 trayIcon->setContextMenu(trayIconMenu);
340 trayIcon->setToolTip("Bitcoin client");
341 trayIcon->setIcon(QIcon(":/icons/toolbar"));
342 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
343 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
346 // Note: On Mac, the dock icon is used to provide the tray's functionality.
347 MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
348 connect(dockIconHandler, SIGNAL(dockIconClicked()), openBitcoinAction, SLOT(trigger()));
349 trayIconMenu = dockIconHandler->dockMenu();
352 // Configuration of the tray icon (or dock icon) icon menu
353 trayIconMenu->addAction(openBitcoinAction);
354 trayIconMenu->addSeparator();
355 trayIconMenu->addAction(receiveCoinsAction);
356 trayIconMenu->addAction(sendCoinsAction);
357 trayIconMenu->addSeparator();
358 trayIconMenu->addAction(optionsAction);
359 #ifndef Q_WS_MAC // This is built-in on Mac
360 trayIconMenu->addSeparator();
361 trayIconMenu->addAction(quitAction);
364 notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
368 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
370 if(reason == QSystemTrayIcon::Trigger)
372 // Click on system tray icon triggers "open bitcoin"
373 openBitcoinAction->trigger();
379 void BitcoinGUI::optionsClicked()
381 if(!clientModel || !clientModel->getOptionsModel())
384 dlg.setModel(clientModel->getOptionsModel());
388 void BitcoinGUI::aboutClicked()
391 dlg.setModel(clientModel);
395 void BitcoinGUI::setNumConnections(int count)
400 case 0: icon = ":/icons/connect_0"; break;
401 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
402 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
403 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
404 default: icon = ":/icons/connect_4"; break;
406 labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
407 labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
410 void BitcoinGUI::setNumBlocks(int count)
414 int initTotal = clientModel->getNumBlocksAtStartup();
415 int total = clientModel->getNumBlocksOfPeers();
420 progressBarLabel->setVisible(true);
421 progressBar->setVisible(true);
422 progressBar->setMaximum(total - initTotal);
423 progressBar->setValue(count - initTotal);
424 tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
428 progressBarLabel->setVisible(false);
429 progressBar->setVisible(false);
430 tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
433 QDateTime now = QDateTime::currentDateTime();
434 QDateTime lastBlockDate = clientModel->getLastBlockDate();
435 int secs = lastBlockDate.secsTo(now);
438 // Represent time from last generated block in human readable text
441 text = tr("%n second(s) ago","",secs);
443 else if(secs < 60*60)
445 text = tr("%n minute(s) ago","",secs/60);
447 else if(secs < 24*60*60)
449 text = tr("%n hour(s) ago","",secs/(60*60));
453 text = tr("%n day(s) ago","",secs/(60*60*24));
456 // Set icon state: spinning if catching up, tick otherwise
459 tooltip = tr("Up to date") + QString("\n") + tooltip;
460 labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
464 tooltip = tr("Catching up...") + QString("\n") + tooltip;
465 labelBlocksIcon->setMovie(syncIconMovie);
466 syncIconMovie->start();
469 tooltip += QString("\n");
470 tooltip += tr("Last received block was generated %1.").arg(text);
472 labelBlocksIcon->setToolTip(tooltip);
473 progressBarLabel->setToolTip(tooltip);
474 progressBar->setToolTip(tooltip);
477 void BitcoinGUI::error(const QString &title, const QString &message)
479 // Report errors from network/worker thread
480 notificator->notify(Notificator::Critical, title, message);
483 void BitcoinGUI::changeEvent(QEvent *e)
485 #ifndef Q_WS_MAC // Ignored on Mac
486 if(e->type() == QEvent::WindowStateChange)
488 if(clientModel && clientModel->getOptionsModel()->getMinimizeToTray())
503 QMainWindow::changeEvent(e);
506 void BitcoinGUI::closeEvent(QCloseEvent *event)
510 #ifndef Q_WS_MAC // Ignored on Mac
511 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
512 !clientModel->getOptionsModel()->getMinimizeOnClose())
518 QMainWindow::closeEvent(event);
521 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
524 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
525 "which goes to the nodes that process your transaction and helps to support the network. "
526 "Do you want to pay the fee?").arg(
527 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
528 QMessageBox::StandardButton retval = QMessageBox::question(
529 this, tr("Sending..."), strMessage,
530 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
531 *payFee = (retval == QMessageBox::Yes);
534 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
536 if(!walletModel || !clientModel)
538 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
539 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
540 .data(Qt::EditRole).toULongLong();
541 if(!clientModel->inInitialBlockDownload())
543 // On new transaction, make an info balloon
544 // Unless the initial block download is in progress, to prevent balloon-spam
545 QString date = ttm->index(start, TransactionTableModel::Date, parent)
547 QString type = ttm->index(start, TransactionTableModel::Type, parent)
549 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
551 QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
552 TransactionTableModel::ToAddress, parent)
553 .data(Qt::DecorationRole));
555 notificator->notify(Notificator::Information,
556 (amount)<0 ? tr("Sent transaction") :
557 tr("Incoming transaction"),
563 .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
565 .arg(address), icon);
569 void BitcoinGUI::gotoOverviewPage()
571 overviewAction->setChecked(true);
572 centralWidget->setCurrentWidget(overviewPage);
574 exportAction->setEnabled(false);
575 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
578 void BitcoinGUI::gotoHistoryPage()
580 historyAction->setChecked(true);
581 centralWidget->setCurrentWidget(transactionsPage);
583 exportAction->setEnabled(true);
584 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
585 connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
588 void BitcoinGUI::gotoAddressBookPage()
590 addressBookAction->setChecked(true);
591 centralWidget->setCurrentWidget(addressBookPage);
593 exportAction->setEnabled(true);
594 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
595 connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
598 void BitcoinGUI::gotoReceiveCoinsPage()
600 receiveCoinsAction->setChecked(true);
601 centralWidget->setCurrentWidget(receiveCoinsPage);
603 exportAction->setEnabled(true);
604 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
605 connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
608 void BitcoinGUI::gotoSendCoinsPage()
610 sendCoinsAction->setChecked(true);
611 centralWidget->setCurrentWidget(sendCoinsPage);
613 exportAction->setEnabled(false);
614 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
617 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
620 if(event->mimeData()->hasUrls())
621 event->acceptProposedAction();
624 void BitcoinGUI::dropEvent(QDropEvent *event)
626 if(event->mimeData()->hasUrls())
629 QList<QUrl> urls = event->mimeData()->urls();
630 foreach(const QUrl &url, urls)
632 sendCoinsPage->handleURL(&url);
636 event->acceptProposedAction();
639 void BitcoinGUI::setEncryptionStatus(int status)
643 case WalletModel::Unencrypted:
644 labelEncryptionIcon->hide();
645 encryptWalletAction->setChecked(false);
646 changePassphraseAction->setEnabled(false);
647 encryptWalletAction->setEnabled(true);
649 case WalletModel::Unlocked:
650 labelEncryptionIcon->show();
651 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
652 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
653 encryptWalletAction->setChecked(true);
654 changePassphraseAction->setEnabled(true);
655 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
657 case WalletModel::Locked:
658 labelEncryptionIcon->show();
659 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
660 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
661 encryptWalletAction->setChecked(true);
662 changePassphraseAction->setEnabled(true);
663 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
668 void BitcoinGUI::encryptWallet(bool status)
672 AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
673 AskPassphraseDialog::Decrypt, this);
674 dlg.setModel(walletModel);
677 setEncryptionStatus(walletModel->getEncryptionStatus());
680 void BitcoinGUI::changePassphrase()
682 AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
683 dlg.setModel(walletModel);
687 void BitcoinGUI::unlockWallet()
691 // Unlock wallet when requested by wallet model
692 if(walletModel->getEncryptionStatus() == WalletModel::Locked)
694 AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
695 dlg.setModel(walletModel);