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"
14 #include "editaddressdialog.h"
15 #include "optionsmodel.h"
16 #include "transactiondescdialog.h"
17 #include "addresstablemodel.h"
18 #include "transactionview.h"
22 #include <QApplication>
23 #include <QMainWindow>
28 #include <QVBoxLayout>
34 #include <QPushButton>
37 #include <QMessageBox>
38 #include <QProgressBar>
44 BitcoinGUI::BitcoinGUI(QWidget *parent):
45 QMainWindow(parent), trayIcon(0)
48 setWindowTitle(tr("Bitcoin"));
49 setWindowIcon(QIcon(":icons/bitcoin"));
54 QMenu *file = menuBar()->addMenu("&File");
55 file->addAction(sendcoins);
57 file->addAction(quit);
59 QMenu *settings = menuBar()->addMenu("&Settings");
60 settings->addAction(receivingAddresses);
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(addressbook);
72 // Address: <address>: New... : Paste to clipboard
73 QHBoxLayout *hbox_address = new QHBoxLayout();
74 hbox_address->addWidget(new QLabel(tr("Your Bitcoin address:")));
75 address = new QLineEdit();
76 address->setReadOnly(true);
77 address->setFont(GUIUtil::bitcoinAddressFont());
78 address->setToolTip(tr("Your current default receiving address"));
79 hbox_address->addWidget(address);
81 QPushButton *button_new = new QPushButton(tr("&New..."));
82 button_new->setToolTip(tr("Create new receiving address"));
83 button_new->setIcon(QIcon(":/icons/add"));
84 QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
85 button_clipboard->setToolTip(tr("Copy current receiving address to the system clipboard"));
86 button_clipboard->setIcon(QIcon(":/icons/editcopy"));
87 hbox_address->addWidget(button_new);
88 hbox_address->addWidget(button_clipboard);
91 QHBoxLayout *hbox_balance = new QHBoxLayout();
92 hbox_balance->addWidget(new QLabel(tr("Balance:")));
93 hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
95 labelBalance = new QLabel();
96 labelBalance->setFont(QFont("Monospace", -1, QFont::Bold));
97 labelBalance->setToolTip(tr("Your current balance"));
98 hbox_balance->addWidget(labelBalance);
99 hbox_balance->addStretch(1);
101 QVBoxLayout *vbox = new QVBoxLayout();
102 vbox->addLayout(hbox_address);
103 vbox->addLayout(hbox_balance);
105 transactionView = new TransactionView(this);
106 connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
107 vbox->addWidget(transactionView);
109 QWidget *centralwidget = new QWidget(this);
110 centralwidget->setLayout(vbox);
111 setCentralWidget(centralwidget);
116 labelConnections = new QLabel();
117 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
118 labelConnections->setMinimumWidth(150);
119 labelConnections->setToolTip(tr("Number of connections to other clients"));
121 labelBlocks = new QLabel();
122 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
123 labelBlocks->setMinimumWidth(130);
124 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
126 labelTransactions = new QLabel();
127 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
128 labelTransactions->setMinimumWidth(130);
129 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
131 // Progress bar for blocks download
132 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
133 progressBarLabel->setVisible(false);
134 progressBar = new QProgressBar();
135 progressBar->setToolTip(tr("Block chain synchronization in progress"));
136 progressBar->setVisible(false);
138 statusBar()->addWidget(progressBarLabel);
139 statusBar()->addWidget(progressBar);
140 statusBar()->addPermanentWidget(labelConnections);
141 statusBar()->addPermanentWidget(labelBlocks);
142 statusBar()->addPermanentWidget(labelTransactions);
145 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
146 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
151 void BitcoinGUI::createActions()
153 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
154 quit->setToolTip(tr("Quit application"));
155 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
156 sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
157 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
158 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
159 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
160 about->setToolTip(tr("Show information about Bitcoin"));
161 receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("Your &Receiving Addresses..."), this);
162 receivingAddresses->setToolTip(tr("Show the list of receiving addresses and edit their labels"));
163 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
164 options->setToolTip(tr("Modify configuration options for bitcoin"));
165 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
166 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
168 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
169 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
170 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
171 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
172 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
173 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
174 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
177 void BitcoinGUI::setModel(ClientModel *model)
181 // Keep up to date with client
182 setBalance(model->getBalance());
183 connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
185 setNumConnections(model->getNumConnections());
186 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
188 setNumTransactions(model->getNumTransactions());
189 connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
191 setNumBlocks(model->getNumBlocks());
192 connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
194 setAddress(model->getAddressTableModel()->getDefaultAddress());
195 connect(model->getAddressTableModel(), SIGNAL(defaultAddressChanged(QString)), this, SLOT(setAddress(QString)));
197 // Report errors from network/worker thread
198 connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
200 // Put transaction list in tabs
201 transactionView->setModel(model->getTransactionTableModel());
203 // Balloon popup for new transaction
204 connect(model->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
205 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
208 void BitcoinGUI::createTrayIcon()
210 QMenu *trayIconMenu = new QMenu(this);
211 trayIconMenu->addAction(openBitcoin);
212 trayIconMenu->addAction(sendcoins);
213 trayIconMenu->addAction(options);
214 trayIconMenu->addSeparator();
215 trayIconMenu->addAction(quit);
217 trayIcon = new QSystemTrayIcon(this);
218 trayIcon->setContextMenu(trayIconMenu);
219 trayIcon->setIcon(QIcon(":/icons/toolbar"));
220 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
221 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
225 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
227 if(reason == QSystemTrayIcon::DoubleClick)
229 // Doubleclick on system tray icon triggers "open bitcoin"
230 openBitcoin->trigger();
234 void BitcoinGUI::sendcoinsClicked()
241 void BitcoinGUI::addressbookClicked()
243 AddressBookDialog dlg(AddressBookDialog::ForEditing);
244 dlg.setModel(model->getAddressTableModel());
245 dlg.setTab(AddressBookDialog::SendingTab);
249 void BitcoinGUI::receivingAddressesClicked()
251 AddressBookDialog dlg(AddressBookDialog::ForEditing);
252 dlg.setModel(model->getAddressTableModel());
253 dlg.setTab(AddressBookDialog::ReceivingTab);
257 void BitcoinGUI::optionsClicked()
260 dlg.setModel(model->getOptionsModel());
264 void BitcoinGUI::aboutClicked()
270 void BitcoinGUI::newAddressClicked()
272 EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
273 dlg.setModel(model->getAddressTableModel());
276 QString newAddress = dlg.saveCurrentRow();
280 void BitcoinGUI::copyClipboardClicked()
282 // Copy text in address to clipboard
283 QApplication::clipboard()->setText(address->text());
286 void BitcoinGUI::setBalance(qint64 balance)
288 labelBalance->setText(QString::fromStdString(FormatMoney(balance)) + QString(" BTC"));
291 void BitcoinGUI::setAddress(const QString &addr)
293 address->setText(addr);
296 void BitcoinGUI::setNumConnections(int count)
301 case 0: icon = ":/icons/connect_0"; break;
302 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
303 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
304 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
305 default: icon = ":/icons/connect_4"; break;
307 labelConnections->setTextFormat(Qt::RichText);
308 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
311 void BitcoinGUI::setNumBlocks(int count)
313 int total = model->getTotalBlocksEstimate();
316 progressBarLabel->setVisible(true);
317 progressBar->setVisible(true);
318 progressBar->setMaximum(total);
319 progressBar->setValue(count);
323 progressBarLabel->setVisible(false);
324 progressBar->setVisible(false);
327 labelBlocks->setText(tr("%n block(s)", "", count));
330 void BitcoinGUI::setNumTransactions(int count)
332 labelTransactions->setText(tr("%n transaction(s)", "", count));
335 void BitcoinGUI::error(const QString &title, const QString &message)
337 // Report errors from network/worker thread
338 if(trayIcon->supportsMessages())
340 // Show as "balloon" message if possible
341 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
345 // Fall back to old fashioned popup dialog if not
346 QMessageBox::critical(this, title,
348 QMessageBox::Ok, QMessageBox::Ok);
352 void BitcoinGUI::changeEvent(QEvent *e)
354 if (e->type() == QEvent::WindowStateChange)
356 if(model->getOptionsModel()->getMinimizeToTray())
369 QMainWindow::changeEvent(e);
372 void BitcoinGUI::closeEvent(QCloseEvent *event)
374 if(!model->getOptionsModel()->getMinimizeToTray() &&
375 !model->getOptionsModel()->getMinimizeOnClose())
379 QMainWindow::closeEvent(event);
382 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
385 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
386 "which goes to the nodes that process your transaction and helps to support the network. "
387 "Do you want to pay the fee?").arg(QString::fromStdString(FormatMoney(nFeeRequired)));
388 QMessageBox::StandardButton retval = QMessageBox::question(
389 this, tr("Sending..."), strMessage,
390 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
391 *payFee = (retval == QMessageBox::Yes);
394 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
396 // A transaction is doubleclicked
397 TransactionDescDialog dlg(idx);
401 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
403 TransactionTableModel *ttm = model->getTransactionTableModel();
404 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
405 .data(Qt::EditRole).toULongLong();
406 if(amount>0 && !model->inInitialBlockDownload())
408 // On incoming transaction, make an info balloon
409 // Unless the initial block download is in progress, to prevent balloon-spam
410 QString date = ttm->index(start, TransactionTableModel::Date, parent)
412 QString type = ttm->index(start, TransactionTableModel::Type, parent)
414 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
417 trayIcon->showMessage(tr("Incoming transaction"),
418 tr("Date: ") + date + "\n" +
419 tr("Amount: ") + QString::fromStdString(FormatMoney(amount, true)) + "\n" +
420 tr("Type: ") + type + "\n" +
421 tr("Address: ") + address + "\n",
422 QSystemTrayIcon::Information);