1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 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"
13 #include "transactiondescdialog.h"
14 #include "transactionfilterproxy.h"
15 #include "transactionrecord.h"
16 #include "transactiontablemodel.h"
17 #include "walletmodel.h"
19 #include "ui_interface.h"
22 #include <QDateTimeEdit>
23 #include <QDesktopServices>
24 #include <QDoubleValidator>
25 #include <QHBoxLayout>
26 #include <QHeaderView>
32 #include <QSignalMapper>
35 #include <QVBoxLayout>
37 TransactionView::TransactionView(QWidget *parent) :
38 QWidget(parent), model(0), transactionProxyModel(0),
42 setContentsMargins(0,0,0,0);
44 QHBoxLayout *hlayout = new QHBoxLayout();
45 hlayout->setContentsMargins(0,0,0,0);
47 hlayout->setSpacing(5);
48 hlayout->addSpacing(26);
50 hlayout->setSpacing(0);
51 hlayout->addSpacing(23);
54 dateWidget = new QComboBox(this);
56 dateWidget->setFixedWidth(121);
58 dateWidget->setFixedWidth(120);
60 dateWidget->addItem(tr("All"), All);
61 dateWidget->addItem(tr("Today"), Today);
62 dateWidget->addItem(tr("This week"), ThisWeek);
63 dateWidget->addItem(tr("This month"), ThisMonth);
64 dateWidget->addItem(tr("Last month"), LastMonth);
65 dateWidget->addItem(tr("This year"), ThisYear);
66 dateWidget->addItem(tr("Range..."), Range);
67 hlayout->addWidget(dateWidget);
69 typeWidget = new QComboBox(this);
71 typeWidget->setFixedWidth(121);
73 typeWidget->setFixedWidth(120);
76 typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
77 typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
78 TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
79 typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
80 TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
81 typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
82 typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
83 typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
85 hlayout->addWidget(typeWidget);
87 addressWidget = new QLineEdit(this);
88 #if QT_VERSION >= 0x040700
89 addressWidget->setPlaceholderText(tr("Enter address or label to search"));
91 hlayout->addWidget(addressWidget);
93 amountWidget = new QLineEdit(this);
94 #if QT_VERSION >= 0x040700
95 amountWidget->setPlaceholderText(tr("Min amount"));
98 amountWidget->setFixedWidth(97);
100 amountWidget->setFixedWidth(100);
102 amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
103 hlayout->addWidget(amountWidget);
105 QVBoxLayout *vlayout = new QVBoxLayout(this);
106 vlayout->setContentsMargins(0,0,0,0);
107 vlayout->setSpacing(0);
109 QTableView *view = new QTableView(this);
110 vlayout->addLayout(hlayout);
111 vlayout->addWidget(createDateRangeWidget());
112 vlayout->addWidget(view);
113 vlayout->setSpacing(0);
114 int width = view->verticalScrollBar()->sizeHint().width();
115 // Cover scroll bar width with spacing
117 hlayout->addSpacing(width+2);
119 hlayout->addSpacing(width);
121 // Always show scroll bar
122 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
123 view->setTabKeyNavigation(false);
124 view->setContextMenuPolicy(Qt::CustomContextMenu);
126 view->installEventFilter(this);
128 transactionView = view;
131 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
132 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
133 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
134 QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
135 QAction *editLabelAction = new QAction(tr("Edit label"), this);
136 QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
138 contextMenu = new QMenu();
139 contextMenu->addAction(copyAddressAction);
140 contextMenu->addAction(copyLabelAction);
141 contextMenu->addAction(copyAmountAction);
142 contextMenu->addAction(copyTxIDAction);
143 contextMenu->addAction(editLabelAction);
144 contextMenu->addAction(showDetailsAction);
146 mapperThirdPartyTxUrls = new QSignalMapper(this);
149 connect(mapperThirdPartyTxUrls, SIGNAL(mapped(QString)), this, SLOT(openThirdPartyTxUrl(QString)));
151 connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
152 connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
153 connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
154 connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
156 connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
157 connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
159 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
160 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
161 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
162 connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
163 connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
164 connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
167 void TransactionView::setModel(WalletModel *model)
172 transactionProxyModel = new TransactionFilterProxy(this);
173 transactionProxyModel->setSourceModel(model->getTransactionTableModel());
174 transactionProxyModel->setDynamicSortFilter(true);
175 transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
176 transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
178 transactionProxyModel->setSortRole(Qt::EditRole);
180 transactionView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
181 transactionView->setModel(transactionProxyModel);
182 transactionView->setAlternatingRowColors(true);
183 transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
184 transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
185 transactionView->setSortingEnabled(true);
186 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
187 transactionView->verticalHeader()->hide();
189 transactionView->setColumnWidth(TransactionTableModel::Status, STATUS_COLUMN_WIDTH);
190 transactionView->setColumnWidth(TransactionTableModel::Date, DATE_COLUMN_WIDTH);
191 transactionView->setColumnWidth(TransactionTableModel::Type, TYPE_COLUMN_WIDTH);
192 transactionView->setColumnWidth(TransactionTableModel::Amount, AMOUNT_MINIMUM_COLUMN_WIDTH);
194 columnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(transactionView, AMOUNT_MINIMUM_COLUMN_WIDTH, MINIMUM_COLUMN_WIDTH);
196 if (model->getOptionsModel())
198 // Add third party transaction URLs to context menu
199 QStringList listUrls = model->getOptionsModel()->getThirdPartyTxUrls().split("|", QString::SkipEmptyParts);
200 for (int i = 0; i < listUrls.size(); ++i)
202 QString host = QUrl(listUrls[i].trimmed(), QUrl::StrictMode).host();
205 QAction *thirdPartyTxUrlAction = new QAction(host, this); // use host as menu item label
207 contextMenu->addSeparator();
208 contextMenu->addAction(thirdPartyTxUrlAction);
209 connect(thirdPartyTxUrlAction, SIGNAL(triggered()), mapperThirdPartyTxUrls, SLOT(map()));
210 mapperThirdPartyTxUrls->setMapping(thirdPartyTxUrlAction, listUrls[i].trimmed());
217 void TransactionView::chooseDate(int idx)
219 if(!transactionProxyModel)
221 QDate current = QDate::currentDate();
222 dateRangeWidget->setVisible(false);
223 switch(dateWidget->itemData(idx).toInt())
226 transactionProxyModel->setDateRange(
227 TransactionFilterProxy::MIN_DATE,
228 TransactionFilterProxy::MAX_DATE);
231 transactionProxyModel->setDateRange(
233 TransactionFilterProxy::MAX_DATE);
237 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
238 transactionProxyModel->setDateRange(
239 QDateTime(startOfWeek),
240 TransactionFilterProxy::MAX_DATE);
244 transactionProxyModel->setDateRange(
245 QDateTime(QDate(current.year(), current.month(), 1)),
246 TransactionFilterProxy::MAX_DATE);
249 transactionProxyModel->setDateRange(
250 QDateTime(QDate(current.year(), current.month()-1, 1)),
251 QDateTime(QDate(current.year(), current.month(), 1)));
254 transactionProxyModel->setDateRange(
255 QDateTime(QDate(current.year(), 1, 1)),
256 TransactionFilterProxy::MAX_DATE);
259 dateRangeWidget->setVisible(true);
265 void TransactionView::chooseType(int idx)
267 if(!transactionProxyModel)
269 transactionProxyModel->setTypeFilter(
270 typeWidget->itemData(idx).toInt());
273 void TransactionView::changedPrefix(const QString &prefix)
275 if(!transactionProxyModel)
277 transactionProxyModel->setAddressPrefix(prefix);
280 void TransactionView::changedAmount(const QString &amount)
282 if(!transactionProxyModel)
284 qint64 amount_parsed = 0;
285 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
287 transactionProxyModel->setMinAmount(amount_parsed);
291 transactionProxyModel->setMinAmount(0);
295 void TransactionView::exportClicked()
297 // CSV is currently the only supported format
298 QString filename = GUIUtil::getSaveFileName(this,
299 tr("Export Transaction History"), QString(),
300 tr("Comma separated file (*.csv)"), NULL);
302 if (filename.isNull())
305 CSVModelWriter writer(filename);
307 // name, column, role
308 writer.setModel(transactionProxyModel);
309 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
310 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
311 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
312 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
313 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
314 writer.addColumn(BitcoinUnits::getAmountColumnTitle(model->getOptionsModel()->getDisplayUnit()), 0, TransactionTableModel::FormattedAmountRole);
315 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
317 if(!writer.write()) {
318 emit message(tr("Exporting Failed"), tr("There was an error trying to save the transaction history to %1.").arg(filename),
319 CClientUIInterface::MSG_ERROR);
322 emit message(tr("Exporting Successful"), tr("The transaction history was successfully saved to %1.").arg(filename),
323 CClientUIInterface::MSG_INFORMATION);
327 void TransactionView::contextualMenu(const QPoint &point)
329 QModelIndex index = transactionView->indexAt(point);
332 contextMenu->exec(QCursor::pos());
336 void TransactionView::copyAddress()
338 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
341 void TransactionView::copyLabel()
343 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
346 void TransactionView::copyAmount()
348 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
351 void TransactionView::copyTxID()
353 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::TxIDRole);
356 void TransactionView::editLabel()
358 if(!transactionView->selectionModel() ||!model)
360 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
361 if(!selection.isEmpty())
363 AddressTableModel *addressBook = model->getAddressTableModel();
366 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
367 if(address.isEmpty())
369 // If this transaction has no associated address, exit
372 // Is address in address book? Address book can miss address when a transaction is
373 // sent from outside the UI.
374 int idx = addressBook->lookupAddress(address);
377 // Edit sending / receiving address
378 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
379 // Determine type of address, launch appropriate editor dialog type
380 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
382 EditAddressDialog dlg(
383 type == AddressTableModel::Receive
384 ? EditAddressDialog::EditReceivingAddress
385 : EditAddressDialog::EditSendingAddress, this);
386 dlg.setModel(addressBook);
392 // Add sending address
393 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
395 dlg.setModel(addressBook);
396 dlg.setAddress(address);
402 void TransactionView::showDetails()
404 if(!transactionView->selectionModel())
406 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
407 if(!selection.isEmpty())
409 TransactionDescDialog dlg(selection.at(0));
414 void TransactionView::openThirdPartyTxUrl(QString url)
416 if(!transactionView || !transactionView->selectionModel())
418 QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
419 if(!selection.isEmpty())
420 QDesktopServices::openUrl(QUrl::fromUserInput(url.replace("%s", selection.at(0).data(TransactionTableModel::TxHashRole).toString())));
423 QWidget *TransactionView::createDateRangeWidget()
425 dateRangeWidget = new QFrame();
426 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
427 dateRangeWidget->setContentsMargins(1,1,1,1);
428 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
429 layout->setContentsMargins(0,0,0,0);
430 layout->addSpacing(23);
431 layout->addWidget(new QLabel(tr("Range:")));
433 dateFrom = new QDateTimeEdit(this);
434 dateFrom->setDisplayFormat("dd/MM/yy");
435 dateFrom->setCalendarPopup(true);
436 dateFrom->setMinimumWidth(100);
437 dateFrom->setDate(QDate::currentDate().addDays(-7));
438 layout->addWidget(dateFrom);
439 layout->addWidget(new QLabel(tr("to")));
441 dateTo = new QDateTimeEdit(this);
442 dateTo->setDisplayFormat("dd/MM/yy");
443 dateTo->setCalendarPopup(true);
444 dateTo->setMinimumWidth(100);
445 dateTo->setDate(QDate::currentDate());
446 layout->addWidget(dateTo);
447 layout->addStretch();
450 dateRangeWidget->setVisible(false);
453 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
454 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
456 return dateRangeWidget;
459 void TransactionView::dateRangeChanged()
461 if(!transactionProxyModel)
463 transactionProxyModel->setDateRange(
464 QDateTime(dateFrom->date()),
465 QDateTime(dateTo->date()).addDays(1));
468 void TransactionView::focusTransaction(const QModelIndex &idx)
470 if(!transactionProxyModel)
472 QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
473 transactionView->scrollTo(targetIdx);
474 transactionView->setCurrentIndex(targetIdx);
475 transactionView->setFocus();
478 // We override the virtual resizeEvent of the QWidget to adjust tables column
479 // sizes as the tables width is proportional to the dialogs width.
480 void TransactionView::resizeEvent(QResizeEvent* event)
482 QWidget::resizeEvent(event);
483 columnResizingFixer->stretchColumnWidth(TransactionTableModel::ToAddress);
486 // Need to override default Ctrl+C action for amount as default behaviour is just to copy DisplayRole text
487 bool TransactionView::eventFilter(QObject *obj, QEvent *event)
489 if (event->type() == QEvent::KeyPress)
491 QKeyEvent *ke = static_cast<QKeyEvent *>(event);
492 if (ke->key() == Qt::Key_C && ke->modifiers().testFlag(Qt::ControlModifier))
494 QModelIndex i = this->transactionView->currentIndex();
495 if (i.isValid() && i.column() == TransactionTableModel::Amount)
497 GUIUtil::setClipboard(i.data(TransactionTableModel::FormattedAmountRole).toString());
502 return QWidget::eventFilter(obj, event);