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(optionsAction);
60 file->addAction(quitAction);
62 QMenu *help = menuBar()->addMenu("&Help");
63 help->addAction(aboutAction);
66 QToolBar *toolbar = addToolBar("Main toolbar");
67 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
68 toolbar->addAction(overviewAction);
69 toolbar->addAction(sendCoinsAction);
70 toolbar->addAction(receiveCoinsAction);
71 toolbar->addAction(historyAction);
72 toolbar->addAction(addressBookAction);
74 QToolBar *toolbar2 = addToolBar("Transactions toolbar");
75 toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
76 toolbar2->addAction(exportAction);
79 overviewPage = new OverviewPage();
80 QVBoxLayout *vbox = new QVBoxLayout();
82 transactionView = new TransactionView(this);
83 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), transactionView, SLOT(showDetails()));
84 vbox->addWidget(transactionView);
86 transactionsPage = new QWidget(this);
87 transactionsPage->setLayout(vbox);
89 addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
91 receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
93 sendCoinsPage = new SendCoinsDialog(this);
95 centralWidget = new QStackedWidget(this);
96 centralWidget->addWidget(overviewPage);
97 centralWidget->addWidget(transactionsPage);
98 centralWidget->addWidget(addressBookPage);
99 centralWidget->addWidget(receiveCoinsPage);
100 centralWidget->addWidget(sendCoinsPage);
101 setCentralWidget(centralWidget);
106 labelConnections = new QLabel();
107 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
108 labelConnections->setMinimumWidth(150);
109 labelConnections->setMaximumWidth(150);
110 labelConnections->setToolTip(tr("Number of connections to other clients"));
112 labelBlocks = new QLabel();
113 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
114 labelBlocks->setMinimumWidth(150);
115 labelBlocks->setMaximumWidth(150);
116 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
118 // Progress bar for blocks download
119 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
120 progressBarLabel->setVisible(false);
121 progressBar = new QProgressBar();
122 progressBar->setToolTip(tr("Block chain synchronization in progress"));
123 progressBar->setVisible(false);
125 statusBar()->addWidget(progressBarLabel);
126 statusBar()->addWidget(progressBar);
127 statusBar()->addPermanentWidget(labelConnections);
128 statusBar()->addPermanentWidget(labelBlocks);
135 void BitcoinGUI::createActions()
137 QActionGroup *tabGroup = new QActionGroup(this);
139 overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
140 overviewAction->setCheckable(true);
141 tabGroup->addAction(overviewAction);
143 historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
144 historyAction->setCheckable(true);
145 tabGroup->addAction(historyAction);
147 addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
148 addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
149 addressBookAction->setCheckable(true);
150 tabGroup->addAction(addressBookAction);
152 receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
153 receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
154 receiveCoinsAction->setCheckable(true);
155 tabGroup->addAction(receiveCoinsAction);
157 sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
158 sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
159 sendCoinsAction->setCheckable(true);
160 tabGroup->addAction(sendCoinsAction);
162 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
163 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
164 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
165 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
166 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
168 quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
169 quitAction->setToolTip(tr("Quit application"));
170 aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
171 aboutAction->setToolTip(tr("Show information about Bitcoin"));
172 optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
173 optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
174 openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
175 openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
176 exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
177 exportAction->setToolTip(tr("Export data in current view to a file"));
179 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
180 connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
181 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
182 connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(show()));
185 void BitcoinGUI::setClientModel(ClientModel *clientModel)
187 this->clientModel = clientModel;
189 if(clientModel->isTestNet())
191 QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
192 setWindowTitle(title_testnet);
193 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
196 trayIcon->setToolTip(title_testnet);
197 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
201 // Keep up to date with client
202 setNumConnections(clientModel->getNumConnections());
203 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
205 setNumBlocks(clientModel->getNumBlocks());
206 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
208 // Report errors from network/worker thread
209 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
212 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
214 this->walletModel = walletModel;
216 // Report errors from wallet thread
217 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
219 // Put transaction list in tabs
220 transactionView->setModel(walletModel);
222 overviewPage->setModel(walletModel);
223 addressBookPage->setModel(walletModel->getAddressTableModel());
224 receiveCoinsPage->setModel(walletModel->getAddressTableModel());
225 sendCoinsPage->setModel(walletModel);
227 // Balloon popup for new transaction
228 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
229 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
232 void BitcoinGUI::createTrayIcon()
234 QMenu *trayIconMenu = new QMenu(this);
235 trayIconMenu->addAction(openBitcoinAction);
236 trayIconMenu->addAction(sendCoinsAction);
237 trayIconMenu->addAction(optionsAction);
238 trayIconMenu->addSeparator();
239 trayIconMenu->addAction(quitAction);
241 trayIcon = new QSystemTrayIcon(this);
242 trayIcon->setContextMenu(trayIconMenu);
243 trayIcon->setToolTip("Bitcoin client");
244 trayIcon->setIcon(QIcon(":/icons/toolbar"));
245 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
246 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
250 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
252 if(reason == QSystemTrayIcon::DoubleClick)
254 // Doubleclick on system tray icon triggers "open bitcoin"
255 openBitcoinAction->trigger();
259 void BitcoinGUI::optionsClicked()
262 dlg.setModel(clientModel->getOptionsModel());
266 void BitcoinGUI::aboutClicked()
269 dlg.setModel(clientModel);
273 void BitcoinGUI::setNumConnections(int count)
278 case 0: icon = ":/icons/connect_0"; break;
279 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
280 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
281 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
282 default: icon = ":/icons/connect_4"; break;
284 labelConnections->setTextFormat(Qt::RichText);
285 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
288 void BitcoinGUI::setNumBlocks(int count)
290 int total = clientModel->getTotalBlocksEstimate();
293 progressBarLabel->setVisible(true);
294 progressBar->setVisible(true);
295 progressBar->setMaximum(total);
296 progressBar->setValue(count);
300 progressBarLabel->setVisible(false);
301 progressBar->setVisible(false);
304 QDateTime now = QDateTime::currentDateTime();
305 QDateTime lastBlockDate = clientModel->getLastBlockDate();
306 int secs = lastBlockDate.secsTo(now);
308 QString icon = ":/icons/notsynced";
310 // "Up to date" icon, and outdated icon
314 icon = ":/icons/synced";
316 else if(secs < 60*60)
318 text = tr("%n minute(s) ago","",secs/60);
320 else if(secs < 24*60*60)
322 text = tr("%n hour(s) ago","",secs/(60*60));
326 text = tr("%n day(s) ago","",secs/(60*60*24));
329 labelBlocks->setText("<img src=\""+icon+"\"> " + text);
330 labelBlocks->setToolTip(tr("Downloaded %n block(s) of transaction history. Last block was generated %1.", "", count)
331 .arg(QLocale::system().toString(lastBlockDate)));
334 void BitcoinGUI::error(const QString &title, const QString &message)
336 // Report errors from network/worker thread
337 if(trayIcon->supportsMessages())
339 // Show as "balloon" message if possible
340 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
344 // Fall back to old fashioned popup dialog if not
345 QMessageBox::critical(this, title,
347 QMessageBox::Ok, QMessageBox::Ok);
351 void BitcoinGUI::changeEvent(QEvent *e)
353 if (e->type() == QEvent::WindowStateChange)
355 if(clientModel->getOptionsModel()->getMinimizeToTray())
368 QMainWindow::changeEvent(e);
371 void BitcoinGUI::closeEvent(QCloseEvent *event)
373 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
374 !clientModel->getOptionsModel()->getMinimizeOnClose())
378 QMainWindow::closeEvent(event);
381 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
384 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
385 "which goes to the nodes that process your transaction and helps to support the network. "
386 "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
387 QMessageBox::StandardButton retval = QMessageBox::question(
388 this, tr("Sending..."), strMessage,
389 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
390 *payFee = (retval == QMessageBox::Yes);
393 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
395 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
396 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
397 .data(Qt::EditRole).toULongLong();
398 if(!clientModel->inInitialBlockDownload())
400 // On incoming transaction, make an info balloon
401 // Unless the initial block download is in progress, to prevent balloon-spam
402 QString date = ttm->index(start, TransactionTableModel::Date, parent)
404 QString type = ttm->index(start, TransactionTableModel::Type, parent)
406 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
409 trayIcon->showMessage((amount)<0 ? tr("Sent transaction") :
410 tr("Incoming transaction"),
411 tr("Date: ") + date + "\n" +
412 tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
413 tr("Type: ") + type + "\n" +
414 tr("Address: ") + address + "\n",
415 QSystemTrayIcon::Information);
419 void BitcoinGUI::gotoOverviewPage()
421 overviewAction->setChecked(true);
422 centralWidget->setCurrentWidget(overviewPage);
424 exportAction->setEnabled(false);
425 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
428 void BitcoinGUI::gotoHistoryPage()
430 historyAction->setChecked(true);
431 centralWidget->setCurrentWidget(transactionsPage);
433 exportAction->setEnabled(true);
434 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
435 connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
438 void BitcoinGUI::gotoAddressBookPage()
440 addressBookAction->setChecked(true);
441 centralWidget->setCurrentWidget(addressBookPage);
443 exportAction->setEnabled(true);
444 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
445 connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
448 void BitcoinGUI::gotoReceiveCoinsPage()
450 receiveCoinsAction->setChecked(true);
451 centralWidget->setCurrentWidget(receiveCoinsPage);
453 exportAction->setEnabled(true);
454 disconnect(exportAction, SIGNAL(triggered()), 0, 0);
455 connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
458 void BitcoinGUI::gotoSendCoinsPage()
460 sendCoinsAction->setChecked(true);
461 sendCoinsPage->clear();
462 centralWidget->setCurrentWidget(sendCoinsPage);
464 exportAction->setEnabled(false);
465 disconnect(exportAction, SIGNAL(triggered()), 0, 0);