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>
43 BitcoinGUI::BitcoinGUI(QWidget *parent):
50 setWindowTitle(tr("Bitcoin Wallet"));
51 setWindowIcon(QIcon(":icons/bitcoin"));
56 QMenu *file = menuBar()->addMenu("&File");
57 file->addAction(sendCoinsAction);
58 file->addAction(receiveCoinsAction);
60 file->addAction(quitAction);
62 QMenu *settings = menuBar()->addMenu("&Settings");
63 settings->addAction(optionsAction);
65 QMenu *help = menuBar()->addMenu("&Help");
66 help->addAction(aboutAction);
69 QToolBar *toolbar = addToolBar("Main toolbar");
70 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
71 toolbar->addAction(overviewAction);
72 toolbar->addAction(sendCoinsAction);
73 toolbar->addAction(receiveCoinsAction);
74 toolbar->addAction(historyAction);
75 toolbar->addAction(addressBookAction);
77 QToolBar *toolbar2 = addToolBar("Transactions toolbar");
78 toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
79 toolbar2->addAction(exportAction);
82 overviewPage = new OverviewPage();
83 QVBoxLayout *vbox = new QVBoxLayout();
85 transactionView = new TransactionView(this);
86 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
87 vbox->addWidget(transactionView);
89 transactionsPage = new QWidget(this);
90 transactionsPage->setLayout(vbox);
92 addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
94 receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
96 sendCoinsPage = new SendCoinsDialog(this);
98 centralWidget = new QStackedWidget(this);
99 centralWidget->addWidget(overviewPage);
100 centralWidget->addWidget(transactionsPage);
101 centralWidget->addWidget(addressBookPage);
102 centralWidget->addWidget(receiveCoinsPage);
103 centralWidget->addWidget(sendCoinsPage);
104 setCentralWidget(centralWidget);
109 labelConnections = new QLabel();
110 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
111 labelConnections->setMinimumWidth(150);
112 labelConnections->setToolTip(tr("Number of connections to other clients"));
114 labelBlocks = new QLabel();
115 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
116 labelBlocks->setMinimumWidth(130);
117 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
119 // Progress bar for blocks download
120 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
121 progressBarLabel->setVisible(false);
122 progressBar = new QProgressBar();
123 progressBar->setToolTip(tr("Block chain synchronization in progress"));
124 progressBar->setVisible(false);
126 statusBar()->addWidget(progressBarLabel);
127 statusBar()->addWidget(progressBar);
128 statusBar()->addPermanentWidget(labelConnections);
129 statusBar()->addPermanentWidget(labelBlocks);
136 void BitcoinGUI::createActions()
138 QActionGroup *tabGroup = new QActionGroup(this);
140 overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
141 overviewAction->setCheckable(true);
142 tabGroup->addAction(overviewAction);
144 historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
145 historyAction->setCheckable(true);
146 tabGroup->addAction(historyAction);
148 addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
149 addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
150 addressBookAction->setCheckable(true);
151 tabGroup->addAction(addressBookAction);
153 receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
154 receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
155 receiveCoinsAction->setCheckable(true);
156 tabGroup->addAction(receiveCoinsAction);
158 sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
159 sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
160 sendCoinsAction->setCheckable(true);
161 tabGroup->addAction(sendCoinsAction);
163 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
164 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
165 connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
166 connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
167 connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
169 quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
170 quitAction->setToolTip(tr("Quit application"));
171 aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
172 aboutAction->setToolTip(tr("Show information about Bitcoin"));
173 optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
174 optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
175 openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
176 openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
177 exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
178 exportAction->setToolTip(tr("Export data in current view to a file"));
180 connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
181 connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
182 connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
183 connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(show()));
184 connect(exportAction, SIGNAL(triggered()), this, SLOT(exportClicked()));
187 void BitcoinGUI::setClientModel(ClientModel *clientModel)
189 this->clientModel = clientModel;
191 if(clientModel->isTestNet())
193 QString title_testnet = tr("Bitcoin Wallet [testnet]");
194 setWindowTitle(title_testnet);
195 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
198 trayIcon->setToolTip(title_testnet);
199 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
203 // Keep up to date with client
204 setNumConnections(clientModel->getNumConnections());
205 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
207 setNumBlocks(clientModel->getNumBlocks());
208 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
210 // Report errors from network/worker thread
211 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
214 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
216 this->walletModel = walletModel;
218 // Keep up to date with wallet
219 setBalance(walletModel->getBalance());
220 connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
222 setNumTransactions(walletModel->getNumTransactions());
223 connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
225 // Report errors from wallet thread
226 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
228 // Put transaction list in tabs
229 transactionView->setModel(walletModel->getTransactionTableModel());
231 addressBookPage->setModel(walletModel->getAddressTableModel());
232 receiveCoinsPage->setModel(walletModel->getAddressTableModel());
233 sendCoinsPage->setModel(walletModel);
235 // Balloon popup for new transaction
236 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
237 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
240 void BitcoinGUI::createTrayIcon()
242 QMenu *trayIconMenu = new QMenu(this);
243 trayIconMenu->addAction(openBitcoinAction);
244 trayIconMenu->addAction(sendCoinsAction);
245 trayIconMenu->addAction(optionsAction);
246 trayIconMenu->addSeparator();
247 trayIconMenu->addAction(quitAction);
249 trayIcon = new QSystemTrayIcon(this);
250 trayIcon->setContextMenu(trayIconMenu);
251 trayIcon->setToolTip("Bitcoin client");
252 trayIcon->setIcon(QIcon(":/icons/toolbar"));
253 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
254 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
258 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
260 if(reason == QSystemTrayIcon::DoubleClick)
262 // Doubleclick on system tray icon triggers "open bitcoin"
263 openBitcoinAction->trigger();
267 void BitcoinGUI::optionsClicked()
270 dlg.setModel(clientModel->getOptionsModel());
274 void BitcoinGUI::aboutClicked()
277 dlg.setModel(clientModel);
281 void BitcoinGUI::setBalance(qint64 balance)
283 overviewPage->setBalance(balance);
286 void BitcoinGUI::setNumConnections(int count)
291 case 0: icon = ":/icons/connect_0"; break;
292 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
293 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
294 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
295 default: icon = ":/icons/connect_4"; break;
297 labelConnections->setTextFormat(Qt::RichText);
298 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
301 void BitcoinGUI::setNumBlocks(int count)
303 int total = clientModel->getTotalBlocksEstimate();
306 progressBarLabel->setVisible(true);
307 progressBar->setVisible(true);
308 progressBar->setMaximum(total);
309 progressBar->setValue(count);
313 progressBarLabel->setVisible(false);
314 progressBar->setVisible(false);
317 labelBlocks->setText(tr("%n block(s)", "", count));
320 void BitcoinGUI::setNumTransactions(int count)
322 overviewPage->setNumTransactions(count);
325 void BitcoinGUI::error(const QString &title, const QString &message)
327 // Report errors from network/worker thread
328 if(trayIcon->supportsMessages())
330 // Show as "balloon" message if possible
331 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
335 // Fall back to old fashioned popup dialog if not
336 QMessageBox::critical(this, title,
338 QMessageBox::Ok, QMessageBox::Ok);
342 void BitcoinGUI::changeEvent(QEvent *e)
344 if (e->type() == QEvent::WindowStateChange)
346 if(clientModel->getOptionsModel()->getMinimizeToTray())
359 QMainWindow::changeEvent(e);
362 void BitcoinGUI::closeEvent(QCloseEvent *event)
364 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
365 !clientModel->getOptionsModel()->getMinimizeOnClose())
369 QMainWindow::closeEvent(event);
372 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
375 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
376 "which goes to the nodes that process your transaction and helps to support the network. "
377 "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
378 QMessageBox::StandardButton retval = QMessageBox::question(
379 this, tr("Sending..."), strMessage,
380 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
381 *payFee = (retval == QMessageBox::Yes);
384 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
386 // A transaction is doubleclicked
387 TransactionDescDialog dlg(idx);
391 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
393 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
394 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
395 .data(Qt::EditRole).toULongLong();
396 if(amount>0 && !clientModel->inInitialBlockDownload())
398 // On incoming transaction, make an info balloon
399 // Unless the initial block download is in progress, to prevent balloon-spam
400 QString date = ttm->index(start, TransactionTableModel::Date, parent)
402 QString type = ttm->index(start, TransactionTableModel::Type, parent)
404 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
407 trayIcon->showMessage(tr("Incoming transaction"),
408 tr("Date: ") + date + "\n" +
409 tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
410 tr("Type: ") + type + "\n" +
411 tr("Address: ") + address + "\n",
412 QSystemTrayIcon::Information);
416 void BitcoinGUI::gotoOverviewPage()
418 overviewAction->setChecked(true);
419 centralWidget->setCurrentWidget(overviewPage);
420 exportAction->setEnabled(false);
423 void BitcoinGUI::gotoHistoryPage()
425 historyAction->setChecked(true);
426 centralWidget->setCurrentWidget(transactionsPage);
427 exportAction->setEnabled(true);
430 void BitcoinGUI::gotoAddressBookPage()
432 addressBookAction->setChecked(true);
433 centralWidget->setCurrentWidget(addressBookPage);
434 exportAction->setEnabled(false); // TODO
437 void BitcoinGUI::gotoReceiveCoinsPage()
439 receiveCoinsAction->setChecked(true);
440 centralWidget->setCurrentWidget(receiveCoinsPage);
441 exportAction->setEnabled(false); // TODO
444 void BitcoinGUI::gotoSendCoinsPage()
446 sendCoinsAction->setChecked(true);
447 centralWidget->setCurrentWidget(sendCoinsPage);
448 exportAction->setEnabled(false);
451 void BitcoinGUI::exportClicked()
453 // Redirect to the right view, as soon as export for other views
454 // (such as address book) is implemented.
455 transactionView->exportClicked();