21eb1c72eab96cd39944748af47d1815bb768e5e
[novacoin.git] / src / qt / transactionview.cpp
1 #include "transactionview.h"
2
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"
13 #include "guiutil.h"
14
15 #include <QScrollBar>
16 #include <QComboBox>
17 #include <QDoubleValidator>
18 #include <QHBoxLayout>
19 #include <QVBoxLayout>
20 #include <QLineEdit>
21 #include <QTableView>
22 #include <QHeaderView>
23 #include <QPushButton>
24 #include <QMessageBox>
25 #include <QPoint>
26 #include <QMenu>
27 #include <QApplication>
28 #include <QClipboard>
29 #include <QLabel>
30 #include <QDateTimeEdit>
31 #include <QDesktopServices>
32 #include <QSignalMapper>
33 #include <QUrl>
34
35 TransactionView::TransactionView(QWidget *parent) :
36     QWidget(parent), model(0), transactionProxyModel(0),
37     transactionView(0)
38 {
39     // Build filter row
40     setContentsMargins(0,0,0,0);
41
42     QHBoxLayout *hlayout = new QHBoxLayout();
43     hlayout->setContentsMargins(0,0,0,0);
44 #ifdef Q_OS_MAC
45     hlayout->setSpacing(5);
46     hlayout->addSpacing(26);
47 #else
48     hlayout->setSpacing(0);
49     hlayout->addSpacing(23);
50 #endif
51
52     dateWidget = new QComboBox(this);
53 #ifdef Q_OS_MAC
54     dateWidget->setFixedWidth(121);
55 #else
56     dateWidget->setFixedWidth(120);
57 #endif
58     dateWidget->addItem(tr("All"), All);
59     dateWidget->addItem(tr("Today"), Today);
60     dateWidget->addItem(tr("This week"), ThisWeek);
61     dateWidget->addItem(tr("This month"), ThisMonth);
62     dateWidget->addItem(tr("Last month"), LastMonth);
63     dateWidget->addItem(tr("This year"), ThisYear);
64     dateWidget->addItem(tr("Range..."), Range);
65     hlayout->addWidget(dateWidget);
66
67     typeWidget = new QComboBox(this);
68 #ifdef Q_OS_MAC
69     typeWidget->setFixedWidth(121);
70 #else
71     typeWidget->setFixedWidth(120);
72 #endif
73
74     typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
75     typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
76                                         TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
77     typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
78                                   TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
79     typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
80     typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
81     typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
82
83     hlayout->addWidget(typeWidget);
84
85     addressWidget = new QLineEdit(this);
86 #if QT_VERSION >= 0x040700
87     /* Do not move this to the XML file, Qt before 4.7 will choke on it */
88     addressWidget->setPlaceholderText(tr("Enter address or label to search"));
89 #endif
90     hlayout->addWidget(addressWidget);
91
92     amountWidget = new QLineEdit(this);
93 #if QT_VERSION >= 0x040700
94     /* Do not move this to the XML file, Qt before 4.7 will choke on it */
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     transactionView = view;
127
128     // Actions
129     QAction *copyAddressAction = new QAction(tr("Copy address"), this);
130     QAction *copyLabelAction = new QAction(tr("Copy label"), this);
131     QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
132     QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
133     QAction *editLabelAction = new QAction(tr("Edit label"), this);
134     QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
135
136     contextMenu = new QMenu();
137     contextMenu->addAction(copyAddressAction);
138     contextMenu->addAction(copyLabelAction);
139     contextMenu->addAction(copyAmountAction);
140     contextMenu->addAction(copyTxIDAction);
141     contextMenu->addAction(editLabelAction);
142     contextMenu->addAction(showDetailsAction);
143
144     mapperThirdPartyTxUrls = new QSignalMapper(this);
145
146     // Connect actions
147     connect(mapperThirdPartyTxUrls, SIGNAL(mapped(QString)), this, SLOT(openThirdPartyTxUrl(QString)));
148
149     connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
150     connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
151     connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
152     connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
153
154     connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
155     connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
156
157     connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
158     connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
159     connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
160     connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
161     connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
162     connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
163 }
164
165 void TransactionView::setModel(WalletModel *model)
166 {
167     this->model = model;
168     if(model)
169     {
170         transactionProxyModel = new TransactionFilterProxy(this);
171         transactionProxyModel->setSourceModel(model->getTransactionTableModel());
172         transactionProxyModel->setDynamicSortFilter(true);
173         transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
174         transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
175
176 //        transactionProxyModel->setSortRole(Qt::EditRole);
177         transactionProxyModel->setSortRole(TransactionTableModel::DateRole);
178
179         transactionView->setModel(transactionProxyModel);
180         transactionView->setAlternatingRowColors(true);
181         transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
182         transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
183         transactionView->setSortingEnabled(true);
184         transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
185         transactionView->verticalHeader()->hide();
186
187         transactionView->horizontalHeader()->resizeSection(
188                 TransactionTableModel::Status, 23);
189         transactionView->horizontalHeader()->resizeSection(
190                 TransactionTableModel::Date, 120);
191         transactionView->horizontalHeader()->resizeSection(
192                 TransactionTableModel::Type, 120);
193 #if QT_VERSION < 0x050000
194         transactionView->horizontalHeader()->setResizeMode(
195                 TransactionTableModel::ToAddress, QHeaderView::Stretch);
196 #else
197         transactionView->horizontalHeader()->setSectionResizeMode(TransactionTableModel::ToAddress, QHeaderView::Stretch);
198 #endif
199         transactionView->horizontalHeader()->resizeSection(
200                 TransactionTableModel::Amount, 100);
201
202         if (model->getOptionsModel())
203         {
204             // Add third party transaction URLs to context menu
205             QStringList listUrls = model->getOptionsModel()->getThirdPartyTxUrls().split("|", QString::SkipEmptyParts);
206             for (int i = 0; i < listUrls.size(); ++i)
207             {
208                 QString host = QUrl(listUrls[i].trimmed(), QUrl::StrictMode).host();
209                 if (!host.isEmpty())
210                 {
211                     QAction *thirdPartyTxUrlAction = new QAction(host, this); // use host as menu item label
212                     if (i == 0)
213                         contextMenu->addSeparator();
214                     contextMenu->addAction(thirdPartyTxUrlAction);
215                     connect(thirdPartyTxUrlAction, SIGNAL(triggered()), mapperThirdPartyTxUrls, SLOT(map()));
216                     mapperThirdPartyTxUrls->setMapping(thirdPartyTxUrlAction, listUrls[i].trimmed());
217                  }
218              }
219         }
220     }
221 }
222
223 void TransactionView::chooseDate(int idx)
224 {
225     if(!transactionProxyModel)
226         return;
227     QDate current = QDate::currentDate();
228     dateRangeWidget->setVisible(false);
229     switch(dateWidget->itemData(idx).toInt())
230     {
231     case All:
232         transactionProxyModel->setDateRange(
233                 TransactionFilterProxy::MIN_DATE,
234                 TransactionFilterProxy::MAX_DATE);
235         break;
236     case Today:
237         transactionProxyModel->setDateRange(
238                 QDateTime(current),
239                 TransactionFilterProxy::MAX_DATE);
240         break;
241     case ThisWeek: {
242         // Find last Monday
243         QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
244         transactionProxyModel->setDateRange(
245                 QDateTime(startOfWeek),
246                 TransactionFilterProxy::MAX_DATE);
247
248         } break;
249     case ThisMonth:
250         transactionProxyModel->setDateRange(
251                 QDateTime(QDate(current.year(), current.month(), 1)),
252                 TransactionFilterProxy::MAX_DATE);
253         break;
254     case LastMonth:
255         transactionProxyModel->setDateRange(
256                 QDateTime(QDate(current.year(), current.month()-1, 1)),
257                 QDateTime(QDate(current.year(), current.month(), 1)));
258         break;
259     case ThisYear:
260         transactionProxyModel->setDateRange(
261                 QDateTime(QDate(current.year(), 1, 1)),
262                 TransactionFilterProxy::MAX_DATE);
263         break;
264     case Range:
265         dateRangeWidget->setVisible(true);
266         dateRangeChanged();
267         break;
268     }
269 }
270
271 void TransactionView::chooseType(int idx)
272 {
273     if(!transactionProxyModel)
274         return;
275     transactionProxyModel->setTypeFilter(
276         typeWidget->itemData(idx).toInt());
277 }
278
279 void TransactionView::changedPrefix(const QString &prefix)
280 {
281     if(!transactionProxyModel)
282         return;
283     transactionProxyModel->setAddressPrefix(prefix);
284 }
285
286 void TransactionView::changedAmount(const QString &amount)
287 {
288     if(!transactionProxyModel)
289         return;
290     qint64 amount_parsed = 0;
291     if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
292     {
293         transactionProxyModel->setMinAmount(amount_parsed);
294     }
295     else
296     {
297         transactionProxyModel->setMinAmount(0);
298     }
299 }
300
301 void TransactionView::exportClicked()
302 {
303     // CSV is currently the only supported format
304     QString filename = GUIUtil::getSaveFileName(
305             this,
306             tr("Export Transaction Data"), QString(),
307             tr("Comma separated file (*.csv)"));
308
309     if (filename.isNull()) return;
310
311     CSVModelWriter writer(filename);
312
313     // name, column, role
314     writer.setModel(transactionProxyModel);
315     writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
316     writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
317     writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
318     writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
319     writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
320     writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
321     writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
322
323     if(!writer.write())
324     {
325         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
326                               QMessageBox::Abort, QMessageBox::Abort);
327     }
328 }
329
330 void TransactionView::contextualMenu(const QPoint &point)
331 {
332     QModelIndex index = transactionView->indexAt(point);
333     if(index.isValid())
334     {
335         contextMenu->exec(QCursor::pos());
336     }
337 }
338
339 void TransactionView::copyAddress()
340 {
341     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
342 }
343
344 void TransactionView::copyLabel()
345 {
346     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
347 }
348
349 void TransactionView::copyAmount()
350 {
351     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
352 }
353
354 void TransactionView::copyTxID()
355 {
356     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::TxIDRole);
357 }
358
359 void TransactionView::editLabel()
360 {
361     if(!transactionView->selectionModel() ||!model)
362         return;
363     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
364     if(!selection.isEmpty())
365     {
366         AddressTableModel *addressBook = model->getAddressTableModel();
367         if(!addressBook)
368             return;
369         QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
370         if(address.isEmpty())
371         {
372             // If this transaction has no associated address, exit
373             return;
374         }
375         // Is address in address book? Address book can miss address when a transaction is
376         // sent from outside the UI.
377         int idx = addressBook->lookupAddress(address);
378         if(idx != -1)
379         {
380             // Edit sending / receiving address
381             QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
382             // Determine type of address, launch appropriate editor dialog type
383             QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
384
385             EditAddressDialog dlg(type==AddressTableModel::Receive
386                                          ? EditAddressDialog::EditReceivingAddress
387                                          : EditAddressDialog::EditSendingAddress,
388                                   this);
389             dlg.setModel(addressBook);
390             dlg.loadRow(idx);
391             dlg.exec();
392         }
393         else
394         {
395             // Add sending address
396             EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
397                                   this);
398             dlg.setModel(addressBook);
399             dlg.setAddress(address);
400             dlg.exec();
401         }
402     }
403 }
404
405 void TransactionView::showDetails()
406 {
407     if(!transactionView->selectionModel())
408         return;
409     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
410     if(!selection.isEmpty())
411     {
412         TransactionDescDialog dlg(selection.at(0));
413         dlg.exec();
414     }
415 }
416
417 void TransactionView::openThirdPartyTxUrl(QString url)
418 {
419     if(!transactionView->selectionModel())
420        return;
421     QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
422     if(!selection.isEmpty())
423         QDesktopServices::openUrl(QUrl::fromUserInput(url.replace("%s", selection.at(0).data(TransactionTableModel::TxHashRole).toString())));
424 }
425
426 QWidget *TransactionView::createDateRangeWidget()
427 {
428     dateRangeWidget = new QFrame();
429     dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
430     dateRangeWidget->setContentsMargins(1,1,1,1);
431     QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
432     layout->setContentsMargins(0,0,0,0);
433     layout->addSpacing(23);
434     layout->addWidget(new QLabel(tr("Range:")));
435
436     dateFrom = new QDateTimeEdit(this);
437     dateFrom->setDisplayFormat("dd/MM/yy");
438     dateFrom->setCalendarPopup(true);
439     dateFrom->setMinimumWidth(100);
440     dateFrom->setDate(QDate::currentDate().addDays(-7));
441     layout->addWidget(dateFrom);
442     layout->addWidget(new QLabel(tr("to")));
443
444     dateTo = new QDateTimeEdit(this);
445     dateTo->setDisplayFormat("dd/MM/yy");
446     dateTo->setCalendarPopup(true);
447     dateTo->setMinimumWidth(100);
448     dateTo->setDate(QDate::currentDate());
449     layout->addWidget(dateTo);
450     layout->addStretch();
451
452     // Hide by default
453     dateRangeWidget->setVisible(false);
454
455     // Notify on change
456     connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
457     connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
458
459     return dateRangeWidget;
460 }
461
462 void TransactionView::dateRangeChanged()
463 {
464     if(!transactionProxyModel)
465         return;
466     transactionProxyModel->setDateRange(
467             QDateTime(dateFrom->date()),
468             QDateTime(dateTo->date()).addDays(1));
469 }
470
471 void TransactionView::focusTransaction(const QModelIndex &idx)
472 {
473     if(!transactionProxyModel)
474         return;
475     QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
476     transactionView->scrollTo(targetIdx);
477     transactionView->setCurrentIndex(targetIdx);
478     transactionView->setFocus();
479 }