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