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