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 <QDoubleValidator>
24 #include <QHBoxLayout>
25 #include <QHeaderView>
32 #include <QVBoxLayout>
34 TransactionView::TransactionView(QWidget *parent) :
35 QWidget(parent), model(0), transactionProxyModel(0),
39 setContentsMargins(0,0,0,0);
41 QHBoxLayout *hlayout = new QHBoxLayout();
42 hlayout->setContentsMargins(0,0,0,0);
44 hlayout->setSpacing(5);
45 hlayout->addSpacing(26);
47 hlayout->setSpacing(0);
48 hlayout->addSpacing(23);
51 dateWidget = new QComboBox(this);
53 dateWidget->setFixedWidth(121);
55 dateWidget->setFixedWidth(120);
57 dateWidget->addItem(tr("All"), All);
58 dateWidget->addItem(tr("Today"), Today);
59 dateWidget->addItem(tr("This week"), ThisWeek);
60 dateWidget->addItem(tr("This month"), ThisMonth);
61 dateWidget->addItem(tr("Last month"), LastMonth);
62 dateWidget->addItem(tr("This year"), ThisYear);
63 dateWidget->addItem(tr("Range..."), Range);
64 hlayout->addWidget(dateWidget);
66 typeWidget = new QComboBox(this);
68 typeWidget->setFixedWidth(121);
70 typeWidget->setFixedWidth(120);
73 typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
74 typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
75 TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
76 typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
77 TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
78 typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
79 typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
80 typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
82 hlayout->addWidget(typeWidget);
84 addressWidget = new QLineEdit(this);
85 #if QT_VERSION >= 0x040700
86 addressWidget->setPlaceholderText(tr("Enter address or label to search"));
88 hlayout->addWidget(addressWidget);
90 amountWidget = new QLineEdit(this);
91 #if QT_VERSION >= 0x040700
92 amountWidget->setPlaceholderText(tr("Min amount"));
95 amountWidget->setFixedWidth(97);
97 amountWidget->setFixedWidth(100);
99 amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
100 hlayout->addWidget(amountWidget);
102 QVBoxLayout *vlayout = new QVBoxLayout(this);
103 vlayout->setContentsMargins(0,0,0,0);
104 vlayout->setSpacing(0);
106 QTableView *view = new QTableView(this);
107 vlayout->addLayout(hlayout);
108 vlayout->addWidget(createDateRangeWidget());
109 vlayout->addWidget(view);
110 vlayout->setSpacing(0);
111 int width = view->verticalScrollBar()->sizeHint().width();
112 // Cover scroll bar width with spacing
114 hlayout->addSpacing(width+2);
116 hlayout->addSpacing(width);
118 // Always show scroll bar
119 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
120 view->setTabKeyNavigation(false);
121 view->setContextMenuPolicy(Qt::CustomContextMenu);
123 transactionView = view;
126 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
127 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
128 QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
129 QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
130 QAction *editLabelAction = new QAction(tr("Edit label"), this);
131 QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
133 contextMenu = new QMenu();
134 contextMenu->addAction(copyAddressAction);
135 contextMenu->addAction(copyLabelAction);
136 contextMenu->addAction(copyAmountAction);
137 contextMenu->addAction(copyTxIDAction);
138 contextMenu->addAction(editLabelAction);
139 contextMenu->addAction(showDetailsAction);
142 connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
143 connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
144 connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
145 connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
147 connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
148 connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
150 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
151 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
152 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
153 connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
154 connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
155 connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
158 void TransactionView::setModel(WalletModel *model)
163 transactionProxyModel = new TransactionFilterProxy(this);
164 transactionProxyModel->setSourceModel(model->getTransactionTableModel());
165 transactionProxyModel->setDynamicSortFilter(true);
166 transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
167 transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
169 transactionProxyModel->setSortRole(Qt::EditRole);
171 transactionView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
172 transactionView->setModel(transactionProxyModel);
173 transactionView->setAlternatingRowColors(true);
174 transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
175 transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
176 transactionView->setSortingEnabled(true);
177 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
178 transactionView->verticalHeader()->hide();
180 transactionView->setColumnWidth(TransactionTableModel::Status, STATUS_COLUMN_WIDTH);
181 transactionView->setColumnWidth(TransactionTableModel::Date, DATE_COLUMN_WIDTH);
182 transactionView->setColumnWidth(TransactionTableModel::Type, TYPE_COLUMN_WIDTH);
183 transactionView->setColumnWidth(TransactionTableModel::Amount, AMOUNT_MINIMUM_COLUMN_WIDTH);
185 columnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(transactionView, AMOUNT_MINIMUM_COLUMN_WIDTH, MINIMUM_COLUMN_WIDTH);
189 void TransactionView::chooseDate(int idx)
191 if(!transactionProxyModel)
193 QDate current = QDate::currentDate();
194 dateRangeWidget->setVisible(false);
195 switch(dateWidget->itemData(idx).toInt())
198 transactionProxyModel->setDateRange(
199 TransactionFilterProxy::MIN_DATE,
200 TransactionFilterProxy::MAX_DATE);
203 transactionProxyModel->setDateRange(
205 TransactionFilterProxy::MAX_DATE);
209 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
210 transactionProxyModel->setDateRange(
211 QDateTime(startOfWeek),
212 TransactionFilterProxy::MAX_DATE);
216 transactionProxyModel->setDateRange(
217 QDateTime(QDate(current.year(), current.month(), 1)),
218 TransactionFilterProxy::MAX_DATE);
221 transactionProxyModel->setDateRange(
222 QDateTime(QDate(current.year(), current.month()-1, 1)),
223 QDateTime(QDate(current.year(), current.month(), 1)));
226 transactionProxyModel->setDateRange(
227 QDateTime(QDate(current.year(), 1, 1)),
228 TransactionFilterProxy::MAX_DATE);
231 dateRangeWidget->setVisible(true);
237 void TransactionView::chooseType(int idx)
239 if(!transactionProxyModel)
241 transactionProxyModel->setTypeFilter(
242 typeWidget->itemData(idx).toInt());
245 void TransactionView::changedPrefix(const QString &prefix)
247 if(!transactionProxyModel)
249 transactionProxyModel->setAddressPrefix(prefix);
252 void TransactionView::changedAmount(const QString &amount)
254 if(!transactionProxyModel)
256 qint64 amount_parsed = 0;
257 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
259 transactionProxyModel->setMinAmount(amount_parsed);
263 transactionProxyModel->setMinAmount(0);
267 void TransactionView::exportClicked()
269 // CSV is currently the only supported format
270 QString filename = GUIUtil::getSaveFileName(this,
271 tr("Export Transaction History"), QString(),
272 tr("Comma separated file (*.csv)"), NULL);
274 if (filename.isNull())
277 CSVModelWriter writer(filename);
279 // name, column, role
280 writer.setModel(transactionProxyModel);
281 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
282 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
283 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
284 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
285 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
286 writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
287 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
289 if(!writer.write()) {
290 emit message(tr("Exporting Failed"), tr("There was an error trying to save the transaction history to %1.").arg(filename),
291 CClientUIInterface::MSG_ERROR);
294 emit message(tr("Exporting Successful"), tr("The transaction history was successfully saved to %1.").arg(filename),
295 CClientUIInterface::MSG_INFORMATION);
299 void TransactionView::contextualMenu(const QPoint &point)
301 QModelIndex index = transactionView->indexAt(point);
304 contextMenu->exec(QCursor::pos());
308 void TransactionView::copyAddress()
310 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
313 void TransactionView::copyLabel()
315 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
318 void TransactionView::copyAmount()
320 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
323 void TransactionView::copyTxID()
325 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::TxIDRole);
328 void TransactionView::editLabel()
330 if(!transactionView->selectionModel() ||!model)
332 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
333 if(!selection.isEmpty())
335 AddressTableModel *addressBook = model->getAddressTableModel();
338 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
339 if(address.isEmpty())
341 // If this transaction has no associated address, exit
344 // Is address in address book? Address book can miss address when a transaction is
345 // sent from outside the UI.
346 int idx = addressBook->lookupAddress(address);
349 // Edit sending / receiving address
350 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
351 // Determine type of address, launch appropriate editor dialog type
352 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
354 EditAddressDialog dlg(
355 type == AddressTableModel::Receive
356 ? EditAddressDialog::EditReceivingAddress
357 : EditAddressDialog::EditSendingAddress, this);
358 dlg.setModel(addressBook);
364 // Add sending address
365 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
367 dlg.setModel(addressBook);
368 dlg.setAddress(address);
374 void TransactionView::showDetails()
376 if(!transactionView->selectionModel())
378 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
379 if(!selection.isEmpty())
381 TransactionDescDialog dlg(selection.at(0));
386 QWidget *TransactionView::createDateRangeWidget()
388 dateRangeWidget = new QFrame();
389 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
390 dateRangeWidget->setContentsMargins(1,1,1,1);
391 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
392 layout->setContentsMargins(0,0,0,0);
393 layout->addSpacing(23);
394 layout->addWidget(new QLabel(tr("Range:")));
396 dateFrom = new QDateTimeEdit(this);
397 dateFrom->setDisplayFormat("dd/MM/yy");
398 dateFrom->setCalendarPopup(true);
399 dateFrom->setMinimumWidth(100);
400 dateFrom->setDate(QDate::currentDate().addDays(-7));
401 layout->addWidget(dateFrom);
402 layout->addWidget(new QLabel(tr("to")));
404 dateTo = new QDateTimeEdit(this);
405 dateTo->setDisplayFormat("dd/MM/yy");
406 dateTo->setCalendarPopup(true);
407 dateTo->setMinimumWidth(100);
408 dateTo->setDate(QDate::currentDate());
409 layout->addWidget(dateTo);
410 layout->addStretch();
413 dateRangeWidget->setVisible(false);
416 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
417 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
419 return dateRangeWidget;
422 void TransactionView::dateRangeChanged()
424 if(!transactionProxyModel)
426 transactionProxyModel->setDateRange(
427 QDateTime(dateFrom->date()),
428 QDateTime(dateTo->date()).addDays(1));
431 void TransactionView::focusTransaction(const QModelIndex &idx)
433 if(!transactionProxyModel)
435 QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
436 transactionView->scrollTo(targetIdx);
437 transactionView->setCurrentIndex(targetIdx);
438 transactionView->setFocus();
441 // We override the virtual resizeEvent of the QWidget to adjust tables column
442 // sizes as the tables width is proportional to the dialogs width.
443 void TransactionView::resizeEvent(QResizeEvent* event)
445 QWidget::resizeEvent(event);
446 columnResizingFixer->stretchColumnWidth(TransactionTableModel::ToAddress);