Finish implementation of address book
[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
15 #include "main.h"
16
17 #include <QApplication>
18 #include <QMainWindow>
19 #include <QMenuBar>
20 #include <QMenu>
21 #include <QIcon>
22 #include <QTabWidget>
23 #include <QVBoxLayout>
24 #include <QWidget>
25 #include <QToolBar>
26 #include <QStatusBar>
27 #include <QLabel>
28 #include <QTableView>
29 #include <QLineEdit>
30 #include <QPushButton>
31 #include <QHeaderView>
32 #include <QLocale>
33 #include <QSortFilterProxyModel>
34 #include <QClipboard>
35 #include <QMessageBox>
36
37 #include <QDebug>
38
39 #include <iostream>
40
41 BitcoinGUI::BitcoinGUI(QWidget *parent):
42     QMainWindow(parent), trayIcon(0)
43 {
44     resize(850, 550);
45     setWindowTitle(tr("Bitcoin"));
46     setWindowIcon(QIcon(":icons/bitcoin"));
47
48     createActions();
49
50     /* Menus */
51     QMenu *file = menuBar()->addMenu("&File");
52     file->addAction(sendcoins);
53     file->addSeparator();
54     file->addAction(quit);
55     
56     QMenu *settings = menuBar()->addMenu("&Settings");
57     settings->addAction(receiving_addresses);
58     settings->addAction(options);
59
60     QMenu *help = menuBar()->addMenu("&Help");
61     help->addAction(about);
62     
63     /* Toolbar */
64     QToolBar *toolbar = addToolBar("Main toolbar");
65     toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
66     toolbar->addAction(sendcoins);
67     toolbar->addAction(addressbook);
68
69     /* Address: <address>: New... : Paste to clipboard */
70     QHBoxLayout *hbox_address = new QHBoxLayout();
71     hbox_address->addWidget(new QLabel(tr("Your Bitcoin Address:")));
72     address = new QLineEdit();
73     address->setReadOnly(true);
74     address->setFont(GUIUtil::bitcoinAddressFont());
75     hbox_address->addWidget(address);
76     
77     QPushButton *button_new = new QPushButton(tr("&New..."));
78     QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
79     hbox_address->addWidget(button_new);
80     hbox_address->addWidget(button_clipboard);
81     
82     /* Balance: <balance> */
83     QHBoxLayout *hbox_balance = new QHBoxLayout();
84     hbox_balance->addWidget(new QLabel(tr("Balance:")));
85     hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
86
87     labelBalance = new QLabel();
88     labelBalance->setFont(QFont("Monospace"));
89     hbox_balance->addWidget(labelBalance);
90     hbox_balance->addStretch(1);
91     
92     QVBoxLayout *vbox = new QVBoxLayout();
93     vbox->addLayout(hbox_address);
94     vbox->addLayout(hbox_balance);
95     
96     transaction_model = new TransactionTableModel(this);
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     receiving_addresses = 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(receiving_addresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
144     connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
145     connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
146 }
147
148 void BitcoinGUI::setModel(ClientModel *model)
149 {
150     this->model = model;
151
152     setBalance(model->getBalance());
153     connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
154
155     setNumConnections(model->getNumConnections());
156     connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
157
158     setNumTransactions(model->getNumTransactions());
159     connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
160
161     setNumBlocks(model->getNumBlocks());
162     connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
163
164     setAddress(model->getAddress());
165     connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
166
167     /* Report errors from network/worker thread */
168     connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
169 }
170
171 void BitcoinGUI::createTrayIcon()
172 {
173     QMenu *trayIconMenu = new QMenu(this);
174     trayIconMenu->addAction(openBitCoin);
175     trayIconMenu->addAction(sendcoins);
176     trayIconMenu->addAction(options);
177     trayIconMenu->addSeparator();
178     trayIconMenu->addAction(quit);
179
180     trayIcon = new QSystemTrayIcon(this);
181     trayIcon->setContextMenu(trayIconMenu);
182     trayIcon->setIcon(QIcon(":/icons/toolbar"));
183     trayIcon->show();
184 }
185
186 QWidget *BitcoinGUI::createTabs()
187 {
188     QStringList tab_filters, tab_labels;
189     tab_filters << "^."
190             << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
191             << "^["+TransactionTableModel::Sent+"]"
192             << "^["+TransactionTableModel::Received+"]";
193     tab_labels  << tr("All transactions")
194                 << tr("Sent/Received")
195                 << tr("Sent")
196                 << tr("Received");
197     QTabWidget *tabs = new QTabWidget(this);
198
199     for(int i = 0; i < tab_labels.size(); ++i)
200     {
201         QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
202         proxy_model->setSourceModel(transaction_model);
203         proxy_model->setDynamicSortFilter(true);
204         proxy_model->setFilterRole(TransactionTableModel::TypeRole);
205         proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
206         proxy_model->setSortRole(Qt::EditRole);
207
208         QTableView *transaction_table = new QTableView(this);
209         transaction_table->setModel(proxy_model);
210         transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
211         transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
212         transaction_table->setSortingEnabled(true);
213         transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
214         transaction_table->verticalHeader()->hide();
215
216         transaction_table->horizontalHeader()->resizeSection(
217                 TransactionTableModel::Status, 120);
218         transaction_table->horizontalHeader()->resizeSection(
219                 TransactionTableModel::Date, 120);
220         transaction_table->horizontalHeader()->setResizeMode(
221                 TransactionTableModel::Description, QHeaderView::Stretch);
222         transaction_table->horizontalHeader()->resizeSection(
223                 TransactionTableModel::Debit, 79);
224         transaction_table->horizontalHeader()->resizeSection(
225                 TransactionTableModel::Credit, 79);
226
227         tabs->addTab(transaction_table, tab_labels.at(i));
228     }
229     return tabs;
230 }
231
232 void BitcoinGUI::sendcoinsClicked()
233 {
234     SendCoinsDialog dlg;
235     dlg.setModel(model);
236     dlg.exec();
237 }
238
239 void BitcoinGUI::addressbookClicked()
240 {
241     AddressBookDialog dlg;
242     dlg.setTab(AddressBookDialog::SendingTab);
243     dlg.exec();
244 }
245
246 void BitcoinGUI::receivingAddressesClicked()
247 {
248     AddressBookDialog dlg;
249     dlg.setTab(AddressBookDialog::ReceivingTab);
250     dlg.exec();
251 }
252
253 void BitcoinGUI::optionsClicked()
254 {
255     OptionsDialog dlg;
256     dlg.setModel(model->getOptionsModel());
257     dlg.exec();
258 }
259
260 void BitcoinGUI::aboutClicked()
261 {
262     AboutDialog dlg;
263     dlg.exec();
264 }
265
266 void BitcoinGUI::newAddressClicked()
267 {
268     qDebug() << "New address clicked";
269     /* TODO: generate new address */
270 }
271
272 void BitcoinGUI::copyClipboardClicked()
273 {
274     /* Copy text in address to clipboard */
275     QApplication::clipboard()->setText(address->text());
276 }
277
278 void BitcoinGUI::setBalance(qint64 balance)
279 {
280     labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
281 }
282
283 void BitcoinGUI::setAddress(const QString &addr)
284 {
285     address->setText(addr);
286 }
287
288 void BitcoinGUI::setNumConnections(int count)
289 {
290     labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections(s)", "", count));
291 }
292
293 void BitcoinGUI::setNumBlocks(int count)
294 {
295     labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
296 }
297
298 void BitcoinGUI::setNumTransactions(int count)
299 {
300     labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
301 }
302
303 void BitcoinGUI::error(const QString &title, const QString &message)
304 {
305     /* Report errors from network/worker thread */
306     QMessageBox::critical(this, title,
307         message,
308         QMessageBox::Ok, QMessageBox::Ok);
309 }