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