4e5eaa06836c1ae51e45cbf60feb5a3783ec6a1d
[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 "headers.h"
22
23 #include <QApplication>
24 #include <QMainWindow>
25 #include <QMenuBar>
26 #include <QMenu>
27 #include <QIcon>
28 #include <QTabWidget>
29 #include <QVBoxLayout>
30 #include <QWidget>
31 #include <QToolBar>
32 #include <QStatusBar>
33 #include <QLabel>
34 #include <QLineEdit>
35 #include <QPushButton>
36 #include <QLocale>
37 #include <QClipboard>
38 #include <QMessageBox>
39 #include <QProgressBar>
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"));
53     setWindowIcon(QIcon(":icons/bitcoin"));
54
55     createActions();
56
57     // Menus
58     QMenu *file = menuBar()->addMenu("&File");
59     file->addAction(sendcoins);
60     file->addSeparator();
61     file->addAction(quit);
62     
63     QMenu *settings = menuBar()->addMenu("&Settings");
64     settings->addAction(receivingAddresses);
65     settings->addAction(options);
66
67     QMenu *help = menuBar()->addMenu("&Help");
68     help->addAction(about);
69     
70     // Toolbar
71     QToolBar *toolbar = addToolBar("Main toolbar");
72     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
73     toolbar->addAction(sendcoins);
74     toolbar->addAction(addressbook);
75
76     // Address: <address>: New... : Paste to clipboard
77     QHBoxLayout *hbox_address = new QHBoxLayout();
78     hbox_address->addWidget(new QLabel(tr("Your Bitcoin address:")));
79     address = new QLineEdit();
80     address->setReadOnly(true);
81     address->setFont(GUIUtil::bitcoinAddressFont());
82     address->setToolTip(tr("Your current default receiving address"));
83     hbox_address->addWidget(address);
84     
85     QPushButton *button_new = new QPushButton(tr("&New address..."));
86     button_new->setToolTip(tr("Create new receiving address"));
87     button_new->setIcon(QIcon(":/icons/add"));
88     QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
89     button_clipboard->setToolTip(tr("Copy current receiving address to the system clipboard"));
90     button_clipboard->setIcon(QIcon(":/icons/editcopy"));
91     hbox_address->addWidget(button_new);
92     hbox_address->addWidget(button_clipboard);
93
94     // Balance: <balance>
95     QHBoxLayout *hbox_balance = new QHBoxLayout();
96     hbox_balance->addWidget(new QLabel(tr("Balance:")));
97     hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
98
99     labelBalance = new QLabel();
100     labelBalance->setFont(QFont("Monospace", -1, QFont::Bold));
101     labelBalance->setToolTip(tr("Your current balance"));
102     hbox_balance->addWidget(labelBalance);
103     hbox_balance->addStretch(1);
104     
105     QVBoxLayout *vbox = new QVBoxLayout();
106     vbox->addLayout(hbox_address);
107     vbox->addLayout(hbox_balance);
108
109     transactionView = new TransactionView(this);
110     connect(transactionView, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(transactionDetails(const QModelIndex&)));
111     vbox->addWidget(transactionView);
112
113     QWidget *centralwidget = new QWidget(this);
114     centralwidget->setLayout(vbox);
115     setCentralWidget(centralwidget);
116     
117     // Create status bar
118     statusBar();
119
120     labelConnections = new QLabel();
121     labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
122     labelConnections->setMinimumWidth(150);
123     labelConnections->setToolTip(tr("Number of connections to other clients"));
124
125     labelBlocks = new QLabel();
126     labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
127     labelBlocks->setMinimumWidth(130);
128     labelBlocks->setToolTip(tr("Number of blocks in the block chain"));
129
130     labelTransactions = new QLabel();
131     labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
132     labelTransactions->setMinimumWidth(130);
133     labelTransactions->setToolTip(tr("Number of transactions in your wallet"));
134
135     // Progress bar for blocks download
136     progressBarLabel = new QLabel(tr("Synchronizing with network..."));
137     progressBarLabel->setVisible(false);
138     progressBar = new QProgressBar();
139     progressBar->setToolTip(tr("Block chain synchronization in progress"));
140     progressBar->setVisible(false);
141
142     statusBar()->addWidget(progressBarLabel);
143     statusBar()->addWidget(progressBar);
144     statusBar()->addPermanentWidget(labelConnections);
145     statusBar()->addPermanentWidget(labelBlocks);
146     statusBar()->addPermanentWidget(labelTransactions);
147
148     // Action bindings
149     connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
150     connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
151
152     createTrayIcon();
153 }
154
155 void BitcoinGUI::createActions()
156 {
157     quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
158     quit->setToolTip(tr("Quit application"));
159     sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
160     sendcoins->setToolTip(tr("Send coins to a bitcoin address"));
161     addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
162     addressbook->setToolTip(tr("Edit the list of stored addresses and labels"));
163     about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
164     about->setToolTip(tr("Show information about Bitcoin"));
165     receivingAddresses = new QAction(QIcon(":/icons/receiving_addresses"), tr("Your &Receiving Addresses..."), this);
166     receivingAddresses->setToolTip(tr("Show the list of receiving addresses and edit their labels"));
167     options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
168     options->setToolTip(tr("Modify configuration options for bitcoin"));
169     openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
170     openBitcoin->setToolTip(tr("Show the Bitcoin window"));
171
172     connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
173     connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
174     connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
175     connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
176     connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
177     connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
178     connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
179 }
180
181 void BitcoinGUI::setClientModel(ClientModel *clientModel)
182 {
183     this->clientModel = clientModel;
184
185     if(clientModel->isTestNet())
186     {
187         setWindowTitle(tr("Bitcoin [testnet]"));
188         setWindowIcon(QIcon(":icons/bitcoin_testnet"));
189         if(trayIcon)
190         {
191             trayIcon->setToolTip(tr("Bitcoin [testnet]"));
192             trayIcon->setIcon(QIcon(":/icons/toolbar_testnet"));
193         }
194     }
195
196     // Keep up to date with client
197     setNumConnections(clientModel->getNumConnections());
198     connect(clientModel, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
199
200     setNumBlocks(clientModel->getNumBlocks());
201     connect(clientModel, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
202
203     // Report errors from network/worker thread
204     connect(clientModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
205 }
206
207 void BitcoinGUI::setWalletModel(WalletModel *walletModel)
208 {
209     this->walletModel = walletModel;
210
211     // Keep up to date with wallet
212     setBalance(walletModel->getBalance());
213     connect(walletModel, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
214
215     setNumTransactions(walletModel->getNumTransactions());
216     connect(walletModel, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
217
218     setAddress(walletModel->getAddressTableModel()->getDefaultAddress());
219     connect(walletModel->getAddressTableModel(), SIGNAL(defaultAddressChanged(QString)), this, SLOT(setAddress(QString)));
220
221     // Report errors from wallet thread
222     connect(walletModel, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
223
224     // Put transaction list in tabs
225     transactionView->setModel(walletModel->getTransactionTableModel());
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(openBitcoin);
236     trayIconMenu->addAction(sendcoins);
237     trayIconMenu->addAction(options);
238     trayIconMenu->addSeparator();
239     trayIconMenu->addAction(quit);
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         openBitcoin->trigger();
256     }
257 }
258
259 void BitcoinGUI::sendcoinsClicked()
260 {
261     SendCoinsDialog dlg;
262     dlg.setModel(walletModel);
263     dlg.exec();
264 }
265
266 void BitcoinGUI::addressbookClicked()
267 {
268     AddressBookDialog dlg(AddressBookDialog::ForEditing);
269     dlg.setModel(walletModel->getAddressTableModel());
270     dlg.setTab(AddressBookDialog::SendingTab);
271     dlg.exec();
272 }
273
274 void BitcoinGUI::receivingAddressesClicked()
275 {
276     AddressBookDialog dlg(AddressBookDialog::ForEditing);
277     dlg.setModel(walletModel->getAddressTableModel());
278     dlg.setTab(AddressBookDialog::ReceivingTab);
279     dlg.exec();
280 }
281
282 void BitcoinGUI::optionsClicked()
283 {
284     OptionsDialog dlg;
285     dlg.setModel(clientModel->getOptionsModel());
286     dlg.exec();
287 }
288
289 void BitcoinGUI::aboutClicked()
290 {
291     AboutDialog dlg;
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(QString::fromStdString(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(QString::fromStdString(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: ") + QString::fromStdString(FormatMoney(amount, true)) + "\n" +
445                               tr("Type: ") + type + "\n" +
446                               tr("Address: ") + address + "\n",
447                               QSystemTrayIcon::Information);
448     }
449 }