1 // Copyright (c) 2011-2013 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #include "transactionview.h"
7 #include "addresstablemodel.h"
8 #include "bitcoinunits.h"
9 #include "csvmodelwriter.h"
10 #include "editaddressdialog.h"
12 #include "optionsmodel.h"
14 #include "transactiondescdialog.h"
15 #include "transactionfilterproxy.h"
16 #include "transactionrecord.h"
17 #include "transactiontablemodel.h"
18 #include "walletmodel.h"
20 #include "ui_interface.h"
23 #include <QDateTimeEdit>
24 #include <QDesktopServices>
25 #include <QDoubleValidator>
26 #include <QHBoxLayout>
27 #include <QHeaderView>
33 #include <QSignalMapper>
36 #include <QVBoxLayout>
38 TransactionView::TransactionView(QWidget *parent) :
39 QWidget(parent), model(0), transactionProxyModel(0),
43 setContentsMargins(0,0,0,0);
45 QHBoxLayout *hlayout = new QHBoxLayout();
46 hlayout->setContentsMargins(0,0,0,0);
48 hlayout->setSpacing(5);
49 hlayout->addSpacing(26);
51 hlayout->setSpacing(0);
52 hlayout->addSpacing(23);
55 watchOnlyWidget = new QComboBox(this);
56 watchOnlyWidget->setFixedWidth(24);
57 watchOnlyWidget->addItem("", TransactionFilterProxy::WatchOnlyFilter_All);
58 watchOnlyWidget->addItem(SingleColorIcon(":/icons/eye_plus"), "", TransactionFilterProxy::WatchOnlyFilter_Yes);
59 watchOnlyWidget->addItem(SingleColorIcon(":/icons/eye_minus"), "", TransactionFilterProxy::WatchOnlyFilter_No);
60 hlayout->addWidget(watchOnlyWidget);
62 dateWidget = new QComboBox(this);
64 dateWidget->setFixedWidth(121);
66 dateWidget->setFixedWidth(120);
68 dateWidget->addItem(tr("All"), All);
69 dateWidget->addItem(tr("Today"), Today);
70 dateWidget->addItem(tr("This week"), ThisWeek);
71 dateWidget->addItem(tr("This month"), ThisMonth);
72 dateWidget->addItem(tr("Last month"), LastMonth);
73 dateWidget->addItem(tr("This year"), ThisYear);
74 dateWidget->addItem(tr("Range..."), Range);
75 hlayout->addWidget(dateWidget);
77 typeWidget = new QComboBox(this);
79 typeWidget->setFixedWidth(121);
81 typeWidget->setFixedWidth(120);
84 typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
85 typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
86 TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
87 typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
88 TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
89 typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
90 typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
91 typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
93 hlayout->addWidget(typeWidget);
95 addressWidget = new QLineEdit(this);
96 #if QT_VERSION >= 0x040700
97 addressWidget->setPlaceholderText(tr("Enter address or label to search"));
99 hlayout->addWidget(addressWidget);
101 amountWidget = new QLineEdit(this);
102 #if QT_VERSION >= 0x040700
103 amountWidget->setPlaceholderText(tr("Min amount"));
106 amountWidget->setFixedWidth(97);
108 amountWidget->setFixedWidth(100);
110 amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
111 hlayout->addWidget(amountWidget);
113 QVBoxLayout *vlayout = new QVBoxLayout(this);
114 vlayout->setContentsMargins(0,0,0,0);
115 vlayout->setSpacing(0);
117 QTableView *view = new QTableView(this);
118 vlayout->addLayout(hlayout);
119 vlayout->addWidget(createDateRangeWidget());
120 vlayout->addWidget(view);
121 vlayout->setSpacing(0);
122 int width = view->verticalScrollBar()->sizeHint().width();
123 // Cover scroll bar width with spacing
125 hlayout->addSpacing(width+2);
127 hlayout->addSpacing(width);
129 // Always show scroll bar
130 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
131 view->setTabKeyNavigation(false);
132 view->setContextMenuPolicy(Qt::CustomContextMenu);
134 view->installEventFilter(this);
136 transactionView = view;
139 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
140 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
141 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
142 QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
143 QAction *editLabelAction = new QAction(tr("Edit label"), this);
144 QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
146 contextMenu = new QMenu();
147 contextMenu->addAction(copyAddressAction);
148 contextMenu->addAction(copyLabelAction);
149 contextMenu->addAction(copyAmountAction);
150 contextMenu->addAction(copyTxIDAction);
151 contextMenu->addAction(editLabelAction);
152 contextMenu->addAction(showDetailsAction);
154 mapperThirdPartyTxUrls = new QSignalMapper(this);
157 connect(mapperThirdPartyTxUrls, SIGNAL(mapped(QString)), this, SLOT(openThirdPartyTxUrl(QString)));
159 connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
160 connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
161 connect(watchOnlyWidget, SIGNAL(activated(int)), this, SLOT(chooseWatchonly(int)));
162 connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
163 connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
165 connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
166 connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
168 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
169 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
170 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
171 connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
172 connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
173 connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
176 void TransactionView::setModel(WalletModel *model)
181 transactionProxyModel = new TransactionFilterProxy(this);
182 transactionProxyModel->setSourceModel(model->getTransactionTableModel());
183 transactionProxyModel->setDynamicSortFilter(true);
184 transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
185 transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
187 transactionProxyModel->setSortRole(Qt::EditRole);
189 transactionView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
190 transactionView->setModel(transactionProxyModel);
191 transactionView->setAlternatingRowColors(true);
192 transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
193 transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
194 transactionView->setSortingEnabled(true);
195 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
196 transactionView->verticalHeader()->hide();
198 transactionView->setColumnWidth(TransactionTableModel::Status, STATUS_COLUMN_WIDTH);
199 transactionView->setColumnWidth(TransactionTableModel::Watchonly, WATCHONLY_COLUMN_WIDTH);
200 transactionView->setColumnWidth(TransactionTableModel::Date, DATE_COLUMN_WIDTH);
201 transactionView->setColumnWidth(TransactionTableModel::Type, TYPE_COLUMN_WIDTH);
202 transactionView->setColumnWidth(TransactionTableModel::Amount, AMOUNT_MINIMUM_COLUMN_WIDTH);
204 columnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(transactionView, AMOUNT_MINIMUM_COLUMN_WIDTH, MINIMUM_COLUMN_WIDTH);
206 if (model->getOptionsModel())
208 // Add third party transaction URLs to context menu
209 QStringList listUrls = model->getOptionsModel()->getThirdPartyTxUrls().split("|", QString::SkipEmptyParts);
210 for (int i = 0; i < listUrls.size(); ++i)
212 QString host = QUrl(listUrls[i].trimmed(), QUrl::StrictMode).host();
215 QAction *thirdPartyTxUrlAction = new QAction(host, this); // use host as menu item label
217 contextMenu->addSeparator();
218 contextMenu->addAction(thirdPartyTxUrlAction);
219 connect(thirdPartyTxUrlAction, SIGNAL(triggered()), mapperThirdPartyTxUrls, SLOT(map()));
220 mapperThirdPartyTxUrls->setMapping(thirdPartyTxUrlAction, listUrls[i].trimmed());
225 // show/hide column Watch-only
226 updateWatchOnlyColumn(model->haveWatchOnly());
229 connect(model, SIGNAL(notifyWatchonlyChanged(bool)), this, SLOT(updateWatchOnlyColumn(bool)));
233 void TransactionView::chooseDate(int idx)
235 if(!transactionProxyModel)
237 QDate current = QDate::currentDate();
238 dateRangeWidget->setVisible(false);
239 switch(dateWidget->itemData(idx).toInt())
242 transactionProxyModel->setDateRange(
243 TransactionFilterProxy::MIN_DATE,
244 TransactionFilterProxy::MAX_DATE);
247 transactionProxyModel->setDateRange(
249 TransactionFilterProxy::MAX_DATE);
253 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
254 transactionProxyModel->setDateRange(
255 QDateTime(startOfWeek),
256 TransactionFilterProxy::MAX_DATE);
260 transactionProxyModel->setDateRange(
261 QDateTime(QDate(current.year(), current.month(), 1)),
262 TransactionFilterProxy::MAX_DATE);
265 transactionProxyModel->setDateRange(
266 QDateTime(QDate(current.year(), current.month()-1, 1)),
267 QDateTime(QDate(current.year(), current.month(), 1)));
270 transactionProxyModel->setDateRange(
271 QDateTime(QDate(current.year(), 1, 1)),
272 TransactionFilterProxy::MAX_DATE);
275 dateRangeWidget->setVisible(true);
281 void TransactionView::chooseType(int idx)
283 if(!transactionProxyModel)
285 transactionProxyModel->setTypeFilter(
286 typeWidget->itemData(idx).toInt());
289 void TransactionView::chooseWatchonly(int idx)
291 if(!transactionProxyModel)
293 transactionProxyModel->setWatchOnlyFilter(
294 (TransactionFilterProxy::WatchOnlyFilter)watchOnlyWidget->itemData(idx).toInt());
297 void TransactionView::changedPrefix(const QString &prefix)
299 if(!transactionProxyModel)
301 transactionProxyModel->setAddressPrefix(prefix);
304 void TransactionView::changedAmount(const QString &amount)
306 if(!transactionProxyModel)
308 CAmount amount_parsed = 0;
309 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
311 transactionProxyModel->setMinAmount(amount_parsed);
315 transactionProxyModel->setMinAmount(0);
319 void TransactionView::exportClicked()
321 // CSV is currently the only supported format
322 QString filename = GUIUtil::getSaveFileName(this,
323 tr("Export Transaction History"), QString(),
324 tr("Comma separated file (*.csv)"), NULL);
326 if (filename.isNull())
329 CSVModelWriter writer(filename);
331 // name, column, role
332 writer.setModel(transactionProxyModel);
333 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
334 if (model && model->haveWatchOnly())
335 writer.addColumn(tr("Watch-only"), TransactionTableModel::Watchonly);
336 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
337 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
338 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
339 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
340 writer.addColumn(BitcoinUnits::getAmountColumnTitle(model->getOptionsModel()->getDisplayUnit()), 0, TransactionTableModel::FormattedAmountRole);
341 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
343 if(!writer.write()) {
344 emit message(tr("Exporting Failed"), tr("There was an error trying to save the transaction history to %1.").arg(filename),
345 CClientUIInterface::MSG_ERROR);
348 emit message(tr("Exporting Successful"), tr("The transaction history was successfully saved to %1.").arg(filename),
349 CClientUIInterface::MSG_INFORMATION);
353 void TransactionView::contextualMenu(const QPoint &point)
355 QModelIndex index = transactionView->indexAt(point);
358 contextMenu->exec(QCursor::pos());
362 void TransactionView::copyAddress()
364 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
367 void TransactionView::copyLabel()
369 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
372 void TransactionView::copyAmount()
374 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
377 void TransactionView::copyTxID()
379 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::TxIDRole);
382 void TransactionView::editLabel()
384 if(!transactionView->selectionModel() ||!model)
386 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
387 if(!selection.isEmpty())
389 AddressTableModel *addressBook = model->getAddressTableModel();
392 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
393 if(address.isEmpty())
395 // If this transaction has no associated address, exit
398 // Is address in address book? Address book can miss address when a transaction is
399 // sent from outside the UI.
400 int idx = addressBook->lookupAddress(address);
403 // Edit sending / receiving address
404 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
405 // Determine type of address, launch appropriate editor dialog type
406 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
408 EditAddressDialog dlg(
409 type == AddressTableModel::Receive
410 ? EditAddressDialog::EditReceivingAddress
411 : EditAddressDialog::EditSendingAddress, this);
412 dlg.setModel(addressBook);
418 // Add sending address
419 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
421 dlg.setModel(addressBook);
422 dlg.setAddress(address);
428 void TransactionView::showDetails()
430 if(!transactionView->selectionModel())
432 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
433 if(!selection.isEmpty())
435 TransactionDescDialog dlg(selection.at(0));
440 void TransactionView::openThirdPartyTxUrl(QString url)
442 if(!transactionView || !transactionView->selectionModel())
444 QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
445 if(!selection.isEmpty())
446 QDesktopServices::openUrl(QUrl::fromUserInput(url.replace("%s", selection.at(0).data(TransactionTableModel::TxHashRole).toString())));
449 QWidget *TransactionView::createDateRangeWidget()
451 dateRangeWidget = new QFrame();
452 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
453 dateRangeWidget->setContentsMargins(1,1,1,1);
454 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
455 layout->setContentsMargins(0,0,0,0);
456 layout->addSpacing(23);
457 layout->addWidget(new QLabel(tr("Range:")));
459 dateFrom = new QDateTimeEdit(this);
460 dateFrom->setDisplayFormat("dd/MM/yy");
461 dateFrom->setCalendarPopup(true);
462 dateFrom->setMinimumWidth(100);
463 dateFrom->setDate(QDate::currentDate().addDays(-7));
464 layout->addWidget(dateFrom);
465 layout->addWidget(new QLabel(tr("to")));
467 dateTo = new QDateTimeEdit(this);
468 dateTo->setDisplayFormat("dd/MM/yy");
469 dateTo->setCalendarPopup(true);
470 dateTo->setMinimumWidth(100);
471 dateTo->setDate(QDate::currentDate());
472 layout->addWidget(dateTo);
473 layout->addStretch();
476 dateRangeWidget->setVisible(false);
479 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
480 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
482 return dateRangeWidget;
485 void TransactionView::dateRangeChanged()
487 if(!transactionProxyModel)
489 transactionProxyModel->setDateRange(
490 QDateTime(dateFrom->date()),
491 QDateTime(dateTo->date()).addDays(1));
494 void TransactionView::focusTransaction(const QModelIndex &idx)
496 if(!transactionProxyModel)
498 QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
499 transactionView->scrollTo(targetIdx);
500 transactionView->setCurrentIndex(targetIdx);
501 transactionView->setFocus();
504 // We override the virtual resizeEvent of the QWidget to adjust tables column
505 // sizes as the tables width is proportional to the dialogs width.
506 void TransactionView::resizeEvent(QResizeEvent* event)
508 QWidget::resizeEvent(event);
509 columnResizingFixer->stretchColumnWidth(TransactionTableModel::ToAddress);
512 // Need to override default Ctrl+C action for amount as default behaviour is just to copy DisplayRole text
513 bool TransactionView::eventFilter(QObject *obj, QEvent *event)
515 if (event->type() == QEvent::KeyPress)
517 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
518 if (ke->key() == Qt::Key_C && ke->modifiers().testFlag(Qt::ControlModifier))
520 QModelIndex i = this->transactionView->currentIndex();
521 if (i.isValid() && i.column() == TransactionTableModel::Amount)
523 GUIUtil::setClipboard(i.data(TransactionTableModel::FormattedAmountRole).toString());
528 return QWidget::eventFilter(obj, event);
531 // show/hide column Watch-only
532 void TransactionView::updateWatchOnlyColumn(bool fHaveWatchOnly)
534 watchOnlyWidget->setVisible(fHaveWatchOnly);
535 transactionView->setColumnHidden(TransactionTableModel::Watchonly, !fHaveWatchOnly);