]> Git Repo - VerusCoin.git/blob - src/qt/transactionview.cpp
Merge pull request #3255
[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 <QDoubleValidator>
24 #include <QHBoxLayout>
25 #include <QHeaderView>
26 #include <QLabel>
27 #include <QLineEdit>
28 #include <QMenu>
29 #include <QPoint>
30 #include <QScrollBar>
31 #include <QTableView>
32 #include <QVBoxLayout>
33
34 TransactionView::TransactionView(QWidget *parent) :
35     QWidget(parent), model(0), transactionProxyModel(0),
36     transactionView(0)
37 {
38     // Build filter row
39     setContentsMargins(0,0,0,0);
40
41     QHBoxLayout *hlayout = new QHBoxLayout();
42     hlayout->setContentsMargins(0,0,0,0);
43 #ifdef Q_OS_MAC
44     hlayout->setSpacing(5);
45     hlayout->addSpacing(26);
46 #else
47     hlayout->setSpacing(0);
48     hlayout->addSpacing(23);
49 #endif
50
51     dateWidget = new QComboBox(this);
52 #ifdef Q_OS_MAC
53     dateWidget->setFixedWidth(121);
54 #else
55     dateWidget->setFixedWidth(120);
56 #endif
57     dateWidget->addItem(tr("All"), All);
58     dateWidget->addItem(tr("Today"), Today);
59     dateWidget->addItem(tr("This week"), ThisWeek);
60     dateWidget->addItem(tr("This month"), ThisMonth);
61     dateWidget->addItem(tr("Last month"), LastMonth);
62     dateWidget->addItem(tr("This year"), ThisYear);
63     dateWidget->addItem(tr("Range..."), Range);
64     hlayout->addWidget(dateWidget);
65
66     typeWidget = new QComboBox(this);
67 #ifdef Q_OS_MAC
68     typeWidget->setFixedWidth(121);
69 #else
70     typeWidget->setFixedWidth(120);
71 #endif
72
73     typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
74     typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
75                                         TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
76     typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
77                                   TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
78     typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
79     typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
80     typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
81
82     hlayout->addWidget(typeWidget);
83
84     addressWidget = new QLineEdit(this);
85 #if QT_VERSION >= 0x040700
86     addressWidget->setPlaceholderText(tr("Enter address or label to search"));
87 #endif
88     hlayout->addWidget(addressWidget);
89
90     amountWidget = new QLineEdit(this);
91 #if QT_VERSION >= 0x040700
92     amountWidget->setPlaceholderText(tr("Min amount"));
93 #endif
94 #ifdef Q_OS_MAC
95     amountWidget->setFixedWidth(97);
96 #else
97     amountWidget->setFixedWidth(100);
98 #endif
99     amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
100     hlayout->addWidget(amountWidget);
101
102     QVBoxLayout *vlayout = new QVBoxLayout(this);
103     vlayout->setContentsMargins(0,0,0,0);
104     vlayout->setSpacing(0);
105
106     QTableView *view = new QTableView(this);
107     vlayout->addLayout(hlayout);
108     vlayout->addWidget(createDateRangeWidget());
109     vlayout->addWidget(view);
110     vlayout->setSpacing(0);
111     int width = view->verticalScrollBar()->sizeHint().width();
112     // Cover scroll bar width with spacing
113 #ifdef Q_OS_MAC
114     hlayout->addSpacing(width+2);
115 #else
116     hlayout->addSpacing(width);
117 #endif
118     // Always show scroll bar
119     view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
120     view->setTabKeyNavigation(false);
121     view->setContextMenuPolicy(Qt::CustomContextMenu);
122
123     transactionView = view;
124
125     // Actions
126     QAction *copyAddressAction = new QAction(tr("Copy address"), this);
127     QAction *copyLabelAction = new QAction(tr("Copy label"), this);
128     QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
129     QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
130     QAction *editLabelAction = new QAction(tr("Edit label"), this);
131     QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
132
133     contextMenu = new QMenu();
134     contextMenu->addAction(copyAddressAction);
135     contextMenu->addAction(copyLabelAction);
136     contextMenu->addAction(copyAmountAction);
137     contextMenu->addAction(copyTxIDAction);
138     contextMenu->addAction(editLabelAction);
139     contextMenu->addAction(showDetailsAction);
140
141     // Connect actions
142     connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
143     connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
144     connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
145     connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
146
147     connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
148     connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
149
150     connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
151     connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
152     connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
153     connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
154     connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
155     connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
156 }
157
158 void TransactionView::setModel(WalletModel *model)
159 {
160     this->model = model;
161     if(model)
162     {
163         transactionProxyModel = new TransactionFilterProxy(this);
164         transactionProxyModel->setSourceModel(model->getTransactionTableModel());
165         transactionProxyModel->setDynamicSortFilter(true);
166         transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
167         transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
168
169         transactionProxyModel->setSortRole(Qt::EditRole);
170
171         transactionView->setModel(transactionProxyModel);
172         transactionView->setAlternatingRowColors(true);
173         transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
174         transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
175         transactionView->setSortingEnabled(true);
176         transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
177         transactionView->verticalHeader()->hide();
178
179         transactionView->horizontalHeader()->resizeSection(TransactionTableModel::Status, 23);
180         transactionView->horizontalHeader()->resizeSection(TransactionTableModel::Date, 120);
181         transactionView->horizontalHeader()->resizeSection(TransactionTableModel::Type, 120);
182 #if QT_VERSION < 0x050000
183         transactionView->horizontalHeader()->setResizeMode(TransactionTableModel::ToAddress, QHeaderView::Stretch);
184 #else
185         transactionView->horizontalHeader()->setSectionResizeMode(TransactionTableModel::ToAddress, QHeaderView::Stretch);
186 #endif
187         transactionView->horizontalHeader()->resizeSection(TransactionTableModel::Amount, 100);
188     }
189 }
190
191 void TransactionView::chooseDate(int idx)
192 {
193     if(!transactionProxyModel)
194         return;
195     QDate current = QDate::currentDate();
196     dateRangeWidget->setVisible(false);
197     switch(dateWidget->itemData(idx).toInt())
198     {
199     case All:
200         transactionProxyModel->setDateRange(
201                 TransactionFilterProxy::MIN_DATE,
202                 TransactionFilterProxy::MAX_DATE);
203         break;
204     case Today:
205         transactionProxyModel->setDateRange(
206                 QDateTime(current),
207                 TransactionFilterProxy::MAX_DATE);
208         break;
209     case ThisWeek: {
210         // Find last Monday
211         QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
212         transactionProxyModel->setDateRange(
213                 QDateTime(startOfWeek),
214                 TransactionFilterProxy::MAX_DATE);
215
216         } break;
217     case ThisMonth:
218         transactionProxyModel->setDateRange(
219                 QDateTime(QDate(current.year(), current.month(), 1)),
220                 TransactionFilterProxy::MAX_DATE);
221         break;
222     case LastMonth:
223         transactionProxyModel->setDateRange(
224                 QDateTime(QDate(current.year(), current.month()-1, 1)),
225                 QDateTime(QDate(current.year(), current.month(), 1)));
226         break;
227     case ThisYear:
228         transactionProxyModel->setDateRange(
229                 QDateTime(QDate(current.year(), 1, 1)),
230                 TransactionFilterProxy::MAX_DATE);
231         break;
232     case Range:
233         dateRangeWidget->setVisible(true);
234         dateRangeChanged();
235         break;
236     }
237 }
238
239 void TransactionView::chooseType(int idx)
240 {
241     if(!transactionProxyModel)
242         return;
243     transactionProxyModel->setTypeFilter(
244         typeWidget->itemData(idx).toInt());
245 }
246
247 void TransactionView::changedPrefix(const QString &prefix)
248 {
249     if(!transactionProxyModel)
250         return;
251     transactionProxyModel->setAddressPrefix(prefix);
252 }
253
254 void TransactionView::changedAmount(const QString &amount)
255 {
256     if(!transactionProxyModel)
257         return;
258     qint64 amount_parsed = 0;
259     if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
260     {
261         transactionProxyModel->setMinAmount(amount_parsed);
262     }
263     else
264     {
265         transactionProxyModel->setMinAmount(0);
266     }
267 }
268
269 void TransactionView::exportClicked()
270 {
271     // CSV is currently the only supported format
272     QString filename = GUIUtil::getSaveFileName(this,
273         tr("Export Transaction History"), QString(),
274         tr("Comma separated file (*.csv)"), NULL);
275
276     if (filename.isNull())
277         return;
278
279     CSVModelWriter writer(filename);
280
281     // name, column, role
282     writer.setModel(transactionProxyModel);
283     writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
284     writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
285     writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
286     writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
287     writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
288     writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
289     writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
290
291     if(!writer.write()) {
292         emit message(tr("Exporting Failed"), tr("There was an error trying to save the transaction history to %1.").arg(filename),
293             CClientUIInterface::MSG_ERROR);
294     }
295     else {
296         emit message(tr("Exporting Successful"), tr("The transaction history was successfully saved to %1.").arg(filename),
297             CClientUIInterface::MSG_INFORMATION);
298     }
299 }
300
301 void TransactionView::contextualMenu(const QPoint &point)
302 {
303     QModelIndex index = transactionView->indexAt(point);
304     if(index.isValid())
305     {
306         contextMenu->exec(QCursor::pos());
307     }
308 }
309
310 void TransactionView::copyAddress()
311 {
312     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
313 }
314
315 void TransactionView::copyLabel()
316 {
317     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
318 }
319
320 void TransactionView::copyAmount()
321 {
322     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
323 }
324
325 void TransactionView::copyTxID()
326 {
327     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::TxIDRole);
328 }
329
330 void TransactionView::editLabel()
331 {
332     if(!transactionView->selectionModel() ||!model)
333         return;
334     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
335     if(!selection.isEmpty())
336     {
337         AddressTableModel *addressBook = model->getAddressTableModel();
338         if(!addressBook)
339             return;
340         QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
341         if(address.isEmpty())
342         {
343             // If this transaction has no associated address, exit
344             return;
345         }
346         // Is address in address book? Address book can miss address when a transaction is
347         // sent from outside the UI.
348         int idx = addressBook->lookupAddress(address);
349         if(idx != -1)
350         {
351             // Edit sending / receiving address
352             QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
353             // Determine type of address, launch appropriate editor dialog type
354             QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
355
356             EditAddressDialog dlg(
357                 type == AddressTableModel::Receive
358                 ? EditAddressDialog::EditReceivingAddress
359                 : EditAddressDialog::EditSendingAddress, this);
360             dlg.setModel(addressBook);
361             dlg.loadRow(idx);
362             dlg.exec();
363         }
364         else
365         {
366             // Add sending address
367             EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
368                 this);
369             dlg.setModel(addressBook);
370             dlg.setAddress(address);
371             dlg.exec();
372         }
373     }
374 }
375
376 void TransactionView::showDetails()
377 {
378     if(!transactionView->selectionModel())
379         return;
380     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
381     if(!selection.isEmpty())
382     {
383         TransactionDescDialog dlg(selection.at(0));
384         dlg.exec();
385     }
386 }
387
388 QWidget *TransactionView::createDateRangeWidget()
389 {
390     dateRangeWidget = new QFrame();
391     dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
392     dateRangeWidget->setContentsMargins(1,1,1,1);
393     QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
394     layout->setContentsMargins(0,0,0,0);
395     layout->addSpacing(23);
396     layout->addWidget(new QLabel(tr("Range:")));
397
398     dateFrom = new QDateTimeEdit(this);
399     dateFrom->setDisplayFormat("dd/MM/yy");
400     dateFrom->setCalendarPopup(true);
401     dateFrom->setMinimumWidth(100);
402     dateFrom->setDate(QDate::currentDate().addDays(-7));
403     layout->addWidget(dateFrom);
404     layout->addWidget(new QLabel(tr("to")));
405
406     dateTo = new QDateTimeEdit(this);
407     dateTo->setDisplayFormat("dd/MM/yy");
408     dateTo->setCalendarPopup(true);
409     dateTo->setMinimumWidth(100);
410     dateTo->setDate(QDate::currentDate());
411     layout->addWidget(dateTo);
412     layout->addStretch();
413
414     // Hide by default
415     dateRangeWidget->setVisible(false);
416
417     // Notify on change
418     connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
419     connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
420
421     return dateRangeWidget;
422 }
423
424 void TransactionView::dateRangeChanged()
425 {
426     if(!transactionProxyModel)
427         return;
428     transactionProxyModel->setDateRange(
429             QDateTime(dateFrom->date()),
430             QDateTime(dateTo->date()).addDays(1));
431 }
432
433 void TransactionView::focusTransaction(const QModelIndex &idx)
434 {
435     if(!transactionProxyModel)
436         return;
437     QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
438     transactionView->scrollTo(targetIdx);
439     transactionView->setCurrentIndex(targetIdx);
440     transactionView->setFocus();
441 }
This page took 0.080148 seconds and 4 git commands to generate.