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