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 Wallet"));
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 QString title_testnet = tr("Bitcoin Wallet [testnet]");
174 setWindowTitle(title_testnet);
175 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
178 trayIcon->setToolTip(title_testnet);
179 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
183 // Keep up to date with client
184 setNumConnections(clientModel->getNumConnections());
185 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
187 setNumBlocks(clientModel->getNumBlocks());
188 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
190 // Report errors from network/worker thread
191 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
194 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
196 this->walletModel = walletModel;
198 // Keep up to date with wallet
199 setBalance(walletModel->getBalance());
200 connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
202 setNumTransactions(walletModel->getNumTransactions());
203 connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
205 // Report errors from wallet thread
206 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
208 // Put transaction list in tabs
209 transactionView->setModel(walletModel->getTransactionTableModel());
211 // Balloon popup for new transaction
212 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
213 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
216 void BitcoinGUI::createTrayIcon()
218 QMenu *trayIconMenu = new QMenu(this);
219 trayIconMenu->addAction(openBitcoin);
220 trayIconMenu->addAction(sendCoins);
221 trayIconMenu->addAction(options);
222 trayIconMenu->addSeparator();
223 trayIconMenu->addAction(quit);
225 trayIcon = new QSystemTrayIcon(this);
226 trayIcon->setContextMenu(trayIconMenu);
227 trayIcon->setToolTip("Bitcoin client");
228 trayIcon->setIcon(QIcon(":/icons/toolbar"));
229 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
230 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
234 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
236 if(reason == QSystemTrayIcon::DoubleClick)
238 // Doubleclick on system tray icon triggers "open bitcoin"
239 openBitcoin->trigger();
243 void BitcoinGUI::sendCoinsClicked()
246 dlg.setModel(walletModel);
250 void BitcoinGUI::addressbookClicked()
252 AddressBookDialog dlg(AddressBookDialog::ForEditing);
253 dlg.setModel(walletModel->getAddressTableModel());
254 dlg.setTab(AddressBookDialog::SendingTab);
258 void BitcoinGUI::receiveCoinsClicked()
260 AddressBookDialog dlg(AddressBookDialog::ForEditing);
261 dlg.setModel(walletModel->getAddressTableModel());
262 dlg.setTab(AddressBookDialog::ReceivingTab);
266 void BitcoinGUI::optionsClicked()
269 dlg.setModel(clientModel->getOptionsModel());
273 void BitcoinGUI::aboutClicked()
276 dlg.setModel(clientModel);
280 void BitcoinGUI::setBalance(qint64 balance)
282 overviewPage->setBalance(balance);
285 void BitcoinGUI::setNumConnections(int count)
290 case 0: icon = ":/icons/connect_0"; break;
291 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
292 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
293 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
294 default: icon = ":/icons/connect_4"; break;
296 labelConnections->setTextFormat(Qt::RichText);
297 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
300 void BitcoinGUI::setNumBlocks(int count)
302 int total = clientModel->getTotalBlocksEstimate();
305 progressBarLabel->setVisible(true);
306 progressBar->setVisible(true);
307 progressBar->setMaximum(total);
308 progressBar->setValue(count);
312 progressBarLabel->setVisible(false);
313 progressBar->setVisible(false);
316 labelBlocks->setText(tr("%n block(s)", "", count));
319 void BitcoinGUI::setNumTransactions(int count)
321 labelTransactions->setText(tr("%n transaction(s)", "", count));
324 void BitcoinGUI::error(const QString &title, const QString &message)
326 // Report errors from network/worker thread
327 if(trayIcon->supportsMessages())
329 // Show as "balloon" message if possible
330 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
334 // Fall back to old fashioned popup dialog if not
335 QMessageBox::critical(this, title,
337 QMessageBox::Ok, QMessageBox::Ok);
341 void BitcoinGUI::changeEvent(QEvent *e)
343 if (e->type() == QEvent::WindowStateChange)
345 if(clientModel->getOptionsModel()->getMinimizeToTray())
358 QMainWindow::changeEvent(e);
361 void BitcoinGUI::closeEvent(QCloseEvent *event)
363 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
364 !clientModel->getOptionsModel()->getMinimizeOnClose())
368 QMainWindow::closeEvent(event);
371 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
374 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
375 "which goes to the nodes that process your transaction and helps to support the network. "
376 "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
377 QMessageBox::StandardButton retval = QMessageBox::question(
378 this, tr("Sending..."), strMessage,
379 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
380 *payFee = (retval == QMessageBox::Yes);
383 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
385 // A transaction is doubleclicked
386 TransactionDescDialog dlg(idx);
390 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
392 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
393 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
394 .data(Qt::EditRole).toULongLong();
395 if(amount>0 && !clientModel->inInitialBlockDownload())
397 // On incoming transaction, make an info balloon
398 // Unless the initial block download is in progress, to prevent balloon-spam
399 QString date = ttm->index(start, TransactionTableModel::Date, parent)
401 QString type = ttm->index(start, TransactionTableModel::Type, parent)
403 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
406 trayIcon->showMessage(tr("Incoming transaction"),
407 tr("Date: ") + date + "\n" +
408 tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
409 tr("Type: ") + type + "\n" +
410 tr("Address: ") + address + "\n",
411 QSystemTrayIcon::Information);
415 void BitcoinGUI::gotoOverviewTab()
417 overviewAction->setChecked(true);
418 centralWidget->setCurrentWidget(overviewPage);
421 void BitcoinGUI::gotoHistoryTab()
423 historyAction->setChecked(true);
424 centralWidget->setCurrentWidget(transactionsPage);