implement filtering, action listeners
[novacoin.git] / 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 "SettingsDialog.h"
10 #include "SendCoinsDialog.h"
11
12 #include <QApplication>
13 #include <QMainWindow>
14 #include <QMenuBar>
15 #include <QMenu>
16 #include <QIcon>
17 #include <QTabWidget>
18 #include <QVBoxLayout>
19 #include <QWidget>
20 #include <QToolBar>
21 #include <QStatusBar>
22 #include <QLabel>
23 #include <QTableView>
24 #include <QLineEdit>
25 #include <QPushButton>
26 #include <QHeaderView>
27 #include <QLocale>
28 #include <QSortFilterProxyModel>
29
30 #include <QDebug>
31
32 #include <iostream>
33
34 BitcoinGUI::BitcoinGUI(QWidget *parent):
35     QMainWindow(parent)
36 {
37     resize(850, 550);
38     setWindowTitle(tr("Bitcoin"));
39     setWindowIcon(QIcon(":icons/bitcoin"));
40     
41     QAction *quit = new QAction(QIcon(":/icons/quit"), tr("&Quit"), this);
42     QAction *sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
43     QAction *addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address book"), this);
44     QAction *about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
45     QAction *receiving_addresses = new QAction(QIcon(":/icons/receiving-addresses"), tr("Your &Receiving Addresses..."), this);
46     QAction *options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
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     QLineEdit *edit_address = new QLineEdit();
71     edit_address->setReadOnly(true);
72     hbox_address->addWidget(edit_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     QLabel *label_balance = new QLabel(QLocale::system().toString(1345.54));
85     label_balance->setFont(QFont("Teletype"));
86     hbox_balance->addWidget(label_balance);
87     hbox_balance->addStretch(1);
88     
89     /* Tab widget */
90     QVBoxLayout *vbox = new QVBoxLayout();
91     vbox->addLayout(hbox_address);
92     vbox->addLayout(hbox_balance);
93     
94     TransactionTableModel *transaction_model = new TransactionTableModel(this);
95
96     /* setupTabs */
97     QStringList tab_filters, tab_labels;
98     tab_filters << "^."
99                 << "^[sr]"
100                 << "^[s]"
101                 << "^[r]";
102     tab_labels  << tr("All transactions")
103                 << tr("Sent/Received")
104                 << tr("Sent")
105                 << tr("Received");
106     QTabWidget *tabs = new QTabWidget(this);
107
108     for(int i = 0; i < tab_labels.size(); ++i)
109     {
110         QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
111         proxy_model->setSourceModel(transaction_model);
112         proxy_model->setDynamicSortFilter(true);
113         proxy_model->setFilterRole(Qt::UserRole);
114         proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
115
116         QTableView *transaction_table = new QTableView(this);
117         transaction_table->setModel(proxy_model);
118         transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
119         transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
120         transaction_table->verticalHeader()->hide();
121
122         transaction_table->horizontalHeader()->resizeSection(
123                 TransactionTableModel::Status, 112);
124         transaction_table->horizontalHeader()->resizeSection(
125                 TransactionTableModel::Date, 112);
126         transaction_table->horizontalHeader()->setResizeMode(
127                 TransactionTableModel::Description, QHeaderView::Stretch);
128         transaction_table->horizontalHeader()->resizeSection(
129                 TransactionTableModel::Debit, 79);
130         transaction_table->horizontalHeader()->resizeSection(
131                 TransactionTableModel::Credit, 79);
132
133         tabs->addTab(transaction_table, tab_labels.at(i));
134     }
135    
136     vbox->addWidget(tabs);
137
138     QWidget *centralwidget = new QWidget(this);
139     centralwidget->setLayout(vbox);
140     setCentralWidget(centralwidget);
141     
142     /* Status bar */
143     statusBar();
144     
145     QLabel *label_connections = new QLabel("6 connections", this);
146     label_connections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
147     label_connections->setMinimumWidth(100);
148     
149     QLabel *label_blocks = new QLabel("6 blocks", this);
150     label_blocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
151     label_blocks->setMinimumWidth(100);
152     
153     QLabel *label_transactions = new QLabel("6 transactions", this);
154     label_transactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
155     label_transactions->setMinimumWidth(100);
156     
157     statusBar()->addPermanentWidget(label_connections);
158     statusBar()->addPermanentWidget(label_blocks);
159     statusBar()->addPermanentWidget(label_transactions);
160      
161     /* Action bindings */
162     connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
163     connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
164     connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
165     connect(receiving_addresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
166     connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
167     connect(button_new, SIGNAL(triggered()), this, SLOT(newAddressClicked()));
168     connect(button_clipboard, SIGNAL(triggered()), this, SLOT(copyClipboardClicked()));
169 }
170
171 void BitcoinGUI::sendcoinsClicked()
172 {
173     qDebug() << "Send coins clicked";
174 }
175
176 void BitcoinGUI::addressbookClicked()
177 {
178     qDebug() << "Address book clicked";
179 }
180
181 void BitcoinGUI::optionsClicked()
182 {
183     qDebug() << "Options clicked";
184 }
185
186 void BitcoinGUI::receivingAddressesClicked()
187 {
188     qDebug() << "Receiving addresses clicked";
189 }
190
191 void BitcoinGUI::newAddressClicked()
192 {
193     qDebug() << "New address clicked";
194 }
195
196 void BitcoinGUI::copyClipboardClicked()
197 {
198     qDebug() << "Copy to clipboard";
199 }