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"
23 #include <QApplication>
24 #include <QMainWindow>
29 #include <QVBoxLayout>
35 #include <QPushButton>
38 #include <QMessageBox>
39 #include <QProgressBar>
45 BitcoinGUI::BitcoinGUI(QWidget *parent):
52 setWindowTitle(tr("Bitcoin"));
53 setWindowIcon(QIcon(":icons/bitcoin"));
58 QMenu *file = menuBar()->addMenu("&File");
59 file->addAction(sendcoins);
61 file->addAction(quit);
63 QMenu *settings = menuBar()->addMenu("&Settings");
64 settings->addAction(receivingAddresses);
65 settings->addAction(options);
67 QMenu *help = menuBar()->addMenu("&Help");
68 help->addAction(about);
71 QToolBar *toolbar = addToolBar("Main toolbar");
72 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
73 toolbar->addAction(sendcoins);
74 toolbar->addAction(addressbook);
75 toolbar->addAction(receivingAddresses);
77 // Address: <address>: New... : Paste to clipboard
78 QHBoxLayout *hbox_address = new QHBoxLayout();
79 hbox_address->addWidget(new QLabel(tr("Your Bitcoin address:")));
80 address = new QLineEdit();
81 address->setReadOnly(true);
82 address->setFont(GUIUtil::bitcoinAddressFont());
83 address->setToolTip(tr("Your current default receiving address"));
84 hbox_address->addWidget(address);
86 QPushButton *button_new = new QPushButton(tr("&New address..."));
87 button_new->setToolTip(tr("Create new receiving address"));
88 button_new->setIcon(QIcon(":/icons/add"));
89 QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
90 button_clipboard->setToolTip(tr("Copy current receiving address to the system clipboard"));
91 button_clipboard->setIcon(QIcon(":/icons/editcopy"));
92 hbox_address->addWidget(button_new);
93 hbox_address->addWidget(button_clipboard);
96 QHBoxLayout *hbox_balance = new QHBoxLayout();
97 hbox_balance->addWidget(new QLabel(tr("Balance:")));
98 hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
100 labelBalance = new QLabel();
101 labelBalance->setFont(QFont("Monospace", -1, QFont::Bold));
102 labelBalance->setToolTip(tr("Your current balance"));
103 hbox_balance->addWidget(labelBalance);
104 hbox_balance->addStretch(1);
106 QVBoxLayout *vbox = new QVBoxLayout();
107 vbox->addLayout(hbox_address);
108 vbox->addLayout(hbox_balance);
110 transactionView = new TransactionView(this);
111 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
112 vbox->addWidget(transactionView);
114 QWidget *centralwidget = new QWidget(this);
115 centralwidget->setLayout(vbox);
116 setCentralWidget(centralwidget);
121 labelConnections = new QLabel();
122 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
123 labelConnections->setMinimumWidth(150);
124 labelConnections->setToolTip(tr("Number of connections to other clients"));
126 labelBlocks = new QLabel();
127 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
128 labelBlocks->setMinimumWidth(130);
129 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
131 labelTransactions = new QLabel();
132 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
133 labelTransactions->setMinimumWidth(130);
134 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
136 // Progress bar for blocks download
137 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
138 progressBarLabel->setVisible(false);
139 progressBar = new QProgressBar();
140 progressBar->setToolTip(tr("Block chain synchronization in progress"));
141 progressBar->setVisible(false);
143 statusBar()->addWidget(progressBarLabel);
144 statusBar()->addWidget(progressBar);
145 statusBar()->addPermanentWidget(labelConnections);
146 statusBar()->addPermanentWidget(labelBlocks);
147 statusBar()->addPermanentWidget(labelTransactions);
150 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
151 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
156 void BitcoinGUI::createActions()
158 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
159 quit->setToolTip(tr("Quit application"));
160 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
161 sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
162 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
163 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
164 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
165 about->setToolTip(tr("Show information about Bitcoin"));
166 receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receiving Addresses..."), this);
167 receivingAddresses->setToolTip(tr("Show the list of receiving addresses and edit their labels"));
168 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
169 options->setToolTip(tr("Modify configuration options for bitcoin"));
170 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
171 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
173 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
174 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
175 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
176 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
177 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
178 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
179 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
182 void BitcoinGUI::setClientModel(ClientModel *clientModel)
184 this->clientModel = clientModel;
186 if(clientModel->isTestNet())
188 setWindowTitle(tr("Bitcoin [testnet]"));
189 setWindowIcon(QIcon(":icons/bitcoin_testnet"));
192 trayIcon->setToolTip(tr("Bitcoin [testnet]"));
193 trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
197 // Keep up to date with client
198 setNumConnections(clientModel->getNumConnections());
199 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
201 setNumBlocks(clientModel->getNumBlocks());
202 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
204 // Report errors from network/worker thread
205 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
208 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
210 this->walletModel = walletModel;
212 // Keep up to date with wallet
213 setBalance(walletModel->getBalance());
214 connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
216 setNumTransactions(walletModel->getNumTransactions());
217 connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
219 setAddress(walletModel->getAddressTableModel()->getDefaultAddress());
220 connect(walletModel->getAddressTableModel(), SIGNAL(defaultAddressChanged(QString)), this, SLOT(setAddress(QString)));
222 // Report errors from wallet thread
223 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
225 // Put transaction list in tabs
226 transactionView->setModel(walletModel->getTransactionTableModel());
228 // Balloon popup for new transaction
229 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
230 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
233 void BitcoinGUI::createTrayIcon()
235 QMenu *trayIconMenu = new QMenu(this);
236 trayIconMenu->addAction(openBitcoin);
237 trayIconMenu->addAction(sendcoins);
238 trayIconMenu->addAction(options);
239 trayIconMenu->addSeparator();
240 trayIconMenu->addAction(quit);
242 trayIcon = new QSystemTrayIcon(this);
243 trayIcon->setContextMenu(trayIconMenu);
244 trayIcon->setToolTip("Bitcoin client");
245 trayIcon->setIcon(QIcon(":/icons/toolbar"));
246 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
247 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
251 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
253 if(reason == QSystemTrayIcon::DoubleClick)
255 // Doubleclick on system tray icon triggers "open bitcoin"
256 openBitcoin->trigger();
260 void BitcoinGUI::sendcoinsClicked()
263 dlg.setModel(walletModel);
267 void BitcoinGUI::addressbookClicked()
269 AddressBookDialog dlg(AddressBookDialog::ForEditing);
270 dlg.setModel(walletModel->getAddressTableModel());
271 dlg.setTab(AddressBookDialog::SendingTab);
275 void BitcoinGUI::receivingAddressesClicked()
277 AddressBookDialog dlg(AddressBookDialog::ForEditing);
278 dlg.setModel(walletModel->getAddressTableModel());
279 dlg.setTab(AddressBookDialog::ReceivingTab);
283 void BitcoinGUI::optionsClicked()
286 dlg.setModel(clientModel->getOptionsModel());
290 void BitcoinGUI::aboutClicked()
296 void BitcoinGUI::newAddressClicked()
298 EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
299 dlg.setModel(walletModel->getAddressTableModel());
302 QString newAddress = dlg.saveCurrentRow();
306 void BitcoinGUI::copyClipboardClicked()
308 // Copy text in address to clipboard
309 QApplication::clipboard()->setText(address->text());
312 void BitcoinGUI::setBalance(qint64 balance)
314 labelBalance->setText(QString::fromStdString(FormatMoney(balance)) + QString(" BTC"));
317 void BitcoinGUI::setAddress(const QString &addr)
319 address->setText(addr);
322 void BitcoinGUI::setNumConnections(int count)
327 case 0: icon = ":/icons/connect_0"; break;
328 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
329 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
330 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
331 default: icon = ":/icons/connect_4"; break;
333 labelConnections->setTextFormat(Qt::RichText);
334 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
337 void BitcoinGUI::setNumBlocks(int count)
339 int total = clientModel->getTotalBlocksEstimate();
342 progressBarLabel->setVisible(true);
343 progressBar->setVisible(true);
344 progressBar->setMaximum(total);
345 progressBar->setValue(count);
349 progressBarLabel->setVisible(false);
350 progressBar->setVisible(false);
353 labelBlocks->setText(tr("%n block(s)", "", count));
356 void BitcoinGUI::setNumTransactions(int count)
358 labelTransactions->setText(tr("%n transaction(s)", "", count));
361 void BitcoinGUI::error(const QString &title, const QString &message)
363 // Report errors from network/worker thread
364 if(trayIcon->supportsMessages())
366 // Show as "balloon" message if possible
367 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
371 // Fall back to old fashioned popup dialog if not
372 QMessageBox::critical(this, title,
374 QMessageBox::Ok, QMessageBox::Ok);
378 void BitcoinGUI::changeEvent(QEvent *e)
380 if (e->type() == QEvent::WindowStateChange)
382 if(clientModel->getOptionsModel()->getMinimizeToTray())
395 QMainWindow::changeEvent(e);
398 void BitcoinGUI::closeEvent(QCloseEvent *event)
400 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
401 !clientModel->getOptionsModel()->getMinimizeOnClose())
405 QMainWindow::closeEvent(event);
408 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
411 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
412 "which goes to the nodes that process your transaction and helps to support the network. "
413 "Do you want to pay the fee?").arg(QString::fromStdString(FormatMoney(nFeeRequired)));
414 QMessageBox::StandardButton retval = QMessageBox::question(
415 this, tr("Sending..."), strMessage,
416 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
417 *payFee = (retval == QMessageBox::Yes);
420 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
422 // A transaction is doubleclicked
423 TransactionDescDialog dlg(idx);
427 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
429 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
430 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
431 .data(Qt::EditRole).toULongLong();
432 if(amount>0 && !clientModel->inInitialBlockDownload())
434 // On incoming transaction, make an info balloon
435 // Unless the initial block download is in progress, to prevent balloon-spam
436 QString date = ttm->index(start, TransactionTableModel::Date, parent)
438 QString type = ttm->index(start, TransactionTableModel::Type, parent)
440 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
443 trayIcon->showMessage(tr("Incoming transaction"),
444 tr("Date: ") + date + "\n" +
445 tr("Amount: ") + QString::fromStdString(FormatMoney(amount, true)) + "\n" +
446 tr("Type: ") + type + "\n" +
447 tr("Address: ") + address + "\n",
448 QSystemTrayIcon::Information);