1 #include "transactionview.h"
3 #include "transactionfilterproxy.h"
4 #include "transactionrecord.h"
5 #include "walletmodel.h"
6 #include "addresstablemodel.h"
7 #include "transactiontablemodel.h"
8 #include "bitcoinunits.h"
9 #include "csvmodelwriter.h"
10 #include "transactiondescdialog.h"
11 #include "editaddressdialog.h"
12 #include "optionsmodel.h"
18 #include <QDoubleValidator>
19 #include <QHBoxLayout>
20 #include <QVBoxLayout>
23 #include <QHeaderView>
24 #include <QPushButton>
25 #include <QMessageBox>
28 #include <QApplication>
31 #include <QDateTimeEdit>
32 #include <QDesktopServices>
33 #include <QSignalMapper>
36 TransactionView::TransactionView(QWidget *parent) :
37 QWidget(parent), model(0), transactionProxyModel(0),
41 setContentsMargins(0,0,0,0);
43 QHBoxLayout *hlayout = new QHBoxLayout();
44 hlayout->setContentsMargins(0,0,0,0);
46 hlayout->setSpacing(5);
47 hlayout->addSpacing(26);
49 hlayout->setSpacing(0);
50 hlayout->addSpacing(23);
53 dateWidget = new QComboBox(this);
55 dateWidget->setFixedWidth(121);
57 dateWidget->setFixedWidth(120);
59 dateWidget->addItem(tr("All"), All);
60 dateWidget->addItem(tr("Today"), Today);
61 dateWidget->addItem(tr("This week"), ThisWeek);
62 dateWidget->addItem(tr("This month"), ThisMonth);
63 dateWidget->addItem(tr("Last month"), LastMonth);
64 dateWidget->addItem(tr("This year"), ThisYear);
65 dateWidget->addItem(tr("Range..."), Range);
66 hlayout->addWidget(dateWidget);
68 typeWidget = new QComboBox(this);
70 typeWidget->setFixedWidth(121);
72 typeWidget->setFixedWidth(120);
75 typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
76 typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
77 TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
78 typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
79 TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
80 typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
81 typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
82 typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
84 hlayout->addWidget(typeWidget);
86 addressWidget = new QLineEdit(this);
87 #if QT_VERSION >= 0x040700
88 /* Do not move this to the XML file, Qt before 4.7 will choke on it */
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 /* Do not move this to the XML file, Qt before 4.7 will choke on it */
96 amountWidget->setPlaceholderText(tr("Min amount"));
99 amountWidget->setFixedWidth(97);
101 amountWidget->setFixedWidth(100);
103 amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
104 hlayout->addWidget(amountWidget);
106 QVBoxLayout *vlayout = new QVBoxLayout(this);
107 vlayout->setContentsMargins(0,0,0,0);
108 vlayout->setSpacing(0);
110 QTableView *view = new QTableView(this);
111 vlayout->addLayout(hlayout);
112 vlayout->addWidget(createDateRangeWidget());
113 vlayout->addWidget(view);
114 vlayout->setSpacing(0);
115 int width = view->verticalScrollBar()->sizeHint().width();
116 // Cover scroll bar width with spacing
118 hlayout->addSpacing(width+2);
120 hlayout->addSpacing(width);
122 // Always show scroll bar
123 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
124 view->setTabKeyNavigation(false);
125 view->setContextMenuPolicy(Qt::CustomContextMenu);
127 transactionView = view;
130 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
131 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
132 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
133 QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
134 QAction *editLabelAction = new QAction(tr("Edit label"), this);
135 QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
136 QAction *clearOrphansAction = new QAction(tr("Clear orphans"), 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);
145 contextMenu->addSeparator();
146 contextMenu->addAction(clearOrphansAction);
148 mapperThirdPartyTxUrls = new QSignalMapper(this);
151 connect(mapperThirdPartyTxUrls, SIGNAL(mapped(QString)), this, SLOT(openThirdPartyTxUrl(QString)));
153 connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
154 connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
155 connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
156 connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
158 connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
159 connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
161 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
162 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
163 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
164 connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
165 connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
166 connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
167 connect(clearOrphansAction, SIGNAL(triggered()), this, SLOT(clearOrphans()));
170 void TransactionView::setModel(WalletModel *model, bool fShoudAddThirdPartyURL)
175 transactionProxyModel = new TransactionFilterProxy(this);
176 transactionProxyModel->setSourceModel(model->getTransactionTableModel());
177 transactionProxyModel->setDynamicSortFilter(true);
178 transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
179 transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
181 // transactionProxyModel->setSortRole(Qt::EditRole);
182 transactionProxyModel->setSortRole(TransactionTableModel::DateRole);
184 transactionView->setModel(transactionProxyModel);
185 transactionView->setAlternatingRowColors(true);
186 transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
187 transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
188 transactionView->setSortingEnabled(true);
189 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
190 transactionView->verticalHeader()->hide();
192 transactionView->horizontalHeader()->resizeSection(
193 TransactionTableModel::Status, 23);
194 transactionView->horizontalHeader()->resizeSection(
195 TransactionTableModel::Date, 120);
196 transactionView->horizontalHeader()->resizeSection(
197 TransactionTableModel::Type, 120);
198 #if QT_VERSION < 0x050000
199 transactionView->horizontalHeader()->setResizeMode(
200 TransactionTableModel::ToAddress, QHeaderView::Stretch);
202 transactionView->horizontalHeader()->setSectionResizeMode(TransactionTableModel::ToAddress, QHeaderView::Stretch);
204 transactionView->horizontalHeader()->resizeSection(
205 TransactionTableModel::Amount, 100);
207 if (model->getOptionsModel() && fShoudAddThirdPartyURL)
210 // Add third party transaction URLs to context menu
211 QStringList listUrls = model->getOptionsModel()->getThirdPartyTxUrls().split("|", QString::SkipEmptyParts);
212 for (int i = 0; i < listUrls.size(); ++i)
214 QString host = QUrl(listUrls[i].trimmed(), QUrl::StrictMode).host();
217 QAction *thirdPartyTxUrlAction = new QAction(host, this); // use host as menu item label
219 contextMenu->addSeparator();
220 contextMenu->addAction(thirdPartyTxUrlAction);
221 connect(thirdPartyTxUrlAction, SIGNAL(triggered()), mapperThirdPartyTxUrls, SLOT(map()));
222 mapperThirdPartyTxUrls->setMapping(thirdPartyTxUrlAction, listUrls[i].trimmed());
229 void TransactionView::chooseDate(int idx)
231 if(!transactionProxyModel)
233 QDate current = QDate::currentDate();
234 dateRangeWidget->setVisible(false);
235 switch(dateWidget->itemData(idx).toInt())
238 transactionProxyModel->setDateRange(
239 TransactionFilterProxy::MIN_DATE,
240 TransactionFilterProxy::MAX_DATE);
243 transactionProxyModel->setDateRange(
245 TransactionFilterProxy::MAX_DATE);
249 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
250 transactionProxyModel->setDateRange(
251 QDateTime(startOfWeek),
252 TransactionFilterProxy::MAX_DATE);
256 transactionProxyModel->setDateRange(
257 QDateTime(QDate(current.year(), current.month(), 1)),
258 TransactionFilterProxy::MAX_DATE);
261 transactionProxyModel->setDateRange(
262 QDateTime(QDate(current.year(), current.month()-1, 1)),
263 QDateTime(QDate(current.year(), current.month(), 1)));
266 transactionProxyModel->setDateRange(
267 QDateTime(QDate(current.year(), 1, 1)),
268 TransactionFilterProxy::MAX_DATE);
271 dateRangeWidget->setVisible(true);
277 void TransactionView::chooseType(int idx)
279 if(!transactionProxyModel)
281 transactionProxyModel->setTypeFilter(
282 typeWidget->itemData(idx).toInt());
285 void TransactionView::changedPrefix(const QString &prefix)
287 if(!transactionProxyModel)
289 transactionProxyModel->setAddressPrefix(prefix);
292 void TransactionView::changedAmount(const QString &amount)
294 if(!transactionProxyModel)
296 qint64 amount_parsed = 0;
297 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
299 transactionProxyModel->setMinAmount(amount_parsed);
303 transactionProxyModel->setMinAmount(0);
307 void TransactionView::exportClicked()
309 // CSV is currently the only supported format
310 QString filename = GUIUtil::getSaveFileName(
312 tr("Export Transaction Data"), QString(),
313 tr("Comma separated file (*.csv)"));
315 if (filename.isNull()) return;
317 CSVModelWriter writer(filename);
319 // name, column, role
320 writer.setModel(transactionProxyModel);
321 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
322 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
323 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
324 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
325 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
326 writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
327 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
331 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
332 QMessageBox::Abort, QMessageBox::Abort);
336 void TransactionView::contextualMenu(const QPoint &point)
338 QModelIndex index = transactionView->indexAt(point);
341 contextMenu->exec(QCursor::pos());
345 void TransactionView::copyAddress()
347 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
350 void TransactionView::copyLabel()
352 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
355 void TransactionView::copyAmount()
357 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
360 void TransactionView::copyTxID()
362 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::TxIDRole);
365 void TransactionView::editLabel()
367 if(!transactionView->selectionModel() ||!model)
369 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
370 if(!selection.isEmpty())
372 AddressTableModel *addressBook = model->getAddressTableModel();
375 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
376 if(address.isEmpty())
378 // If this transaction has no associated address, exit
381 // Is address in address book? Address book can miss address when a transaction is
382 // sent from outside the UI.
383 int idx = addressBook->lookupAddress(address);
386 // Edit sending / receiving address
387 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
388 // Determine type of address, launch appropriate editor dialog type
389 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
391 EditAddressDialog dlg(type==AddressTableModel::Receive
392 ? EditAddressDialog::EditReceivingAddress
393 : EditAddressDialog::EditSendingAddress,
395 dlg.setModel(addressBook);
401 // Add sending address
402 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
404 dlg.setModel(addressBook);
405 dlg.setAddress(address);
411 void TransactionView::showDetails()
413 if(!transactionView->selectionModel())
415 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
416 if(!selection.isEmpty())
418 TransactionDescDialog dlg(selection.at(0));
423 void TransactionView::clearOrphans()
428 model->clearOrphans();
429 model->getTransactionTableModel()->refresh();
430 delete transactionProxyModel;
431 setModel(model, false);
432 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
433 transactionView->sortByColumn(TransactionTableModel::Date, Qt::DescendingOrder);
436 void TransactionView::openThirdPartyTxUrl(QString url)
438 if(!transactionView->selectionModel())
440 QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
441 if(!selection.isEmpty())
442 QDesktopServices::openUrl(QUrl::fromUserInput(url.replace("%s", selection.at(0).data(TransactionTableModel::TxHashRole).toString())));
445 QWidget *TransactionView::createDateRangeWidget()
447 dateRangeWidget = new QFrame();
448 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
449 dateRangeWidget->setContentsMargins(1,1,1,1);
450 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
451 layout->setContentsMargins(0,0,0,0);
452 layout->addSpacing(23);
453 layout->addWidget(new QLabel(tr("Range:")));
455 dateFrom = new QDateTimeEdit(this);
456 dateFrom->setDisplayFormat("dd/MM/yy");
457 dateFrom->setCalendarPopup(true);
458 dateFrom->setMinimumWidth(100);
459 dateFrom->setDate(QDate::currentDate().addDays(-7));
460 layout->addWidget(dateFrom);
461 layout->addWidget(new QLabel(tr("to")));
463 dateTo = new QDateTimeEdit(this);
464 dateTo->setDisplayFormat("dd/MM/yy");
465 dateTo->setCalendarPopup(true);
466 dateTo->setMinimumWidth(100);
467 dateTo->setDate(QDate::currentDate());
468 layout->addWidget(dateTo);
469 layout->addStretch();
472 dateRangeWidget->setVisible(false);
475 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
476 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
478 return dateRangeWidget;
481 void TransactionView::dateRangeChanged()
483 if(!transactionProxyModel)
485 transactionProxyModel->setDateRange(
486 QDateTime(dateFrom->date()),
487 QDateTime(dateTo->date()).addDays(1));
490 void TransactionView::focusTransaction(const QModelIndex &idx)
492 if(!transactionProxyModel)
494 QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
495 transactionView->scrollTo(targetIdx);
496 transactionView->setCurrentIndex(targetIdx);
497 transactionView->setFocus();