4 * W.J. van der Laan 2011
6 #include "bitcoingui.h"
7 #include "transactiontablemodel.h"
8 #include "addressbookdialog.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"));
51 setWindowIcon(QIcon(":icons/bitcoin"));
56 QMenu *file = menuBar()->addMenu("&File");
57 file->addAction(sendCoins);
58 file->addAction(receiveCoins);
60 file->addAction(quit);
62 QMenu *settings = menuBar()->addMenu("&Settings");
63 settings->addAction(options);
65 QMenu *help = menuBar()->addMenu("&Help");
66 help->addAction(about);
69 QToolBar *toolbar = addToolBar("Main toolbar");
70 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
71 toolbar->addAction(overviewAction);
72 toolbar->addAction(historyAction);
73 toolbar->addSeparator();
74 toolbar->addAction(sendCoins);
75 toolbar->addAction(receiveCoins);
76 toolbar->addAction(addressbook);
78 overviewPage = new OverviewPage();
80 QVBoxLayout *vbox = new QVBoxLayout();
82 transactionView = new TransactionView(this);
83 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
84 vbox->addWidget(transactionView);
86 transactionsPage = new QWidget(this);
87 transactionsPage->setLayout(vbox);
89 centralWidget = new QStackedWidget(this);
90 centralWidget->addWidget(overviewPage);
91 centralWidget->addWidget(transactionsPage);
92 setCentralWidget(centralWidget);
97 labelConnections = new QLabel();
98 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
99 labelConnections->setMinimumWidth(150);
100 labelConnections->setToolTip(tr("Number of connections to other clients"));
102 labelBlocks = new QLabel();
103 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
104 labelBlocks->setMinimumWidth(130);
105 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
107 labelTransactions = new QLabel();
108 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
109 labelTransactions->setMinimumWidth(130);
110 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
112 // Progress bar for blocks download
113 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
114 progressBarLabel->setVisible(false);
115 progressBar = new QProgressBar();
116 progressBar->setToolTip(tr("Block chain synchronization in progress"));
117 progressBar->setVisible(false);
119 statusBar()->addWidget(progressBarLabel);
120 statusBar()->addWidget(progressBar);
121 statusBar()->addPermanentWidget(labelConnections);
122 statusBar()->addPermanentWidget(labelBlocks);
123 statusBar()->addPermanentWidget(labelTransactions);
130 void BitcoinGUI::createActions()
132 QActionGroup *tabGroup = new QActionGroup(this);
133 overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
134 overviewAction->setCheckable(true);
135 tabGroup->addAction(overviewAction);
136 historyAction = new QAction(QIcon(":/icons/history"), tr("&History"), this);
137 historyAction->setCheckable(true);
138 tabGroup->addAction(historyAction);
140 connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewTab()));
141 connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryTab()));
143 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
144 quit->setToolTip(tr("Quit application"));
145 sendCoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
146 sendCoins->setToolTip(tr("Send coins to a bitcoin address"));
147 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
148 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
149 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
150 about->setToolTip(tr("Show information about Bitcoin"));
151 receiveCoins = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
152 receiveCoins->setToolTip(tr("Show the list of addresses for receiving payments"));
153 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
154 options->setToolTip(tr("Modify configuration options for bitcoin"));
155 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
156 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
158 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
159 connect(sendCoins, SIGNAL(triggered()), this, SLOT(sendCoinsClicked()));
160 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
161 connect(receiveCoins, SIGNAL(triggered()), this, SLOT(receiveCoinsClicked()));
162 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
163 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
164 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
167 void BitcoinGUI::setClientModel(ClientModel *clientModel)
169 this->clientModel = clientModel;
171 if(clientModel->isTestNet())
173 setWindowTitle(tr("Bitcoin [testnet]"));
174 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
177 trayIcon->setToolTip(tr("Bitcoin [testnet]"));
178 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
182 // Keep up to date with client
183 setNumConnections(clientModel->getNumConnections());
184 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
186 setNumBlocks(clientModel->getNumBlocks());
187 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
189 // Report errors from network/worker thread
190 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
193 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
195 this->walletModel = walletModel;
197 // Keep up to date with wallet
198 setBalance(walletModel->getBalance());
199 connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
201 setNumTransactions(walletModel->getNumTransactions());
202 connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
204 // Report errors from wallet thread
205 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
207 // Put transaction list in tabs
208 transactionView->setModel(walletModel->getTransactionTableModel());
210 // Balloon popup for new transaction
211 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
212 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
215 void BitcoinGUI::createTrayIcon()
217 QMenu *trayIconMenu = new QMenu(this);
218 trayIconMenu->addAction(openBitcoin);
219 trayIconMenu->addAction(sendCoins);
220 trayIconMenu->addAction(options);
221 trayIconMenu->addSeparator();
222 trayIconMenu->addAction(quit);
224 trayIcon = new QSystemTrayIcon(this);
225 trayIcon->setContextMenu(trayIconMenu);
226 trayIcon->setToolTip("Bitcoin client");
227 trayIcon->setIcon(QIcon(":/icons/toolbar"));
228 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
229 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
233 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
235 if(reason == QSystemTrayIcon::DoubleClick)
237 // Doubleclick on system tray icon triggers "open bitcoin"
238 openBitcoin->trigger();
242 void BitcoinGUI::sendCoinsClicked()
245 dlg.setModel(walletModel);
249 void BitcoinGUI::addressbookClicked()
251 AddressBookDialog dlg(AddressBookDialog::ForEditing);
252 dlg.setModel(walletModel->getAddressTableModel());
253 dlg.setTab(AddressBookDialog::SendingTab);
257 void BitcoinGUI::receiveCoinsClicked()
259 AddressBookDialog dlg(AddressBookDialog::ForEditing);
260 dlg.setModel(walletModel->getAddressTableModel());
261 dlg.setTab(AddressBookDialog::ReceivingTab);
265 void BitcoinGUI::optionsClicked()
268 dlg.setModel(clientModel->getOptionsModel());
272 void BitcoinGUI::aboutClicked()
275 dlg.setModel(clientModel);
279 void BitcoinGUI::setBalance(qint64 balance)
281 overviewPage->setBalance(balance);
284 void BitcoinGUI::setNumConnections(int count)
289 case 0: icon = ":/icons/connect_0"; break;
290 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
291 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
292 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
293 default: icon = ":/icons/connect_4"; break;
295 labelConnections->setTextFormat(Qt::RichText);
296 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
299 void BitcoinGUI::setNumBlocks(int count)
301 int total = clientModel->getTotalBlocksEstimate();
304 progressBarLabel->setVisible(true);
305 progressBar->setVisible(true);
306 progressBar->setMaximum(total);
307 progressBar->setValue(count);
311 progressBarLabel->setVisible(false);
312 progressBar->setVisible(false);
315 labelBlocks->setText(tr("%n block(s)", "", count));
318 void BitcoinGUI::setNumTransactions(int count)
320 labelTransactions->setText(tr("%n transaction(s)", "", count));
323 void BitcoinGUI::error(const QString &title, const QString &message)
325 // Report errors from network/worker thread
326 if(trayIcon->supportsMessages())
328 // Show as "balloon" message if possible
329 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
333 // Fall back to old fashioned popup dialog if not
334 QMessageBox::critical(this, title,
336 QMessageBox::Ok, QMessageBox::Ok);
340 void BitcoinGUI::changeEvent(QEvent *e)
342 if (e->type() == QEvent::WindowStateChange)
344 if(clientModel->getOptionsModel()->getMinimizeToTray())
357 QMainWindow::changeEvent(e);
360 void BitcoinGUI::closeEvent(QCloseEvent *event)
362 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
363 !clientModel->getOptionsModel()->getMinimizeOnClose())
367 QMainWindow::closeEvent(event);
370 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
373 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
374 "which goes to the nodes that process your transaction and helps to support the network. "
375 "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
376 QMessageBox::StandardButton retval = QMessageBox::question(
377 this, tr("Sending..."), strMessage,
378 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
379 *payFee = (retval == QMessageBox::Yes);
382 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
384 // A transaction is doubleclicked
385 TransactionDescDialog dlg(idx);
389 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
391 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
392 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
393 .data(Qt::EditRole).toULongLong();
394 if(amount>0 && !clientModel->inInitialBlockDownload())
396 // On incoming transaction, make an info balloon
397 // Unless the initial block download is in progress, to prevent balloon-spam
398 QString date = ttm->index(start, TransactionTableModel::Date, parent)
400 QString type = ttm->index(start, TransactionTableModel::Type, parent)
402 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
405 trayIcon->showMessage(tr("Incoming transaction"),
406 tr("Date: ") + date + "\n" +
407 tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
408 tr("Type: ") + type + "\n" +
409 tr("Address: ") + address + "\n",
410 QSystemTrayIcon::Information);
414 void BitcoinGUI::gotoOverviewTab()
416 overviewAction->setChecked(true);
417 centralWidget->setCurrentWidget(overviewPage);
420 void BitcoinGUI::gotoHistoryTab()
422 historyAction->setChecked(true);
423 centralWidget->setCurrentWidget(transactionsPage);