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"
21 #include <QApplication>
22 #include <QMainWindow>
27 #include <QVBoxLayout>
33 #include <QPushButton>
36 #include <QMessageBox>
37 #include <QProgressBar>
43 BitcoinGUI::BitcoinGUI(QWidget *parent):
50 setWindowTitle(tr("Bitcoin"));
51 setWindowIcon(QIcon(":icons/bitcoin"));
56 QMenu *file = menuBar()->addMenu("&File");
57 file->addAction(sendcoins);
59 file->addAction(quit);
61 QMenu *settings = menuBar()->addMenu("&Settings");
62 settings->addAction(receivingAddresses);
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(sendcoins);
72 toolbar->addAction(addressbook);
73 toolbar->addAction(receivingAddresses);
76 QHBoxLayout *hbox_balance = new QHBoxLayout();
77 hbox_balance->addWidget(new QLabel(tr("Balance:")));
78 hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
80 labelBalance = new QLabel();
81 labelBalance->setFont(QFont("Monospace", -1, QFont::Bold));
82 labelBalance->setToolTip(tr("Your current balance"));
83 hbox_balance->addWidget(labelBalance);
84 hbox_balance->addStretch(1);
86 QVBoxLayout *vbox = new QVBoxLayout();
87 vbox->addLayout(hbox_balance);
89 transactionView = new TransactionView(this);
90 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
91 vbox->addWidget(transactionView);
93 QWidget *centralwidget = new QWidget(this);
94 centralwidget->setLayout(vbox);
95 setCentralWidget(centralwidget);
100 labelConnections = new QLabel();
101 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
102 labelConnections->setMinimumWidth(150);
103 labelConnections->setToolTip(tr("Number of connections to other clients"));
105 labelBlocks = new QLabel();
106 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
107 labelBlocks->setMinimumWidth(130);
108 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
110 labelTransactions = new QLabel();
111 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
112 labelTransactions->setMinimumWidth(130);
113 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
115 // Progress bar for blocks download
116 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
117 progressBarLabel->setVisible(false);
118 progressBar = new QProgressBar();
119 progressBar->setToolTip(tr("Block chain synchronization in progress"));
120 progressBar->setVisible(false);
122 statusBar()->addWidget(progressBarLabel);
123 statusBar()->addWidget(progressBar);
124 statusBar()->addPermanentWidget(labelConnections);
125 statusBar()->addPermanentWidget(labelBlocks);
126 statusBar()->addPermanentWidget(labelTransactions);
131 void BitcoinGUI::createActions()
133 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
134 quit->setToolTip(tr("Quit application"));
135 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
136 sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
137 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
138 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
139 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
140 about->setToolTip(tr("Show information about Bitcoin"));
141 receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receiving Addresses..."), this);
142 receivingAddresses->setToolTip(tr("Show the list of addresses for receiving payments"));
143 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
144 options->setToolTip(tr("Modify configuration options for bitcoin"));
145 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
146 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
148 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
149 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
150 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
151 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
152 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
153 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
154 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
157 void BitcoinGUI::setClientModel(ClientModel *clientModel)
159 this->clientModel = clientModel;
161 if(clientModel->isTestNet())
163 setWindowTitle(tr("Bitcoin [testnet]"));
164 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
167 trayIcon->setToolTip(tr("Bitcoin [testnet]"));
168 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
172 // Keep up to date with client
173 setNumConnections(clientModel->getNumConnections());
174 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
176 setNumBlocks(clientModel->getNumBlocks());
177 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
179 // Report errors from network/worker thread
180 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
183 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
185 this->walletModel = walletModel;
187 // Keep up to date with wallet
188 setBalance(walletModel->getBalance());
189 connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
191 setNumTransactions(walletModel->getNumTransactions());
192 connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
194 // Report errors from wallet thread
195 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
197 // Put transaction list in tabs
198 transactionView->setModel(walletModel->getTransactionTableModel());
200 // Balloon popup for new transaction
201 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
202 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
205 void BitcoinGUI::createTrayIcon()
207 QMenu *trayIconMenu = new QMenu(this);
208 trayIconMenu->addAction(openBitcoin);
209 trayIconMenu->addAction(sendcoins);
210 trayIconMenu->addAction(options);
211 trayIconMenu->addSeparator();
212 trayIconMenu->addAction(quit);
214 trayIcon = new QSystemTrayIcon(this);
215 trayIcon->setContextMenu(trayIconMenu);
216 trayIcon->setToolTip("Bitcoin client");
217 trayIcon->setIcon(QIcon(":/icons/toolbar"));
218 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
219 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
223 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
225 if(reason == QSystemTrayIcon::DoubleClick)
227 // Doubleclick on system tray icon triggers "open bitcoin"
228 openBitcoin->trigger();
232 void BitcoinGUI::sendcoinsClicked()
235 dlg.setModel(walletModel);
239 void BitcoinGUI::addressbookClicked()
241 AddressBookDialog dlg(AddressBookDialog::ForEditing);
242 dlg.setModel(walletModel->getAddressTableModel());
243 dlg.setTab(AddressBookDialog::SendingTab);
247 void BitcoinGUI::receivingAddressesClicked()
249 AddressBookDialog dlg(AddressBookDialog::ForEditing);
250 dlg.setModel(walletModel->getAddressTableModel());
251 dlg.setTab(AddressBookDialog::ReceivingTab);
255 void BitcoinGUI::optionsClicked()
258 dlg.setModel(clientModel->getOptionsModel());
262 void BitcoinGUI::aboutClicked()
265 dlg.setModel(clientModel);
269 void BitcoinGUI::setBalance(qint64 balance)
271 labelBalance->setText(GUIUtil::formatMoney(balance) + QString(" BTC"));
274 void BitcoinGUI::setNumConnections(int count)
279 case 0: icon = ":/icons/connect_0"; break;
280 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
281 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
282 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
283 default: icon = ":/icons/connect_4"; break;
285 labelConnections->setTextFormat(Qt::RichText);
286 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
289 void BitcoinGUI::setNumBlocks(int count)
291 int total = clientModel->getTotalBlocksEstimate();
294 progressBarLabel->setVisible(true);
295 progressBar->setVisible(true);
296 progressBar->setMaximum(total);
297 progressBar->setValue(count);
301 progressBarLabel->setVisible(false);
302 progressBar->setVisible(false);
305 labelBlocks->setText(tr("%n block(s)", "", count));
308 void BitcoinGUI::setNumTransactions(int count)
310 labelTransactions->setText(tr("%n transaction(s)", "", count));
313 void BitcoinGUI::error(const QString &title, const QString &message)
315 // Report errors from network/worker thread
316 if(trayIcon->supportsMessages())
318 // Show as "balloon" message if possible
319 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
323 // Fall back to old fashioned popup dialog if not
324 QMessageBox::critical(this, title,
326 QMessageBox::Ok, QMessageBox::Ok);
330 void BitcoinGUI::changeEvent(QEvent *e)
332 if (e->type() == QEvent::WindowStateChange)
334 if(clientModel->getOptionsModel()->getMinimizeToTray())
347 QMainWindow::changeEvent(e);
350 void BitcoinGUI::closeEvent(QCloseEvent *event)
352 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
353 !clientModel->getOptionsModel()->getMinimizeOnClose())
357 QMainWindow::closeEvent(event);
360 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
363 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
364 "which goes to the nodes that process your transaction and helps to support the network. "
365 "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
366 QMessageBox::StandardButton retval = QMessageBox::question(
367 this, tr("Sending..."), strMessage,
368 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
369 *payFee = (retval == QMessageBox::Yes);
372 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
374 // A transaction is doubleclicked
375 TransactionDescDialog dlg(idx);
379 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
381 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
382 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
383 .data(Qt::EditRole).toULongLong();
384 if(amount>0 && !clientModel->inInitialBlockDownload())
386 // On incoming transaction, make an info balloon
387 // Unless the initial block download is in progress, to prevent balloon-spam
388 QString date = ttm->index(start, TransactionTableModel::Date, parent)
390 QString type = ttm->index(start, TransactionTableModel::Type, parent)
392 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
395 trayIcon->showMessage(tr("Incoming transaction"),
396 tr("Date: ") + date + "\n" +
397 tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
398 tr("Type: ") + type + "\n" +
399 tr("Address: ") + address + "\n",
400 QSystemTrayIcon::Information);