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"
15 #include "editaddressdialog.h"
16 #include "optionsmodel.h"
17 #include "transactiondescdialog.h"
18 #include "addresstablemodel.h"
19 #include "transactionview.h"
20 #include "overviewpage.h"
22 #include <QApplication>
23 #include <QMainWindow>
28 #include <QVBoxLayout>
33 #include <QPushButton>
35 #include <QMessageBox>
36 #include <QProgressBar>
37 #include <QStackedWidget>
44 BitcoinGUI::BitcoinGUI(QWidget *parent):
51 setWindowTitle(tr("Bitcoin Wallet"));
52 setWindowIcon(QIcon(":icons/bitcoin"));
57 QMenu *file = menuBar()->addMenu("&File");
58 file->addAction(sendCoinsAction);
59 file->addAction(receiveCoinsAction);
61 file->addAction(quitAction);
63 QMenu *settings = menuBar()->addMenu("&Settings");
64 settings->addAction(optionsAction);
66 QMenu *help = menuBar()->addMenu("&Help");
67 help->addAction(aboutAction);
70 QToolBar *toolbar = addToolBar("Main toolbar");
71 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
72 toolbar->addAction(overviewAction);
73 toolbar->addAction(sendCoinsAction);
74 toolbar->addAction(receiveCoinsAction);
75 toolbar->addAction(historyAction);
76 toolbar->addAction(addressBookAction);
78 QToolBar *toolbar2 = addToolBar("Transactions toolbar");
79 toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
80 toolbar2->addAction(exportAction);
83 overviewPage = new OverviewPage();
84 QVBoxLayout *vbox = new QVBoxLayout();
86 transactionView = new TransactionView(this);
87 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), transactionView, SLOT(showDetails()));
88 vbox->addWidget(transactionView);
90 transactionsPage = new QWidget(this);
91 transactionsPage->setLayout(vbox);
93 addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
95 receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
97 sendCoinsPage = new SendCoinsDialog(this);
99 centralWidget = new QStackedWidget(this);
100 centralWidget->addWidget(overviewPage);
101 centralWidget->addWidget(transactionsPage);
102 centralWidget->addWidget(addressBookPage);
103 centralWidget->addWidget(receiveCoinsPage);
104 centralWidget->addWidget(sendCoinsPage);
105 setCentralWidget(centralWidget);
110 labelConnections = new QLabel();
111 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
112 labelConnections->setMinimumWidth(150);
113 labelConnections->setMaximumWidth(150);
114 labelConnections->setToolTip(tr("Number of connections to other clients"));
116 labelBlocks = new QLabel();
117 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
118 labelBlocks->setMinimumWidth(150);
119 labelBlocks->setMaximumWidth(150);
120 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
122 // Progress bar for blocks download
123 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
124 progressBarLabel->setVisible(false);
125 progressBar = new QProgressBar();
126 progressBar->setToolTip(tr("Block chain synchronization in progress"));
127 progressBar->setVisible(false);
129 statusBar()->addWidget(progressBarLabel);
130 statusBar()->addWidget(progressBar);
131 statusBar()->addPermanentWidget(labelConnections);
132 statusBar()->addPermanentWidget(labelBlocks);
139 void BitcoinGUI::createActions()
141 QActionGroup *tabGroup = new QActionGroup(this);
143 overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
144 overviewAction->setCheckable(true);
145 tabGroup->addAction(overviewAction);
147 historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
148 historyAction->setCheckable(true);
149 tabGroup->addAction(historyAction);
151 addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
152 addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
153 addressBookAction->setCheckable(true);
154 tabGroup->addAction(addressBookAction);
156 receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
157 receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
158 receiveCoinsAction->setCheckable(true);
159 tabGroup->addAction(receiveCoinsAction);
161 sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
162 sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
163 sendCoinsAction->setCheckable(true);
164 tabGroup->addAction(sendCoinsAction);
166 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
167 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
168 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
169 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
170 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
172 quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
173 quitAction->setToolTip(tr("Quit application"));
174 aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
175 aboutAction->setToolTip(tr("Show information about Bitcoin"));
176 optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
177 optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
178 openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
179 openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
180 exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
181 exportAction->setToolTip(tr("Export data in current view to a file"));
183 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
184 connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
185 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
186 connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(show()));
189 void BitcoinGUI::setClientModel(ClientModel *clientModel)
191 this->clientModel = clientModel;
193 if(clientModel->isTestNet())
195 QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
196 setWindowTitle(title_testnet);
197 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
200 trayIcon->setToolTip(title_testnet);
201 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
205 // Keep up to date with client
206 setNumConnections(clientModel->getNumConnections());
207 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
209 setNumBlocks(clientModel->getNumBlocks());
210 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
212 // Report errors from network/worker thread
213 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
216 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
218 this->walletModel = walletModel;
220 // Report errors from wallet thread
221 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
223 // Put transaction list in tabs
224 transactionView->setModel(walletModel);
226 overviewPage->setModel(walletModel);
227 addressBookPage->setModel(walletModel->getAddressTableModel());
228 receiveCoinsPage->setModel(walletModel->getAddressTableModel());
229 sendCoinsPage->setModel(walletModel);
231 // Balloon popup for new transaction
232 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
233 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
236 void BitcoinGUI::createTrayIcon()
238 QMenu *trayIconMenu = new QMenu(this);
239 trayIconMenu->addAction(openBitcoinAction);
240 trayIconMenu->addAction(sendCoinsAction);
241 trayIconMenu->addAction(optionsAction);
242 trayIconMenu->addSeparator();
243 trayIconMenu->addAction(quitAction);
245 trayIcon = new QSystemTrayIcon(this);
246 trayIcon->setContextMenu(trayIconMenu);
247 trayIcon->setToolTip("Bitcoin client");
248 trayIcon->setIcon(QIcon(":/icons/toolbar"));
249 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
250 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
254 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
256 if(reason == QSystemTrayIcon::DoubleClick)
258 // Doubleclick on system tray icon triggers "open bitcoin"
259 openBitcoinAction->trigger();
263 void BitcoinGUI::optionsClicked()
266 dlg.setModel(clientModel->getOptionsModel());
270 void BitcoinGUI::aboutClicked()
273 dlg.setModel(clientModel);
277 void BitcoinGUI::setNumConnections(int count)
282 case 0: icon = ":/icons/connect_0"; break;
283 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
284 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
285 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
286 default: icon = ":/icons/connect_4"; break;
288 labelConnections->setTextFormat(Qt::RichText);
289 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
292 void BitcoinGUI::setNumBlocks(int count)
294 int total = clientModel->getTotalBlocksEstimate();
297 progressBarLabel->setVisible(true);
298 progressBar->setVisible(true);
299 progressBar->setMaximum(total);
300 progressBar->setValue(count);
304 progressBarLabel->setVisible(false);
305 progressBar->setVisible(false);
308 QDateTime now = QDateTime::currentDateTime();
309 QDateTime lastBlockDate = clientModel->getLastBlockDate();
310 int secs = lastBlockDate.secsTo(now);
312 QString icon = ":/icons/notsynced";
314 // "Up to date" icon, and outdated icon
318 icon = ":/icons/synced";
320 else if(secs < 60*60)
322 text = tr("%n minute(s) ago","",secs/60);
324 else if(secs < 24*60*60)
326 text = tr("%n hour(s) ago","",secs/(60*60));
330 text = tr("%n day(s) ago","",secs/(60*60*24));
333 labelBlocks->setText("<img src=\""+icon+"\"> " + text);
334 labelBlocks->setToolTip(tr("Downloaded %n block(s) of transaction history. Last block was generated %1.", "", count)
335 .arg(QLocale::system().toString(lastBlockDate)));
338 void BitcoinGUI::error(const QString &title, const QString &message)
340 // Report errors from network/worker thread
341 if(trayIcon->supportsMessages())
343 // Show as "balloon" message if possible
344 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
348 // Fall back to old fashioned popup dialog if not
349 QMessageBox::critical(this, title,
351 QMessageBox::Ok, QMessageBox::Ok);
355 void BitcoinGUI::changeEvent(QEvent *e)
357 if (e->type() == QEvent::WindowStateChange)
359 if(clientModel->getOptionsModel()->getMinimizeToTray())
372 QMainWindow::changeEvent(e);
375 void BitcoinGUI::closeEvent(QCloseEvent *event)
377 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
378 !clientModel->getOptionsModel()->getMinimizeOnClose())
382 QMainWindow::closeEvent(event);
385 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
388 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
389 "which goes to the nodes that process your transaction and helps to support the network. "
390 "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
391 QMessageBox::StandardButton retval = QMessageBox::question(
392 this, tr("Sending..."), strMessage,
393 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
394 *payFee = (retval == QMessageBox::Yes);
397 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
399 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
400 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
401 .data(Qt::EditRole).toULongLong();
402 if(!clientModel->inInitialBlockDownload())
404 // On incoming transaction, make an info balloon
405 // Unless the initial block download is in progress, to prevent balloon-spam
406 QString date = ttm->index(start, TransactionTableModel::Date, parent)
408 QString type = ttm->index(start, TransactionTableModel::Type, parent)
410 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
413 trayIcon->showMessage((amount)<0 ? tr("Sent transaction") :
414 tr("Incoming transaction"),
415 tr("Date: ") + date + "\n" +
416 tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
417 tr("Type: ") + type + "\n" +
418 tr("Address: ") + address + "\n",
419 QSystemTrayIcon::Information);
423 void BitcoinGUI::gotoOverviewPage()
425 overviewAction->setChecked(true);
426 centralWidget->setCurrentWidget(overviewPage);
428 exportAction->setEnabled(false);
429 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
432 void BitcoinGUI::gotoHistoryPage()
434 historyAction->setChecked(true);
435 centralWidget->setCurrentWidget(transactionsPage);
437 exportAction->setEnabled(true);
438 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
439 connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
442 void BitcoinGUI::gotoAddressBookPage()
444 addressBookAction->setChecked(true);
445 centralWidget->setCurrentWidget(addressBookPage);
447 exportAction->setEnabled(true);
448 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
449 connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
452 void BitcoinGUI::gotoReceiveCoinsPage()
454 receiveCoinsAction->setChecked(true);
455 centralWidget->setCurrentWidget(receiveCoinsPage);
457 exportAction->setEnabled(true);
458 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
459 connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
462 void BitcoinGUI::gotoSendCoinsPage()
464 sendCoinsAction->setChecked(true);
465 sendCoinsPage->clear();
466 centralWidget->setCurrentWidget(sendCoinsPage);
468 exportAction->setEnabled(false);
469 disconnect(exportAction, SIGNAL(triggered()), 0, 0);