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("Mint by stake"), TransactionFilterProxy::TYPE(TransactionRecord::StakeMint));
79 typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
81 hlayout->addWidget(typeWidget);
83 addressWidget = new QLineEdit(this);
84 #if QT_VERSION >= 0x040700
85 addressWidget->setPlaceholderText(tr("Enter address or label to search"));
87 hlayout->addWidget(addressWidget);
89 amountWidget = new QLineEdit(this);
90 #if QT_VERSION >= 0x040700
91 amountWidget->setPlaceholderText(tr("Min amount"));
94 amountWidget->setFixedWidth(97);
96 amountWidget->setFixedWidth(100);
98 amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
99 hlayout->addWidget(amountWidget);
101 QVBoxLayout *vlayout = new QVBoxLayout(this);
102 vlayout->setContentsMargins(0,0,0,0);
103 vlayout->setSpacing(0);
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 *copyAmountAction = new QAction(tr("Copy amount"), this);
128 QAction *editLabelAction = new QAction(tr("Edit label"), this);
129 QAction *showDetailsAction = new QAction(tr("Show details..."), this);
131 contextMenu = new QMenu();
132 contextMenu->addAction(copyAddressAction);
133 contextMenu->addAction(copyLabelAction);
134 contextMenu->addAction(copyAmountAction);
135 contextMenu->addAction(editLabelAction);
136 contextMenu->addAction(showDetailsAction);
139 connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
140 connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
141 connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
142 connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
144 connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
145 connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
147 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
148 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
149 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
150 connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
151 connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
154 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 if(!transactionProxyModel)
190 QDate current = QDate::currentDate();
191 dateRangeWidget->setVisible(false);
192 switch(dateWidget->itemData(idx).toInt())
195 transactionProxyModel->setDateRange(
196 TransactionFilterProxy::MIN_DATE,
197 TransactionFilterProxy::MAX_DATE);
200 transactionProxyModel->setDateRange(
202 TransactionFilterProxy::MAX_DATE);
206 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
207 transactionProxyModel->setDateRange(
208 QDateTime(startOfWeek),
209 TransactionFilterProxy::MAX_DATE);
213 transactionProxyModel->setDateRange(
214 QDateTime(QDate(current.year(), current.month(), 1)),
215 TransactionFilterProxy::MAX_DATE);
218 transactionProxyModel->setDateRange(
219 QDateTime(QDate(current.year(), current.month()-1, 1)),
220 QDateTime(QDate(current.year(), current.month(), 1)));
223 transactionProxyModel->setDateRange(
224 QDateTime(QDate(current.year(), 1, 1)),
225 TransactionFilterProxy::MAX_DATE);
228 dateRangeWidget->setVisible(true);
234 void TransactionView::chooseType(int idx)
236 if(!transactionProxyModel)
238 transactionProxyModel->setTypeFilter(
239 typeWidget->itemData(idx).toInt());
242 void TransactionView::changedPrefix(const QString &prefix)
244 if(!transactionProxyModel)
246 transactionProxyModel->setAddressPrefix(prefix);
249 void TransactionView::changedAmount(const QString &amount)
251 if(!transactionProxyModel)
253 qint64 amount_parsed = 0;
254 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
256 transactionProxyModel->setMinAmount(amount_parsed);
260 transactionProxyModel->setMinAmount(0);
264 void TransactionView::exportClicked()
266 // CSV is currently the only supported format
267 QString filename = GUIUtil::getSaveFileName(
269 tr("Export Transaction Data"), QString(),
270 tr("Comma separated file (*.csv)"));
272 if (filename.isNull()) return;
274 CSVModelWriter writer(filename);
276 // name, column, role
277 writer.setModel(transactionProxyModel);
278 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
279 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
280 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
281 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
282 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
283 writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
284 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
288 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
289 QMessageBox::Abort, QMessageBox::Abort);
293 void TransactionView::contextualMenu(const QPoint &point)
295 QModelIndex index = transactionView->indexAt(point);
298 contextMenu->exec(QCursor::pos());
302 void TransactionView::copyAddress()
304 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
307 void TransactionView::copyLabel()
309 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
312 void TransactionView::copyAmount()
314 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
317 void TransactionView::editLabel()
319 if(!transactionView->selectionModel() ||!model)
321 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
322 if(!selection.isEmpty())
324 AddressTableModel *addressBook = model->getAddressTableModel();
327 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
328 if(address.isEmpty())
330 // If this transaction has no associated address, exit
333 // Is address in address book? Address book can miss address when a transaction is
334 // sent from outside the UI.
335 int idx = addressBook->lookupAddress(address);
338 // Edit sending / receiving address
339 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
340 // Determine type of address, launch appropriate editor dialog type
341 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
343 EditAddressDialog dlg(type==AddressTableModel::Receive
344 ? EditAddressDialog::EditReceivingAddress
345 : EditAddressDialog::EditSendingAddress,
347 dlg.setModel(addressBook);
353 // Add sending address
354 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
356 dlg.setModel(addressBook);
357 dlg.setAddress(address);
363 void TransactionView::showDetails()
365 if(!transactionView->selectionModel())
367 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
368 if(!selection.isEmpty())
370 TransactionDescDialog dlg(selection.at(0));
375 QWidget *TransactionView::createDateRangeWidget()
377 dateRangeWidget = new QFrame();
378 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
379 dateRangeWidget->setContentsMargins(1,1,1,1);
380 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
381 layout->setContentsMargins(0,0,0,0);
382 layout->addSpacing(23);
383 layout->addWidget(new QLabel(tr("Range:")));
385 dateFrom = new QDateTimeEdit(this);
386 dateFrom->setDisplayFormat("dd/MM/yy");
387 dateFrom->setCalendarPopup(true);
388 dateFrom->setMinimumWidth(100);
389 dateFrom->setDate(QDate::currentDate().addDays(-7));
390 layout->addWidget(dateFrom);
391 layout->addWidget(new QLabel(tr("to")));
393 dateTo = new QDateTimeEdit(this);
394 dateTo->setDisplayFormat("dd/MM/yy");
395 dateTo->setCalendarPopup(true);
396 dateTo->setMinimumWidth(100);
397 dateTo->setDate(QDate::currentDate());
398 layout->addWidget(dateTo);
399 layout->addStretch();
402 dateRangeWidget->setVisible(false);
405 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
406 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
408 return dateRangeWidget;
411 void TransactionView::dateRangeChanged()
413 if(!transactionProxyModel)
415 transactionProxyModel->setDateRange(
416 QDateTime(dateFrom->date()),
417 QDateTime(dateTo->date()).addDays(1));