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