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>
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::RecvFromOther));
74 typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
75 TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
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 = GUIUtil::getSaveFileName(
270 tr("Export Transaction Data"), QString(),
271 tr("Comma separated file (*.csv)"));
273 if (filename.isNull()) return;
275 CSVModelWriter writer(filename);
277 // name, column, role
278 writer.setModel(transactionProxyModel);
279 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
280 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
281 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
282 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
283 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
284 writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
285 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
289 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
290 QMessageBox::Abort, QMessageBox::Abort);
294 void TransactionView::contextualMenu(const QPoint &point)
296 QModelIndex index = transactionView->indexAt(point);
299 contextMenu->exec(QCursor::pos());
303 void TransactionView::copyAddress()
305 if(!transactionView->selectionModel())
307 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
308 if(!selection.isEmpty())
310 QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::AddressRole).toString());
314 void TransactionView::copyLabel()
316 if(!transactionView->selectionModel())
318 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
319 if(!selection.isEmpty())
321 QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::LabelRole).toString());
325 void TransactionView::editLabel()
327 if(!transactionView->selectionModel() ||!model)
329 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
330 if(!selection.isEmpty())
332 AddressTableModel *addressBook = model->getAddressTableModel();
335 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
336 if(address.isEmpty())
338 // If this transaction has no associated address, exit
341 // Is address in address book? Address book can miss address when a transaction is
342 // sent from outside the UI.
343 int idx = addressBook->lookupAddress(address);
346 // Edit sending / receiving address
347 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
348 // Determine type of address, launch appropriate editor dialog type
349 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
351 EditAddressDialog dlg(type==AddressTableModel::Receive
352 ? EditAddressDialog::EditReceivingAddress
353 : EditAddressDialog::EditSendingAddress,
355 dlg.setModel(addressBook);
361 // Add sending address
362 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
364 dlg.setModel(addressBook);
365 dlg.setAddress(address);
371 void TransactionView::showDetails()
373 if(!transactionView->selectionModel())
375 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
376 if(!selection.isEmpty())
378 TransactionDescDialog dlg(selection.at(0));
383 QWidget *TransactionView::createDateRangeWidget()
385 dateRangeWidget = new QFrame();
386 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
387 dateRangeWidget->setContentsMargins(1,1,1,1);
388 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
389 layout->setContentsMargins(0,0,0,0);
390 layout->addSpacing(23);
391 layout->addWidget(new QLabel(tr("Range:")));
393 dateFrom = new QDateTimeEdit(this);
394 dateFrom->setDisplayFormat("dd/MM/yy");
395 dateFrom->setCalendarPopup(true);
396 dateFrom->setMinimumWidth(100);
397 dateFrom->setDate(QDate::currentDate().addDays(-7));
398 layout->addWidget(dateFrom);
399 layout->addWidget(new QLabel(tr("to")));
401 dateTo = new QDateTimeEdit(this);
402 dateTo->setDisplayFormat("dd/MM/yy");
403 dateTo->setCalendarPopup(true);
404 dateTo->setMinimumWidth(100);
405 dateTo->setDate(QDate::currentDate());
406 layout->addWidget(dateTo);
407 layout->addStretch();
410 dateRangeWidget->setVisible(false);
413 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
414 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
416 return dateRangeWidget;
419 void TransactionView::dateRangeChanged()
421 if(!transactionProxyModel)
423 transactionProxyModel->setDateRange(
424 QDateTime(dateFrom->date()),
425 QDateTime(dateTo->date()).addDays(1));