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);
104 QTableView *view = new QTableView(this);
105 vlayout->addLayout(hlayout);
106 vlayout->addWidget(createDateRangeWidget());
107 vlayout->addWidget(view);
108 vlayout->setSpacing(0);
109 int width = view->verticalScrollBar()->sizeHint().width();
110 // Cover scroll bar width with spacing
112 hlayout->addSpacing(width+2);
114 hlayout->addSpacing(width);
116 // Always show scroll bar
117 view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
118 view->setTabKeyNavigation(false);
119 view->setContextMenuPolicy(Qt::CustomContextMenu);
121 transactionView = view;
124 QAction *copyAddressAction = new QAction(tr("Copy address"), this);
125 QAction *copyLabelAction = new QAction(tr("Copy label"), this);
126 QAction *copyAmountAction = new QAction(tr("Copy amount"), 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(copyAmountAction);
134 contextMenu->addAction(editLabelAction);
135 contextMenu->addAction(showDetailsAction);
138 connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
139 connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
140 connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
141 connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
143 connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
144 connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
146 connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
147 connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
148 connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
149 connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
150 connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
153 void TransactionView::setModel(WalletModel *model)
158 transactionProxyModel = new TransactionFilterProxy(this);
159 transactionProxyModel->setSourceModel(model->getTransactionTableModel());
160 transactionProxyModel->setDynamicSortFilter(true);
162 transactionProxyModel->setSortRole(Qt::EditRole);
164 transactionView->setModel(transactionProxyModel);
165 transactionView->setAlternatingRowColors(true);
166 transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
167 transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
168 transactionView->setSortingEnabled(true);
169 transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
170 transactionView->verticalHeader()->hide();
172 transactionView->horizontalHeader()->resizeSection(
173 TransactionTableModel::Status, 23);
174 transactionView->horizontalHeader()->resizeSection(
175 TransactionTableModel::Date, 120);
176 transactionView->horizontalHeader()->resizeSection(
177 TransactionTableModel::Type, 120);
178 transactionView->horizontalHeader()->setResizeMode(
179 TransactionTableModel::ToAddress, QHeaderView::Stretch);
180 transactionView->horizontalHeader()->resizeSection(
181 TransactionTableModel::Amount, 100);
185 void TransactionView::chooseDate(int idx)
187 if(!transactionProxyModel)
189 QDate current = QDate::currentDate();
190 dateRangeWidget->setVisible(false);
191 switch(dateWidget->itemData(idx).toInt())
194 transactionProxyModel->setDateRange(
195 TransactionFilterProxy::MIN_DATE,
196 TransactionFilterProxy::MAX_DATE);
199 transactionProxyModel->setDateRange(
201 TransactionFilterProxy::MAX_DATE);
205 QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
206 transactionProxyModel->setDateRange(
207 QDateTime(startOfWeek),
208 TransactionFilterProxy::MAX_DATE);
212 transactionProxyModel->setDateRange(
213 QDateTime(QDate(current.year(), current.month(), 1)),
214 TransactionFilterProxy::MAX_DATE);
217 transactionProxyModel->setDateRange(
218 QDateTime(QDate(current.year(), current.month()-1, 1)),
219 QDateTime(QDate(current.year(), current.month(), 1)));
222 transactionProxyModel->setDateRange(
223 QDateTime(QDate(current.year(), 1, 1)),
224 TransactionFilterProxy::MAX_DATE);
227 dateRangeWidget->setVisible(true);
233 void TransactionView::chooseType(int idx)
235 if(!transactionProxyModel)
237 transactionProxyModel->setTypeFilter(
238 typeWidget->itemData(idx).toInt());
241 void TransactionView::changedPrefix(const QString &prefix)
243 if(!transactionProxyModel)
245 transactionProxyModel->setAddressPrefix(prefix);
248 void TransactionView::changedAmount(const QString &amount)
250 if(!transactionProxyModel)
252 qint64 amount_parsed = 0;
253 if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
255 transactionProxyModel->setMinAmount(amount_parsed);
259 transactionProxyModel->setMinAmount(0);
263 void TransactionView::exportClicked()
265 // CSV is currently the only supported format
266 QString filename = GUIUtil::getSaveFileName(
268 tr("Export Transaction Data"), QString(),
269 tr("Comma separated file (*.csv)"));
271 if (filename.isNull()) return;
273 CSVModelWriter writer(filename);
275 // name, column, role
276 writer.setModel(transactionProxyModel);
277 writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
278 writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
279 writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
280 writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
281 writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
282 writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
283 writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
287 QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
288 QMessageBox::Abort, QMessageBox::Abort);
292 void TransactionView::contextualMenu(const QPoint &point)
294 QModelIndex index = transactionView->indexAt(point);
297 contextMenu->exec(QCursor::pos());
301 void TransactionView::copyAddress()
303 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
306 void TransactionView::copyLabel()
308 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
311 void TransactionView::copyAmount()
313 GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
316 void TransactionView::editLabel()
318 if(!transactionView->selectionModel() ||!model)
320 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
321 if(!selection.isEmpty())
323 AddressTableModel *addressBook = model->getAddressTableModel();
326 QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
327 if(address.isEmpty())
329 // If this transaction has no associated address, exit
332 // Is address in address book? Address book can miss address when a transaction is
333 // sent from outside the UI.
334 int idx = addressBook->lookupAddress(address);
337 // Edit sending / receiving address
338 QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
339 // Determine type of address, launch appropriate editor dialog type
340 QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
342 EditAddressDialog dlg(type==AddressTableModel::Receive
343 ? EditAddressDialog::EditReceivingAddress
344 : EditAddressDialog::EditSendingAddress,
346 dlg.setModel(addressBook);
352 // Add sending address
353 EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
355 dlg.setModel(addressBook);
356 dlg.setAddress(address);
362 void TransactionView::showDetails()
364 if(!transactionView->selectionModel())
366 QModelIndexList selection = transactionView->selectionModel()->selectedRows();
367 if(!selection.isEmpty())
369 TransactionDescDialog dlg(selection.at(0));
374 QWidget *TransactionView::createDateRangeWidget()
376 dateRangeWidget = new QFrame();
377 dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
378 dateRangeWidget->setContentsMargins(1,1,1,1);
379 QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
380 layout->setContentsMargins(0,0,0,0);
381 layout->addSpacing(23);
382 layout->addWidget(new QLabel(tr("Range:")));
384 dateFrom = new QDateTimeEdit(this);
385 dateFrom->setDisplayFormat("dd/MM/yy");
386 dateFrom->setCalendarPopup(true);
387 dateFrom->setMinimumWidth(100);
388 dateFrom->setDate(QDate::currentDate().addDays(-7));
389 layout->addWidget(dateFrom);
390 layout->addWidget(new QLabel(tr("to")));
392 dateTo = new QDateTimeEdit(this);
393 dateTo->setDisplayFormat("dd/MM/yy");
394 dateTo->setCalendarPopup(true);
395 dateTo->setMinimumWidth(100);
396 dateTo->setDate(QDate::currentDate());
397 layout->addWidget(dateTo);
398 layout->addStretch();
401 dateRangeWidget->setVisible(false);
404 connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
405 connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
407 return dateRangeWidget;
410 void TransactionView::dateRangeChanged()
412 if(!transactionProxyModel)
414 transactionProxyModel->setDateRange(
415 QDateTime(dateFrom->date()),
416 QDateTime(dateTo->date()).addDays(1));