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