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