ask fee
[novacoin.git] / gui / src / 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
17 #include "main.h"
18
19 #include <QApplication>
20 #include <QMainWindow>
21 #include <QMenuBar>
22 #include <QMenu>
23 #include <QIcon>
24 #include <QTabWidget>
25 #include <QVBoxLayout>
26 #include <QWidget>
27 #include <QToolBar>
28 #include <QStatusBar>
29 #include <QLabel>
30 #include <QTableView>
31 #include <QLineEdit>
32 #include <QPushButton>
33 #include <QHeaderView>
34 #include <QLocale>
35 #include <QSortFilterProxyModel>
36 #include <QClipboard>
37 #include <QMessageBox>
38
39 #include <QDebug>
40
41 #include <iostream>
42
43 BitcoinGUI::BitcoinGUI(QWidget *parent):
44     QMainWindow(parent), trayIcon(0)
45 {
46     resize(850, 550);
47     setWindowTitle(tr("Bitcoin"));
48     setWindowIcon(QIcon(":icons/bitcoin"));
49
50     createActions();
51
52     // Menus
53     QMenu *file = menuBar()->addMenu("&File");
54     file->addAction(sendcoins);
55     file->addSeparator();
56     file->addAction(quit);
57     
58     QMenu *settings = menuBar()->addMenu("&Settings");
59     settings->addAction(receivingAddresses);
60     settings->addAction(options);
61
62     QMenu *help = menuBar()->addMenu("&Help");
63     help->addAction(about);
64     
65     // Toolbar
66     QToolBar *toolbar = addToolBar("Main toolbar");
67     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
68     toolbar->addAction(sendcoins);
69     toolbar->addAction(addressbook);
70
71     // Address: <address>: New... : Paste to clipboard
72     QHBoxLayout *hbox_address = new QHBoxLayout();
73     hbox_address->addWidget(new QLabel(tr("Your Bitcoin Address:")));
74     address = new QLineEdit();
75     address->setReadOnly(true);
76     address->setFont(GUIUtil::bitcoinAddressFont());
77     hbox_address->addWidget(address);
78     
79     QPushButton *button_new = new QPushButton(tr("&New..."));
80     QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
81     hbox_address->addWidget(button_new);
82     hbox_address->addWidget(button_clipboard);
83     
84     // Balance: <balance>
85     QHBoxLayout *hbox_balance = new QHBoxLayout();
86     hbox_balance->addWidget(new QLabel(tr("Balance:")));
87     hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
88
89     labelBalance = new QLabel();
90     labelBalance->setFont(QFont("Monospace"));
91     hbox_balance->addWidget(labelBalance);
92     hbox_balance->addStretch(1);
93     
94     QVBoxLayout *vbox = new QVBoxLayout();
95     vbox->addLayout(hbox_address);
96     vbox->addLayout(hbox_balance);
97
98     vbox->addWidget(createTabs());
99
100     QWidget *centralwidget = new QWidget(this);
101     centralwidget->setLayout(vbox);
102     setCentralWidget(centralwidget);
103     
104     // Create status bar
105     statusBar();
106     
107     labelConnections = new QLabel();
108     labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
109     labelConnections->setMinimumWidth(130);
110     
111     labelBlocks = new QLabel();
112     labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
113     labelBlocks->setMinimumWidth(130);
114     
115     labelTransactions = new QLabel();
116     labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
117     labelTransactions->setMinimumWidth(130);
118     
119     statusBar()->addPermanentWidget(labelConnections);
120     statusBar()->addPermanentWidget(labelBlocks);
121     statusBar()->addPermanentWidget(labelTransactions);
122      
123     // Action bindings
124     connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
125     connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
126
127     createTrayIcon();
128 }
129
130 void BitcoinGUI::createActions()
131 {
132     quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
133     sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
134     addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
135     about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
136     receivingAddresses = new QAction(QIcon(":/icons/receiving-addresses"), tr("Your &Receiving Addresses..."), this);
137     options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
138     openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
139
140     connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
141     connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
142     connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
143     connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
144     connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
145     connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
146     connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
147 }
148
149 void BitcoinGUI::setModel(ClientModel *model)
150 {
151     this->model = model;
152
153     // Keep up to date with client
154     setBalance(model->getBalance());
155     connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
156
157     setNumConnections(model->getNumConnections());
158     connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
159
160     setNumTransactions(model->getNumTransactions());
161     connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
162
163     setNumBlocks(model->getNumBlocks());
164     connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
165
166     setAddress(model->getAddress());
167     connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
168
169     // Report errors from network/worker thread
170     connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));    
171
172     // Put transaction list in tabs
173     setTabsModel(model->getTransactionTableModel());
174 }
175
176 void BitcoinGUI::createTrayIcon()
177 {
178     QMenu *trayIconMenu = new QMenu(this);
179     trayIconMenu->addAction(openBitcoin);
180     trayIconMenu->addAction(sendcoins);
181     trayIconMenu->addAction(options);
182     trayIconMenu->addSeparator();
183     trayIconMenu->addAction(quit);
184
185     trayIcon = new QSystemTrayIcon(this);
186     trayIcon->setContextMenu(trayIconMenu);
187     trayIcon->setIcon(QIcon(":/icons/toolbar"));
188     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
189             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
190     trayIcon->show();
191 }
192
193 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
194 {
195     if(reason == QSystemTrayIcon::DoubleClick)
196     {
197         // Doubleclick on system tray icon triggers "open bitcoin"
198         openBitcoin->trigger();
199     }
200 }
201
202 QWidget *BitcoinGUI::createTabs()
203 {
204     QStringList tab_labels;
205     tab_labels  << tr("All transactions")
206                 << tr("Sent/Received")
207                 << tr("Sent")
208                 << tr("Received");
209
210     QTabWidget *tabs = new QTabWidget(this);
211     for(int i = 0; i < tab_labels.size(); ++i)
212     {
213         QTableView *view = new QTableView(this);
214         tabs->addTab(view, tab_labels.at(i));
215         transactionViews.append(view);
216     }
217
218     return tabs;
219 }
220
221 void BitcoinGUI::setTabsModel(QAbstractItemModel *transaction_model)
222 {
223     QStringList tab_filters;
224     tab_filters << "^."
225             << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
226             << "^["+TransactionTableModel::Sent+"]"
227             << "^["+TransactionTableModel::Received+"]";
228
229     for(int i = 0; i < transactionViews.size(); ++i)
230     {
231         QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
232         proxy_model->setSourceModel(transaction_model);
233         proxy_model->setDynamicSortFilter(true);
234         proxy_model->setFilterRole(TransactionTableModel::TypeRole);
235         proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
236         proxy_model->setSortRole(Qt::EditRole);
237
238         QTableView *transaction_table = transactionViews.at(i);
239         transaction_table->setModel(proxy_model);
240         transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
241         transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
242         transaction_table->setSortingEnabled(true);
243         transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
244         transaction_table->verticalHeader()->hide();
245
246         transaction_table->horizontalHeader()->resizeSection(
247                 TransactionTableModel::Status, 120);
248         transaction_table->horizontalHeader()->resizeSection(
249                 TransactionTableModel::Date, 120);
250         transaction_table->horizontalHeader()->setResizeMode(
251                 TransactionTableModel::Description, QHeaderView::Stretch);
252         transaction_table->horizontalHeader()->resizeSection(
253                 TransactionTableModel::Debit, 79);
254         transaction_table->horizontalHeader()->resizeSection(
255                 TransactionTableModel::Credit, 79);
256     }
257 }
258
259 void BitcoinGUI::sendcoinsClicked()
260 {
261     SendCoinsDialog dlg;
262     dlg.setModel(model);
263     dlg.exec();
264 }
265
266 void BitcoinGUI::addressbookClicked()
267 {
268     AddressBookDialog dlg;
269     dlg.setModel(model->getAddressTableModel());
270     dlg.setTab(AddressBookDialog::SendingTab);
271     dlg.exec();
272 }
273
274 void BitcoinGUI::receivingAddressesClicked()
275 {
276     AddressBookDialog dlg;
277     dlg.setModel(model->getAddressTableModel());
278     dlg.setTab(AddressBookDialog::ReceivingTab);
279     dlg.exec();
280 }
281
282 void BitcoinGUI::optionsClicked()
283 {
284     OptionsDialog dlg;
285     dlg.setModel(model->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(model->getAddressTableModel());
299     if(dlg.exec())
300     {
301         QString newAddress = dlg.saveCurrentRow();
302         // Set returned address as new default addres
303         if(!newAddress.isEmpty())
304         {
305             model->setAddress(newAddress);
306         }
307     }
308 }
309
310 void BitcoinGUI::copyClipboardClicked()
311 {
312     // Copy text in address to clipboard
313     QApplication::clipboard()->setText(address->text());
314 }
315
316 void BitcoinGUI::setBalance(qint64 balance)
317 {
318     labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
319 }
320
321 void BitcoinGUI::setAddress(const QString &addr)
322 {
323     address->setText(addr);
324 }
325
326 void BitcoinGUI::setNumConnections(int count)
327 {
328     labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections(s)", "", count));
329 }
330
331 void BitcoinGUI::setNumBlocks(int count)
332 {
333     labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
334 }
335
336 void BitcoinGUI::setNumTransactions(int count)
337 {
338     labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
339 }
340
341 void BitcoinGUI::error(const QString &title, const QString &message)
342 {
343     // Report errors from network/worker thread
344     if(trayIcon->supportsMessages())
345     {
346         // Show as "balloon" message if possible
347         trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
348     } else {
349         // Fall back to old fashioned popup dialog if not
350         QMessageBox::critical(this, title,
351             message,
352             QMessageBox::Ok, QMessageBox::Ok);
353     }
354 }
355
356 void BitcoinGUI::changeEvent(QEvent *e)
357 {
358     if (e->type() == QEvent::WindowStateChange)
359     {
360         if(model->getOptionsModel()->getMinimizeToTray())
361         {
362             if (isMinimized())
363             {
364                 hide();
365                 e->ignore();
366             } else {
367                 e->accept();
368             }
369         }
370     }
371     QMainWindow::changeEvent(e);
372 }
373
374 void BitcoinGUI::closeEvent(QCloseEvent *event)
375 {
376     if(!model->getOptionsModel()->getMinimizeToTray() &&
377        !model->getOptionsModel()->getMinimizeOnClose())
378     {
379         qApp->quit();
380     }
381     QMainWindow::closeEvent(event);
382 }
383
384 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
385 {
386     QString strMessage =
387         tr("This transaction is over the size limit.  You can still send it for a fee of %1, "
388           "which goes to the nodes that process your transaction and helps to support the network.  "
389           "Do you want to pay the fee?").arg(QString::fromStdString(FormatMoney(nFeeRequired)));
390     QMessageBox::StandardButton retval = QMessageBox::question(
391           this, tr("Sending..."), strMessage,
392           QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
393     *payFee = (retval == QMessageBox::Yes);
394 }