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>
32 #include <QPushButton>
34 #include <QMessageBox>
35 #include <QProgressBar>
41 BitcoinGUI::BitcoinGUI(QWidget *parent):
48 setWindowTitle(tr("Bitcoin"));
49 setWindowIcon(QIcon(":icons/bitcoin"));
54 QMenu *file = menuBar()->addMenu("&File");
55 file->addAction(sendCoins);
56 file->addAction(receiveCoins);
58 file->addAction(quit);
60 QMenu *settings = menuBar()->addMenu("&Settings");
61 settings->addAction(options);
63 QMenu *help = menuBar()->addMenu("&Help");
64 help->addAction(about);
67 QToolBar *toolbar = addToolBar("Main toolbar");
68 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
69 toolbar->addAction(sendCoins);
70 toolbar->addAction(receiveCoins);
71 toolbar->addAction(addressbook);
74 QHBoxLayout *hbox_balance = new QHBoxLayout();
75 hbox_balance->addWidget(new QLabel(tr("Balance:")));
76 hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
78 labelBalance = new QLabel();
79 labelBalance->setFont(QFont("Monospace", -1, QFont::Bold));
80 labelBalance->setToolTip(tr("Your current balance"));
81 labelBalance->setTextInteractionFlags(Qt::TextSelectableByMouse|Qt::TextSelectableByKeyboard);
82 hbox_balance->addWidget(labelBalance);
83 hbox_balance->addStretch(1);
85 QVBoxLayout *vbox = new QVBoxLayout();
86 vbox->addLayout(hbox_balance);
88 transactionView = new TransactionView(this);
89 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
90 vbox->addWidget(transactionView);
92 QWidget *centralwidget = new QWidget(this);
93 centralwidget->setLayout(vbox);
94 setCentralWidget(centralwidget);
99 labelConnections = new QLabel();
100 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
101 labelConnections->setMinimumWidth(150);
102 labelConnections->setToolTip(tr("Number of connections to other clients"));
104 labelBlocks = new QLabel();
105 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
106 labelBlocks->setMinimumWidth(130);
107 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
109 labelTransactions = new QLabel();
110 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
111 labelTransactions->setMinimumWidth(130);
112 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
114 // Progress bar for blocks download
115 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
116 progressBarLabel->setVisible(false);
117 progressBar = new QProgressBar();
118 progressBar->setToolTip(tr("Block chain synchronization in progress"));
119 progressBar->setVisible(false);
121 statusBar()->addWidget(progressBarLabel);
122 statusBar()->addWidget(progressBar);
123 statusBar()->addPermanentWidget(labelConnections);
124 statusBar()->addPermanentWidget(labelBlocks);
125 statusBar()->addPermanentWidget(labelTransactions);
130 void BitcoinGUI::createActions()
132 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
133 quit->setToolTip(tr("Quit application"));
134 sendCoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
135 sendCoins->setToolTip(tr("Send coins to a bitcoin address"));
136 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
137 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
138 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
139 about->setToolTip(tr("Show information about Bitcoin"));
140 receiveCoins = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
141 receiveCoins->setToolTip(tr("Show the list of addresses for receiving payments"));
142 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
143 options->setToolTip(tr("Modify configuration options for bitcoin"));
144 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
145 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
147 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
148 connect(sendCoins, SIGNAL(triggered()), this, SLOT(sendCoinsClicked()));
149 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
150 connect(receiveCoins, SIGNAL(triggered()), this, SLOT(receiveCoinsClicked()));
151 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
152 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
153 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
156 void BitcoinGUI::setClientModel(ClientModel *clientModel)
158 this->clientModel = clientModel;
160 if(clientModel->isTestNet())
162 setWindowTitle(tr("Bitcoin [testnet]"));
163 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
166 trayIcon->setToolTip(tr("Bitcoin [testnet]"));
167 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
171 // Keep up to date with client
172 setNumConnections(clientModel->getNumConnections());
173 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
175 setNumBlocks(clientModel->getNumBlocks());
176 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
178 // Report errors from network/worker thread
179 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
182 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
184 this->walletModel = walletModel;
186 // Keep up to date with wallet
187 setBalance(walletModel->getBalance());
188 connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
190 setNumTransactions(walletModel->getNumTransactions());
191 connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
193 // Report errors from wallet thread
194 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
196 // Put transaction list in tabs
197 transactionView->setModel(walletModel->getTransactionTableModel());
199 // Balloon popup for new transaction
200 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
201 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
204 void BitcoinGUI::createTrayIcon()
206 QMenu *trayIconMenu = new QMenu(this);
207 trayIconMenu->addAction(openBitcoin);
208 trayIconMenu->addAction(sendCoins);
209 trayIconMenu->addAction(options);
210 trayIconMenu->addSeparator();
211 trayIconMenu->addAction(quit);
213 trayIcon = new QSystemTrayIcon(this);
214 trayIcon->setContextMenu(trayIconMenu);
215 trayIcon->setToolTip("Bitcoin client");
216 trayIcon->setIcon(QIcon(":/icons/toolbar"));
217 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
218 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
222 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
224 if(reason == QSystemTrayIcon::DoubleClick)
226 // Doubleclick on system tray icon triggers "open bitcoin"
227 openBitcoin->trigger();
231 void BitcoinGUI::sendCoinsClicked()
234 dlg.setModel(walletModel);
238 void BitcoinGUI::addressbookClicked()
240 AddressBookDialog dlg(AddressBookDialog::ForEditing);
241 dlg.setModel(walletModel->getAddressTableModel());
242 dlg.setTab(AddressBookDialog::SendingTab);
246 void BitcoinGUI::receiveCoinsClicked()
248 AddressBookDialog dlg(AddressBookDialog::ForEditing);
249 dlg.setModel(walletModel->getAddressTableModel());
250 dlg.setTab(AddressBookDialog::ReceivingTab);
254 void BitcoinGUI::optionsClicked()
257 dlg.setModel(clientModel->getOptionsModel());
261 void BitcoinGUI::aboutClicked()
264 dlg.setModel(clientModel);
268 void BitcoinGUI::setBalance(qint64 balance)
270 labelBalance->setText(GUIUtil::formatMoney(balance) + QString(" BTC"));
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 labelBlocks->setText(tr("%n block(s)", "", count));
307 void BitcoinGUI::setNumTransactions(int count)
309 labelTransactions->setText(tr("%n transaction(s)", "", count));
312 void BitcoinGUI::error(const QString &title, const QString &message)
314 // Report errors from network/worker thread
315 if(trayIcon->supportsMessages())
317 // Show as "balloon" message if possible
318 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
322 // Fall back to old fashioned popup dialog if not
323 QMessageBox::critical(this, title,
325 QMessageBox::Ok, QMessageBox::Ok);
329 void BitcoinGUI::changeEvent(QEvent *e)
331 if (e->type() == QEvent::WindowStateChange)
333 if(clientModel->getOptionsModel()->getMinimizeToTray())
346 QMainWindow::changeEvent(e);
349 void BitcoinGUI::closeEvent(QCloseEvent *event)
351 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
352 !clientModel->getOptionsModel()->getMinimizeOnClose())
356 QMainWindow::closeEvent(event);
359 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
362 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
363 "which goes to the nodes that process your transaction and helps to support the network. "
364 "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
365 QMessageBox::StandardButton retval = QMessageBox::question(
366 this, tr("Sending..."), strMessage,
367 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
368 *payFee = (retval == QMessageBox::Yes);
371 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
373 // A transaction is doubleclicked
374 TransactionDescDialog dlg(idx);
378 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
380 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
381 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
382 .data(Qt::EditRole).toULongLong();
383 if(amount>0 && !clientModel->inInitialBlockDownload())
385 // On incoming transaction, make an info balloon
386 // Unless the initial block download is in progress, to prevent balloon-spam
387 QString date = ttm->index(start, TransactionTableModel::Date, parent)
389 QString type = ttm->index(start, TransactionTableModel::Type, parent)
391 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
394 trayIcon->showMessage(tr("Incoming transaction"),
395 tr("Date: ") + date + "\n" +
396 tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
397 tr("Type: ") + type + "\n" +
398 tr("Address: ") + address + "\n",
399 QSystemTrayIcon::Information);