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(quitAction);
70 QMenu *settings = menuBar()->addMenu(tr("&Settings"));
71 settings->addAction(encryptWalletAction);
72 settings->addAction(changePassphraseAction);
73 settings->addSeparator();
74 settings->addAction(optionsAction);
76 QMenu *help = menuBar()->addMenu(tr("&Help"));
77 help->addAction(aboutAction);
80 QToolBar *toolbar = addToolBar(tr("Tabs toolbar"));
81 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
82 toolbar->addAction(overviewAction);
83 toolbar->addAction(sendCoinsAction);
84 toolbar->addAction(receiveCoinsAction);
85 toolbar->addAction(historyAction);
86 toolbar->addAction(addressBookAction);
88 QToolBar *toolbar2 = addToolBar(tr("Actions toolbar"));
89 toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
90 toolbar2->addAction(exportAction);
93 overviewPage = new OverviewPage();
95 transactionsPage = new QWidget(this);
96 QVBoxLayout *vbox = new QVBoxLayout();
97 transactionView = new TransactionView(this);
98 vbox->addWidget(transactionView);
99 transactionsPage->setLayout(vbox);
101 addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
103 receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
105 sendCoinsPage = new SendCoinsDialog(this);
107 centralWidget = new QStackedWidget(this);
108 centralWidget->addWidget(overviewPage);
109 centralWidget->addWidget(transactionsPage);
110 centralWidget->addWidget(addressBookPage);
111 centralWidget->addWidget(receiveCoinsPage);
112 centralWidget->addWidget(sendCoinsPage);
113 setCentralWidget(centralWidget);
118 // Status bar notification icons
119 QFrame *frameBlocks = new QFrame();
120 //frameBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
121 frameBlocks->setContentsMargins(0,0,0,0);
122 frameBlocks->setMinimumWidth(56);
123 frameBlocks->setMaximumWidth(56);
124 QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
125 frameBlocksLayout->setContentsMargins(3,0,3,0);
126 frameBlocksLayout->setSpacing(3);
127 labelEncryptionIcon = new QLabel();
128 labelConnectionsIcon = new QLabel();
129 labelBlocksIcon = new QLabel();
130 frameBlocksLayout->addStretch();
131 frameBlocksLayout->addWidget(labelEncryptionIcon);
132 frameBlocksLayout->addStretch();
133 frameBlocksLayout->addWidget(labelConnectionsIcon);
134 frameBlocksLayout->addStretch();
135 frameBlocksLayout->addWidget(labelBlocksIcon);
136 frameBlocksLayout->addStretch();
138 // Progress bar for blocks download
139 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
140 progressBarLabel->setVisible(false);
141 progressBar = new QProgressBar();
142 progressBar->setToolTip(tr("Block chain synchronization in progress"));
143 progressBar->setVisible(false);
145 statusBar()->addWidget(progressBarLabel);
146 statusBar()->addWidget(progressBar);
147 statusBar()->addPermanentWidget(frameBlocks);
151 syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
153 // Clicking on a transaction on the overview page simply sends you to transaction history page
154 connect(overviewPage, SIGNAL(transactionClicked(QModelIndex)), this, SLOT(gotoHistoryPage()));
156 // Doubleclicking on a transaction on the transaction history page shows details
157 connect(transactionView, SIGNAL(doubleClicked(QModelIndex)), transactionView, SLOT(showDetails()));
162 void BitcoinGUI::createActions()
164 QActionGroup *tabGroup = new QActionGroup(this);
166 overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
167 overviewAction->setToolTip(tr("Show general overview of wallet"));
168 overviewAction->setCheckable(true);
169 tabGroup->addAction(overviewAction);
171 historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
172 historyAction->setToolTip(tr("Browse transaction history"));
173 historyAction->setCheckable(true);
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 tabGroup->addAction(addressBookAction);
181 receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
182 receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
183 receiveCoinsAction->setCheckable(true);
184 tabGroup->addAction(receiveCoinsAction);
186 sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
187 sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
188 sendCoinsAction->setCheckable(true);
189 tabGroup->addAction(sendCoinsAction);
191 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
192 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
193 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
194 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
195 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
197 quitAction = new QAction(QIcon(":/icons/quit"), tr("E&xit"), this);
198 quitAction->setToolTip(tr("Quit application"));
199 quitAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Q));
200 aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
201 aboutAction->setToolTip(tr("Show information about Bitcoin"));
202 optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
203 optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
204 openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
205 openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
206 exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
207 exportAction->setToolTip(tr("Export the current view to a file"));
208 encryptWalletAction = new QAction(QIcon(":/icons/lock_closed"), tr("&Encrypt Wallet"), this);
209 encryptWalletAction->setToolTip(tr("Encrypt or decrypt wallet"));
210 encryptWalletAction->setCheckable(true);
211 changePassphraseAction = new QAction(QIcon(":/icons/key"), tr("&Change Passphrase"), this);
212 changePassphraseAction->setToolTip(tr("Change the passphrase used for wallet encryption"));
214 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
215 connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
216 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
217 connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(showNormal()));
218 connect(encryptWalletAction, SIGNAL(triggered(bool)), this, SLOT(encryptWallet(bool)));
219 connect(changePassphraseAction, SIGNAL(triggered()), this, SLOT(changePassphrase()));
222 void BitcoinGUI::setClientModel(ClientModel *clientModel)
224 this->clientModel = clientModel;
226 if(clientModel->isTestNet())
228 QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
229 setWindowTitle(title_testnet);
230 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
233 trayIcon->setToolTip(title_testnet);
234 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
238 // Keep up to date with client
239 setNumConnections(clientModel->getNumConnections());
240 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
242 setNumBlocks(clientModel->getNumBlocks());
243 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
245 // Report errors from network/worker thread
246 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
249 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
251 this->walletModel = walletModel;
253 // Report errors from wallet thread
254 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
256 // Put transaction list in tabs
257 transactionView->setModel(walletModel);
259 overviewPage->setModel(walletModel);
260 addressBookPage->setModel(walletModel->getAddressTableModel());
261 receiveCoinsPage->setModel(walletModel->getAddressTableModel());
262 sendCoinsPage->setModel(walletModel);
264 setEncryptionStatus(walletModel->getEncryptionStatus());
265 connect(walletModel, SIGNAL(encryptionStatusChanged(int)), this, SLOT(setEncryptionStatus(int)));
267 // Balloon popup for new transaction
268 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(QModelIndex,int,int)),
269 this, SLOT(incomingTransaction(QModelIndex,int,int)));
271 // Ask for passphrase if needed
272 connect(walletModel, SIGNAL(requireUnlock()), this, SLOT(unlockWallet()));
275 void BitcoinGUI::createTrayIcon()
277 QMenu *trayIconMenu = new QMenu(this);
278 trayIconMenu->addAction(openBitcoinAction);
279 trayIconMenu->addAction(optionsAction);
280 trayIconMenu->addSeparator();
281 trayIconMenu->addAction(quitAction);
283 trayIcon = new QSystemTrayIcon(this);
284 trayIcon->setContextMenu(trayIconMenu);
285 trayIcon->setToolTip("Bitcoin client");
286 trayIcon->setIcon(QIcon(":/icons/toolbar"));
287 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
288 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
291 notificator = new Notificator(tr("bitcoin-qt"), trayIcon);
294 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
296 if(reason == QSystemTrayIcon::Trigger)
298 // Click on system tray icon triggers "open bitcoin"
299 openBitcoinAction->trigger();
304 void BitcoinGUI::optionsClicked()
307 dlg.setModel(clientModel->getOptionsModel());
311 void BitcoinGUI::aboutClicked()
314 dlg.setModel(clientModel);
318 void BitcoinGUI::setNumConnections(int count)
323 case 0: icon = ":/icons/connect_0"; break;
324 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
325 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
326 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
327 default: icon = ":/icons/connect_4"; break;
329 labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
330 labelConnectionsIcon->setToolTip(tr("%n active connection(s) to Bitcoin network", "", count));
333 void BitcoinGUI::setNumBlocks(int count)
335 int initTotal = clientModel->getNumBlocksAtStartup();
336 int total = clientModel->getNumBlocksOfPeers();
341 progressBarLabel->setVisible(true);
342 progressBar->setVisible(true);
343 progressBar->setMaximum(total - initTotal);
344 progressBar->setValue(count - initTotal);
345 tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
349 progressBarLabel->setVisible(false);
350 progressBar->setVisible(false);
351 tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
354 QDateTime now = QDateTime::currentDateTime();
355 QDateTime lastBlockDate = clientModel->getLastBlockDate();
356 int secs = lastBlockDate.secsTo(now);
359 // Represent time from last generated block in human readable text
362 text = tr("%n second(s) ago","",secs);
364 else if(secs < 60*60)
366 text = tr("%n minute(s) ago","",secs/60);
368 else if(secs < 24*60*60)
370 text = tr("%n hour(s) ago","",secs/(60*60));
374 text = tr("%n day(s) ago","",secs/(60*60*24));
377 // Set icon state: spinning if catching up, tick otherwise
380 tooltip = tr("Up to date") + QString("\n") + tooltip;
381 labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
385 tooltip = tr("Catching up...") + QString("\n") + tooltip;
386 labelBlocksIcon->setMovie(syncIconMovie);
387 syncIconMovie->start();
390 tooltip += QString("\n");
391 tooltip += tr("Last received block was generated %1.").arg(text);
393 labelBlocksIcon->setToolTip(tooltip);
394 progressBarLabel->setToolTip(tooltip);
395 progressBar->setToolTip(tooltip);
398 void BitcoinGUI::error(const QString &title, const QString &message)
400 // Report errors from network/worker thread
401 notificator->notify(Notificator::Critical, title, message);
404 void BitcoinGUI::changeEvent(QEvent *e)
406 if (e->type() == QEvent::WindowStateChange)
408 if(clientModel->getOptionsModel()->getMinimizeToTray())
422 QMainWindow::changeEvent(e);
425 void BitcoinGUI::closeEvent(QCloseEvent *event)
427 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
428 !clientModel->getOptionsModel()->getMinimizeOnClose())
432 QMainWindow::closeEvent(event);
435 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
438 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
439 "which goes to the nodes that process your transaction and helps to support the network. "
440 "Do you want to pay the fee?").arg(
441 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
442 QMessageBox::StandardButton retval = QMessageBox::question(
443 this, tr("Sending..."), strMessage,
444 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
445 *payFee = (retval == QMessageBox::Yes);
448 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
450 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
451 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
452 .data(Qt::EditRole).toULongLong();
453 if(!clientModel->inInitialBlockDownload())
455 // On new transaction, make an info balloon
456 // Unless the initial block download is in progress, to prevent balloon-spam
457 QString date = ttm->index(start, TransactionTableModel::Date, parent)
459 QString type = ttm->index(start, TransactionTableModel::Type, parent)
461 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
463 QIcon icon = qvariant_cast<QIcon>(ttm->index(start,
464 TransactionTableModel::ToAddress, parent)
465 .data(Qt::DecorationRole));
467 notificator->notify(Notificator::Information,
468 (amount)<0 ? tr("Sent transaction") :
469 tr("Incoming transaction"),
475 .arg(BitcoinUnits::formatWithUnit(walletModel->getOptionsModel()->getDisplayUnit(), amount, true))
477 .arg(address), icon);
481 void BitcoinGUI::gotoOverviewPage()
483 overviewAction->setChecked(true);
484 centralWidget->setCurrentWidget(overviewPage);
486 exportAction->setEnabled(false);
487 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
490 void BitcoinGUI::gotoHistoryPage()
492 historyAction->setChecked(true);
493 centralWidget->setCurrentWidget(transactionsPage);
495 exportAction->setEnabled(true);
496 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
497 connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
500 void BitcoinGUI::gotoAddressBookPage()
502 addressBookAction->setChecked(true);
503 centralWidget->setCurrentWidget(addressBookPage);
505 exportAction->setEnabled(true);
506 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
507 connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
510 void BitcoinGUI::gotoReceiveCoinsPage()
512 receiveCoinsAction->setChecked(true);
513 centralWidget->setCurrentWidget(receiveCoinsPage);
515 exportAction->setEnabled(true);
516 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
517 connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
520 void BitcoinGUI::gotoSendCoinsPage()
522 sendCoinsAction->setChecked(true);
523 centralWidget->setCurrentWidget(sendCoinsPage);
525 exportAction->setEnabled(false);
526 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
529 void BitcoinGUI::dragEnterEvent(QDragEnterEvent *event)
532 if(event->mimeData()->hasUrls())
533 event->acceptProposedAction();
536 void BitcoinGUI::dropEvent(QDropEvent *event)
538 if(event->mimeData()->hasUrls())
541 QList<QUrl> urls = event->mimeData()->urls();
542 foreach(const QUrl &url, urls)
544 sendCoinsPage->handleURL(&url);
548 event->acceptProposedAction();
551 void BitcoinGUI::setEncryptionStatus(int status)
555 case WalletModel::Unencrypted:
556 labelEncryptionIcon->hide();
557 encryptWalletAction->setChecked(false);
558 changePassphraseAction->setEnabled(false);
559 encryptWalletAction->setEnabled(true);
561 case WalletModel::Unlocked:
562 labelEncryptionIcon->show();
563 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_open").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
564 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>unlocked</b>"));
565 encryptWalletAction->setChecked(true);
566 changePassphraseAction->setEnabled(true);
567 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
569 case WalletModel::Locked:
570 labelEncryptionIcon->show();
571 labelEncryptionIcon->setPixmap(QIcon(":/icons/lock_closed").pixmap(STATUSBAR_ICONSIZE,STATUSBAR_ICONSIZE));
572 labelEncryptionIcon->setToolTip(tr("Wallet is <b>encrypted</b> and currently <b>locked</b>"));
573 encryptWalletAction->setChecked(true);
574 changePassphraseAction->setEnabled(true);
575 encryptWalletAction->setEnabled(false); // TODO: decrypt currently not supported
580 void BitcoinGUI::encryptWallet(bool status)
582 AskPassphraseDialog dlg(status ? AskPassphraseDialog::Encrypt:
583 AskPassphraseDialog::Decrypt, this);
584 dlg.setModel(walletModel);
587 setEncryptionStatus(walletModel->getEncryptionStatus());
590 void BitcoinGUI::changePassphrase()
592 AskPassphraseDialog dlg(AskPassphraseDialog::ChangePass, this);
593 dlg.setModel(walletModel);
597 void BitcoinGUI::unlockWallet()
599 // Unlock wallet when requested by wallet model
600 if(walletModel->getEncryptionStatus() == WalletModel::Locked)
602 AskPassphraseDialog dlg(AskPassphraseDialog::Unlock, this);
603 dlg.setModel(walletModel);