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