Improved Mac experience; QDoubleSpinBox for BitcoinAmountField
[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
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     QDate current = QDate::currentDate();
189     dateRangeWidget->setVisible(false);
190     switch(dateWidget->itemData(idx).toInt())
191     {
192     case All:
193         transactionProxyModel->setDateRange(
194                 TransactionFilterProxy::MIN_DATE,
195                 TransactionFilterProxy::MAX_DATE);
196         break;
197     case Today:
198         transactionProxyModel->setDateRange(
199                 QDateTime(current),
200                 TransactionFilterProxy::MAX_DATE);
201         break;
202     case ThisWeek: {
203         // Find last monday
204         QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
205         transactionProxyModel->setDateRange(
206                 QDateTime(startOfWeek),
207                 TransactionFilterProxy::MAX_DATE);
208
209         } break;
210     case ThisMonth:
211         transactionProxyModel->setDateRange(
212                 QDateTime(QDate(current.year(), current.month(), 1)),
213                 TransactionFilterProxy::MAX_DATE);
214         break;
215     case LastMonth:
216         transactionProxyModel->setDateRange(
217                 QDateTime(QDate(current.year(), current.month()-1, 1)),
218                 QDateTime(QDate(current.year(), current.month(), 1)));
219         break;
220     case ThisYear:
221         transactionProxyModel->setDateRange(
222                 QDateTime(QDate(current.year(), 1, 1)),
223                 TransactionFilterProxy::MAX_DATE);
224         break;
225     case Range:
226         dateRangeWidget->setVisible(true);
227         dateRangeChanged();
228         break;
229     }
230 }
231
232 void TransactionView::chooseType(int idx)
233 {
234     transactionProxyModel->setTypeFilter(
235         typeWidget->itemData(idx).toInt());
236 }
237
238 void TransactionView::changedPrefix(const QString &prefix)
239 {
240     transactionProxyModel->setAddressPrefix(prefix);
241 }
242
243 void TransactionView::changedAmount(const QString &amount)
244 {
245     qint64 amount_parsed = 0;
246     if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
247     {
248         transactionProxyModel->setMinAmount(amount_parsed);
249     }
250     else
251     {
252         transactionProxyModel->setMinAmount(0);
253     }
254 }
255
256 void TransactionView::exportClicked()
257 {
258     // CSV is currently the only supported format
259     QString filename = QFileDialog::getSaveFileName(
260             this,
261             tr("Export Transaction Data"),
262             QDir::currentPath(),
263             tr("Comma separated file (*.csv)"));
264
265     if (filename.isNull()) return;
266
267     CSVModelWriter writer(filename);
268
269     // name, column, role
270     writer.setModel(transactionProxyModel);
271     writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
272     writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
273     writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
274     writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
275     writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
276     writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
277     writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
278
279     if(!writer.write())
280     {
281         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
282                               QMessageBox::Abort, QMessageBox::Abort);
283     }
284 }
285
286 void TransactionView::contextualMenu(const QPoint &point)
287 {
288     QModelIndex index = transactionView->indexAt(point);
289     if(index.isValid())
290     {
291         contextMenu->exec(QCursor::pos());
292     }
293 }
294
295 void TransactionView::copyAddress()
296 {
297     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
298     if(!selection.isEmpty())
299     {
300         QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::AddressRole).toString());
301     }
302 }
303
304 void TransactionView::copyLabel()
305 {
306     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
307     if(!selection.isEmpty())
308     {
309         QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::LabelRole).toString());
310     }
311 }
312
313 void TransactionView::editLabel()
314 {
315     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
316     if(!selection.isEmpty())
317     {
318         AddressTableModel *addressBook = model->getAddressTableModel();
319         QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
320         if(address.isEmpty())
321         {
322             // If this transaction has no associated address, exit
323             return;
324         }
325         int idx = addressBook->lookupAddress(address);
326         if(idx != -1)
327         {
328             // Edit sending / receiving address
329             QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
330             // Determine type of address, launch appropriate editor dialog type
331             QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
332
333             EditAddressDialog dlg(type==AddressTableModel::Receive
334                                          ? EditAddressDialog::EditReceivingAddress
335                                          : EditAddressDialog::EditSendingAddress,
336                                   this);
337             dlg.setModel(addressBook);
338             dlg.loadRow(idx);
339             dlg.exec();
340         }
341         else
342         {
343             // Add sending address
344             EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
345                                   this);
346             dlg.exec();
347         }
348     }
349 }
350
351 void TransactionView::showDetails()
352 {
353     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
354     if(!selection.isEmpty())
355     {
356         TransactionDescDialog dlg(selection.at(0));
357         dlg.exec();
358     }
359 }
360
361 QWidget *TransactionView::createDateRangeWidget()
362 {
363     dateRangeWidget = new QFrame();
364     dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
365     dateRangeWidget->setContentsMargins(1,1,1,1);
366     QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
367     layout->setContentsMargins(0,0,0,0);
368     layout->addSpacing(23);
369     layout->addWidget(new QLabel(tr("Range:")));
370
371     dateFrom = new QDateTimeEdit(this);
372     dateFrom->setDisplayFormat("dd/MM/yy");
373     dateFrom->setCalendarPopup(true);
374     dateFrom->setMinimumWidth(100);
375     dateFrom->setDate(QDate::currentDate().addDays(-7));
376     layout->addWidget(dateFrom);
377     layout->addWidget(new QLabel(tr("to")));
378
379     dateTo = new QDateTimeEdit(this);
380     dateTo->setDisplayFormat("dd/MM/yy");
381     dateTo->setCalendarPopup(true);
382     dateTo->setMinimumWidth(100);
383     dateTo->setDate(QDate::currentDate());
384     layout->addWidget(dateTo);
385     layout->addStretch();
386
387     // Hide by default
388     dateRangeWidget->setVisible(false);
389
390     // Notify on change
391     connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
392     connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
393
394     return dateRangeWidget;
395 }
396
397 void TransactionView::dateRangeChanged()
398 {
399     transactionProxyModel->setDateRange(
400             QDateTime(dateFrom->date()),
401             QDateTime(dateTo->date()).addDays(1));
402 }