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);
75 // Address: <address>: New... : Paste to clipboard
76 QHBoxLayout *hbox_address = new QHBoxLayout();
77 hbox_address->addWidget(new QLabel(tr("Your Bitcoin address:")));
78 address = new QLineEdit();
79 address->setReadOnly(true);
80 address->setFont(GUIUtil::bitcoinAddressFont());
81 address->setToolTip(tr("Your current default receiving address"));
82 hbox_address->addWidget(address);
84 QPushButton *button_new = new QPushButton(tr("&New address..."));
85 button_new->setToolTip(tr("Create new receiving address"));
86 button_new->setIcon(QIcon(":/icons/add"));
87 QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
88 button_clipboard->setToolTip(tr("Copy current receiving address to the system clipboard"));
89 button_clipboard->setIcon(QIcon(":/icons/editcopy"));
90 hbox_address->addWidget(button_new);
91 hbox_address->addWidget(button_clipboard);
94 QHBoxLayout *hbox_balance = new QHBoxLayout();
95 hbox_balance->addWidget(new QLabel(tr("Balance:")));
96 hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
98 labelBalance = new QLabel();
99 labelBalance->setFont(QFont("Monospace", -1, QFont::Bold));
100 labelBalance->setToolTip(tr("Your current balance"));
101 hbox_balance->addWidget(labelBalance);
102 hbox_balance->addStretch(1);
104 QVBoxLayout *vbox = new QVBoxLayout();
105 vbox->addLayout(hbox_address);
106 vbox->addLayout(hbox_balance);
108 transactionView = new TransactionView(this);
109 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
110 vbox->addWidget(transactionView);
112 QWidget *centralwidget = new QWidget(this);
113 centralwidget->setLayout(vbox);
114 setCentralWidget(centralwidget);
119 labelConnections = new QLabel();
120 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
121 labelConnections->setMinimumWidth(150);
122 labelConnections->setToolTip(tr("Number of connections to other clients"));
124 labelBlocks = new QLabel();
125 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
126 labelBlocks->setMinimumWidth(130);
127 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
129 labelTransactions = new QLabel();
130 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
131 labelTransactions->setMinimumWidth(130);
132 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
134 // Progress bar for blocks download
135 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
136 progressBarLabel->setVisible(false);
137 progressBar = new QProgressBar();
138 progressBar->setToolTip(tr("Block chain synchronization in progress"));
139 progressBar->setVisible(false);
141 statusBar()->addWidget(progressBarLabel);
142 statusBar()->addWidget(progressBar);
143 statusBar()->addPermanentWidget(labelConnections);
144 statusBar()->addPermanentWidget(labelBlocks);
145 statusBar()->addPermanentWidget(labelTransactions);
148 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
149 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
154 void BitcoinGUI::createActions()
156 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
157 quit->setToolTip(tr("Quit application"));
158 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
159 sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
160 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
161 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
162 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
163 about->setToolTip(tr("Show information about Bitcoin"));
164 receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receiving Addresses..."), this);
165 receivingAddresses->setToolTip(tr("Show the list of receiving addresses and edit their labels"));
166 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
167 options->setToolTip(tr("Modify configuration options for bitcoin"));
168 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
169 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
171 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
172 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
173 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
174 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
175 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
176 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
177 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
180 void BitcoinGUI::setClientModel(ClientModel *clientModel)
182 this->clientModel = clientModel;
184 if(clientModel->isTestNet())
186 setWindowTitle(tr("Bitcoin [testnet]"));
187 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
190 trayIcon->setToolTip(tr("Bitcoin [testnet]"));
191 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
195 // Keep up to date with client
196 setNumConnections(clientModel->getNumConnections());
197 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
199 setNumBlocks(clientModel->getNumBlocks());
200 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
202 // Report errors from network/worker thread
203 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
206 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
208 this->walletModel = walletModel;
210 // Keep up to date with wallet
211 setBalance(walletModel->getBalance());
212 connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
214 setNumTransactions(walletModel->getNumTransactions());
215 connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
217 setAddress(walletModel->getAddressTableModel()->getDefaultAddress());
218 connect(walletModel->getAddressTableModel(), SIGNAL(defaultAddressChanged(QString)), this, SLOT(setAddress(QString)));
220 // Report errors from wallet thread
221 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
223 // Put transaction list in tabs
224 transactionView->setModel(walletModel->getTransactionTableModel());
226 // Balloon popup for new transaction
227 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
228 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
231 void BitcoinGUI::createTrayIcon()
233 QMenu *trayIconMenu = new QMenu(this);
234 trayIconMenu->addAction(openBitcoin);
235 trayIconMenu->addAction(sendcoins);
236 trayIconMenu->addAction(options);
237 trayIconMenu->addSeparator();
238 trayIconMenu->addAction(quit);
240 trayIcon = new QSystemTrayIcon(this);
241 trayIcon->setContextMenu(trayIconMenu);
242 trayIcon->setToolTip("Bitcoin client");
243 trayIcon->setIcon(QIcon(":/icons/toolbar"));
244 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
245 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
249 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
251 if(reason == QSystemTrayIcon::DoubleClick)
253 // Doubleclick on system tray icon triggers "open bitcoin"
254 openBitcoin->trigger();
258 void BitcoinGUI::sendcoinsClicked()
261 dlg.setModel(walletModel);
265 void BitcoinGUI::addressbookClicked()
267 AddressBookDialog dlg(AddressBookDialog::ForEditing);
268 dlg.setModel(walletModel->getAddressTableModel());
269 dlg.setTab(AddressBookDialog::SendingTab);
273 void BitcoinGUI::receivingAddressesClicked()
275 AddressBookDialog dlg(AddressBookDialog::ForEditing);
276 dlg.setModel(walletModel->getAddressTableModel());
277 dlg.setTab(AddressBookDialog::ReceivingTab);
281 void BitcoinGUI::optionsClicked()
284 dlg.setModel(clientModel->getOptionsModel());
288 void BitcoinGUI::aboutClicked()
291 dlg.setModel(clientModel);
295 void BitcoinGUI::newAddressClicked()
297 EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
298 dlg.setModel(walletModel->getAddressTableModel());
301 QString newAddress = dlg.saveCurrentRow();
305 void BitcoinGUI::copyClipboardClicked()
307 // Copy text in address to clipboard
308 QApplication::clipboard()->setText(address->text());
311 void BitcoinGUI::setBalance(qint64 balance)
313 labelBalance->setText(GUIUtil::formatMoney(balance) + QString(" BTC"));
316 void BitcoinGUI::setAddress(const QString &addr)
318 address->setText(addr);
321 void BitcoinGUI::setNumConnections(int count)
326 case 0: icon = ":/icons/connect_0"; break;
327 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
328 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
329 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
330 default: icon = ":/icons/connect_4"; break;
332 labelConnections->setTextFormat(Qt::RichText);
333 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
336 void BitcoinGUI::setNumBlocks(int count)
338 int total = clientModel->getTotalBlocksEstimate();
341 progressBarLabel->setVisible(true);
342 progressBar->setVisible(true);
343 progressBar->setMaximum(total);
344 progressBar->setValue(count);
348 progressBarLabel->setVisible(false);
349 progressBar->setVisible(false);
352 labelBlocks->setText(tr("%n block(s)", "", count));
355 void BitcoinGUI::setNumTransactions(int count)
357 labelTransactions->setText(tr("%n transaction(s)", "", count));
360 void BitcoinGUI::error(const QString &title, const QString &message)
362 // Report errors from network/worker thread
363 if(trayIcon->supportsMessages())
365 // Show as "balloon" message if possible
366 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
370 // Fall back to old fashioned popup dialog if not
371 QMessageBox::critical(this, title,
373 QMessageBox::Ok, QMessageBox::Ok);
377 void BitcoinGUI::changeEvent(QEvent *e)
379 if (e->type() == QEvent::WindowStateChange)
381 if(clientModel->getOptionsModel()->getMinimizeToTray())
394 QMainWindow::changeEvent(e);
397 void BitcoinGUI::closeEvent(QCloseEvent *event)
399 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
400 !clientModel->getOptionsModel()->getMinimizeOnClose())
404 QMainWindow::closeEvent(event);
407 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
410 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
411 "which goes to the nodes that process your transaction and helps to support the network. "
412 "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
413 QMessageBox::StandardButton retval = QMessageBox::question(
414 this, tr("Sending..."), strMessage,
415 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
416 *payFee = (retval == QMessageBox::Yes);
419 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
421 // A transaction is doubleclicked
422 TransactionDescDialog dlg(idx);
426 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
428 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
429 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
430 .data(Qt::EditRole).toULongLong();
431 if(amount>0 && !clientModel->inInitialBlockDownload())
433 // On incoming transaction, make an info balloon
434 // Unless the initial block download is in progress, to prevent balloon-spam
435 QString date = ttm->index(start, TransactionTableModel::Date, parent)
437 QString type = ttm->index(start, TransactionTableModel::Type, parent)
439 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
442 trayIcon->showMessage(tr("Incoming transaction"),
443 tr("Date: ") + date + "\n" +
444 tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
445 tr("Type: ") + type + "\n" +
446 tr("Address: ") + address + "\n",
447 QSystemTrayIcon::Information);