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"
21 #include <QApplication>
22 #include <QMainWindow>
27 #include <QVBoxLayout>
34 #include <QPushButton>
35 #include <QHeaderView>
37 #include <QSortFilterProxyModel>
39 #include <QMessageBox>
40 #include <QProgressBar>
46 BitcoinGUI::BitcoinGUI(QWidget *parent):
47 QMainWindow(parent), trayIcon(0)
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);
74 // Address: <address>: New... : Paste to clipboard
75 QHBoxLayout *hbox_address = new QHBoxLayout();
76 hbox_address->addWidget(new QLabel(tr("Your Bitcoin address:")));
77 address = new QLineEdit();
78 address->setReadOnly(true);
79 address->setFont(GUIUtil::bitcoinAddressFont());
80 address->setToolTip(tr("Your current default receiving address"));
81 hbox_address->addWidget(address);
83 QPushButton *button_new = new QPushButton(tr("&New..."));
84 button_new->setToolTip(tr("Create new receiving address"));
85 QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
86 button_clipboard->setToolTip(tr("Copy current receiving address to the system clipboard"));
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 vbox->addWidget(createTabs());
107 QWidget *centralwidget = new QWidget(this);
108 centralwidget->setLayout(vbox);
109 setCentralWidget(centralwidget);
114 labelConnections = new QLabel();
115 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
116 labelConnections->setMinimumWidth(150);
117 labelConnections->setToolTip(tr("Number of connections to other clients"));
119 labelBlocks = new QLabel();
120 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
121 labelBlocks->setMinimumWidth(130);
122 labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
124 labelTransactions = new QLabel();
125 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
126 labelTransactions->setMinimumWidth(130);
127 labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
129 // Progress bar for blocks download
130 progressBarLabel = new QLabel(tr("Synchronizing with network..."));
131 progressBarLabel->setVisible(false);
132 progressBar = new QProgressBar();
133 progressBar->setToolTip(tr("Block chain synchronization in progress"));
134 progressBar->setVisible(false);
136 statusBar()->addWidget(progressBarLabel);
137 statusBar()->addWidget(progressBar);
138 statusBar()->addPermanentWidget(labelConnections);
139 statusBar()->addPermanentWidget(labelBlocks);
140 statusBar()->addPermanentWidget(labelTransactions);
143 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
144 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
149 void BitcoinGUI::createActions()
151 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
152 quit->setToolTip(tr("Quit application"));
153 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
154 sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
155 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
156 addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
157 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
158 about->setToolTip(tr("Show information about Bitcoin"));
159 receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("Your &Receiving Addresses..."), this);
160 receivingAddresses->setToolTip(tr("Show the list of receiving addresses and edit their labels"));
161 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
162 options->setToolTip(tr("Modify configuration options for bitcoin"));
163 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
164 openBitcoin->setToolTip(tr("Show the Bitcoin window"));
166 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
167 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
168 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
169 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
170 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
171 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
172 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
175 void BitcoinGUI::setModel(ClientModel *model)
179 // Keep up to date with client
180 setBalance(model->getBalance());
181 connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
183 setNumConnections(model->getNumConnections());
184 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
186 setNumTransactions(model->getNumTransactions());
187 connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
189 setNumBlocks(model->getNumBlocks());
190 connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
192 setAddress(model->getAddressTableModel()->getDefaultAddress());
193 connect(model->getAddressTableModel(), SIGNAL(defaultAddressChanged(QString)), this, SLOT(setAddress(QString)));
195 // Report errors from network/worker thread
196 connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
198 // Put transaction list in tabs
199 setTabsModel(model->getTransactionTableModel());
202 void BitcoinGUI::createTrayIcon()
204 QMenu *trayIconMenu = new QMenu(this);
205 trayIconMenu->addAction(openBitcoin);
206 trayIconMenu->addAction(sendcoins);
207 trayIconMenu->addAction(options);
208 trayIconMenu->addSeparator();
209 trayIconMenu->addAction(quit);
211 trayIcon = new QSystemTrayIcon(this);
212 trayIcon->setContextMenu(trayIconMenu);
213 trayIcon->setIcon(QIcon(":/icons/toolbar"));
214 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
215 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
219 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
221 if(reason == QSystemTrayIcon::DoubleClick)
223 // Doubleclick on system tray icon triggers "open bitcoin"
224 openBitcoin->trigger();
228 QWidget *BitcoinGUI::createTabs()
230 QStringList tab_labels;
231 tab_labels << tr("All transactions")
232 << tr("Sent/Received")
236 QTabWidget *tabs = new QTabWidget(this);
237 for(int i = 0; i < tab_labels.size(); ++i)
239 QTableView *view = new QTableView(this);
240 tabs->addTab(view, tab_labels.at(i));
242 connect(view, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
243 transactionViews.append(view);
249 void BitcoinGUI::setTabsModel(QAbstractItemModel *transaction_model)
251 QStringList tab_filters;
253 << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
254 << "^["+TransactionTableModel::Sent+"]"
255 << "^["+TransactionTableModel::Received+"]";
257 for(int i = 0; i < transactionViews.size(); ++i)
259 QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
260 proxy_model->setSourceModel(transaction_model);
261 proxy_model->setDynamicSortFilter(true);
262 proxy_model->setFilterRole(TransactionTableModel::TypeRole);
263 proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
264 proxy_model->setSortRole(Qt::EditRole);
266 QTableView *transaction_table = transactionViews.at(i);
267 transaction_table->setModel(proxy_model);
268 transaction_table->setAlternatingRowColors(true);
269 transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
270 transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
271 transaction_table->setSortingEnabled(true);
272 transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
273 transaction_table->verticalHeader()->hide();
275 transaction_table->horizontalHeader()->resizeSection(
276 TransactionTableModel::Status, 23);
277 transaction_table->horizontalHeader()->resizeSection(
278 TransactionTableModel::Date, 120);
279 transaction_table->horizontalHeader()->resizeSection(
280 TransactionTableModel::Type, 120);
281 transaction_table->horizontalHeader()->setResizeMode(
282 TransactionTableModel::ToAddress, QHeaderView::Stretch);
283 transaction_table->horizontalHeader()->resizeSection(
284 TransactionTableModel::Amount, 79);
287 connect(transaction_model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
288 this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
291 void BitcoinGUI::sendcoinsClicked()
298 void BitcoinGUI::addressbookClicked()
300 AddressBookDialog dlg(AddressBookDialog::ForEditing);
301 dlg.setModel(model->getAddressTableModel());
302 dlg.setTab(AddressBookDialog::SendingTab);
306 void BitcoinGUI::receivingAddressesClicked()
308 AddressBookDialog dlg(AddressBookDialog::ForEditing);
309 dlg.setModel(model->getAddressTableModel());
310 dlg.setTab(AddressBookDialog::ReceivingTab);
314 void BitcoinGUI::optionsClicked()
317 dlg.setModel(model->getOptionsModel());
321 void BitcoinGUI::aboutClicked()
327 void BitcoinGUI::newAddressClicked()
329 EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
330 dlg.setModel(model->getAddressTableModel());
333 QString newAddress = dlg.saveCurrentRow();
337 void BitcoinGUI::copyClipboardClicked()
339 // Copy text in address to clipboard
340 QApplication::clipboard()->setText(address->text());
343 void BitcoinGUI::setBalance(qint64 balance)
345 labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
348 void BitcoinGUI::setAddress(const QString &addr)
350 address->setText(addr);
353 void BitcoinGUI::setNumConnections(int count)
358 case 0: icon = ":/icons/connect_0"; break;
359 case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
360 case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
361 case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
362 default: icon = ":/icons/connect_4"; break;
364 labelConnections->setTextFormat(Qt::RichText);
365 labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
368 void BitcoinGUI::setNumBlocks(int count)
370 int total = model->getTotalBlocksEstimate();
373 progressBarLabel->setVisible(true);
374 progressBar->setVisible(true);
375 progressBar->setMaximum(total);
376 progressBar->setValue(count);
380 progressBarLabel->setVisible(false);
381 progressBar->setVisible(false);
384 labelBlocks->setText(tr("%n block(s)", "", count));
387 void BitcoinGUI::setNumTransactions(int count)
389 labelTransactions->setText(tr("%n transaction(s)", "", count));
392 void BitcoinGUI::error(const QString &title, const QString &message)
394 // Report errors from network/worker thread
395 if(trayIcon->supportsMessages())
397 // Show as "balloon" message if possible
398 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
402 // Fall back to old fashioned popup dialog if not
403 QMessageBox::critical(this, title,
405 QMessageBox::Ok, QMessageBox::Ok);
409 void BitcoinGUI::changeEvent(QEvent *e)
411 if (e->type() == QEvent::WindowStateChange)
413 if(model->getOptionsModel()->getMinimizeToTray())
426 QMainWindow::changeEvent(e);
429 void BitcoinGUI::closeEvent(QCloseEvent *event)
431 if(!model->getOptionsModel()->getMinimizeToTray() &&
432 !model->getOptionsModel()->getMinimizeOnClose())
436 QMainWindow::closeEvent(event);
439 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
442 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
443 "which goes to the nodes that process your transaction and helps to support the network. "
444 "Do you want to pay the fee?").arg(QString::fromStdString(FormatMoney(nFeeRequired)));
445 QMessageBox::StandardButton retval = QMessageBox::question(
446 this, tr("Sending..."), strMessage,
447 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
448 *payFee = (retval == QMessageBox::Yes);
451 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
453 // A transaction is doubleclicked
454 TransactionDescDialog dlg(idx);
458 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
460 TransactionTableModel *ttm = model->getTransactionTableModel();
461 qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
462 .data(Qt::EditRole).toULongLong();
463 if(amount>0 && !model->inInitialBlockDownload())
465 // On incoming transaction, make an info balloon
466 // Unless the initial block download is in progress, to prevent balloon-spam
467 QString date = ttm->index(start, TransactionTableModel::Date, parent)
469 QString type = ttm->index(start, TransactionTableModel::Type, parent)
471 QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
474 trayIcon->showMessage(tr("Incoming transaction"),
475 tr("Date: ") + date + "\n" +
476 tr("Amount: ") + QString::fromStdString(FormatMoney(amount, true)) + "\n" +
477 tr("Type: ") + type + "\n" +
478 tr("Address: ") + address + "\n",
479 QSystemTrayIcon::Information);