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