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