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