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"
14 #include "editaddressdialog.h"
15 #include "optionsmodel.h"
19 #include <QApplication>
20 #include <QMainWindow>
25 #include <QVBoxLayout>
32 #include <QPushButton>
33 #include <QHeaderView>
35 #include <QSortFilterProxyModel>
37 #include <QMessageBox>
43 BitcoinGUI::BitcoinGUI(QWidget *parent):
44 QMainWindow(parent), trayIcon(0)
47 setWindowTitle(tr("Bitcoin"));
48 setWindowIcon(QIcon(":icons/bitcoin"));
53 QMenu *file = menuBar()->addMenu("&File");
54 file->addAction(sendcoins);
56 file->addAction(quit);
58 QMenu *settings = menuBar()->addMenu("&Settings");
59 settings->addAction(receivingAddresses);
60 settings->addAction(options);
62 QMenu *help = menuBar()->addMenu("&Help");
63 help->addAction(about);
66 QToolBar *toolbar = addToolBar("Main toolbar");
67 toolbar->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
68 toolbar->addAction(sendcoins);
69 toolbar->addAction(addressbook);
71 // Address: <address>: New... : Paste to clipboard
72 QHBoxLayout *hbox_address = new QHBoxLayout();
73 hbox_address->addWidget(new QLabel(tr("Your Bitcoin Address:")));
74 address = new QLineEdit();
75 address->setReadOnly(true);
76 address->setFont(GUIUtil::bitcoinAddressFont());
77 hbox_address->addWidget(address);
79 QPushButton *button_new = new QPushButton(tr("&New..."));
80 QPushButton *button_clipboard = new QPushButton(tr("&Copy to clipboard"));
81 hbox_address->addWidget(button_new);
82 hbox_address->addWidget(button_clipboard);
85 QHBoxLayout *hbox_balance = new QHBoxLayout();
86 hbox_balance->addWidget(new QLabel(tr("Balance:")));
87 hbox_balance->addSpacing(5);/* Add some spacing between the label and the text */
89 labelBalance = new QLabel();
90 labelBalance->setFont(QFont("Monospace"));
91 hbox_balance->addWidget(labelBalance);
92 hbox_balance->addStretch(1);
94 QVBoxLayout *vbox = new QVBoxLayout();
95 vbox->addLayout(hbox_address);
96 vbox->addLayout(hbox_balance);
98 transaction_model = new TransactionTableModel(this);
99 vbox->addWidget(createTabs());
101 QWidget *centralwidget = new QWidget(this);
102 centralwidget->setLayout(vbox);
103 setCentralWidget(centralwidget);
108 labelConnections = new QLabel();
109 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
110 labelConnections->setMinimumWidth(130);
112 labelBlocks = new QLabel();
113 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
114 labelBlocks->setMinimumWidth(130);
116 labelTransactions = new QLabel();
117 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
118 labelTransactions->setMinimumWidth(130);
120 statusBar()->addPermanentWidget(labelConnections);
121 statusBar()->addPermanentWidget(labelBlocks);
122 statusBar()->addPermanentWidget(labelTransactions);
125 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
126 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
131 void BitcoinGUI::createActions()
133 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
134 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
135 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
136 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
137 receivingAddresses = new QAction(QIcon(":/icons/receiving-addresses"), tr("Your &Receiving Addresses..."), this);
138 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
139 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
141 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
142 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
143 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
144 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
145 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
146 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
147 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
150 void BitcoinGUI::setModel(ClientModel *model)
154 // Keep up to date with client
155 setBalance(model->getBalance());
156 connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
158 setNumConnections(model->getNumConnections());
159 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
161 setNumTransactions(model->getNumTransactions());
162 connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
164 setNumBlocks(model->getNumBlocks());
165 connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
167 setAddress(model->getAddress());
168 connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
170 // Report errors from network/worker thread
171 connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
174 void BitcoinGUI::createTrayIcon()
176 QMenu *trayIconMenu = new QMenu(this);
177 trayIconMenu->addAction(openBitcoin);
178 trayIconMenu->addAction(sendcoins);
179 trayIconMenu->addAction(options);
180 trayIconMenu->addSeparator();
181 trayIconMenu->addAction(quit);
183 trayIcon = new QSystemTrayIcon(this);
184 trayIcon->setContextMenu(trayIconMenu);
185 trayIcon->setIcon(QIcon(":/icons/toolbar"));
186 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
187 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
191 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
193 if(reason == QSystemTrayIcon::DoubleClick)
195 // Doubleclick on system tray icon triggers "open bitcoin"
196 openBitcoin->trigger();
200 QWidget *BitcoinGUI::createTabs()
202 QStringList tab_filters, tab_labels;
204 << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
205 << "^["+TransactionTableModel::Sent+"]"
206 << "^["+TransactionTableModel::Received+"]";
207 tab_labels << tr("All transactions")
208 << tr("Sent/Received")
211 QTabWidget *tabs = new QTabWidget(this);
213 for(int i = 0; i < tab_labels.size(); ++i)
215 QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
216 proxy_model->setSourceModel(transaction_model);
217 proxy_model->setDynamicSortFilter(true);
218 proxy_model->setFilterRole(TransactionTableModel::TypeRole);
219 proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
220 proxy_model->setSortRole(Qt::EditRole);
222 QTableView *transaction_table = new QTableView(this);
223 transaction_table->setModel(proxy_model);
224 transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
225 transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
226 transaction_table->setSortingEnabled(true);
227 transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
228 transaction_table->verticalHeader()->hide();
230 transaction_table->horizontalHeader()->resizeSection(
231 TransactionTableModel::Status, 120);
232 transaction_table->horizontalHeader()->resizeSection(
233 TransactionTableModel::Date, 120);
234 transaction_table->horizontalHeader()->setResizeMode(
235 TransactionTableModel::Description, QHeaderView::Stretch);
236 transaction_table->horizontalHeader()->resizeSection(
237 TransactionTableModel::Debit, 79);
238 transaction_table->horizontalHeader()->resizeSection(
239 TransactionTableModel::Credit, 79);
241 tabs->addTab(transaction_table, tab_labels.at(i));
246 void BitcoinGUI::sendcoinsClicked()
251 qDebug() << "After close";
254 void BitcoinGUI::addressbookClicked()
256 AddressBookDialog dlg;
257 dlg.setModel(model->getAddressTableModel());
258 dlg.setTab(AddressBookDialog::SendingTab);
262 void BitcoinGUI::receivingAddressesClicked()
264 AddressBookDialog dlg;
265 dlg.setModel(model->getAddressTableModel());
266 dlg.setTab(AddressBookDialog::ReceivingTab);
270 void BitcoinGUI::optionsClicked()
273 dlg.setModel(model->getOptionsModel());
277 void BitcoinGUI::aboutClicked()
283 void BitcoinGUI::newAddressClicked()
285 EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
286 dlg.setModel(model->getAddressTableModel());
289 QString newAddress = dlg.saveCurrentRow();
290 // Set returned address as new default addres
291 if(!newAddress.isEmpty())
293 model->setAddress(newAddress);
298 void BitcoinGUI::copyClipboardClicked()
300 // Copy text in address to clipboard
301 QApplication::clipboard()->setText(address->text());
304 void BitcoinGUI::setBalance(qint64 balance)
306 labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
309 void BitcoinGUI::setAddress(const QString &addr)
311 address->setText(addr);
314 void BitcoinGUI::setNumConnections(int count)
316 labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections(s)", "", count));
319 void BitcoinGUI::setNumBlocks(int count)
321 labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
324 void BitcoinGUI::setNumTransactions(int count)
326 labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
329 void BitcoinGUI::error(const QString &title, const QString &message)
331 // Report errors from network/worker thread
332 QMessageBox::critical(this, title,
334 QMessageBox::Ok, QMessageBox::Ok);
337 void BitcoinGUI::changeEvent(QEvent *e)
339 if (e->type() == QEvent::WindowStateChange)
341 if(model->getOptionsModel()->getMinimizeToTray())
352 QMainWindow::changeEvent(e);
355 void BitcoinGUI::closeEvent(QCloseEvent *event)
357 if(!model->getOptionsModel()->getMinimizeToTray() &&
358 !model->getOptionsModel()->getMinimizeOnClose())
362 QMainWindow::closeEvent(event);