tab reorg phase 1: split main gui into "overview" and "history"
[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 "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
39 #include <QDebug>
40
41 #include <iostream>
42
43 BitcoinGUI::BitcoinGUI(QWidget *parent):
44     QMainWindow(parent),
45     clientModel(0),
46     walletModel(0),
47     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->addAction(receiveCoins);
59     file->addSeparator();
60     file->addAction(quit);
61     
62     QMenu *settings = menuBar()->addMenu("&Settings");
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(overviewAction);
72     toolbar->addAction(historyAction);
73     toolbar->addSeparator();
74     toolbar->addAction(sendCoins);
75     toolbar->addAction(receiveCoins);
76     toolbar->addAction(addressbook);
77
78     overviewPage = new OverviewPage();
79
80     QVBoxLayout *vbox = new QVBoxLayout();
81
82     transactionView = new TransactionView(this);
83     connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
84     vbox->addWidget(transactionView);
85
86     transactionsPage = new QWidget(this);
87     transactionsPage->setLayout(vbox);
88
89     centralWidget = new QStackedWidget(this);
90     centralWidget->addWidget(overviewPage);
91     centralWidget->addWidget(transactionsPage);
92     setCentralWidget(centralWidget);
93     
94     // Create status bar
95     statusBar();
96
97     labelConnections = new QLabel();
98     labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
99     labelConnections->setMinimumWidth(150);
100     labelConnections->setToolTip(tr("Number of connections to other clients"));
101
102     labelBlocks = new QLabel();
103     labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
104     labelBlocks->setMinimumWidth(130);
105     labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
106
107     labelTransactions = new QLabel();
108     labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
109     labelTransactions->setMinimumWidth(130);
110     labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
111
112     // Progress bar for blocks download
113     progressBarLabel = new QLabel(tr("Synchronizing with network..."));
114     progressBarLabel->setVisible(false);
115     progressBar = new QProgressBar();
116     progressBar->setToolTip(tr("Block chain synchronization in progress"));
117     progressBar->setVisible(false);
118
119     statusBar()->addWidget(progressBarLabel);
120     statusBar()->addWidget(progressBar);
121     statusBar()->addPermanentWidget(labelConnections);
122     statusBar()->addPermanentWidget(labelBlocks);
123     statusBar()->addPermanentWidget(labelTransactions);
124
125     createTrayIcon();
126
127     gotoOverviewTab();
128 }
129
130 void BitcoinGUI::createActions()
131 {
132     QActionGroup *tabGroup = new QActionGroup(this);
133     overviewAction = new QAction(QIcon(":/icons/overview"), tr("&Overview"), this);
134     overviewAction->setCheckable(true);
135     tabGroup->addAction(overviewAction);
136     historyAction = new QAction(QIcon(":/icons/history"), tr("&History"), this);
137     historyAction->setCheckable(true);
138     tabGroup->addAction(historyAction);
139
140     connect(overviewAction, SIGNAL(triggered()), this, SLOT(gotoOverviewTab()));
141     connect(historyAction, SIGNAL(triggered()), this, SLOT(gotoHistoryTab()));
142
143     quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
144     quit->setToolTip(tr("Quit application"));
145     sendCoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
146     sendCoins->setToolTip(tr("Send coins to a bitcoin address"));
147     addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
148     addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
149     about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
150     about->setToolTip(tr("Show information about Bitcoin"));
151     receiveCoins = new QAction(QIcon(":/icons/receiving_addresses"), tr("&Receive coins"), this);
152     receiveCoins->setToolTip(tr("Show the list of addresses for receiving payments"));
153     options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
154     options->setToolTip(tr("Modify configuration options for bitcoin"));
155     openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
156     openBitcoin->setToolTip(tr("Show the Bitcoin window"));
157
158     connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
159     connect(sendCoins, SIGNAL(triggered()), this, SLOT(sendCoinsClicked()));
160     connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
161     connect(receiveCoins, SIGNAL(triggered()), this, SLOT(receiveCoinsClicked()));
162     connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
163     connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
164     connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
165 }
166
167 void BitcoinGUI::setClientModel(ClientModel *clientModel)
168 {
169     this->clientModel = clientModel;
170
171     if(clientModel->isTestNet())
172     {
173         setWindowTitle(tr("Bitcoin [testnet]"));
174         setWindowIcon(QIcon(":icons/bitcoin_testnet"));
175         if(trayIcon)
176         {
177             trayIcon->setToolTip(tr("Bitcoin [testnet]"));
178             trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
179         }
180     }
181
182     // Keep up to date with client
183     setNumConnections(clientModel->getNumConnections());
184     connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
185
186     setNumBlocks(clientModel->getNumBlocks());
187     connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
188
189     // Report errors from network/worker thread
190     connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
191 }
192
193 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
194 {
195     this->walletModel = walletModel;
196
197     // Keep up to date with wallet
198     setBalance(walletModel->getBalance());
199     connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
200
201     setNumTransactions(walletModel->getNumTransactions());
202     connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
203
204     // Report errors from wallet thread
205     connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
206
207     // Put transaction list in tabs
208     transactionView->setModel(walletModel->getTransactionTableModel());
209
210     // Balloon popup for new transaction
211     connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
212             this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
213 }
214
215 void BitcoinGUI::createTrayIcon()
216 {
217     QMenu *trayIconMenu = new QMenu(this);
218     trayIconMenu->addAction(openBitcoin);
219     trayIconMenu->addAction(sendCoins);
220     trayIconMenu->addAction(options);
221     trayIconMenu->addSeparator();
222     trayIconMenu->addAction(quit);
223
224     trayIcon = new QSystemTrayIcon(this);
225     trayIcon->setContextMenu(trayIconMenu);
226     trayIcon->setToolTip("Bitcoin client");
227     trayIcon->setIcon(QIcon(":/icons/toolbar"));
228     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
229             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
230     trayIcon->show();
231 }
232
233 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
234 {
235     if(reason == QSystemTrayIcon::DoubleClick)
236     {
237         // Doubleclick on system tray icon triggers "open bitcoin"
238         openBitcoin->trigger();
239     }
240 }
241
242 void BitcoinGUI::sendCoinsClicked()
243 {
244     SendCoinsDialog dlg;
245     dlg.setModel(walletModel);
246     dlg.exec();
247 }
248
249 void BitcoinGUI::addressbookClicked()
250 {
251     AddressBookDialog dlg(AddressBookDialog::ForEditing);
252     dlg.setModel(walletModel->getAddressTableModel());
253     dlg.setTab(AddressBookDialog::SendingTab);
254     dlg.exec();
255 }
256
257 void BitcoinGUI::receiveCoinsClicked()
258 {
259     AddressBookDialog dlg(AddressBookDialog::ForEditing);
260     dlg.setModel(walletModel->getAddressTableModel());
261     dlg.setTab(AddressBookDialog::ReceivingTab);
262     dlg.exec();
263 }
264
265 void BitcoinGUI::optionsClicked()
266 {
267     OptionsDialog dlg;
268     dlg.setModel(clientModel->getOptionsModel());
269     dlg.exec();
270 }
271
272 void BitcoinGUI::aboutClicked()
273 {
274     AboutDialog dlg;
275     dlg.setModel(clientModel);
276     dlg.exec();
277 }
278
279 void BitcoinGUI::setBalance(qint64 balance)
280 {
281     overviewPage->setBalance(balance);
282 }
283
284 void BitcoinGUI::setNumConnections(int count)
285 {
286     QString icon;
287     switch(count)
288     {
289     case 0: icon = ":/icons/connect_0"; break;
290     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
291     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
292     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
293     default: icon = ":/icons/connect_4"; break;
294     }
295     labelConnections->setTextFormat(Qt::RichText);
296     labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
297 }
298
299 void BitcoinGUI::setNumBlocks(int count)
300 {
301     int total = clientModel->getTotalBlocksEstimate();
302     if(count < total)
303     {
304         progressBarLabel->setVisible(true);
305         progressBar->setVisible(true);
306         progressBar->setMaximum(total);
307         progressBar->setValue(count);
308     }
309     else
310     {
311         progressBarLabel->setVisible(false);
312         progressBar->setVisible(false);
313     }
314
315     labelBlocks->setText(tr("%n block(s)", "", count));
316 }
317
318 void BitcoinGUI::setNumTransactions(int count)
319 {
320     labelTransactions->setText(tr("%n transaction(s)", "", count));
321 }
322
323 void BitcoinGUI::error(const QString &title, const QString &message)
324 {
325     // Report errors from network/worker thread
326     if(trayIcon->supportsMessages())
327     {
328         // Show as "balloon" message if possible
329         trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
330     }
331     else
332     {
333         // Fall back to old fashioned popup dialog if not
334         QMessageBox::critical(this, title,
335             message,
336             QMessageBox::Ok, QMessageBox::Ok);
337     }
338 }
339
340 void BitcoinGUI::changeEvent(QEvent *e)
341 {
342     if (e->type() == QEvent::WindowStateChange)
343     {
344         if(clientModel->getOptionsModel()->getMinimizeToTray())
345         {
346             if (isMinimized())
347             {
348                 hide();
349                 e->ignore();
350             }
351             else
352             {
353                 e->accept();
354             }
355         }
356     }
357     QMainWindow::changeEvent(e);
358 }
359
360 void BitcoinGUI::closeEvent(QCloseEvent *event)
361 {
362     if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
363        !clientModel->getOptionsModel()->getMinimizeOnClose())
364     {
365         qApp->quit();
366     }
367     QMainWindow::closeEvent(event);
368 }
369
370 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
371 {
372     QString strMessage =
373         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
374           "which goes to the nodes that process your transaction and helps to support the network.  "
375           "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
376     QMessageBox::StandardButton retval = QMessageBox::question(
377           this, tr("Sending..."), strMessage,
378           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
379     *payFee = (retval == QMessageBox::Yes);
380 }
381
382 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
383 {
384     // A transaction is doubleclicked
385     TransactionDescDialog dlg(idx);
386     dlg.exec();
387 }
388
389 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
390 {
391     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
392     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
393                     .data(Qt::EditRole).toULongLong();
394     if(amount>0 && !clientModel->inInitialBlockDownload())
395     {
396         // On incoming transaction, make an info balloon
397         // Unless the initial block download is in progress, to prevent balloon-spam
398         QString date = ttm->index(start, TransactionTableModel::Date, parent)
399                         .data().toString();
400         QString type = ttm->index(start, TransactionTableModel::Type, parent)
401                         .data().toString();
402         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
403                         .data().toString();
404
405         trayIcon->showMessage(tr("Incoming transaction"),
406                               tr("Date: ") + date + "\n" +
407                               tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
408                               tr("Type: ") + type + "\n" +
409                               tr("Address: ") + address + "\n",
410                               QSystemTrayIcon::Information);
411     }
412 }
413
414 void BitcoinGUI::gotoOverviewTab()
415 {
416     overviewAction->setChecked(true);
417     centralWidget->setCurrentWidget(overviewPage);
418 }
419
420 void BitcoinGUI::gotoHistoryTab()
421 {
422     historyAction->setChecked(true);
423     centralWidget->setCurrentWidget(transactionsPage);
424 }