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