Implement Minimize to tray / Minimize on close
[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     transaction_model = new TransactionTableModel(this);
99     vbox->addWidget(createTabs());
100
101     QWidget *centralwidget = new QWidget(this);
102     centralwidget->setLayout(vbox);
103     setCentralWidget(centralwidget);
104     
105     // Create status bar
106     statusBar();
107     
108     labelConnections = new QLabel();
109     labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
110     labelConnections->setMinimumWidth(130);
111     
112     labelBlocks = new QLabel();
113     labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
114     labelBlocks->setMinimumWidth(130);
115     
116     labelTransactions = new QLabel();
117     labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
118     labelTransactions->setMinimumWidth(130);
119     
120     statusBar()->addPermanentWidget(labelConnections);
121     statusBar()->addPermanentWidget(labelBlocks);
122     statusBar()->addPermanentWidget(labelTransactions);
123      
124     // Action bindings
125     connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
126     connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
127
128     createTrayIcon();
129 }
130
131 void BitcoinGUI::createActions()
132 {
133     quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
134     sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
135     addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
136     about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
137     receivingAddresses = new QAction(QIcon(":/icons/receiving-addresses"), tr("Your &Receiving Addresses..."), this);
138     options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
139     openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
140
141     connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
142     connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
143     connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
144     connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
145     connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
146     connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
147     connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
148 }
149
150 void BitcoinGUI::setModel(ClientModel *model)
151 {
152     this->model = model;
153
154     // Keep up to date with client
155     setBalance(model->getBalance());
156     connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
157
158     setNumConnections(model->getNumConnections());
159     connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
160
161     setNumTransactions(model->getNumTransactions());
162     connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
163
164     setNumBlocks(model->getNumBlocks());
165     connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
166
167     setAddress(model->getAddress());
168     connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
169
170     // Report errors from network/worker thread
171     connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));    
172 }
173
174 void BitcoinGUI::createTrayIcon()
175 {
176     QMenu *trayIconMenu = new QMenu(this);
177     trayIconMenu->addAction(openBitcoin);
178     trayIconMenu->addAction(sendcoins);
179     trayIconMenu->addAction(options);
180     trayIconMenu->addSeparator();
181     trayIconMenu->addAction(quit);
182
183     trayIcon = new QSystemTrayIcon(this);
184     trayIcon->setContextMenu(trayIconMenu);
185     trayIcon->setIcon(QIcon(":/icons/toolbar"));
186     connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
187             this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
188     trayIcon->show();
189 }
190
191 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
192 {
193     if(reason == QSystemTrayIcon::DoubleClick)
194     {
195         // Doubleclick on system tray icon triggers "open bitcoin"
196         openBitcoin->trigger();
197     }
198 }
199
200 QWidget *BitcoinGUI::createTabs()
201 {
202     QStringList tab_filters, tab_labels;
203     tab_filters << "^."
204             << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
205             << "^["+TransactionTableModel::Sent+"]"
206             << "^["+TransactionTableModel::Received+"]";
207     tab_labels  << tr("All transactions")
208                 << tr("Sent/Received")
209                 << tr("Sent")
210                 << tr("Received");
211     QTabWidget *tabs = new QTabWidget(this);
212
213     for(int i = 0; i < tab_labels.size(); ++i)
214     {
215         QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
216         proxy_model->setSourceModel(transaction_model);
217         proxy_model->setDynamicSortFilter(true);
218         proxy_model->setFilterRole(TransactionTableModel::TypeRole);
219         proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
220         proxy_model->setSortRole(Qt::EditRole);
221
222         QTableView *transaction_table = new QTableView(this);
223         transaction_table->setModel(proxy_model);
224         transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
225         transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
226         transaction_table->setSortingEnabled(true);
227         transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
228         transaction_table->verticalHeader()->hide();
229
230         transaction_table->horizontalHeader()->resizeSection(
231                 TransactionTableModel::Status, 120);
232         transaction_table->horizontalHeader()->resizeSection(
233                 TransactionTableModel::Date, 120);
234         transaction_table->horizontalHeader()->setResizeMode(
235                 TransactionTableModel::Description, QHeaderView::Stretch);
236         transaction_table->horizontalHeader()->resizeSection(
237                 TransactionTableModel::Debit, 79);
238         transaction_table->horizontalHeader()->resizeSection(
239                 TransactionTableModel::Credit, 79);
240
241         tabs->addTab(transaction_table, tab_labels.at(i));
242     }
243     return tabs;
244 }
245
246 void BitcoinGUI::sendcoinsClicked()
247 {
248     SendCoinsDialog dlg;
249     dlg.setModel(model);
250     dlg.exec();
251     qDebug() << "After close";
252 }
253
254 void BitcoinGUI::addressbookClicked()
255 {
256     AddressBookDialog dlg;
257     dlg.setModel(model->getAddressTableModel());
258     dlg.setTab(AddressBookDialog::SendingTab);
259     dlg.exec();
260 }
261
262 void BitcoinGUI::receivingAddressesClicked()
263 {
264     AddressBookDialog dlg;
265     dlg.setModel(model->getAddressTableModel());
266     dlg.setTab(AddressBookDialog::ReceivingTab);
267     dlg.exec();
268 }
269
270 void BitcoinGUI::optionsClicked()
271 {
272     OptionsDialog dlg;
273     dlg.setModel(model->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(model->getAddressTableModel());
287     if(dlg.exec())
288     {
289         QString newAddress = dlg.saveCurrentRow();
290         // Set returned address as new default addres
291         if(!newAddress.isEmpty())
292         {
293             model->setAddress(newAddress);
294         }
295     }
296 }
297
298 void BitcoinGUI::copyClipboardClicked()
299 {
300     // Copy text in address to clipboard
301     QApplication::clipboard()->setText(address->text());
302 }
303
304 void BitcoinGUI::setBalance(qint64 balance)
305 {
306     labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
307 }
308
309 void BitcoinGUI::setAddress(const QString &addr)
310 {
311     address->setText(addr);
312 }
313
314 void BitcoinGUI::setNumConnections(int count)
315 {
316     labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections(s)", "", count));
317 }
318
319 void BitcoinGUI::setNumBlocks(int count)
320 {
321     labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
322 }
323
324 void BitcoinGUI::setNumTransactions(int count)
325 {
326     labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
327 }
328
329 void BitcoinGUI::error(const QString &title, const QString &message)
330 {
331     // Report errors from network/worker thread
332     QMessageBox::critical(this, title,
333         message,
334         QMessageBox::Ok, QMessageBox::Ok);
335 }
336
337 void BitcoinGUI::changeEvent(QEvent *e)
338 {
339     if (e->type() == QEvent::WindowStateChange)
340     {
341         if(model->getOptionsModel()->getMinimizeToTray())
342         {
343             if (isMinimized())
344             {
345                 hide();
346                 e->ignore();
347             } else {
348                 e->accept();
349             }
350         }
351     }
352     QMainWindow::changeEvent(e);
353 }
354
355 void BitcoinGUI::closeEvent(QCloseEvent *event)
356 {
357     if(!model->getOptionsModel()->getMinimizeToTray() &&
358        !model->getOptionsModel()->getMinimizeOnClose())
359     {
360         qApp->quit();
361     }
362     QMainWindow::closeEvent(event);
363 }