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