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