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