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