]> Git Repo - VerusCoin.git/blob - src/qt/transactionview.cpp
Merge pull request #4739
[VerusCoin.git] / src / qt / transactionview.cpp
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5 #include "transactionview.h"
6
7 #include "addresstablemodel.h"
8 #include "bitcoinunits.h"
9 #include "csvmodelwriter.h"
10 #include "editaddressdialog.h"
11 #include "guiutil.h"
12 #include "optionsmodel.h"
13 #include "transactiondescdialog.h"
14 #include "transactionfilterproxy.h"
15 #include "transactionrecord.h"
16 #include "transactiontablemodel.h"
17 #include "walletmodel.h"
18
19 #include "ui_interface.h"
20
21 #include <QComboBox>
22 #include <QDateTimeEdit>
23 #include <QDesktopServices>
24 #include <QDoubleValidator>
25 #include <QHBoxLayout>
26 #include <QHeaderView>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QMenu>
30 #include <QPoint>
31 #include <QScrollBar>
32 #include <QSignalMapper>
33 #include <QTableView>
34 #include <QUrl>
35 #include <QVBoxLayout>
36
37 TransactionView::TransactionView(QWidget *parent) :
38     QWidget(parent), model(0), transactionProxyModel(0),
39     transactionView(0)
40 {
41     // Build filter row
42     setContentsMargins(0,0,0,0);
43
44     QHBoxLayout *hlayout = new QHBoxLayout();
45     hlayout->setContentsMargins(0,0,0,0);
46 #ifdef Q_OS_MAC
47     hlayout->setSpacing(5);
48     hlayout->addSpacing(26);
49 #else
50     hlayout->setSpacing(0);
51     hlayout->addSpacing(23);
52 #endif
53
54     dateWidget = new QComboBox(this);
55 #ifdef Q_OS_MAC
56     dateWidget->setFixedWidth(121);
57 #else
58     dateWidget->setFixedWidth(120);
59 #endif
60     dateWidget->addItem(tr("All"), All);
61     dateWidget->addItem(tr("Today"), Today);
62     dateWidget->addItem(tr("This week"), ThisWeek);
63     dateWidget->addItem(tr("This month"), ThisMonth);
64     dateWidget->addItem(tr("Last month"), LastMonth);
65     dateWidget->addItem(tr("This year"), ThisYear);
66     dateWidget->addItem(tr("Range..."), Range);
67     hlayout->addWidget(dateWidget);
68
69     typeWidget = new QComboBox(this);
70 #ifdef Q_OS_MAC
71     typeWidget->setFixedWidth(121);
72 #else
73     typeWidget->setFixedWidth(120);
74 #endif
75
76     typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
77     typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
78                                         TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
79     typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
80                                   TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
81     typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
82     typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
83     typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
84
85     hlayout->addWidget(typeWidget);
86
87     addressWidget = new QLineEdit(this);
88 #if QT_VERSION >= 0x040700
89     addressWidget->setPlaceholderText(tr("Enter address or label to search"));
90 #endif
91     hlayout->addWidget(addressWidget);
92
93     amountWidget = new QLineEdit(this);
94 #if QT_VERSION >= 0x040700
95     amountWidget->setPlaceholderText(tr("Min amount"));
96 #endif
97 #ifdef Q_OS_MAC
98     amountWidget->setFixedWidth(97);
99 #else
100     amountWidget->setFixedWidth(100);
101 #endif
102     amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
103     hlayout->addWidget(amountWidget);
104
105     QVBoxLayout *vlayout = new QVBoxLayout(this);
106     vlayout->setContentsMargins(0,0,0,0);
107     vlayout->setSpacing(0);
108
109     QTableView *view = new QTableView(this);
110     vlayout->addLayout(hlayout);
111     vlayout->addWidget(createDateRangeWidget());
112     vlayout->addWidget(view);
113     vlayout->setSpacing(0);
114     int width = view->verticalScrollBar()->sizeHint().width();
115     // Cover scroll bar width with spacing
116 #ifdef Q_OS_MAC
117     hlayout->addSpacing(width+2);
118 #else
119     hlayout->addSpacing(width);
120 #endif
121     // Always show scroll bar
122     view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
123     view->setTabKeyNavigation(false);
124     view->setContextMenuPolicy(Qt::CustomContextMenu);
125
126     view->installEventFilter(this);
127
128     transactionView = view;
129
130     // Actions
131     QAction *copyAddressAction = new QAction(tr("Copy address"), this);
132     QAction *copyLabelAction = new QAction(tr("Copy label"), this);
133     QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
134     QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
135     QAction *editLabelAction = new QAction(tr("Edit label"), this);
136     QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
137
138     contextMenu = new QMenu();
139     contextMenu->addAction(copyAddressAction);
140     contextMenu->addAction(copyLabelAction);
141     contextMenu->addAction(copyAmountAction);
142     contextMenu->addAction(copyTxIDAction);
143     contextMenu->addAction(editLabelAction);
144     contextMenu->addAction(showDetailsAction);
145
146     mapperThirdPartyTxUrls = new QSignalMapper(this);
147
148     // Connect actions
149     connect(mapperThirdPartyTxUrls, SIGNAL(mapped(QString)), this, SLOT(openThirdPartyTxUrl(QString)));
150
151     connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
152     connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
153     connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
154     connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
155
156     connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
157     connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
158
159     connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
160     connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
161     connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
162     connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
163     connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
164     connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
165 }
166
167 void TransactionView::setModel(WalletModel *model)
168 {
169     this->model = model;
170     if(model)
171     {
172         transactionProxyModel = new TransactionFilterProxy(this);
173         transactionProxyModel->setSourceModel(model->getTransactionTableModel());
174         transactionProxyModel->setDynamicSortFilter(true);
175         transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
176         transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
177
178         transactionProxyModel->setSortRole(Qt::EditRole);
179
180         transactionView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
181         transactionView->setModel(transactionProxyModel);
182         transactionView->setAlternatingRowColors(true);
183         transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
184         transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
185         transactionView->setSortingEnabled(true);
186         transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
187         transactionView->verticalHeader()->hide();
188
189         transactionView->setColumnWidth(TransactionTableModel::Status, STATUS_COLUMN_WIDTH);
190         transactionView->setColumnWidth(TransactionTableModel::Date, DATE_COLUMN_WIDTH);
191         transactionView->setColumnWidth(TransactionTableModel::Type, TYPE_COLUMN_WIDTH);
192         transactionView->setColumnWidth(TransactionTableModel::Amount, AMOUNT_MINIMUM_COLUMN_WIDTH);
193
194         columnResizingFixer = new GUIUtil::TableViewLastColumnResizingFixer(transactionView, AMOUNT_MINIMUM_COLUMN_WIDTH, MINIMUM_COLUMN_WIDTH);
195
196         if (model->getOptionsModel())
197         {
198             // Add third party transaction URLs to context menu
199             QStringList listUrls = model->getOptionsModel()->getThirdPartyTxUrls().split("|", QString::SkipEmptyParts);
200             for (int i = 0; i < listUrls.size(); ++i)
201             {
202                 QString host = QUrl(listUrls[i].trimmed(), QUrl::StrictMode).host();
203                 if (!host.isEmpty())
204                 {
205                     QAction *thirdPartyTxUrlAction = new QAction(host, this); // use host as menu item label
206                     if (i == 0)
207                         contextMenu->addSeparator();
208                     contextMenu->addAction(thirdPartyTxUrlAction);
209                     connect(thirdPartyTxUrlAction, SIGNAL(triggered()), mapperThirdPartyTxUrls, SLOT(map()));
210                     mapperThirdPartyTxUrls->setMapping(thirdPartyTxUrlAction, listUrls[i].trimmed());
211                 }
212             }
213         }
214     }
215 }
216
217 void TransactionView::chooseDate(int idx)
218 {
219     if(!transactionProxyModel)
220         return;
221     QDate current = QDate::currentDate();
222     dateRangeWidget->setVisible(false);
223     switch(dateWidget->itemData(idx).toInt())
224     {
225     case All:
226         transactionProxyModel->setDateRange(
227                 TransactionFilterProxy::MIN_DATE,
228                 TransactionFilterProxy::MAX_DATE);
229         break;
230     case Today:
231         transactionProxyModel->setDateRange(
232                 QDateTime(current),
233                 TransactionFilterProxy::MAX_DATE);
234         break;
235     case ThisWeek: {
236         // Find last Monday
237         QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
238         transactionProxyModel->setDateRange(
239                 QDateTime(startOfWeek),
240                 TransactionFilterProxy::MAX_DATE);
241
242         } break;
243     case ThisMonth:
244         transactionProxyModel->setDateRange(
245                 QDateTime(QDate(current.year(), current.month(), 1)),
246                 TransactionFilterProxy::MAX_DATE);
247         break;
248     case LastMonth:
249         transactionProxyModel->setDateRange(
250                 QDateTime(QDate(current.year(), current.month()-1, 1)),
251                 QDateTime(QDate(current.year(), current.month(), 1)));
252         break;
253     case ThisYear:
254         transactionProxyModel->setDateRange(
255                 QDateTime(QDate(current.year(), 1, 1)),
256                 TransactionFilterProxy::MAX_DATE);
257         break;
258     case Range:
259         dateRangeWidget->setVisible(true);
260         dateRangeChanged();
261         break;
262     }
263 }
264
265 void TransactionView::chooseType(int idx)
266 {
267     if(!transactionProxyModel)
268         return;
269     transactionProxyModel->setTypeFilter(
270         typeWidget->itemData(idx).toInt());
271 }
272
273 void TransactionView::changedPrefix(const QString &prefix)
274 {
275     if(!transactionProxyModel)
276         return;
277     transactionProxyModel->setAddressPrefix(prefix);
278 }
279
280 void TransactionView::changedAmount(const QString &amount)
281 {
282     if(!transactionProxyModel)
283         return;
284     qint64 amount_parsed = 0;
285     if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
286     {
287         transactionProxyModel->setMinAmount(amount_parsed);
288     }
289     else
290     {
291         transactionProxyModel->setMinAmount(0);
292     }
293 }
294
295 void TransactionView::exportClicked()
296 {
297     // CSV is currently the only supported format
298     QString filename = GUIUtil::getSaveFileName(this,
299         tr("Export Transaction History"), QString(),
300         tr("Comma separated file (*.csv)"), NULL);
301
302     if (filename.isNull())
303         return;
304
305     CSVModelWriter writer(filename);
306
307     // name, column, role
308     writer.setModel(transactionProxyModel);
309     writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
310     writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
311     writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
312     writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
313     writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
314     writer.addColumn(BitcoinUnits::getAmountColumnTitle(model->getOptionsModel()->getDisplayUnit()), 0, TransactionTableModel::FormattedAmountRole);
315     writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
316
317     if(!writer.write()) {
318         emit message(tr("Exporting Failed"), tr("There was an error trying to save the transaction history to %1.").arg(filename),
319             CClientUIInterface::MSG_ERROR);
320     }
321     else {
322         emit message(tr("Exporting Successful"), tr("The transaction history was successfully saved to %1.").arg(filename),
323             CClientUIInterface::MSG_INFORMATION);
324     }
325 }
326
327 void TransactionView::contextualMenu(const QPoint &point)
328 {
329     QModelIndex index = transactionView->indexAt(point);
330     if(index.isValid())
331     {
332         contextMenu->exec(QCursor::pos());
333     }
334 }
335
336 void TransactionView::copyAddress()
337 {
338     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
339 }
340
341 void TransactionView::copyLabel()
342 {
343     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
344 }
345
346 void TransactionView::copyAmount()
347 {
348     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
349 }
350
351 void TransactionView::copyTxID()
352 {
353     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::TxIDRole);
354 }
355
356 void TransactionView::editLabel()
357 {
358     if(!transactionView->selectionModel() ||!model)
359         return;
360     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
361     if(!selection.isEmpty())
362     {
363         AddressTableModel *addressBook = model->getAddressTableModel();
364         if(!addressBook)
365             return;
366         QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
367         if(address.isEmpty())
368         {
369             // If this transaction has no associated address, exit
370             return;
371         }
372         // Is address in address book? Address book can miss address when a transaction is
373         // sent from outside the UI.
374         int idx = addressBook->lookupAddress(address);
375         if(idx != -1)
376         {
377             // Edit sending / receiving address
378             QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
379             // Determine type of address, launch appropriate editor dialog type
380             QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
381
382             EditAddressDialog dlg(
383                 type == AddressTableModel::Receive
384                 ? EditAddressDialog::EditReceivingAddress
385                 : EditAddressDialog::EditSendingAddress, this);
386             dlg.setModel(addressBook);
387             dlg.loadRow(idx);
388             dlg.exec();
389         }
390         else
391         {
392             // Add sending address
393             EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
394                 this);
395             dlg.setModel(addressBook);
396             dlg.setAddress(address);
397             dlg.exec();
398         }
399     }
400 }
401
402 void TransactionView::showDetails()
403 {
404     if(!transactionView->selectionModel())
405         return;
406     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
407     if(!selection.isEmpty())
408     {
409         TransactionDescDialog dlg(selection.at(0));
410         dlg.exec();
411     }
412 }
413
414 void TransactionView::openThirdPartyTxUrl(QString url)
415 {
416     if(!transactionView || !transactionView->selectionModel())
417         return;
418     QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
419     if(!selection.isEmpty())
420          QDesktopServices::openUrl(QUrl::fromUserInput(url.replace("%s", selection.at(0).data(TransactionTableModel::TxHashRole).toString())));
421 }
422
423 QWidget *TransactionView::createDateRangeWidget()
424 {
425     dateRangeWidget = new QFrame();
426     dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
427     dateRangeWidget->setContentsMargins(1,1,1,1);
428     QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
429     layout->setContentsMargins(0,0,0,0);
430     layout->addSpacing(23);
431     layout->addWidget(new QLabel(tr("Range:")));
432
433     dateFrom = new QDateTimeEdit(this);
434     dateFrom->setDisplayFormat("dd/MM/yy");
435     dateFrom->setCalendarPopup(true);
436     dateFrom->setMinimumWidth(100);
437     dateFrom->setDate(QDate::currentDate().addDays(-7));
438     layout->addWidget(dateFrom);
439     layout->addWidget(new QLabel(tr("to")));
440
441     dateTo = new QDateTimeEdit(this);
442     dateTo->setDisplayFormat("dd/MM/yy");
443     dateTo->setCalendarPopup(true);
444     dateTo->setMinimumWidth(100);
445     dateTo->setDate(QDate::currentDate());
446     layout->addWidget(dateTo);
447     layout->addStretch();
448
449     // Hide by default
450     dateRangeWidget->setVisible(false);
451
452     // Notify on change
453     connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
454     connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
455
456     return dateRangeWidget;
457 }
458
459 void TransactionView::dateRangeChanged()
460 {
461     if(!transactionProxyModel)
462         return;
463     transactionProxyModel->setDateRange(
464             QDateTime(dateFrom->date()),
465             QDateTime(dateTo->date()).addDays(1));
466 }
467
468 void TransactionView::focusTransaction(const QModelIndex &idx)
469 {
470     if(!transactionProxyModel)
471         return;
472     QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
473     transactionView->scrollTo(targetIdx);
474     transactionView->setCurrentIndex(targetIdx);
475     transactionView->setFocus();
476 }
477
478 // We override the virtual resizeEvent of the QWidget to adjust tables column
479 // sizes as the tables width is proportional to the dialogs width.
480 void TransactionView::resizeEvent(QResizeEvent* event)
481 {
482     QWidget::resizeEvent(event);
483     columnResizingFixer->stretchColumnWidth(TransactionTableModel::ToAddress);
484 }
485
486 // Need to override default Ctrl+C action for amount as default behaviour is just to copy DisplayRole text
487 bool TransactionView::eventFilter(QObject *obj, QEvent *event)
488 {
489     if (event->type() == QEvent::KeyPress)
490     {
491         QKeyEvent *ke = static_cast<QKeyEvent *>(event);
492         if (ke->key() == Qt::Key_C && ke->modifiers().testFlag(Qt::ControlModifier))
493         {
494             QModelIndex i = this->transactionView->currentIndex();
495             if (i.isValid() && i.column() == TransactionTableModel::Amount)
496             {
497                  GUIUtil::setClipboard(i.data(TransactionTableModel::FormattedAmountRole).toString());
498                  return true;
499             }
500         }
501     }
502     return QWidget::eventFilter(obj, event);
503 }
This page took 0.053246 seconds and 4 git commands to generate.