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 vbox->addWidget(createTabs());
100 QWidget *centralwidget = new QWidget(this);
101 centralwidget->setLayout(vbox);
102 setCentralWidget(centralwidget);
107 labelConnections = new QLabel();
108 labelConnections->setFrameStyle(QFrame::Panel | QFrame::Sunken);
109 labelConnections->setMinimumWidth(130);
111 labelBlocks = new QLabel();
112 labelBlocks->setFrameStyle(QFrame::Panel | QFrame::Sunken);
113 labelBlocks->setMinimumWidth(130);
115 labelTransactions = new QLabel();
116 labelTransactions->setFrameStyle(QFrame::Panel | QFrame::Sunken);
117 labelTransactions->setMinimumWidth(130);
119 statusBar()->addPermanentWidget(labelConnections);
120 statusBar()->addPermanentWidget(labelBlocks);
121 statusBar()->addPermanentWidget(labelTransactions);
124 connect(button_new, SIGNAL(clicked()), this, SLOT(newAddressClicked()));
125 connect(button_clipboard, SIGNAL(clicked()), this, SLOT(copyClipboardClicked()));
130 void BitcoinGUI::createActions()
132 quit = new QAction(QIcon(":/icons/quit"), tr("&Exit"), this);
133 sendcoins = new QAction(QIcon(":/icons/send"), tr("&Send coins"), this);
134 addressbook = new QAction(QIcon(":/icons/address-book"), tr("&Address Book"), this);
135 about = new QAction(QIcon(":/icons/bitcoin"), tr("&About"), this);
136 receivingAddresses = new QAction(QIcon(":/icons/receiving-addresses"), tr("Your &Receiving Addresses..."), this);
137 options = new QAction(QIcon(":/icons/options"), tr("&Options..."), this);
138 openBitcoin = new QAction(QIcon(":/icons/bitcoin"), "Open &Bitcoin", this);
140 connect(quit, SIGNAL(triggered()), qApp, SLOT(quit()));
141 connect(sendcoins, SIGNAL(triggered()), this, SLOT(sendcoinsClicked()));
142 connect(addressbook, SIGNAL(triggered()), this, SLOT(addressbookClicked()));
143 connect(receivingAddresses, SIGNAL(triggered()), this, SLOT(receivingAddressesClicked()));
144 connect(options, SIGNAL(triggered()), this, SLOT(optionsClicked()));
145 connect(about, SIGNAL(triggered()), this, SLOT(aboutClicked()));
146 connect(openBitcoin, SIGNAL(triggered()), this, SLOT(show()));
149 void BitcoinGUI::setModel(ClientModel *model)
153 // Keep up to date with client
154 setBalance(model->getBalance());
155 connect(model, SIGNAL(balanceChanged(qint64)), this, SLOT(setBalance(qint64)));
157 setNumConnections(model->getNumConnections());
158 connect(model, SIGNAL(numConnectionsChanged(int)), this, SLOT(setNumConnections(int)));
160 setNumTransactions(model->getNumTransactions());
161 connect(model, SIGNAL(numTransactionsChanged(int)), this, SLOT(setNumTransactions(int)));
163 setNumBlocks(model->getNumBlocks());
164 connect(model, SIGNAL(numBlocksChanged(int)), this, SLOT(setNumBlocks(int)));
166 setAddress(model->getAddress());
167 connect(model, SIGNAL(addressChanged(QString)), this, SLOT(setAddress(QString)));
169 // Report errors from network/worker thread
170 connect(model, SIGNAL(error(QString,QString)), this, SLOT(error(QString,QString)));
172 // Put transaction list in tabs
173 setTabsModel(model->getTransactionTableModel());
176 void BitcoinGUI::createTrayIcon()
178 QMenu *trayIconMenu = new QMenu(this);
179 trayIconMenu->addAction(openBitcoin);
180 trayIconMenu->addAction(sendcoins);
181 trayIconMenu->addAction(options);
182 trayIconMenu->addSeparator();
183 trayIconMenu->addAction(quit);
185 trayIcon = new QSystemTrayIcon(this);
186 trayIcon->setContextMenu(trayIconMenu);
187 trayIcon->setIcon(QIcon(":/icons/toolbar"));
188 connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
189 this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
193 void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
195 if(reason == QSystemTrayIcon::DoubleClick)
197 // Doubleclick on system tray icon triggers "open bitcoin"
198 openBitcoin->trigger();
202 QWidget *BitcoinGUI::createTabs()
204 QStringList tab_labels;
205 tab_labels << tr("All transactions")
206 << tr("Sent/Received")
210 QTabWidget *tabs = new QTabWidget(this);
211 for(int i = 0; i < tab_labels.size(); ++i)
213 QTableView *view = new QTableView(this);
214 tabs->addTab(view, tab_labels.at(i));
215 transactionViews.append(view);
221 void BitcoinGUI::setTabsModel(QAbstractItemModel *transaction_model)
223 QStringList tab_filters;
225 << "^["+TransactionTableModel::Sent+TransactionTableModel::Received+"]"
226 << "^["+TransactionTableModel::Sent+"]"
227 << "^["+TransactionTableModel::Received+"]";
229 for(int i = 0; i < transactionViews.size(); ++i)
231 QSortFilterProxyModel *proxy_model = new QSortFilterProxyModel(this);
232 proxy_model->setSourceModel(transaction_model);
233 proxy_model->setDynamicSortFilter(true);
234 proxy_model->setFilterRole(TransactionTableModel::TypeRole);
235 proxy_model->setFilterRegExp(QRegExp(tab_filters.at(i)));
236 proxy_model->setSortRole(Qt::EditRole);
238 QTableView *transaction_table = transactionViews.at(i);
239 transaction_table->setModel(proxy_model);
240 transaction_table->setSelectionBehavior(QAbstractItemView::SelectRows);
241 transaction_table->setSelectionMode(QAbstractItemView::ExtendedSelection);
242 transaction_table->setSortingEnabled(true);
243 transaction_table->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
244 transaction_table->verticalHeader()->hide();
246 transaction_table->horizontalHeader()->resizeSection(
247 TransactionTableModel::Status, 120);
248 transaction_table->horizontalHeader()->resizeSection(
249 TransactionTableModel::Date, 120);
250 transaction_table->horizontalHeader()->setResizeMode(
251 TransactionTableModel::Description, QHeaderView::Stretch);
252 transaction_table->horizontalHeader()->resizeSection(
253 TransactionTableModel::Debit, 79);
254 transaction_table->horizontalHeader()->resizeSection(
255 TransactionTableModel::Credit, 79);
259 void BitcoinGUI::sendcoinsClicked()
266 void BitcoinGUI::addressbookClicked()
268 AddressBookDialog dlg;
269 dlg.setModel(model->getAddressTableModel());
270 dlg.setTab(AddressBookDialog::SendingTab);
274 void BitcoinGUI::receivingAddressesClicked()
276 AddressBookDialog dlg;
277 dlg.setModel(model->getAddressTableModel());
278 dlg.setTab(AddressBookDialog::ReceivingTab);
282 void BitcoinGUI::optionsClicked()
285 dlg.setModel(model->getOptionsModel());
289 void BitcoinGUI::aboutClicked()
295 void BitcoinGUI::newAddressClicked()
297 EditAddressDialog dlg(EditAddressDialog::NewReceivingAddress);
298 dlg.setModel(model->getAddressTableModel());
301 QString newAddress = dlg.saveCurrentRow();
302 // Set returned address as new default addres
303 if(!newAddress.isEmpty())
305 model->setAddress(newAddress);
310 void BitcoinGUI::copyClipboardClicked()
312 // Copy text in address to clipboard
313 QApplication::clipboard()->setText(address->text());
316 void BitcoinGUI::setBalance(qint64 balance)
318 labelBalance->setText(QString::fromStdString(FormatMoney(balance)));
321 void BitcoinGUI::setAddress(const QString &addr)
323 address->setText(addr);
326 void BitcoinGUI::setNumConnections(int count)
328 labelConnections->setText(QLocale::system().toString(count)+" "+tr("connections(s)", "", count));
331 void BitcoinGUI::setNumBlocks(int count)
333 labelBlocks->setText(QLocale::system().toString(count)+" "+tr("block(s)", "", count));
336 void BitcoinGUI::setNumTransactions(int count)
338 labelTransactions->setText(QLocale::system().toString(count)+" "+tr("transaction(s)", "", count));
341 void BitcoinGUI::error(const QString &title, const QString &message)
343 // Report errors from network/worker thread
344 if(trayIcon->supportsMessages())
346 // Show as "balloon" message if possible
347 trayIcon->showMessage(title, message, QSystemTrayIcon::Critical);
349 // Fall back to old fashioned popup dialog if not
350 QMessageBox::critical(this, title,
352 QMessageBox::Ok, QMessageBox::Ok);
356 void BitcoinGUI::changeEvent(QEvent *e)
358 if (e->type() == QEvent::WindowStateChange)
360 if(model->getOptionsModel()->getMinimizeToTray())
371 QMainWindow::changeEvent(e);
374 void BitcoinGUI::closeEvent(QCloseEvent *event)
376 if(!model->getOptionsModel()->getMinimizeToTray() &&
377 !model->getOptionsModel()->getMinimizeOnClose())
381 QMainWindow::closeEvent(event);
384 void BitcoinGUI::askFee(qint64 nFeeRequired, bool *payFee)
387 tr("This transaction is over the size limit. You can still send it for a fee of %1, "
388 "which goes to the nodes that process your transaction and helps to support the network. "
389 "Do you want to pay the fee?").arg(QString::fromStdString(FormatMoney(nFeeRequired)));
390 QMessageBox::StandardButton retval = QMessageBox::question(
391 this, tr("Sending..."), strMessage,
392 QMessageBox::Yes|QMessageBox::Cancel, QMessageBox::Yes);
393 *payFee = (retval == QMessageBox::Yes);