preparations for multiple unit (uBTC, mBTC, BTC) support, fix amount entry issue
[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 "addressbookpage.h"
9 #include "sendcoinsdialog.h"
10 #include "optionsdialog.h"
11 #include "aboutdialog.h"
12 #include "clientmodel.h"
13 #include "walletmodel.h"
14 #include "editaddressdialog.h"
15 #include "optionsmodel.h"
16 #include "transactiondescdialog.h"
17 #include "addresstablemodel.h"
18 #include "transactionview.h"
19 #include "overviewpage.h"
20 #include "bitcoinunits.h"
21
22 #include <QApplication>
23 #include <QMainWindow>
24 #include <QMenuBar>
25 #include <QMenu>
26 #include <QIcon>
27 #include <QTabWidget>
28 #include <QVBoxLayout>
29 #include <QToolBar>
30 #include <QStatusBar>
31 #include <QLabel>
32 #include <QLineEdit>
33 #include <QPushButton>
34 #include <QLocale>
35 #include <QMessageBox>
36 #include <QProgressBar>
37 #include <QStackedWidget>
38 #include <QDateTime>
39 #include <QMovie>
40
41 #include <QDebug>
42
43 #include <iostream>
44
45 BitcoinGUI::BitcoinGUI(QWidget *parent):
46     QMainWindow(parent),
47     clientModel(0),
48     walletModel(0),
49     trayIcon(0)
50 {
51     resize(850, 550);
52     setWindowTitle(tr("Bitcoin Wallet"));
53     setWindowIcon(QIcon(":icons/bitcoin"));
54
55     createActions();
56
57     // Menus
58     QMenu *file = menuBar()->addMenu("&File");
59     file->addAction(sendCoinsAction);
60     file->addAction(receiveCoinsAction);
61     file->addSeparator();
62     file->addAction(quitAction);
63     
64     QMenu *settings = menuBar()->addMenu("&Settings");
65     settings->addAction(optionsAction);
66
67     QMenu *help = menuBar()->addMenu("&Help");
68     help->addAction(aboutAction);
69     
70     // Toolbar
71     QToolBar *toolbar = addToolBar("Main toolbar");
72     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
73     toolbar->addAction(overviewAction);
74     toolbar->addAction(sendCoinsAction);
75     toolbar->addAction(receiveCoinsAction);
76     toolbar->addAction(historyAction);
77     toolbar->addAction(addressBookAction);
78
79     QToolBar *toolbar2 = addToolBar("Transactions toolbar");
80     toolbar2->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
81     toolbar2->addAction(exportAction);
82
83     // Overview page
84     overviewPage = new OverviewPage();
85     QVBoxLayout *vbox = new QVBoxLayout();
86
87     transactionView = new TransactionView(this);
88     connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), transactionView, SLOT(showDetails()));
89     vbox->addWidget(transactionView);
90
91     transactionsPage = new QWidget(this);
92     transactionsPage->setLayout(vbox);
93
94     addressBookPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::SendingTab);
95
96     receiveCoinsPage = new AddressBookPage(AddressBookPage::ForEditing, AddressBookPage::ReceivingTab);
97
98     sendCoinsPage = new SendCoinsDialog(this);
99
100     centralWidget = new QStackedWidget(this);
101     centralWidget->addWidget(overviewPage);
102     centralWidget->addWidget(transactionsPage);
103     centralWidget->addWidget(addressBookPage);
104     centralWidget->addWidget(receiveCoinsPage);
105     centralWidget->addWidget(sendCoinsPage);
106     setCentralWidget(centralWidget);
107     
108     // Create status bar
109     statusBar();
110
111     // Status bar "Connections" notification
112     QFrame *frameConnections = new QFrame();
113     frameConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
114     frameConnections->setMinimumWidth(150);
115     frameConnections->setMaximumWidth(150);
116     QHBoxLayout *frameConnectionsLayout = new QHBoxLayout(frameConnections);
117     frameConnectionsLayout->setContentsMargins(3,0,3,0);
118     frameConnectionsLayout->setSpacing(3);
119     labelConnectionsIcon = new QLabel();
120     labelConnectionsIcon->setToolTip(tr("Number of connections to other clients"));
121     frameConnectionsLayout->addWidget(labelConnectionsIcon);
122     labelConnections = new QLabel();
123     labelConnections->setToolTip(tr("Number of connections to other clients"));
124     frameConnectionsLayout->addWidget(labelConnections);
125     frameConnectionsLayout->addStretch();
126
127     // Status bar "Blocks" notification
128     QFrame *frameBlocks = new QFrame();
129     frameBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
130     frameBlocks->setMinimumWidth(150);
131     frameBlocks->setMaximumWidth(150);
132     QHBoxLayout *frameBlocksLayout = new QHBoxLayout(frameBlocks);
133     frameBlocksLayout->setContentsMargins(3,0,3,0);
134     frameBlocksLayout->setSpacing(3);
135     labelBlocksIcon = new QLabel();
136     frameBlocksLayout->addWidget(labelBlocksIcon);
137     labelBlocks = new QLabel();
138     frameBlocksLayout->addWidget(labelBlocks);
139     frameBlocksLayout->addStretch();
140
141     // Progress bar for blocks download
142     progressBarLabel = new QLabel(tr("Synchronizing with network..."));
143     progressBarLabel->setVisible(false);
144     progressBar = new QProgressBar();
145     progressBar->setToolTip(tr("Block chain synchronization in progress"));
146     progressBar->setVisible(false);
147
148     statusBar()->addWidget(progressBarLabel);
149     statusBar()->addWidget(progressBar);
150     statusBar()->addPermanentWidget(frameConnections);
151     statusBar()->addPermanentWidget(frameBlocks);
152
153     createTrayIcon();
154
155     syncIconMovie = new QMovie(":/movies/update_spinner", "mng", this);
156
157     gotoOverviewPage();
158 }
159
160 void BitcoinGUI::createActions()
161 {
162     QActionGroup *tabGroup = new QActionGroup(this);
163
164     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
165     overviewAction->setToolTip(tr("Show general overview of wallet"));
166     overviewAction->setCheckable(true);
167     tabGroup->addAction(overviewAction);
168
169     historyAction = new QAction(QIcon(":/icons/history"), tr("&Transactions"), this);
170     historyAction->setToolTip(tr("Browse transaction history"));
171     historyAction->setCheckable(true);
172     tabGroup->addAction(historyAction);
173
174     addressBookAction = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
175     addressBookAction->setToolTip(tr("Edit the list of stored addresses and labels"));
176     addressBookAction->setCheckable(true);
177     tabGroup->addAction(addressBookAction);
178
179     receiveCoinsAction = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
180     receiveCoinsAction->setToolTip(tr("Show the list of addresses for receiving payments"));
181     receiveCoinsAction->setCheckable(true);
182     tabGroup->addAction(receiveCoinsAction);
183
184     sendCoinsAction = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
185     sendCoinsAction->setToolTip(tr("Send coins to a bitcoin address"));
186     sendCoinsAction->setCheckable(true);
187     tabGroup->addAction(sendCoinsAction);
188
189     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewPage()));
190     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryPage()));
191     connect(addressBookAction, SIGNAL(triggered()), this, SLOT(gotoAddressBookPage()));
192     connect(receiveCoinsAction, SIGNAL(triggered()), this, SLOT(gotoReceiveCoinsPage()));
193     connect(sendCoinsAction, SIGNAL(triggered()), this, SLOT(gotoSendCoinsPage()));
194
195     quitAction = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
196     quitAction->setToolTip(tr("Quit application"));
197     aboutAction = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
198     aboutAction->setToolTip(tr("Show information about Bitcoin"));
199     optionsAction = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
200     optionsAction->setToolTip(tr("Modify configuration options for bitcoin"));
201     openBitcoinAction = new QAction(QIcon(":/icons/bitcoin"), tr("Open &Bitcoin"), this);
202     openBitcoinAction->setToolTip(tr("Show the Bitcoin window"));
203     exportAction = new QAction(QIcon(":/icons/export"), tr("&Export..."), this);
204     exportAction->setToolTip(tr("Export the current view to a file"));
205
206     connect(quitAction, SIGNAL(triggered()), qApp, SLOT(quit()));
207     connect(optionsAction, SIGNAL(triggered()), this, SLOT(optionsClicked()));
208     connect(aboutAction, SIGNAL(triggered()), this, SLOT(aboutClicked()));
209     connect(openBitcoinAction, SIGNAL(triggered()), this, SLOT(show()));
210 }
211
212 void BitcoinGUI::setClientModel(ClientModel *clientModel)
213 {
214     this->clientModel = clientModel;
215
216     if(clientModel->isTestNet())
217     {
218         QString title_testnet = windowTitle() + QString(" ") + tr("[testnet]");
219         setWindowTitle(title_testnet);
220         setWindowIcon(QIcon(":icons/bitcoin_testnet"));
221         if(trayIcon)
222         {
223             trayIcon->setToolTip(title_testnet);
224             trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
225         }
226     }
227
228     // Keep up to date with client
229     setNumConnections(clientModel->getNumConnections());
230     connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
231
232     setNumBlocks(clientModel->getNumBlocks());
233     connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
234
235     // Report errors from network/worker thread
236     connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
237 }
238
239 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
240 {
241     this->walletModel = walletModel;
242
243     // Report errors from wallet thread
244     connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
245
246     // Put transaction list in tabs
247     transactionView->setModel(walletModel);
248
249     overviewPage->setModel(walletModel);
250     addressBookPage->setModel(walletModel->getAddressTableModel());
251     receiveCoinsPage->setModel(walletModel->getAddressTableModel());
252     sendCoinsPage->setModel(walletModel);
253
254     // Balloon popup for new transaction
255     connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
256             this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
257 }
258
259 void BitcoinGUI::createTrayIcon()
260 {
261     QMenu *trayIconMenu = new QMenu(this);
262     trayIconMenu->addAction(openBitcoinAction);
263     trayIconMenu->addAction(sendCoinsAction);
264     trayIconMenu->addAction(optionsAction);
265     trayIconMenu->addSeparator();
266     trayIconMenu->addAction(quitAction);
267
268     trayIcon = new QSystemTrayIcon(this);
269     trayIcon->setContextMenu(trayIconMenu);
270     trayIcon->setToolTip("Bitcoin client");
271     trayIcon->setIcon(QIcon(":/icons/toolbar"));
272     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
273             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
274     trayIcon->show();
275 }
276
277 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
278 {
279     if(reason == QSystemTrayIcon::DoubleClick)
280     {
281         // Doubleclick on system tray icon triggers "open bitcoin"
282         openBitcoinAction->trigger();
283     }
284 }
285
286 void BitcoinGUI::optionsClicked()
287 {
288     OptionsDialog dlg;
289     dlg.setModel(clientModel->getOptionsModel());
290     dlg.exec();
291 }
292
293 void BitcoinGUI::aboutClicked()
294 {
295     AboutDialog dlg;
296     dlg.setModel(clientModel);
297     dlg.exec();
298 }
299
300 void BitcoinGUI::setNumConnections(int count)
301 {
302     QString icon;
303     switch(count)
304     {
305     case 0: icon = ":/icons/connect_0"; break;
306     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
307     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
308     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
309     default: icon = ":/icons/connect_4"; break;
310     }
311     labelConnectionsIcon->setPixmap(QIcon(icon).pixmap(16,16));
312     labelConnections->setText(tr("%n connection(s)", "", count));
313 }
314
315 void BitcoinGUI::setNumBlocks(int count)
316 {
317     int total = clientModel->getTotalBlocksEstimate();
318     QString tooltip;
319
320     if(count < total)
321     {
322         progressBarLabel->setVisible(true);
323         progressBar->setVisible(true);
324         progressBar->setMaximum(total);
325         progressBar->setValue(count);
326         tooltip = tr("Downloaded %1 of %2 blocks of transaction history.").arg(count).arg(total);
327     }
328     else
329     {
330         progressBarLabel->setVisible(false);
331         progressBar->setVisible(false);
332         tooltip = tr("Downloaded %1 blocks of transaction history.").arg(count);
333     }
334
335     QDateTime now = QDateTime::currentDateTime();
336     QDateTime lastBlockDate = clientModel->getLastBlockDate();
337     int secs = lastBlockDate.secsTo(now);
338     QString text;
339
340     // Represent time from last generated block in human readable text
341     if(secs < 60)
342     {
343         text = tr("%n second(s) ago","",secs);
344     }
345     else if(secs < 60*60)
346     {
347         text = tr("%n minute(s) ago","",secs/60);
348     }
349     else if(secs < 24*60*60)
350     {
351         text = tr("%n hour(s) ago","",secs/(60*60));
352     }
353     else
354     {
355         text = tr("%n day(s) ago","",secs/(60*60*24));
356     }
357
358     // In the label we want to be less specific
359     QString labelText = text;
360     bool spinning = true;
361     if(secs < 30*60)
362     {
363         labelText = "Up to date";
364         spinning = false;
365     }
366
367     tooltip += QString("\n");
368     tooltip += tr("Last received block was generated %1.").arg(text);
369
370     if(spinning)
371     {
372         labelBlocksIcon->setMovie(syncIconMovie);
373         syncIconMovie->start();
374     }
375     else
376     {
377         labelBlocksIcon->setPixmap(QIcon(":/icons/synced").pixmap(16,16));
378     }
379     labelBlocks->setText(labelText);
380
381     labelBlocksIcon->setToolTip(tooltip);
382     labelBlocks->setToolTip(tooltip);
383     progressBarLabel->setToolTip(tooltip);
384     progressBar->setToolTip(tooltip);
385 }
386
387 void BitcoinGUI::error(const QString &title, const QString &message)
388 {
389     // Report errors from network/worker thread
390     if(trayIcon->supportsMessages())
391     {
392         // Show as "balloon" message if possible
393         trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
394     }
395     else
396     {
397         // Fall back to old fashioned popup dialog if not
398         QMessageBox::critical(this, title,
399             message,
400             QMessageBox::Ok, QMessageBox::Ok);
401     }
402 }
403
404 void BitcoinGUI::changeEvent(QEvent *e)
405 {
406     if (e->type() == QEvent::WindowStateChange)
407     {
408         if(clientModel->getOptionsModel()->getMinimizeToTray())
409         {
410             if (isMinimized())
411             {
412                 hide();
413                 e->ignore();
414             }
415             else
416             {
417                 e->accept();
418             }
419         }
420     }
421     QMainWindow::changeEvent(e);
422 }
423
424 void BitcoinGUI::closeEvent(QCloseEvent *event)
425 {
426     if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
427        !clientModel->getOptionsModel()->getMinimizeOnClose())
428     {
429         qApp->quit();
430     }
431     QMainWindow::closeEvent(event);
432 }
433
434 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
435 {
436     QString strMessage =
437         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
438           "which goes to the nodes that process your transaction and helps to support the network.  "
439           "Do you want to pay the fee?").arg(
440                 BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, nFeeRequired));
441     QMessageBox::StandardButton retval = QMessageBox::question(
442           this, tr("Sending..."), strMessage,
443           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
444     *payFee = (retval == QMessageBox::Yes);
445 }
446
447 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
448 {
449     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
450     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
451                     .data(Qt::EditRole).toULongLong();
452     if(!clientModel->inInitialBlockDownload())
453     {
454         // On incoming transaction, make an info balloon
455         // Unless the initial block download is in progress, to prevent balloon-spam
456         QString date = ttm->index(start, TransactionTableModel::Date, parent)
457                         .data().toString();
458         QString type = ttm->index(start, TransactionTableModel::Type, parent)
459                         .data().toString();
460         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
461                         .data().toString();
462
463         trayIcon->showMessage((amount)<0 ? tr("Sent transaction") :
464                                            tr("Incoming transaction"),
465                               tr("Date: ") + date + "\n" +
466                               tr("Amount: ") + BitcoinUnits::formatWithUnit(BitcoinUnits::BTC, amount, true) + "\n" +
467                               tr("Type: ") + type + "\n" +
468                               tr("Address: ") + address + "\n",
469                               QSystemTrayIcon::Information);
470     }
471 }
472
473 void BitcoinGUI::gotoOverviewPage()
474 {
475     overviewAction->setChecked(true);
476     centralWidget->setCurrentWidget(overviewPage);
477
478     exportAction->setEnabled(false);
479     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
480 }
481
482 void BitcoinGUI::gotoHistoryPage()
483 {
484     historyAction->setChecked(true);
485     centralWidget->setCurrentWidget(transactionsPage);
486
487     exportAction->setEnabled(true);
488     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
489     connect(exportAction, SIGNAL(triggered()), transactionView, SLOT(exportClicked()));
490 }
491
492 void BitcoinGUI::gotoAddressBookPage()
493 {
494     addressBookAction->setChecked(true);
495     centralWidget->setCurrentWidget(addressBookPage);
496
497     exportAction->setEnabled(true);
498     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
499     connect(exportAction, SIGNAL(triggered()), addressBookPage, SLOT(exportClicked()));
500 }
501
502 void BitcoinGUI::gotoReceiveCoinsPage()
503 {
504     receiveCoinsAction->setChecked(true);
505     centralWidget->setCurrentWidget(receiveCoinsPage);
506
507     exportAction->setEnabled(true);
508     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
509     connect(exportAction, SIGNAL(triggered()), receiveCoinsPage, SLOT(exportClicked()));
510 }
511
512 void BitcoinGUI::gotoSendCoinsPage()
513 {
514     sendCoinsAction->setChecked(true);
515     sendCoinsPage->clear();
516     centralWidget->setCurrentWidget(sendCoinsPage);
517
518     exportAction->setEnabled(false);
519     disconnect(exportAction, SIGNAL(triggered()), 0, 0);
520 }
521