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)
159 transactionProxyModel = new TransactionFilterProxy(this);
160 transactionProxyModel->setSourceModel(model->getTransactionTableModel());
161 transactionProxyModel->setDynamicSortFilter(true);
163 transactionProxyModel->setSortRole(Qt::EditRole);
165 transactionView->setModel(transactionProxyModel);
166 transactionView->setAlternatingRowColors(true);
167 transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
168 transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
169 transactionView->setSortingEnabled(true);
170 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
171 transactionView->verticalHeader()->hide();
173 transactionView->horizontalHeader()->resizeSection(
174 TransactionTableModel::Status, 23);
175 transactionView->horizontalHeader()->resizeSection(
176 TransactionTableModel::Date, 120);
177 transactionView->horizontalHeader()->resizeSection(
178 TransactionTableModel::Type, 120);
179 transactionView->horizontalHeader()->setResizeMode(
180 TransactionTableModel::ToAddress, QHeaderView::Stretch);
181 transactionView->horizontalHeader()->resizeSection(
182 TransactionTableModel::Amount, 100);
186 void TransactionView::chooseDate(int idx)
188 QDate current = QDate::currentDate();
189 dateRangeWidget->setVisible(false);
190 switch(dateWidget->itemData(idx).toInt())
193 transactionProxyModel->setDateRange(
194 TransactionFilterProxy::MIN_DATE,
195 TransactionFilterProxy::MAX_DATE);
198 transactionProxyModel->setDateRange(
200 TransactionFilterProxy::MAX_DATE);
204 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
205 transactionProxyModel->setDateRange(
206 QDateTime(startOfWeek),
207 TransactionFilterProxy::MAX_DATE);
211 transactionProxyModel->setDateRange(
212 QDateTime(QDate(current.year(), current.month(), 1)),
213 TransactionFilterProxy::MAX_DATE);
216 transactionProxyModel->setDateRange(
217 QDateTime(QDate(current.year(), current.month()-1, 1)),
218 QDateTime(QDate(current.year(), current.month(), 1)));
221 transactionProxyModel->setDateRange(
222 QDateTime(QDate(current.year(), 1, 1)),
223 TransactionFilterProxy::MAX_DATE);
226 dateRangeWidget->setVisible(true);
232 void TransactionView::chooseType(int idx)
234 transactionProxyModel->setTypeFilter(
235 typeWidget->itemData(idx).toInt());
238 void TransactionView::changedPrefix(const QString &prefix)
240 transactionProxyModel->setAddressPrefix(prefix);
243 void TransactionView::changedAmount(const QString &amount)
245 qint64 amount_parsed = 0;
246 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
248 transactionProxyModel->setMinAmount(amount_parsed);
252 transactionProxyModel->setMinAmount(0);
256 void TransactionView::exportClicked()
258 // CSV is currently the only supported format
259 QString filename = QFileDialog::getSaveFileName(
261 tr("Export Transaction Data"),
263 tr("Comma separated file (*.csv)"));
265 if (filename.isNull()) return;
267 CSVModelWriter writer(filename);
269 // name, column, role
270 writer.setModel(transactionProxyModel);
271 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
272 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
273 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
274 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
275 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
276 writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
277 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
281 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
282 QMessageBox::Abort, QMessageBox::Abort);
286 void TransactionView::contextualMenu(const QPoint &point)
288 QModelIndex index = transactionView->indexAt(point);
291 contextMenu->exec(QCursor::pos());
295 void TransactionView::copyAddress()
297 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
298 if(!selection.isEmpty())
300 QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::AddressRole).toString());
304 void TransactionView::copyLabel()
306 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
307 if(!selection.isEmpty())
309 QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::LabelRole).toString());
313 void TransactionView::editLabel()
315 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
316 if(!selection.isEmpty())
318 AddressTableModel *addressBook = model->getAddressTableModel();
319 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
320 if(address.isEmpty())
322 // If this transaction has no associated address, exit
325 // Is address in address book? Address book can miss address when a transaction is
326 // sent from outside the UI.
327 int idx = addressBook->lookupAddress(address);
330 // Edit sending / receiving address
331 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
332 // Determine type of address, launch appropriate editor dialog type
333 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
335 EditAddressDialog dlg(type==AddressTableModel::Receive
336 ? EditAddressDialog::EditReceivingAddress
337 : EditAddressDialog::EditSendingAddress,
339 dlg.setModel(addressBook);
345 // Add sending address
346 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
348 dlg.setModel(addressBook);
349 dlg.setAddress(address);
355 void TransactionView::showDetails()
357 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
358 if(!selection.isEmpty())
360 TransactionDescDialog dlg(selection.at(0));
365 QWidget *TransactionView::createDateRangeWidget()
367 dateRangeWidget = new QFrame();
368 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
369 dateRangeWidget->setContentsMargins(1,1,1,1);
370 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
371 layout->setContentsMargins(0,0,0,0);
372 layout->addSpacing(23);
373 layout->addWidget(new QLabel(tr("Range:")));
375 dateFrom = new QDateTimeEdit(this);
376 dateFrom->setDisplayFormat("dd/MM/yy");
377 dateFrom->setCalendarPopup(true);
378 dateFrom->setMinimumWidth(100);
379 dateFrom->setDate(QDate::currentDate().addDays(-7));
380 layout->addWidget(dateFrom);
381 layout->addWidget(new QLabel(tr("to")));
383 dateTo = new QDateTimeEdit(this);
384 dateTo->setDisplayFormat("dd/MM/yy");
385 dateTo->setCalendarPopup(true);
386 dateTo->setMinimumWidth(100);
387 dateTo->setDate(QDate::currentDate());
388 layout->addWidget(dateTo);
389 layout->addStretch();
392 dateRangeWidget->setVisible(false);
395 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
396 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
398 return dateRangeWidget;
401 void TransactionView::dateRangeChanged()
403 transactionProxyModel->setDateRange(
404 QDateTime(dateFrom->date()),
405 QDateTime(dateTo->date()).addDays(1));