On initial block chain download, show a progress bar
[novacoin.git] / src / qt / bitcoingui.cpp
1 /*
2  * Qt4 bitcoin GUI.
3  *
4  * W.J. van der Laan 2011
5  */
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 "guiutil.h"
14 #include "editaddressdialog.h"
15 #include "optionsmodel.h"
16 #include "transactiondescdialog.h"
17
18 #include "main.h"
19
20 #include <QApplication>
21 #include <QMainWindow>
22 #include <QMenuBar>
23 #include <QMenu>
24 #include <QIcon>
25 #include <QTabWidget>
26 #include <QVBoxLayout>
27 #include <QWidget>
28 #include <QToolBar>
29 #include <QStatusBar>
30 #include <QLabel>
31 #include <QTableView>
32 #include <QLineEdit>
33 #include <QPushButton>
34 #include <QHeaderView>
35 #include <QLocale>
36 #include <QSortFilterProxyModel>
37 #include <QClipboard>
38 #include <QMessageBox>
39 #include <QProgressBar>
40
41 #include <QDebug>
42
43 #include <iostream>
44
45 BitcoinGUI::BitcoinGUI(QWidget *parent):
46     QMainWindow(parent), trayIcon(0)
47 {
48     resize(850, 550);
49     setWindowTitle(tr("Bitcoin"));
50     setWindowIcon(QIcon(":icons/bitcoin"));
51
52     createActions();
53
54     // Menus
55     QMenu *file = menuBar()->addMenu("&File");
56     file->addAction(sendcoins);
57     file->addSeparator();
58     file->addAction(quit);
59     
60     QMenu *settings = menuBar()->addMenu("&Settings");
61     settings->addAction(receivingAddresses);
62     settings->addAction(options);
63
64     QMenu *help = menuBar()->addMenu("&Help");
65     help->addAction(about);
66     
67     // Toolbar
68     QToolBar *toolbar = addToolBar("Main toolbar");
69     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
70     toolbar->addAction(sendcoins);
71     toolbar->addAction(addressbook);
72
73     // Address: <address>: New... : Paste to clipboard
74     QHBoxLayout *hbox_address = new QHBoxLayout();
75     hbox_address->addWidget(new QLabel(tr("Your Bitcoin address:")));
76     address = new QLineEdit();
77     address->setReadOnly(true);
78     address->setFont(GUIUtil::bitcoinAddressFont());
79     address->setToolTip(tr("Your current default receiving address"));
80     hbox_address->addWidget(address);
81     
82     QPushButton *button_new = new QPushButton(tr("&New..."));
83     button_new->setToolTip(tr("Create new receiving address"));
84     QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
85     button_clipboard->setToolTip(tr("Copy current receiving address to the system clipboard"));
86     hbox_address->addWidget(button_new);
87     hbox_address->addWidget(button_clipboard);
88     
89     // Balance: <balance>
90     QHBoxLayout *hbox_balance = new QHBoxLayout();
91     hbox_balance->addWidget(new QLabel(tr("Balance:")));
92     hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
93
94     labelBalance = new QLabel();
95     labelBalance->setFont(QFont("Monospace"));
96     labelBalance->setToolTip(tr("Your current balance"));
97     hbox_balance->addWidget(labelBalance);
98     hbox_balance->addStretch(1);
99     
100     QVBoxLayout *vbox = new QVBoxLayout();
101     vbox->addLayout(hbox_address);
102     vbox->addLayout(hbox_balance);
103
104     vbox->addWidget(createTabs());
105
106     QWidget *centralwidget = new QWidget(this);
107     centralwidget->setLayout(vbox);
108     setCentralWidget(centralwidget);
109     
110     // Create status bar
111     statusBar();
112
113     labelConnections = new QLabel();
114     labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
115     labelConnections->setMinimumWidth(150);
116     labelConnections->setToolTip(tr("Number of connections to other clients"));
117
118     labelBlocks = new QLabel();
119     labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
120     labelBlocks->setMinimumWidth(130);
121     labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
122
123     labelTransactions = new QLabel();
124     labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
125     labelTransactions->setMinimumWidth(130);
126     labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
127
128     // Progress bar for blocks download
129     progressBarLabel = new QLabel(tr("Downloading initial data..."));
130     progressBarLabel->setVisible(false);
131     progressBar = new QProgressBar();
132     progressBar->setToolTip(tr("Initial block chain download in progress"));
133     progressBar->setVisible(false);
134
135     statusBar()->addWidget(progressBarLabel);
136     statusBar()->addWidget(progressBar);
137     statusBar()->addPermanentWidget(labelConnections);
138     statusBar()->addPermanentWidget(labelBlocks);
139     statusBar()->addPermanentWidget(labelTransactions);
140
141     // Action bindings
142     connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
143     connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
144
145     createTrayIcon();
146 }
147
148 void BitcoinGUI::createActions()
149 {
150     quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
151     quit->setToolTip(tr("Quit application"));
152     sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
153     sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
154     addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
155     addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
156     about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
157     about->setToolTip(tr("Show information about Bitcoin"));
158     receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("Your &Receiving Addresses..."), this);
159     receivingAddresses->setToolTip(tr("Show the list of receiving addresses and edit their labels"));
160     options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
161     options->setToolTip(tr("Modify configuration options for bitcoin"));
162     openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
163     openBitcoin->setToolTip(tr("Show the Bitcoin window"));
164
165     connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
166     connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
167     connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
168     connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
169     connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
170     connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
171     connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
172 }
173
174 void BitcoinGUI::setModel(ClientModel *model)
175 {
176     this->model = model;
177
178     // Keep up to date with client
179     setBalance(model->getBalance());
180     connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
181
182     setNumConnections(model->getNumConnections());
183     connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
184
185     setNumTransactions(model->getNumTransactions());
186     connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
187
188     setNumBlocks(model->getNumBlocks());
189     connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
190
191     setAddress(model->getAddress());
192     connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
193
194     // Report errors from network/worker thread
195     connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));    
196
197     // Put transaction list in tabs
198     setTabsModel(model->getTransactionTableModel());
199 }
200
201 void BitcoinGUI::createTrayIcon()
202 {
203     QMenu *trayIconMenu = new QMenu(this);
204     trayIconMenu->addAction(openBitcoin);
205     trayIconMenu->addAction(sendcoins);
206     trayIconMenu->addAction(options);
207     trayIconMenu->addSeparator();
208     trayIconMenu->addAction(quit);
209
210     trayIcon = new QSystemTrayIcon(this);
211     trayIcon->setContextMenu(trayIconMenu);
212     trayIcon->setIcon(QIcon(":/icons/toolbar"));
213     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
214             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
215     trayIcon->show();
216 }
217
218 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
219 {
220     if(reason == QSystemTrayIcon::DoubleClick)
221     {
222         // Doubleclick on system tray icon triggers "open bitcoin"
223         openBitcoin->trigger();
224     }
225 }
226
227 QWidget *BitcoinGUI::createTabs()
228 {
229     QStringList tab_labels;
230     tab_labels  << tr("All transactions")
231                 << tr("Sent/Received")
232                 << tr("Sent")
233                 << tr("Received");
234
235     QTabWidget *tabs = new QTabWidget(this);
236     for(int i = 0; i < tab_labels.size(); ++i)
237     {
238         QTableView *view = new QTableView(this);
239         tabs->addTab(view, tab_labels.at(i));
240
241         connect(view, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
242         transactionViews.append(view);
243     }
244
245     return tabs;
246 }
247
248 void BitcoinGUI::setTabsModel(QAbstractItemModel *transaction_model)
249 {
250     QStringList tab_filters;
251     tab_filters << "^."
252             << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
253             << "^["+TransactionTableModel::Sent+"]"
254             << "^["+TransactionTableModel::Received+"]";
255
256     for(int i = 0; i < transactionViews.size(); ++i)
257     {
258         QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
259         proxy_model->setSourceModel(transaction_model);
260         proxy_model->setDynamicSortFilter(true);
261         proxy_model->setFilterRole(TransactionTableModel::TypeRole);
262         proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
263         proxy_model->setSortRole(Qt::EditRole);
264
265         QTableView *transaction_table = transactionViews.at(i);
266         transaction_table->setModel(proxy_model);
267         transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
268         transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
269         transaction_table->setSortingEnabled(true);
270         transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
271         transaction_table->verticalHeader()->hide();
272
273         transaction_table->horizontalHeader()->resizeSection(
274                 TransactionTableModel::Status, 23);
275         transaction_table->horizontalHeader()->resizeSection(
276                 TransactionTableModel::Date, 120);
277         transaction_table->horizontalHeader()->setResizeMode(
278                 TransactionTableModel::Description, QHeaderView::Stretch);
279         transaction_table->horizontalHeader()->resizeSection(
280                 TransactionTableModel::Debit, 79);
281         transaction_table->horizontalHeader()->resizeSection(
282                 TransactionTableModel::Credit, 79);
283     }
284
285     connect(transaction_model, SIGNAL(rowsInserted(const QModelIndex &, int, int)),
286             this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
287 }
288
289 void BitcoinGUI::sendcoinsClicked()
290 {
291     SendCoinsDialog dlg;
292     dlg.setModel(model);
293     dlg.exec();
294 }
295
296 void BitcoinGUI::addressbookClicked()
297 {
298     AddressBookDialog dlg(AddressBookDialog::ForEditing);
299     dlg.setModel(model->getAddressTableModel());
300     dlg.setTab(AddressBookDialog::SendingTab);
301     dlg.exec();
302 }
303
304 void BitcoinGUI::receivingAddressesClicked()
305 {
306     AddressBookDialog dlg(AddressBookDialog::ForEditing);
307     dlg.setModel(model->getAddressTableModel());
308     dlg.setTab(AddressBookDialog::ReceivingTab);
309     dlg.exec();
310 }
311
312 void BitcoinGUI::optionsClicked()
313 {
314     OptionsDialog dlg;
315     dlg.setModel(model->getOptionsModel());
316     dlg.exec();
317 }
318
319 void BitcoinGUI::aboutClicked()
320 {
321     AboutDialog dlg;
322     dlg.exec();
323 }
324
325 void BitcoinGUI::newAddressClicked()
326 {
327     EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
328     dlg.setModel(model->getAddressTableModel());
329     if(dlg.exec())
330     {
331         QString newAddress = dlg.saveCurrentRow();
332         // Set returned address as new default addres
333         if(!newAddress.isEmpty())
334         {
335             model->setAddress(newAddress);
336         }
337     }
338 }
339
340 void BitcoinGUI::copyClipboardClicked()
341 {
342     // Copy text in address to clipboard
343     QApplication::clipboard()->setText(address->text());
344 }
345
346 void BitcoinGUI::setBalance(qint64 balance)
347 {
348     labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
349 }
350
351 void BitcoinGUI::setAddress(const QString &addr)
352 {
353     address->setText(addr);
354 }
355
356 void BitcoinGUI::setNumConnections(int count)
357 {
358     QString icon;
359     switch(count)
360     {
361     case 0: icon = ":/icons/connect_0"; break;
362     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
363     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
364     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
365     default: icon = ":/icons/connect_4"; break;
366     }
367     labelConnections->setTextFormat(Qt::RichText);
368     labelConnections->setText("<img src=\""+icon+"\"> " + QLocale::system().toString(count)+" "+tr("connection(s)", "", count));
369 }
370
371 void BitcoinGUI::setNumBlocks(int count)
372 {
373     int total = model->getTotalBlocksEstimate();
374     if(count < total)
375     {
376         progressBarLabel->setVisible(true);
377         progressBar->setVisible(true);
378         progressBar->setMaximum(total);
379         progressBar->setValue(count);
380     }
381     else
382     {
383         progressBarLabel->setVisible(false);
384         progressBar->setVisible(false);
385     }
386
387     labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
388 }
389
390 void BitcoinGUI::setNumTransactions(int count)
391 {
392     labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
393 }
394
395 void BitcoinGUI::error(const QString &title, const QString &message)
396 {
397     // Report errors from network/worker thread
398     if(trayIcon->supportsMessages())
399     {
400         // Show as "balloon" message if possible
401         trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
402     }
403     else
404     {
405         // Fall back to old fashioned popup dialog if not
406         QMessageBox::critical(this, title,
407             message,
408             QMessageBox::Ok, QMessageBox::Ok);
409     }
410 }
411
412 void BitcoinGUI::changeEvent(QEvent *e)
413 {
414     if (e->type() == QEvent::WindowStateChange)
415     {
416         if(model->getOptionsModel()->getMinimizeToTray())
417         {
418             if (isMinimized())
419             {
420                 hide();
421                 e->ignore();
422             }
423             else
424             {
425                 e->accept();
426             }
427         }
428     }
429     QMainWindow::changeEvent(e);
430 }
431
432 void BitcoinGUI::closeEvent(QCloseEvent *event)
433 {
434     if(!model->getOptionsModel()->getMinimizeToTray() &&
435        !model->getOptionsModel()->getMinimizeOnClose())
436     {
437         qApp->quit();
438     }
439     QMainWindow::closeEvent(event);
440 }
441
442 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
443 {
444     QString strMessage =
445         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
446           "which goes to the nodes that process your transaction and helps to support the network.  "
447           "Do you want to pay the fee?").arg(QString::fromStdString(FormatMoney(nFeeRequired)));
448     QMessageBox::StandardButton retval = QMessageBox::question(
449           this, tr("Sending..."), strMessage,
450           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
451     *payFee = (retval == QMessageBox::Yes);
452 }
453
454 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
455 {
456     // A transaction is doubleclicked
457     TransactionDescDialog dlg(idx);
458     dlg.exec();
459 }
460
461 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
462 {
463     TransactionTableModel *ttm = model->getTransactionTableModel();
464     qint64 credit = ttm->index(start, TransactionTableModel::Credit, parent)
465                     .data(Qt::EditRole).toULongLong();
466     qint64 debit = ttm->index(start, TransactionTableModel::Debit, parent)
467                     .data(Qt::EditRole).toULongLong();
468     if((credit+debit)>0 && !model->inInitialBlockDownload())
469     {
470         // On incoming transaction, make an info balloon
471         // Unless the initial block download is in progress, to prevent balloon-spam
472         QString date = ttm->index(start, TransactionTableModel::Date, parent)
473                         .data().toString();
474         QString description = ttm->index(start, TransactionTableModel::Description, parent)
475                         .data().toString();
476
477         trayIcon->showMessage(tr("Incoming transaction"),
478                               "Date: " + date + "\n" +
479                               "Amount: " + QString::fromStdString(FormatMoney(credit+debit, true)) + "\n" +
480                               description,
481                               QSystemTrayIcon::Information);
482     }
483 }