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(&window);
168 window.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 total = clientModel->getTotalBlocksEstimate();
353 progressBarLabel->setVisible(true);
354 progressBar->setVisible(true);
355 progressBar->setMaximum(total);
356 progressBar->setValue(count);
357 tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
361 progressBarLabel->setVisible(false);
362 progressBar->setVisible(false);
363 tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
366 QDateTime now = QDateTime::currentDateTime();
367 QDateTime lastBlockDate = clientModel->getLastBlockDate();
368 int secs = lastBlockDate.secsTo(now);
371 // Represent time from last generated block in human readable text
374 text = tr("%n second(s) ago","",secs);
376 else if(secs < 60*60)
378 text = tr("%n minute(s) ago","",secs/60);
380 else if(secs < 24*60*60)
382 text = tr("%n hour(s) ago","",secs/(60*60));
386 text = tr("%n day(s) ago","",secs/(60*60*24));
389 // Set icon state: spinning if catching up, tick otherwise
392 tooltip = tr("Up to date") + QString("\n") + tooltip;
393 labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
397 tooltip = tr("Catching up...") + QString("\n") + tooltip;
398 labelBlocksIcon->setMovie(syncIconMovie);
399 syncIconMovie->start();
402 tooltip += QString("\n");
403 tooltip += tr("Last received block was generated %1.").arg(text);
405 labelBlocksIcon->setToolTip(tooltip);
406 progressBarLabel->setToolTip(tooltip);
407 progressBar->setToolTip(tooltip);
410 void BitcoinGUI::error(const QString &title, const QString &message)
412 // Report errors from network/worker thread
413 notificator->notify(Notificator::Critical, title, message);
416 void BitcoinGUI::changeEvent(QEvent *e)
418 if (e->type() == QEvent::WindowStateChange)
420 if(clientModel->getOptionsModel()->getMinimizeToTray())
434 setWindowComposition();
435 QMainWindow::changeEvent(e);
438 void BitcoinGUI::closeEvent(QCloseEvent *event)
440 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
441 !clientModel->getOptionsModel()->getMinimizeOnClose())
445 QMainWindow::closeEvent(event);
448 void BitcoinGUI::setWindowComposition()
451 // Make the background transparent on Windows Vista or 7, except when maximized
452 // Otherwise text becomes hard to read
453 if (QtWin::isCompositionEnabled())
455 QPalette pal = palette();
456 QColor bg = pal.window().color();
459 setAttribute(Qt::WA_TranslucentBackground, false);
460 setAttribute(Qt::WA_StyledBackground, true);
461 QBrush wb = pal.window();
464 pal.setColor(QPalette::Window, bg);
470 setAttribute(Qt::WA_TranslucentBackground);
471 setAttribute(Qt::WA_StyledBackground, false);
473 pal.setColor(QPalette::Window, bg);
475 setAttribute(Qt::WA_NoSystemBackground, false);
477 setAttribute(Qt::WA_StyledBackground, false);
483 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
486 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
487 "which goes to the nodes that process your transaction and helps to support the network. "
488 "Do you want to pay the fee?").arg(
489 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
490 QMessageBox::StandardButton retval = QMessageBox::question(
491 this, tr("Sending..."), strMessage,
492 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
493 *payFee = (retval == QMessageBox::Yes);
496 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
498 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
499 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
500 .data(Qt::EditRole).toULongLong();
501 if(!clientModel->inInitialBlockDownload())
503 // On new transaction, make an info balloon
504 // Unless the initial block download is in progress, to prevent balloon-spam
505 QString date = ttm->index(start, TransactionTableModel::Date, parent)
507 QString type = ttm->index(start, TransactionTableModel::Type, parent)
509 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
511 QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
512 TransactionTableModel::ToAddress, parent)
513 .data(Qt::DecorationRole));
515 notificator->notify(Notificator::Information,
516 (amount)<0 ? tr("Sent transaction") :
517 tr("Incoming transaction"),
523 .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
525 .arg(address), icon);
529 void BitcoinGUI::gotoOverviewPage()
531 overviewAction->setChecked(true);
532 centralWidget->setCurrentWidget(overviewPage);
534 exportAction->setEnabled(false);
535 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
538 void BitcoinGUI::gotoHistoryPage()
540 historyAction->setChecked(true);
541 centralWidget->setCurrentWidget(transactionsPage);
543 exportAction->setEnabled(true);
544 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
545 connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
548 void BitcoinGUI::gotoAddressBookPage()
550 addressBookAction->setChecked(true);
551 centralWidget->setCurrentWidget(addressBookPage);
553 exportAction->setEnabled(true);
554 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
555 connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
558 void BitcoinGUI::gotoReceiveCoinsPage()
560 receiveCoinsAction->setChecked(true);
561 centralWidget->setCurrentWidget(receiveCoinsPage);
563 exportAction->setEnabled(true);
564 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
565 connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
568 void BitcoinGUI::gotoSendCoinsPage()
570 sendCoinsAction->setChecked(true);
571 if(centralWidget->currentWidget() != sendCoinsPage)
573 // Clear the current contents if we arrived from another tab
574 sendCoinsPage->clear();
576 centralWidget->setCurrentWidget(sendCoinsPage);
578 exportAction->setEnabled(false);
579 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
582 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
585 if(event->mimeData()->hasUrls())
586 event->acceptProposedAction();
589 void BitcoinGUI::dropEvent(QDropEvent *event)
591 if(event->mimeData()->hasUrls())
594 QList<QUrl> urls = event->mimeData()->urls();
595 foreach(const QUrl &url, urls)
597 sendCoinsPage->handleURL(&url);
601 event->acceptProposedAction();
604 void BitcoinGUI::setEncryptionStatus(int status)
608 case WalletModel::Unencrypted:
609 labelEncryptionIcon->hide();
610 encryptWalletAction->setChecked(false);
611 changePassphraseAction->setEnabled(false);
612 encryptWalletAction->setEnabled(true);
614 case WalletModel::Unlocked:
615 labelEncryptionIcon->show();
616 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
617 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
618 encryptWalletAction->setChecked(true);
619 changePassphraseAction->setEnabled(true);
620 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
622 case WalletModel::Locked:
623 labelEncryptionIcon->show();
624 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
625 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
626 encryptWalletAction->setChecked(true);
627 changePassphraseAction->setEnabled(true);
628 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
633 void BitcoinGUI::encryptWallet(bool status)
635 AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
636 AskPassphraseDialog::Decrypt, this);
637 dlg.setModel(walletModel);
640 setEncryptionStatus(walletModel->getEncryptionStatus());
643 void BitcoinGUI::changePassphrase()
645 AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
646 dlg.setModel(walletModel);
650 void BitcoinGUI::unlockWallet()
652 // Unlock wallet when requested by wallet model
653 if(walletModel->getEncryptionStatus() == WalletModel::Locked)
655 AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
656 dlg.setModel(walletModel);