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);
76 // Address: <address>: New... : Paste to clipboard
77 QHBoxLayout *hbox_address = new QHBoxLayout();
78 hbox_address->addWidget(new QLabel(tr("Your Bitcoin address:")));
79 address = new QLineEdit();
80 address->setReadOnly(true);
81 address->setFont(GUIUtil::bitcoinAddressFont());
82 address->setToolTip(tr("Your current default receiving address"));
83 hbox_address->addWidget(address);
85 QPushButton *button_new = new QPushButton(tr("&New address..."));
86 button_new->setToolTip(tr("Create new receiving address"));
87 button_new->setIcon(QIcon(":/icons/add"));
88 QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
89 button_clipboard->setToolTip(tr("Copy current receiving address to the system clipboard"));
90 button_clipboard->setIcon(QIcon(":/icons/editcopy"));
91 hbox_address->addWidget(button_new);
92 hbox_address->addWidget(button_clipboard);
95 QHBoxLayout *hbox_balance = new QHBoxLayout();
96 hbox_balance->addWidget(new QLabel(tr("Balance:")));
97 hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
99 labelBalance = new QLabel();
100 labelBalance->setFont(QFont("Monospace", -1, QFont::Bold));
101 labelBalance->setToolTip(tr("Your current balance"));
102 hbox_balance->addWidget(labelBalance);
103 hbox_balance->addStretch(1);
105 QVBoxLayout *vbox = new QVBoxLayout();
106 vbox->addLayout(hbox_address);
107 vbox->addLayout(hbox_balance);
109 transactionView = new TransactionView(this);
110 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
111 vbox->addWidget(transactionView);
113 QWidget *centralwidget = new QWidget(this);
114 centralwidget->setLayout(vbox);
115 setCentralWidget(centralwidget);
120 labelConnections = new QLabel();
121 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
122 labelConnections->setMinimumWidth(150);
123 labelConnections->setToolTip(tr("Number of connections to other clients"));
125 labelBlocks = new QLabel();
126 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
127 labelBlocks->setMinimumWidth(130);
128 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
130 labelTransactions = new QLabel();
131 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
132 labelTransactions->setMinimumWidth(130);
133 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
135 // Progress bar for blocks download
136 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
137 progressBarLabel->setVisible(false);
138 progressBar = new QProgressBar();
139 progressBar->setToolTip(tr("Block chain synchronization in progress"));
140 progressBar->setVisible(false);
142 statusBar()->addWidget(progressBarLabel);
143 statusBar()->addWidget(progressBar);
144 statusBar()->addPermanentWidget(labelConnections);
145 statusBar()->addPermanentWidget(labelBlocks);
146 statusBar()->addPermanentWidget(labelTransactions);
149 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
150 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
155 void BitcoinGUI::createActions()
157 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
158 quit->setToolTip(tr("Quit application"));
159 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
160 sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
161 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
162 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
163 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
164 about->setToolTip(tr("Show information about Bitcoin"));
165 receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("Your &Receiving Addresses..."), this);
166 receivingAddresses->setToolTip(tr("Show the list of receiving addresses and edit their labels"));
167 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
168 options->setToolTip(tr("Modify configuration options for bitcoin"));
169 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
170 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
172 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
173 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
174 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
175 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
176 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
177 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
178 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
181 void BitcoinGUI::setClientModel(ClientModel *clientModel)
183 this->clientModel = clientModel;
185 // Keep up to date with client
186 setNumConnections(clientModel->getNumConnections());
187 connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
189 setNumBlocks(clientModel->getNumBlocks());
190 connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
192 // Report errors from network/worker thread
193 connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
196 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
198 this->walletModel = walletModel;
200 // Keep up to date with wallet
201 setBalance(walletModel->getBalance());
202 connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
204 setNumTransactions(walletModel->getNumTransactions());
205 connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
207 setAddress(walletModel->getAddressTableModel()->getDefaultAddress());
208 connect(walletModel->getAddressTableModel(), SIGNAL(defaultAddressChanged(QString)), this, SLOT(setAddress(QString)));
210 // Report errors from wallet thread
211 connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
213 // Put transaction list in tabs
214 transactionView->setModel(walletModel->getTransactionTableModel());
216 // Balloon popup for new transaction
217 connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
218 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
221 void BitcoinGUI::createTrayIcon()
223 QMenu *trayIconMenu = new QMenu(this);
224 trayIconMenu->addAction(openBitcoin);
225 trayIconMenu->addAction(sendcoins);
226 trayIconMenu->addAction(options);
227 trayIconMenu->addSeparator();
228 trayIconMenu->addAction(quit);
230 trayIcon = new QSystemTrayIcon(this);
231 trayIcon->setContextMenu(trayIconMenu);
232 trayIcon->setIcon(QIcon(":/icons/toolbar"));
233 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
234 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
238 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
240 if(reason == QSystemTrayIcon::DoubleClick)
242 // Doubleclick on system tray icon triggers "open bitcoin"
243 openBitcoin->trigger();
247 void BitcoinGUI::sendcoinsClicked()
250 dlg.setModel(walletModel);
254 void BitcoinGUI::addressbookClicked()
256 AddressBookDialog dlg(AddressBookDialog::ForEditing);
257 dlg.setModel(walletModel->getAddressTableModel());
258 dlg.setTab(AddressBookDialog::SendingTab);
262 void BitcoinGUI::receivingAddressesClicked()
264 AddressBookDialog dlg(AddressBookDialog::ForEditing);
265 dlg.setModel(walletModel->getAddressTableModel());
266 dlg.setTab(AddressBookDialog::ReceivingTab);
270 void BitcoinGUI::optionsClicked()
273 dlg.setModel(clientModel->getOptionsModel());
277 void BitcoinGUI::aboutClicked()
283 void BitcoinGUI::newAddressClicked()
285 EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
286 dlg.setModel(walletModel->getAddressTableModel());
289 QString newAddress = dlg.saveCurrentRow();
293 void BitcoinGUI::copyClipboardClicked()
295 // Copy text in address to clipboard
296 QApplication::clipboard()->setText(address->text());
299 void BitcoinGUI::setBalance(qint64 balance)
301 labelBalance->setText(QString::fromStdString(FormatMoney(balance)) + QString(" BTC"));
304 void BitcoinGUI::setAddress(const QString &addr)
306 address->setText(addr);
309 void BitcoinGUI::setNumConnections(int count)
314 case 0: icon = ":/icons/connect_0"; break;
315 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
316 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
317 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
318 default: icon = ":/icons/connect_4"; break;
320 labelConnections->setTextFormat(Qt::RichText);
321 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
324 void BitcoinGUI::setNumBlocks(int count)
326 int total = clientModel->getTotalBlocksEstimate();
329 progressBarLabel->setVisible(true);
330 progressBar->setVisible(true);
331 progressBar->setMaximum(total);
332 progressBar->setValue(count);
336 progressBarLabel->setVisible(false);
337 progressBar->setVisible(false);
340 labelBlocks->setText(tr("%n block(s)", "", count));
343 void BitcoinGUI::setNumTransactions(int count)
345 labelTransactions->setText(tr("%n transaction(s)", "", count));
348 void BitcoinGUI::error(const QString &title, const QString &message)
350 // Report errors from network/worker thread
351 if(trayIcon->supportsMessages())
353 // Show as "balloon" message if possible
354 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
358 // Fall back to old fashioned popup dialog if not
359 QMessageBox::critical(this, title,
361 QMessageBox::Ok, QMessageBox::Ok);
365 void BitcoinGUI::changeEvent(QEvent *e)
367 if (e->type() == QEvent::WindowStateChange)
369 if(clientModel->getOptionsModel()->getMinimizeToTray())
382 QMainWindow::changeEvent(e);
385 void BitcoinGUI::closeEvent(QCloseEvent *event)
387 if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
388 !clientModel->getOptionsModel()->getMinimizeOnClose())
392 QMainWindow::closeEvent(event);
395 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
398 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
399 "which goes to the nodes that process your transaction and helps to support the network. "
400 "Do you want to pay the fee?").arg(QString::fromStdString(FormatMoney(nFeeRequired)));
401 QMessageBox::StandardButton retval = QMessageBox::question(
402 this, tr("Sending..."), strMessage,
403 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
404 *payFee = (retval == QMessageBox::Yes);
407 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
409 // A transaction is doubleclicked
410 TransactionDescDialog dlg(idx);
414 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
416 TransactionTableModel *ttm = walletModel->getTransactionTableModel();
417 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
418 .data(Qt::EditRole).toULongLong();
419 if(amount>0 && !clientModel->inInitialBlockDownload())
421 // On incoming transaction, make an info balloon
422 // Unless the initial block download is in progress, to prevent balloon-spam
423 QString date = ttm->index(start, TransactionTableModel::Date, parent)
425 QString type = ttm->index(start, TransactionTableModel::Type, parent)
427 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
430 trayIcon->showMessage(tr("Incoming transaction"),
431 tr("Date: ") + date + "\n" +
432 tr("Amount: ") + QString::fromStdString(FormatMoney(amount, true)) + "\n" +
433 tr("Type: ") + type + "\n" +
434 tr("Address: ") + address + "\n",
435 QSystemTrayIcon::Information);