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