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"
16 #include <QDoubleValidator>
17 #include <QHBoxLayout>
18 #include <QVBoxLayout>
21 #include <QHeaderView>
22 #include <QPushButton>
23 #include <QFileDialog>
24 #include <QMessageBox>
27 #include <QApplication>
30 #include <QDateTimeEdit>
32 TransactionView::TransactionView(QWidget *parent) :
33 QWidget(parent), model(0), transactionProxyModel(0),
37 setContentsMargins(0,0,0,0);
39 QHBoxLayout *hlayout = new QHBoxLayout();
40 hlayout->setContentsMargins(0,0,0,0);
42 hlayout->setSpacing(5);
43 hlayout->addSpacing(26);
45 hlayout->setSpacing(0);
46 hlayout->addSpacing(23);
49 dateWidget = new QComboBox(this);
51 dateWidget->setFixedWidth(121);
53 dateWidget->setFixedWidth(120);
55 dateWidget->addItem(tr("All"), All);
56 dateWidget->addItem(tr("Today"), Today);
57 dateWidget->addItem(tr("This week"), ThisWeek);
58 dateWidget->addItem(tr("This month"), ThisMonth);
59 dateWidget->addItem(tr("Last month"), LastMonth);
60 dateWidget->addItem(tr("This year"), ThisYear);
61 dateWidget->addItem(tr("Range..."), Range);
62 hlayout->addWidget(dateWidget);
64 typeWidget = new QComboBox(this);
66 typeWidget->setFixedWidth(121);
68 typeWidget->setFixedWidth(120);
71 typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
72 typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
73 TransactionFilterProxy::TYPE(TransactionRecord::RecvFromIP));
74 typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
75 TransactionFilterProxy::TYPE(TransactionRecord::SendToIP));
76 typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
77 typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
78 typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
80 hlayout->addWidget(typeWidget);
82 addressWidget = new QLineEdit(this);
83 #if QT_VERSION >= 0x040700
84 addressWidget->setPlaceholderText(tr("Enter address or label to search"));
86 hlayout->addWidget(addressWidget);
88 amountWidget = new QLineEdit(this);
89 #if QT_VERSION >= 0x040700
90 amountWidget->setPlaceholderText(tr("Min amount"));
93 amountWidget->setFixedWidth(97);
95 amountWidget->setFixedWidth(100);
97 amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
98 hlayout->addWidget(amountWidget);
100 QVBoxLayout *vlayout = new QVBoxLayout(this);
101 vlayout->setContentsMargins(0,0,0,0);
102 vlayout->setSpacing(0);
103 //vlayout->addLayout(hlayout2);
105 QTableView *view = new QTableView(this);
106 vlayout->addLayout(hlayout);
107 vlayout->addWidget(createDateRangeWidget());
108 vlayout->addWidget(view);
109 vlayout->setSpacing(0);
110 int width = view->verticalScrollBar()->sizeHint().width();
111 // Cover scroll bar width with spacing
113 hlayout->addSpacing(width+2);
115 hlayout->addSpacing(width);
117 // Always show scroll bar
118 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
119 view->setTabKeyNavigation(false);
120 view->setContextMenuPolicy(Qt::CustomContextMenu);
122 transactionView = view;
125 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
126 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
127 QAction *editLabelAction = new QAction(tr("Edit label"), this);
128 QAction *showDetailsAction = new QAction(tr("Show details..."), this);
130 contextMenu = new QMenu();
131 contextMenu->addAction(copyAddressAction);
132 contextMenu->addAction(copyLabelAction);
133 contextMenu->addAction(editLabelAction);
134 contextMenu->addAction(showDetailsAction);
137 connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
138 connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
139 connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
140 connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
142 connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
145 SIGNAL(customContextMenuRequested(QPoint)),
147 SLOT(contextualMenu(QPoint)));
149 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
150 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
151 connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
152 connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
155 void TransactionView::setModel(WalletModel *model)
160 transactionProxyModel = new TransactionFilterProxy(this);
161 transactionProxyModel->setSourceModel(model->getTransactionTableModel());
162 transactionProxyModel->setDynamicSortFilter(true);
164 transactionProxyModel->setSortRole(Qt::EditRole);
166 transactionView->setModel(transactionProxyModel);
167 transactionView->setAlternatingRowColors(true);
168 transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
169 transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
170 transactionView->setSortingEnabled(true);
171 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
172 transactionView->verticalHeader()->hide();
174 transactionView->horizontalHeader()->resizeSection(
175 TransactionTableModel::Status, 23);
176 transactionView->horizontalHeader()->resizeSection(
177 TransactionTableModel::Date, 120);
178 transactionView->horizontalHeader()->resizeSection(
179 TransactionTableModel::Type, 120);
180 transactionView->horizontalHeader()->setResizeMode(
181 TransactionTableModel::ToAddress, QHeaderView::Stretch);
182 transactionView->horizontalHeader()->resizeSection(
183 TransactionTableModel::Amount, 100);
187 void TransactionView::chooseDate(int idx)
189 if(!transactionProxyModel)
191 QDate current = QDate::currentDate();
192 dateRangeWidget->setVisible(false);
193 switch(dateWidget->itemData(idx).toInt())
196 transactionProxyModel->setDateRange(
197 TransactionFilterProxy::MIN_DATE,
198 TransactionFilterProxy::MAX_DATE);
201 transactionProxyModel->setDateRange(
203 TransactionFilterProxy::MAX_DATE);
207 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
208 transactionProxyModel->setDateRange(
209 QDateTime(startOfWeek),
210 TransactionFilterProxy::MAX_DATE);
214 transactionProxyModel->setDateRange(
215 QDateTime(QDate(current.year(), current.month(), 1)),
216 TransactionFilterProxy::MAX_DATE);
219 transactionProxyModel->setDateRange(
220 QDateTime(QDate(current.year(), current.month()-1, 1)),
221 QDateTime(QDate(current.year(), current.month(), 1)));
224 transactionProxyModel->setDateRange(
225 QDateTime(QDate(current.year(), 1, 1)),
226 TransactionFilterProxy::MAX_DATE);
229 dateRangeWidget->setVisible(true);
235 void TransactionView::chooseType(int idx)
237 if(!transactionProxyModel)
239 transactionProxyModel->setTypeFilter(
240 typeWidget->itemData(idx).toInt());
243 void TransactionView::changedPrefix(const QString &prefix)
245 if(!transactionProxyModel)
247 transactionProxyModel->setAddressPrefix(prefix);
250 void TransactionView::changedAmount(const QString &amount)
252 if(!transactionProxyModel)
254 qint64 amount_parsed = 0;
255 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
257 transactionProxyModel->setMinAmount(amount_parsed);
261 transactionProxyModel->setMinAmount(0);
265 void TransactionView::exportClicked()
267 // CSV is currently the only supported format
268 QString filename = QFileDialog::getSaveFileName(
270 tr("Export Transaction Data"),
272 tr("Comma separated file (*.csv)"));
274 if (filename.isNull()) return;
276 CSVModelWriter writer(filename);
278 // name, column, role
279 writer.setModel(transactionProxyModel);
280 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
281 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
282 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
283 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
284 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
285 writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
286 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
290 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
291 QMessageBox::Abort, QMessageBox::Abort);
295 void TransactionView::contextualMenu(const QPoint &point)
297 QModelIndex index = transactionView->indexAt(point);
300 contextMenu->exec(QCursor::pos());
304 void TransactionView::copyAddress()
306 if(!transactionView->selectionModel())
308 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
309 if(!selection.isEmpty())
311 QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::AddressRole).toString());
315 void TransactionView::copyLabel()
317 if(!transactionView->selectionModel())
319 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
320 if(!selection.isEmpty())
322 QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::LabelRole).toString());
326 void TransactionView::editLabel()
328 if(!transactionView->selectionModel() ||!model)
330 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
331 if(!selection.isEmpty())
333 AddressTableModel *addressBook = model->getAddressTableModel();
336 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
337 if(address.isEmpty())
339 // If this transaction has no associated address, exit
342 // Is address in address book? Address book can miss address when a transaction is
343 // sent from outside the UI.
344 int idx = addressBook->lookupAddress(address);
347 // Edit sending / receiving address
348 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
349 // Determine type of address, launch appropriate editor dialog type
350 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
352 EditAddressDialog dlg(type==AddressTableModel::Receive
353 ? EditAddressDialog::EditReceivingAddress
354 : EditAddressDialog::EditSendingAddress,
356 dlg.setModel(addressBook);
362 // Add sending address
363 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
365 dlg.setModel(addressBook);
366 dlg.setAddress(address);
372 void TransactionView::showDetails()
374 if(!transactionView->selectionModel())
376 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
377 if(!selection.isEmpty())
379 TransactionDescDialog dlg(selection.at(0));
384 QWidget *TransactionView::createDateRangeWidget()
386 dateRangeWidget = new QFrame();
387 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
388 dateRangeWidget->setContentsMargins(1,1,1,1);
389 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
390 layout->setContentsMargins(0,0,0,0);
391 layout->addSpacing(23);
392 layout->addWidget(new QLabel(tr("Range:")));
394 dateFrom = new QDateTimeEdit(this);
395 dateFrom->setDisplayFormat("dd/MM/yy");
396 dateFrom->setCalendarPopup(true);
397 dateFrom->setMinimumWidth(100);
398 dateFrom->setDate(QDate::currentDate().addDays(-7));
399 layout->addWidget(dateFrom);
400 layout->addWidget(new QLabel(tr("to")));
402 dateTo = new QDateTimeEdit(this);
403 dateTo->setDisplayFormat("dd/MM/yy");
404 dateTo->setCalendarPopup(true);
405 dateTo->setMinimumWidth(100);
406 dateTo->setDate(QDate::currentDate());
407 layout->addWidget(dateTo);
408 layout->addStretch();
411 dateRangeWidget->setVisible(false);
414 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
415 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
417 return dateRangeWidget;
420 void TransactionView::dateRangeChanged()
422 if(!transactionProxyModel)
424 transactionProxyModel->setDateRange(
425 QDateTime(dateFrom->date()),
426 QDateTime(dateTo->date()).addDays(1));