168dffd7cdf70bc86ab39c4cccbe5f37288ddfee
[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->verticalHeader()->hide();
204
205         transaction_table->horizontalHeader()->resizeSection(
206                 TransactionTableModel::Status, 112);
207         transaction_table->horizontalHeader()->resizeSection(
208                 TransactionTableModel::Date, 112);
209         transaction_table->horizontalHeader()->setResizeMode(
210                 TransactionTableModel::Description, QHeaderView::Stretch);
211         transaction_table->horizontalHeader()->resizeSection(
212                 TransactionTableModel::Debit, 79);
213         transaction_table->horizontalHeader()->resizeSection(
214                 TransactionTableModel::Credit, 79);
215
216         tabs->addTab(transaction_table, tab_labels.at(i));
217     }
218     return tabs;
219 }
220
221 void BitcoinGUI::sendcoinsClicked()
222 {
223     SendCoinsDialog dlg;
224     dlg.exec();
225 }
226
227 void BitcoinGUI::addressbookClicked()
228 {
229     AddressBookDialog dlg;
230     dlg.setTab(AddressBookDialog::SendingTab);
231     /* if an address accepted, do a 'send' to specified address */
232     if(dlg.exec())
233     {
234         SendCoinsDialog send(0, dlg.getReturnValue());
235         send.exec();
236     }
237 }
238
239 void BitcoinGUI::receivingAddressesClicked()
240 {
241     AddressBookDialog dlg;
242     dlg.setTab(AddressBookDialog::ReceivingTab);
243     dlg.exec();
244 }
245
246 void BitcoinGUI::optionsClicked()
247 {
248     OptionsDialog dlg;
249     dlg.exec();
250 }
251
252 void BitcoinGUI::aboutClicked()
253 {
254     AboutDialog dlg;
255     dlg.exec();
256 }
257
258 void BitcoinGUI::newAddressClicked()
259 {
260     qDebug() << "New address clicked";
261     /* TODO: generate new address */
262 }
263
264 void BitcoinGUI::copyClipboardClicked()
265 {
266     /* Copy text in address to clipboard */
267     QApplication::clipboard()->setText(address->text());
268 }
269
270 void BitcoinGUI::setBalance(double balance)
271 {
272     labelBalance->setText(QLocale::system().toString(balance, 8));
273 }
274
275 void BitcoinGUI::setAddress(const QString &addr)
276 {
277     address->setText(addr);
278 }
279
280 void BitcoinGUI::setNumConnections(int count)
281 {
282     labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections"));
283 }
284
285 void BitcoinGUI::setNumBlocks(int count)
286 {
287     labelBlocks->setText(QLocale::system().toString(count)+" "+tr("blocks"));
288 }
289
290 void BitcoinGUI::setNumTransactions(int count)
291 {
292     labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transactions"));
293 }