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 <QApplication>
27 #include <QMainWindow>
32 #include <QVBoxLayout>
37 #include <QPushButton>
39 #include <QMessageBox>
40 #include <QProgressBar>
41 #include <QStackedWidget>
45 #include <QDragEnterEvent>
50 BitcoinGUI::BitcoinGUI(QWidget *parent):
54 encryptWalletAction(0),
55 changePassphraseAction(0),
60 setWindowTitle(tr("Bitcoin Wallet"));
61 setWindowIcon(QIcon(":icons/bitcoin"));
68 QMenu *file = menuBar()->addMenu(tr("&File"));
69 file->addAction(sendCoinsAction);
70 file->addAction(receiveCoinsAction);
72 file->addAction(quitAction);
74 QMenu *settings = menuBar()->addMenu(tr("&Settings"));
75 settings->addAction(encryptWalletAction);
76 settings->addAction(changePassphraseAction);
77 settings->addSeparator();
78 settings->addAction(optionsAction);
80 QMenu *help = menuBar()->addMenu(tr("&Help"));
81 help->addAction(aboutAction);
84 QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
85 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
86 toolbar->addAction(overviewAction);
87 toolbar->addAction(sendCoinsAction);
88 toolbar->addAction(receiveCoinsAction);
89 toolbar->addAction(historyAction);
90 toolbar->addAction(addressBookAction);
92 QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
93 toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
94 toolbar2->addAction(exportAction);
97 overviewPage = new OverviewPage();
99 transactionsPage = new QWidget(this);
100 QVBoxLayout *vbox = new QVBoxLayout();
101 transactionView = new TransactionView(this);
102 vbox->addWidget(transactionView);
103 transactionsPage->setLayout(vbox);
105 addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
107 receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
109 sendCoinsPage = new SendCoinsDialog(this);
111 centralWidget = new QStackedWidget(this);
112 centralWidget->addWidget(overviewPage);
113 centralWidget->addWidget(transactionsPage);
114 centralWidget->addWidget(addressBookPage);
115 centralWidget->addWidget(receiveCoinsPage);
116 centralWidget->addWidget(sendCoinsPage);
117 setCentralWidget(centralWidget);
122 // Status bar notification icons
123 QFrame *frameBlocks = new QFrame();
124 //frameBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
125 frameBlocks->setContentsMargins(0,0,0,0);
126 frameBlocks->setMinimumWidth(56);
127 frameBlocks->setMaximumWidth(56);
128 QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
129 frameBlocksLayout->setContentsMargins(3,0,3,0);
130 frameBlocksLayout->setSpacing(3);
131 labelEncryptionIcon = new QLabel();
132 labelConnectionsIcon = new QLabel();
133 labelBlocksIcon = new QLabel();
134 frameBlocksLayout->addStretch();
135 frameBlocksLayout->addWidget(labelEncryptionIcon);
136 frameBlocksLayout->addStretch();
137 frameBlocksLayout->addWidget(labelConnectionsIcon);
138 frameBlocksLayout->addStretch();
139 frameBlocksLayout->addWidget(labelBlocksIcon);
140 frameBlocksLayout->addStretch();
142 // Progress bar for blocks download
143 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
144 progressBarLabel->setVisible(false);
145 progressBar = new QProgressBar();
146 progressBar->setToolTip(tr("Block chain synchronization in progress"));
147 progressBar->setVisible(false);
149 statusBar()->addWidget(progressBarLabel);
150 statusBar()->addWidget(progressBar);
151 statusBar()->addPermanentWidget(frameBlocks);
155 syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
157 // Clicking on a transaction on the overview page simply sends you to transaction history page
158 connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
160 // Doubleclicking on a transaction on the transaction history page shows details
161 connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
164 // Windows-specific customization
165 if (QtWin::isCompositionEnabled())
167 QtWin::extendFrameIntoClientArea(this);
168 setContentsMargins(0, 0, 0, 0);
171 setWindowComposition();
176 void BitcoinGUI::createActions()
178 QActionGroup *tabGroup = new QActionGroup(this);
180 overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
181 overviewAction->setToolTip(tr("Show general overview of wallet"));
182 overviewAction->setCheckable(true);
183 tabGroup->addAction(overviewAction);
185 historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
186 historyAction->setToolTip(tr("Browse transaction history"));
187 historyAction->setCheckable(true);
188 tabGroup->addAction(historyAction);
190 addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
191 addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
192 addressBookAction->setCheckable(true);
193 tabGroup->addAction(addressBookAction);
195 receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
196 receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
197 receiveCoinsAction->setCheckable(true);
198 tabGroup->addAction(receiveCoinsAction);
200 sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
201 sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
202 sendCoinsAction->setCheckable(true);
203 tabGroup->addAction(sendCoinsAction);
205 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
206 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
207 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
208 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
209 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
211 quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
212 quitAction->setToolTip(tr("Quit application"));
213 aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
214 aboutAction->setToolTip(tr("Show information about Bitcoin"));
215 optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
216 optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
217 openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
218 openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
219 exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
220 exportAction->setToolTip(tr("Export the current view to a file"));
221 encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
222 encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
223 encryptWalletAction->setCheckable(true);
224 changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
225 changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
227 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
228 connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
229 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
230 connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
231 connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
232 connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
235 void BitcoinGUI::setClientModel(ClientModel *clientModel)
237 this->clientModel = clientModel;
239 if(clientModel->isTestNet())
241 QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
242 setWindowTitle(title_testnet);
243 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
246 trayIcon->setToolTip(title_testnet);
247 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
251 // Keep up to date with client
252 setNumConnections(clientModel->getNumConnections());
253 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
255 setNumBlocks(clientModel->getNumBlocks());
256 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
258 // Report errors from network/worker thread
259 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
262 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
264 this->walletModel = walletModel;
266 // Report errors from wallet thread
267 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
269 // Put transaction list in tabs
270 transactionView->setModel(walletModel);
272 overviewPage->setModel(walletModel);
273 addressBookPage->setModel(walletModel->getAddressTableModel());
274 receiveCoinsPage->setModel(walletModel->getAddressTableModel());
275 sendCoinsPage->setModel(walletModel);
277 setEncryptionStatus(walletModel->getEncryptionStatus());
278 connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
280 // Balloon popup for new transaction
281 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
282 this, SLOT(incomingTransaction(QModelIndex,int,int)));
284 // Ask for passphrase if needed
285 connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
288 void BitcoinGUI::createTrayIcon()
290 QMenu *trayIconMenu = new QMenu(this);
291 trayIconMenu->addAction(openBitcoinAction);
292 trayIconMenu->addAction(optionsAction);
293 trayIconMenu->addSeparator();
294 trayIconMenu->addAction(quitAction);
296 trayIcon = new QSystemTrayIcon(this);
297 trayIcon->setContextMenu(trayIconMenu);
298 trayIcon->setToolTip("Bitcoin client");
299 trayIcon->setIcon(QIcon(":/icons/toolbar"));
300 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
301 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
304 notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
307 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
309 if(reason == QSystemTrayIcon::Trigger)
311 // Click on system tray icon triggers "open bitcoin"
312 openBitcoinAction->trigger();
317 void BitcoinGUI::optionsClicked()
320 dlg.setModel(clientModel->getOptionsModel());
324 void BitcoinGUI::aboutClicked()
327 dlg.setModel(clientModel);
331 void BitcoinGUI::setNumConnections(int count)
336 case 0: icon = ":/icons/connect_0"; break;
337 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
338 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
339 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
340 default: icon = ":/icons/connect_4"; break;
342 labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
343 labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
346 void BitcoinGUI::setNumBlocks(int count)
348 int initTotal = clientModel->getNumBlocksAtStartup();
349 int total = clientModel->getNumBlocksOfPeers();
354 progressBarLabel->setVisible(true);
355 progressBar->setVisible(true);
356 progressBar->setMaximum(total - initTotal);
357 progressBar->setValue(count - initTotal);
358 tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
362 progressBarLabel->setVisible(false);
363 progressBar->setVisible(false);
364 tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
367 QDateTime now = QDateTime::currentDateTime();
368 QDateTime lastBlockDate = clientModel->getLastBlockDate();
369 int secs = lastBlockDate.secsTo(now);
372 // Represent time from last generated block in human readable text
375 text = tr("%n second(s) ago","",secs);
377 else if(secs < 60*60)
379 text = tr("%n minute(s) ago","",secs/60);
381 else if(secs < 24*60*60)
383 text = tr("%n hour(s) ago","",secs/(60*60));
387 text = tr("%n day(s) ago","",secs/(60*60*24));
390 // Set icon state: spinning if catching up, tick otherwise
393 tooltip = tr("Up to date") + QString("\n") + tooltip;
394 labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
398 tooltip = tr("Catching up...") + QString("\n") + tooltip;
399 labelBlocksIcon->setMovie(syncIconMovie);
400 syncIconMovie->start();
403 tooltip += QString("\n");
404 tooltip += tr("Last received block was generated %1.").arg(text);
406 labelBlocksIcon->setToolTip(tooltip);
407 progressBarLabel->setToolTip(tooltip);
408 progressBar->setToolTip(tooltip);
411 void BitcoinGUI::error(const QString &title, const QString &message)
413 // Report errors from network/worker thread
414 notificator->notify(Notificator::Critical, title, message);
417 void BitcoinGUI::changeEvent(QEvent *e)
419 if (e->type() == QEvent::WindowStateChange)
421 if(clientModel->getOptionsModel()->getMinimizeToTray())
435 setWindowComposition();
436 QMainWindow::changeEvent(e);
439 void BitcoinGUI::closeEvent(QCloseEvent *event)
441 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
442 !clientModel->getOptionsModel()->getMinimizeOnClose())
446 QMainWindow::closeEvent(event);
449 void BitcoinGUI::setWindowComposition()
452 // Make the background transparent on Windows Vista or 7, except when maximized
453 // Otherwise text becomes hard to read
454 if (QtWin::isCompositionEnabled())
456 QPalette pal = palette();
457 QColor bg = pal.window().color();
460 setAttribute(Qt::WA_TranslucentBackground, false);
461 setAttribute(Qt::WA_StyledBackground, true);
462 QBrush wb = pal.window();
465 pal.setColor(QPalette::Window, bg);
471 setAttribute(Qt::WA_TranslucentBackground);
472 setAttribute(Qt::WA_StyledBackground, false);
474 pal.setColor(QPalette::Window, bg);
476 setAttribute(Qt::WA_NoSystemBackground, false);
478 setAttribute(Qt::WA_StyledBackground, false);
484 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
487 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
488 "which goes to the nodes that process your transaction and helps to support the network. "
489 "Do you want to pay the fee?").arg(
490 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
491 QMessageBox::StandardButton retval = QMessageBox::question(
492 this, tr("Sending..."), strMessage,
493 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
494 *payFee = (retval == QMessageBox::Yes);
497 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
499 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
500 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
501 .data(Qt::EditRole).toULongLong();
502 if(!clientModel->inInitialBlockDownload())
504 // On new transaction, make an info balloon
505 // Unless the initial block download is in progress, to prevent balloon-spam
506 QString date = ttm->index(start, TransactionTableModel::Date, parent)
508 QString type = ttm->index(start, TransactionTableModel::Type, parent)
510 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
512 QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
513 TransactionTableModel::ToAddress, parent)
514 .data(Qt::DecorationRole));
516 notificator->notify(Notificator::Information,
517 (amount)<0 ? tr("Sent transaction") :
518 tr("Incoming transaction"),
524 .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
526 .arg(address), icon);
530 void BitcoinGUI::gotoOverviewPage()
532 overviewAction->setChecked(true);
533 centralWidget->setCurrentWidget(overviewPage);
535 exportAction->setEnabled(false);
536 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
539 void BitcoinGUI::gotoHistoryPage()
541 historyAction->setChecked(true);
542 centralWidget->setCurrentWidget(transactionsPage);
544 exportAction->setEnabled(true);
545 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
546 connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
549 void BitcoinGUI::gotoAddressBookPage()
551 addressBookAction->setChecked(true);
552 centralWidget->setCurrentWidget(addressBookPage);
554 exportAction->setEnabled(true);
555 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
556 connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
559 void BitcoinGUI::gotoReceiveCoinsPage()
561 receiveCoinsAction->setChecked(true);
562 centralWidget->setCurrentWidget(receiveCoinsPage);
564 exportAction->setEnabled(true);
565 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
566 connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
569 void BitcoinGUI::gotoSendCoinsPage()
571 sendCoinsAction->setChecked(true);
572 if(centralWidget->currentWidget() != sendCoinsPage)
574 // Clear the current contents if we arrived from another tab
575 sendCoinsPage->clear();
577 centralWidget->setCurrentWidget(sendCoinsPage);
579 exportAction->setEnabled(false);
580 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
583 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
586 if(event->mimeData()->hasUrls())
587 event->acceptProposedAction();
590 void BitcoinGUI::dropEvent(QDropEvent *event)
592 if(event->mimeData()->hasUrls())
595 QList<QUrl> urls = event->mimeData()->urls();
596 foreach(const QUrl &url, urls)
598 sendCoinsPage->handleURL(&url);
602 event->acceptProposedAction();
605 void BitcoinGUI::setEncryptionStatus(int status)
609 case WalletModel::Unencrypted:
610 labelEncryptionIcon->hide();
611 encryptWalletAction->setChecked(false);
612 changePassphraseAction->setEnabled(false);
613 encryptWalletAction->setEnabled(true);
615 case WalletModel::Unlocked:
616 labelEncryptionIcon->show();
617 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
618 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
619 encryptWalletAction->setChecked(true);
620 changePassphraseAction->setEnabled(true);
621 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
623 case WalletModel::Locked:
624 labelEncryptionIcon->show();
625 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
626 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
627 encryptWalletAction->setChecked(true);
628 changePassphraseAction->setEnabled(true);
629 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
634 void BitcoinGUI::encryptWallet(bool status)
636 AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
637 AskPassphraseDialog::Decrypt, this);
638 dlg.setModel(walletModel);
641 setEncryptionStatus(walletModel->getEncryptionStatus());
644 void BitcoinGUI::changePassphrase()
646 AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
647 dlg.setModel(walletModel);
651 void BitcoinGUI::unlockWallet()
653 // Unlock wallet when requested by wallet model
654 if(walletModel->getEncryptionStatus() == WalletModel::Locked)
656 AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
657 dlg.setModel(walletModel);