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