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