3ac87262aa1b4d35477ead9b18c55d7a3607d3b1
[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 Wallet"));
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         QString title_testnet = tr("Bitcoin Wallet [testnet]");
174         setWindowTitle(title_testnet);
175         setWindowIcon(QIcon(":icons/bitcoin_testnet"));
176         if(trayIcon)
177         {
178             trayIcon->setToolTip(title_testnet);
179             trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
180         }
181     }
182
183     // Keep up to date with client
184     setNumConnections(clientModel->getNumConnections());
185     connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
186
187     setNumBlocks(clientModel->getNumBlocks());
188     connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
189
190     // Report errors from network/worker thread
191     connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
192 }
193
194 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
195 {
196     this->walletModel = walletModel;
197
198     // Keep up to date with wallet
199     setBalance(walletModel->getBalance());
200     connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
201
202     setNumTransactions(walletModel->getNumTransactions());
203     connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
204
205     // Report errors from wallet thread
206     connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
207
208     // Put transaction list in tabs
209     transactionView->setModel(walletModel->getTransactionTableModel());
210
211     // Balloon popup for new transaction
212     connect(walletModel->getTransactionTableModel(), SIGNAL(rowsInserted(const QModelIndex &, int, int)),
213             this, SLOT(incomingTransaction(const QModelIndex &, int, int)));
214 }
215
216 void BitcoinGUI::createTrayIcon()
217 {
218     QMenu *trayIconMenu = new QMenu(this);
219     trayIconMenu->addAction(openBitcoin);
220     trayIconMenu->addAction(sendCoins);
221     trayIconMenu->addAction(options);
222     trayIconMenu->addSeparator();
223     trayIconMenu->addAction(quit);
224
225     trayIcon = new QSystemTrayIcon(this);
226     trayIcon->setContextMenu(trayIconMenu);
227     trayIcon->setToolTip("Bitcoin client");
228     trayIcon->setIcon(QIcon(":/icons/toolbar"));
229     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
230             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
231     trayIcon->show();
232 }
233
234 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
235 {
236     if(reason == QSystemTrayIcon::DoubleClick)
237     {
238         // Doubleclick on system tray icon triggers "open bitcoin"
239         openBitcoin->trigger();
240     }
241 }
242
243 void BitcoinGUI::sendCoinsClicked()
244 {
245     SendCoinsDialog dlg;
246     dlg.setModel(walletModel);
247     dlg.exec();
248 }
249
250 void BitcoinGUI::addressbookClicked()
251 {
252     AddressBookDialog dlg(AddressBookDialog::ForEditing);
253     dlg.setModel(walletModel->getAddressTableModel());
254     dlg.setTab(AddressBookDialog::SendingTab);
255     dlg.exec();
256 }
257
258 void BitcoinGUI::receiveCoinsClicked()
259 {
260     AddressBookDialog dlg(AddressBookDialog::ForEditing);
261     dlg.setModel(walletModel->getAddressTableModel());
262     dlg.setTab(AddressBookDialog::ReceivingTab);
263     dlg.exec();
264 }
265
266 void BitcoinGUI::optionsClicked()
267 {
268     OptionsDialog dlg;
269     dlg.setModel(clientModel->getOptionsModel());
270     dlg.exec();
271 }
272
273 void BitcoinGUI::aboutClicked()
274 {
275     AboutDialog dlg;
276     dlg.setModel(clientModel);
277     dlg.exec();
278 }
279
280 void BitcoinGUI::setBalance(qint64 balance)
281 {
282     overviewPage->setBalance(balance);
283 }
284
285 void BitcoinGUI::setNumConnections(int count)
286 {
287     QString icon;
288     switch(count)
289     {
290     case 0: icon = ":/icons/connect_0"; break;
291     case 1: case 2: case 3: icon = ":/icons/connect_1"; break;
292     case 4: case 5: case 6: icon = ":/icons/connect_2"; break;
293     case 7: case 8: case 9: icon = ":/icons/connect_3"; break;
294     default: icon = ":/icons/connect_4"; break;
295     }
296     labelConnections->setTextFormat(Qt::RichText);
297     labelConnections->setText("<img src=\""+icon+"\"> " + tr("%n connection(s)", "", count));
298 }
299
300 void BitcoinGUI::setNumBlocks(int count)
301 {
302     int total = clientModel->getTotalBlocksEstimate();
303     if(count < total)
304     {
305         progressBarLabel->setVisible(true);
306         progressBar->setVisible(true);
307         progressBar->setMaximum(total);
308         progressBar->setValue(count);
309     }
310     else
311     {
312         progressBarLabel->setVisible(false);
313         progressBar->setVisible(false);
314     }
315
316     labelBlocks->setText(tr("%n block(s)", "", count));
317 }
318
319 void BitcoinGUI::setNumTransactions(int count)
320 {
321     labelTransactions->setText(tr("%n transaction(s)", "", count));
322 }
323
324 void BitcoinGUI::error(const QString &title, const QString &message)
325 {
326     // Report errors from network/worker thread
327     if(trayIcon->supportsMessages())
328     {
329         // Show as "balloon" message if possible
330         trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
331     }
332     else
333     {
334         // Fall back to old fashioned popup dialog if not
335         QMessageBox::critical(this, title,
336             message,
337             QMessageBox::Ok, QMessageBox::Ok);
338     }
339 }
340
341 void BitcoinGUI::changeEvent(QEvent *e)
342 {
343     if (e->type() == QEvent::WindowStateChange)
344     {
345         if(clientModel->getOptionsModel()->getMinimizeToTray())
346         {
347             if (isMinimized())
348             {
349                 hide();
350                 e->ignore();
351             }
352             else
353             {
354                 e->accept();
355             }
356         }
357     }
358     QMainWindow::changeEvent(e);
359 }
360
361 void BitcoinGUI::closeEvent(QCloseEvent *event)
362 {
363     if(!clientModel->getOptionsModel()->getMinimizeToTray() &&
364        !clientModel->getOptionsModel()->getMinimizeOnClose())
365     {
366         qApp->quit();
367     }
368     QMainWindow::closeEvent(event);
369 }
370
371 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
372 {
373     QString strMessage =
374         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
375           "which goes to the nodes that process your transaction and helps to support the network.  "
376           "Do you want to pay the fee?").arg(GUIUtil::formatMoney(nFeeRequired));
377     QMessageBox::StandardButton retval = QMessageBox::question(
378           this, tr("Sending..."), strMessage,
379           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
380     *payFee = (retval == QMessageBox::Yes);
381 }
382
383 void BitcoinGUI::transactionDetails(const QModelIndex& idx)
384 {
385     // A transaction is doubleclicked
386     TransactionDescDialog dlg(idx);
387     dlg.exec();
388 }
389
390 void BitcoinGUI::incomingTransaction(const QModelIndex & parent, int start, int end)
391 {
392     TransactionTableModel *ttm = walletModel->getTransactionTableModel();
393     qint64 amount = ttm->index(start, TransactionTableModel::Amount, parent)
394                     .data(Qt::EditRole).toULongLong();
395     if(amount>0 && !clientModel->inInitialBlockDownload())
396     {
397         // On incoming transaction, make an info balloon
398         // Unless the initial block download is in progress, to prevent balloon-spam
399         QString date = ttm->index(start, TransactionTableModel::Date, parent)
400                         .data().toString();
401         QString type = ttm->index(start, TransactionTableModel::Type, parent)
402                         .data().toString();
403         QString address = ttm->index(start, TransactionTableModel::ToAddress, parent)
404                         .data().toString();
405
406         trayIcon->showMessage(tr("Incoming transaction"),
407                               tr("Date: ") + date + "\n" +
408                               tr("Amount: ") + GUIUtil::formatMoney(amount, true) + "\n" +
409                               tr("Type: ") + type + "\n" +
410                               tr("Address: ") + address + "\n",
411                               QSystemTrayIcon::Information);
412     }
413 }
414
415 void BitcoinGUI::gotoOverviewTab()
416 {
417     overviewAction->setChecked(true);
418     centralWidget->setCurrentWidget(overviewPage);
419 }
420
421 void BitcoinGUI::gotoHistoryTab()
422 {
423     historyAction->setChecked(true);
424     centralWidget->setCurrentWidget(transactionsPage);
425 }