4 * W.J. van der Laan 2011
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"
16 #include <QApplication>
17 #include <QMainWindow>
22 #include <QVBoxLayout>
29 #include <QPushButton>
30 #include <QHeaderView>
32 #include <QSortFilterProxyModel>
34 #include <QMessageBox>
40 BitcoinGUI::BitcoinGUI(QWidget *parent):
41 QMainWindow(parent), trayIcon(0)
44 setWindowTitle(tr("Bitcoin"));
45 setWindowIcon(QIcon(":icons/bitcoin"));
50 QMenu *file = menuBar()->addMenu("&File");
51 file->addAction(sendcoins);
53 file->addAction(quit);
55 QMenu *settings = menuBar()->addMenu("&Settings");
56 settings->addAction(receiving_addresses);
57 settings->addAction(options);
59 QMenu *help = menuBar()->addMenu("&Help");
60 help->addAction(about);
63 QToolBar *toolbar = addToolBar("Main toolbar");
64 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
65 toolbar->addAction(sendcoins);
66 toolbar->addAction(addressbook);
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);
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);
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 */
85 labelBalance = new QLabel();
86 labelBalance->setFont(QFont("Teletype"));
87 hbox_balance->addWidget(labelBalance);
88 hbox_balance->addStretch(1);
90 QVBoxLayout *vbox = new QVBoxLayout();
91 vbox->addLayout(hbox_address);
92 vbox->addLayout(hbox_balance);
94 transaction_model = new TransactionTableModel(this);
96 vbox->addWidget(createTabs());
98 QWidget *centralwidget = new QWidget(this);
99 centralwidget->setLayout(vbox);
100 setCentralWidget(centralwidget);
102 /* Create status bar */
105 labelConnections = new QLabel();
106 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
107 labelConnections->setMinimumWidth(130);
109 labelBlocks = new QLabel();
110 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
111 labelBlocks->setMinimumWidth(130);
113 labelTransactions = new QLabel();
114 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
115 labelTransactions->setMinimumWidth(130);
117 statusBar()->addPermanentWidget(labelConnections);
118 statusBar()->addPermanentWidget(labelBlocks);
119 statusBar()->addPermanentWidget(labelTransactions);
121 /* Action bindings */
122 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
123 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
128 void BitcoinGUI::createActions()
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);
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()));
146 void BitcoinGUI::setModel(ClientModel *model)
150 setBalance(model->getBalance());
151 connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
153 setNumConnections(model->getNumConnections());
154 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
156 setNumTransactions(model->getNumTransactions());
157 connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
159 setNumBlocks(model->getNumBlocks());
160 connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
162 setAddress(model->getAddress());
163 connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
165 /* Report errors from network/worker thread */
166 connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
169 void BitcoinGUI::createTrayIcon()
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);
178 trayIcon = new QSystemTrayIcon(this);
179 trayIcon->setContextMenu(trayIconMenu);
180 trayIcon->setIcon(QIcon(":/icons/toolbar"));
184 QWidget *BitcoinGUI::createTabs()
186 QStringList tab_filters, tab_labels;
188 << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
189 << "^["+TransactionTableModel::Sent+"]"
190 << "^["+TransactionTableModel::Received+"]";
191 tab_labels << tr("All transactions")
192 << tr("Sent/Received")
195 QTabWidget *tabs = new QTabWidget(this);
197 for(int i = 0; i < tab_labels.size(); ++i)
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);
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();
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);
225 tabs->addTab(transaction_table, tab_labels.at(i));
230 void BitcoinGUI::sendcoinsClicked()
237 void BitcoinGUI::addressbookClicked()
239 AddressBookDialog dlg;
240 dlg.setTab(AddressBookDialog::SendingTab);
241 /* if an address accepted, do a 'send' to specified address */
244 SendCoinsDialog send(0, dlg.getReturnValue());
249 void BitcoinGUI::receivingAddressesClicked()
251 AddressBookDialog dlg;
252 dlg.setTab(AddressBookDialog::ReceivingTab);
256 void BitcoinGUI::optionsClicked()
259 dlg.setModel(model->getOptionsModel());
263 void BitcoinGUI::aboutClicked()
269 void BitcoinGUI::newAddressClicked()
271 qDebug() << "New address clicked";
272 /* TODO: generate new address */
275 void BitcoinGUI::copyClipboardClicked()
277 /* Copy text in address to clipboard */
278 QApplication::clipboard()->setText(address->text());
281 void BitcoinGUI::setBalance(qint64 balance)
283 labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
286 void BitcoinGUI::setAddress(const QString &addr)
288 address->setText(addr);
291 void BitcoinGUI::setNumConnections(int count)
293 labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections(s)", "", count));
296 void BitcoinGUI::setNumBlocks(int count)
298 labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
301 void BitcoinGUI::setNumTransactions(int count)
303 labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
306 void BitcoinGUI::error(const QString &title, const QString &message)
308 /* Report errors from network/worker thread */
309 QMessageBox::critical(this, title,
311 QMessageBox::Ok, QMessageBox::Ok);