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"
17 #include <QDoubleValidator>
18 #include <QHBoxLayout>
19 #include <QVBoxLayout>
22 #include <QHeaderView>
23 #include <QPushButton>
24 #include <QMessageBox>
27 #include <QApplication>
30 #include <QDateTimeEdit>
31 #include <QDesktopServices>
32 #include <QSignalMapper>
35 TransactionView::TransactionView(QWidget *parent) :
36 QWidget(parent), model(0), transactionProxyModel(0),
40 setContentsMargins(0,0,0,0);
42 QHBoxLayout *hlayout = new QHBoxLayout();
43 hlayout->setContentsMargins(0,0,0,0);
45 hlayout->setSpacing(5);
46 hlayout->addSpacing(26);
48 hlayout->setSpacing(0);
49 hlayout->addSpacing(23);
52 dateWidget = new QComboBox(this);
54 dateWidget->setFixedWidth(121);
56 dateWidget->setFixedWidth(120);
58 dateWidget->addItem(tr("All"), All);
59 dateWidget->addItem(tr("Today"), Today);
60 dateWidget->addItem(tr("This week"), ThisWeek);
61 dateWidget->addItem(tr("This month"), ThisMonth);
62 dateWidget->addItem(tr("Last month"), LastMonth);
63 dateWidget->addItem(tr("This year"), ThisYear);
64 dateWidget->addItem(tr("Range..."), Range);
65 hlayout->addWidget(dateWidget);
67 typeWidget = new QComboBox(this);
69 typeWidget->setFixedWidth(121);
71 typeWidget->setFixedWidth(120);
74 typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
75 typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
76 TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
77 typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
78 TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
79 typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
80 typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
81 typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
83 hlayout->addWidget(typeWidget);
85 addressWidget = new QLineEdit(this);
86 #if QT_VERSION >= 0x040700
87 /* Do not move this to the XML file, Qt before 4.7 will choke on it */
88 addressWidget->setPlaceholderText(tr("Enter address or label to search"));
90 hlayout->addWidget(addressWidget);
92 amountWidget = new QLineEdit(this);
93 #if QT_VERSION >= 0x040700
94 /* Do not move this to the XML file, Qt before 4.7 will choke on it */
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 transactionView = view;
129 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
130 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
131 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
132 QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
133 QAction *editLabelAction = new QAction(tr("Edit label"), this);
134 QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
136 contextMenu = new QMenu();
137 contextMenu->addAction(copyAddressAction);
138 contextMenu->addAction(copyLabelAction);
139 contextMenu->addAction(copyAmountAction);
140 contextMenu->addAction(copyTxIDAction);
141 contextMenu->addAction(editLabelAction);
142 contextMenu->addAction(showDetailsAction);
144 mapperThirdPartyTxUrls = new QSignalMapper(this);
147 connect(mapperThirdPartyTxUrls, SIGNAL(mapped(QString)), this, SLOT(openThirdPartyTxUrl(QString)));
149 connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
150 connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
151 connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
152 connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
154 connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
155 connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
157 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
158 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
159 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
160 connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
161 connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
162 connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
165 void TransactionView::setModel(WalletModel *model)
170 transactionProxyModel = new TransactionFilterProxy(this);
171 transactionProxyModel->setSourceModel(model->getTransactionTableModel());
172 transactionProxyModel->setDynamicSortFilter(true);
173 transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
174 transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
176 // transactionProxyModel->setSortRole(Qt::EditRole);
177 transactionProxyModel->setSortRole(TransactionTableModel::DateRole);
179 transactionView->setModel(transactionProxyModel);
180 transactionView->setAlternatingRowColors(true);
181 transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
182 transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
183 transactionView->setSortingEnabled(true);
184 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
185 transactionView->verticalHeader()->hide();
187 transactionView->horizontalHeader()->resizeSection(
188 TransactionTableModel::Status, 23);
189 transactionView->horizontalHeader()->resizeSection(
190 TransactionTableModel::Date, 120);
191 transactionView->horizontalHeader()->resizeSection(
192 TransactionTableModel::Type, 120);
193 #if QT_VERSION < 0x050000
194 transactionView->horizontalHeader()->setResizeMode(
195 TransactionTableModel::ToAddress, QHeaderView::Stretch);
197 transactionView->horizontalHeader()->setSectionResizeMode(TransactionTableModel::ToAddress, QHeaderView::Stretch);
199 transactionView->horizontalHeader()->resizeSection(
200 TransactionTableModel::Amount, 100);
202 if (model->getOptionsModel())
204 // Add third party transaction URLs to context menu
205 QStringList listUrls = model->getOptionsModel()->getThirdPartyTxUrls().split("|", QString::SkipEmptyParts);
206 for (int i = 0; i < listUrls.size(); ++i)
208 QString host = QUrl(listUrls[i].trimmed(), QUrl::StrictMode).host();
211 QAction *thirdPartyTxUrlAction = new QAction(host, this); // use host as menu item label
213 contextMenu->addSeparator();
214 contextMenu->addAction(thirdPartyTxUrlAction);
215 connect(thirdPartyTxUrlAction, SIGNAL(triggered()), mapperThirdPartyTxUrls, SLOT(map()));
216 mapperThirdPartyTxUrls->setMapping(thirdPartyTxUrlAction, listUrls[i].trimmed());
223 void TransactionView::chooseDate(int idx)
225 if(!transactionProxyModel)
227 QDate current = QDate::currentDate();
228 dateRangeWidget->setVisible(false);
229 switch(dateWidget->itemData(idx).toInt())
232 transactionProxyModel->setDateRange(
233 TransactionFilterProxy::MIN_DATE,
234 TransactionFilterProxy::MAX_DATE);
237 transactionProxyModel->setDateRange(
239 TransactionFilterProxy::MAX_DATE);
243 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
244 transactionProxyModel->setDateRange(
245 QDateTime(startOfWeek),
246 TransactionFilterProxy::MAX_DATE);
250 transactionProxyModel->setDateRange(
251 QDateTime(QDate(current.year(), current.month(), 1)),
252 TransactionFilterProxy::MAX_DATE);
255 transactionProxyModel->setDateRange(
256 QDateTime(QDate(current.year(), current.month()-1, 1)),
257 QDateTime(QDate(current.year(), current.month(), 1)));
260 transactionProxyModel->setDateRange(
261 QDateTime(QDate(current.year(), 1, 1)),
262 TransactionFilterProxy::MAX_DATE);
265 dateRangeWidget->setVisible(true);
271 void TransactionView::chooseType(int idx)
273 if(!transactionProxyModel)
275 transactionProxyModel->setTypeFilter(
276 typeWidget->itemData(idx).toInt());
279 void TransactionView::changedPrefix(const QString &prefix)
281 if(!transactionProxyModel)
283 transactionProxyModel->setAddressPrefix(prefix);
286 void TransactionView::changedAmount(const QString &amount)
288 if(!transactionProxyModel)
290 qint64 amount_parsed = 0;
291 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
293 transactionProxyModel->setMinAmount(amount_parsed);
297 transactionProxyModel->setMinAmount(0);
301 void TransactionView::exportClicked()
303 // CSV is currently the only supported format
304 QString filename = GUIUtil::getSaveFileName(
306 tr("Export Transaction Data"), QString(),
307 tr("Comma separated file (*.csv)"));
309 if (filename.isNull()) return;
311 CSVModelWriter writer(filename);
313 // name, column, role
314 writer.setModel(transactionProxyModel);
315 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
316 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
317 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
318 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
319 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
320 writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
321 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
325 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
326 QMessageBox::Abort, QMessageBox::Abort);
330 void TransactionView::contextualMenu(const QPoint &point)
332 QModelIndex index = transactionView->indexAt(point);
335 contextMenu->exec(QCursor::pos());
339 void TransactionView::copyAddress()
341 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
344 void TransactionView::copyLabel()
346 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
349 void TransactionView::copyAmount()
351 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
354 void TransactionView::copyTxID()
356 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::TxIDRole);
359 void TransactionView::editLabel()
361 if(!transactionView->selectionModel() ||!model)
363 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
364 if(!selection.isEmpty())
366 AddressTableModel *addressBook = model->getAddressTableModel();
369 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
370 if(address.isEmpty())
372 // If this transaction has no associated address, exit
375 // Is address in address book? Address book can miss address when a transaction is
376 // sent from outside the UI.
377 int idx = addressBook->lookupAddress(address);
380 // Edit sending / receiving address
381 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
382 // Determine type of address, launch appropriate editor dialog type
383 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
385 EditAddressDialog dlg(type==AddressTableModel::Receive
386 ? EditAddressDialog::EditReceivingAddress
387 : EditAddressDialog::EditSendingAddress,
389 dlg.setModel(addressBook);
395 // Add sending address
396 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
398 dlg.setModel(addressBook);
399 dlg.setAddress(address);
405 void TransactionView::showDetails()
407 if(!transactionView->selectionModel())
409 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
410 if(!selection.isEmpty())
412 TransactionDescDialog dlg(selection.at(0));
417 void TransactionView::openThirdPartyTxUrl(QString url)
419 if(!transactionView->selectionModel())
421 QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
422 if(!selection.isEmpty())
423 QDesktopServices::openUrl(QUrl::fromUserInput(url.replace("%s", selection.at(0).data(TransactionTableModel::TxHashRole).toString())));
426 QWidget *TransactionView::createDateRangeWidget()
428 dateRangeWidget = new QFrame();
429 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
430 dateRangeWidget->setContentsMargins(1,1,1,1);
431 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
432 layout->setContentsMargins(0,0,0,0);
433 layout->addSpacing(23);
434 layout->addWidget(new QLabel(tr("Range:")));
436 dateFrom = new QDateTimeEdit(this);
437 dateFrom->setDisplayFormat("dd/MM/yy");
438 dateFrom->setCalendarPopup(true);
439 dateFrom->setMinimumWidth(100);
440 dateFrom->setDate(QDate::currentDate().addDays(-7));
441 layout->addWidget(dateFrom);
442 layout->addWidget(new QLabel(tr("to")));
444 dateTo = new QDateTimeEdit(this);
445 dateTo->setDisplayFormat("dd/MM/yy");
446 dateTo->setCalendarPopup(true);
447 dateTo->setMinimumWidth(100);
448 dateTo->setDate(QDate::currentDate());
449 layout->addWidget(dateTo);
450 layout->addStretch();
453 dateRangeWidget->setVisible(false);
456 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
457 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
459 return dateRangeWidget;
462 void TransactionView::dateRangeChanged()
464 if(!transactionProxyModel)
466 transactionProxyModel->setDateRange(
467 QDateTime(dateFrom->date()),
468 QDateTime(dateTo->date()).addDays(1));
471 void TransactionView::focusTransaction(const QModelIndex &idx)
473 if(!transactionProxyModel)
475 QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
476 transactionView->scrollTo(targetIdx);
477 transactionView->setCurrentIndex(targetIdx);
478 transactionView->setFocus();