fix default suffixes in save dialog in GNOME, make it more clear that PNG is used...
[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 <QMessageBox>
24 #include <QPoint>
25 #include <QMenu>
26 #include <QApplication>
27 #include <QClipboard>
28 #include <QLabel>
29 #include <QDateTimeEdit>
30
31 TransactionView::TransactionView(QWidget *parent) :
32     QWidget(parent), model(0), transactionProxyModel(0),
33     transactionView(0)
34 {
35     // Build filter row
36     setContentsMargins(0,0,0,0);
37
38     QHBoxLayout *hlayout = new QHBoxLayout();
39     hlayout->setContentsMargins(0,0,0,0);
40 #ifdef Q_WS_MAC
41     hlayout->setSpacing(5);
42     hlayout->addSpacing(26);
43 #else
44     hlayout->setSpacing(0);
45     hlayout->addSpacing(23);
46 #endif
47
48     dateWidget = new QComboBox(this);
49 #ifdef Q_WS_MAC
50     dateWidget->setFixedWidth(121);
51 #else
52     dateWidget->setFixedWidth(120);
53 #endif
54     dateWidget->addItem(tr("All"), All);
55     dateWidget->addItem(tr("Today"), Today);
56     dateWidget->addItem(tr("This week"), ThisWeek);
57     dateWidget->addItem(tr("This month"), ThisMonth);
58     dateWidget->addItem(tr("Last month"), LastMonth);
59     dateWidget->addItem(tr("This year"), ThisYear);
60     dateWidget->addItem(tr("Range..."), Range);
61     hlayout->addWidget(dateWidget);
62
63     typeWidget = new QComboBox(this);
64 #ifdef Q_WS_MAC
65     typeWidget->setFixedWidth(121);
66 #else
67     typeWidget->setFixedWidth(120);
68 #endif
69
70     typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
71     typeWidget->addItem(tr("Received with"), TransactionFilterProxy::TYPE(TransactionRecord::RecvWithAddress) |
72                                         TransactionFilterProxy::TYPE(TransactionRecord::RecvFromOther));
73     typeWidget->addItem(tr("Sent to"), TransactionFilterProxy::TYPE(TransactionRecord::SendToAddress) |
74                                   TransactionFilterProxy::TYPE(TransactionRecord::SendToOther));
75     typeWidget->addItem(tr("To yourself"), TransactionFilterProxy::TYPE(TransactionRecord::SendToSelf));
76     typeWidget->addItem(tr("Mined"), TransactionFilterProxy::TYPE(TransactionRecord::Generated));
77     typeWidget->addItem(tr("Other"), TransactionFilterProxy::TYPE(TransactionRecord::Other));
78
79     hlayout->addWidget(typeWidget);
80
81     addressWidget = new QLineEdit(this);
82 #if QT_VERSION >= 0x040700
83     addressWidget->setPlaceholderText(tr("Enter address or label to search"));
84 #endif
85     hlayout->addWidget(addressWidget);
86
87     amountWidget = new QLineEdit(this);
88 #if QT_VERSION >= 0x040700
89     amountWidget->setPlaceholderText(tr("Min amount"));
90 #endif
91 #ifdef Q_WS_MAC
92     amountWidget->setFixedWidth(97);
93 #else
94     amountWidget->setFixedWidth(100);
95 #endif
96     amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
97     hlayout->addWidget(amountWidget);
98
99     QVBoxLayout *vlayout = new QVBoxLayout(this);
100     vlayout->setContentsMargins(0,0,0,0);
101     vlayout->setSpacing(0);
102     //vlayout->addLayout(hlayout2);
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 *editLabelAction = new QAction(tr("Edit label"), this);
127     QAction *showDetailsAction = new QAction(tr("Show details..."), this);
128
129     contextMenu = new QMenu();
130     contextMenu->addAction(copyAddressAction);
131     contextMenu->addAction(copyLabelAction);
132     contextMenu->addAction(editLabelAction);
133     contextMenu->addAction(showDetailsAction);
134
135     // Connect actions
136     connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
137     connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
138     connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
139     connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
140
141     connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
142
143     connect(view,
144             SIGNAL(customContextMenuRequested(QPoint)),
145             this,
146             SLOT(contextualMenu(QPoint)));
147
148     connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
149     connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
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 = GUIUtil::getSaveFileName(
268             this,
269             tr("Export Transaction Data"), QString(),
270             tr("Comma separated file (*.csv)"));
271
272     if (filename.isNull()) return;
273
274     CSVModelWriter writer(filename);
275
276     // name, column, role
277     writer.setModel(transactionProxyModel);
278     writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
279     writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
280     writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
281     writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
282     writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
283     writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
284     writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
285
286     if(!writer.write())
287     {
288         QMessageBox::critical(this, tr("Error exporting"), tr("Could not write to file %1.").arg(filename),
289                               QMessageBox::Abort, QMessageBox::Abort);
290     }
291 }
292
293 void TransactionView::contextualMenu(const QPoint &point)
294 {
295     QModelIndex index = transactionView->indexAt(point);
296     if(index.isValid())
297     {
298         contextMenu->exec(QCursor::pos());
299     }
300 }
301
302 void TransactionView::copyAddress()
303 {
304     if(!transactionView->selectionModel())
305         return;
306     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
307     if(!selection.isEmpty())
308     {
309         QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::AddressRole).toString());
310     }
311 }
312
313 void TransactionView::copyLabel()
314 {
315     if(!transactionView->selectionModel())
316         return;
317     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
318     if(!selection.isEmpty())
319     {
320         QApplication::clipboard()->setText(selection.at(0).data(TransactionTableModel::LabelRole).toString());
321     }
322 }
323
324 void TransactionView::editLabel()
325 {
326     if(!transactionView->selectionModel() ||!model)
327         return;
328     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
329     if(!selection.isEmpty())
330     {
331         AddressTableModel *addressBook = model->getAddressTableModel();
332         if(!addressBook)
333             return;
334         QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
335         if(address.isEmpty())
336         {
337             // If this transaction has no associated address, exit
338             return;
339         }
340         // Is address in address book? Address book can miss address when a transaction is
341         // sent from outside the UI.
342         int idx = addressBook->lookupAddress(address);
343         if(idx != -1)
344         {
345             // Edit sending / receiving address
346             QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
347             // Determine type of address, launch appropriate editor dialog type
348             QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
349
350             EditAddressDialog dlg(type==AddressTableModel::Receive
351                                          ? EditAddressDialog::EditReceivingAddress
352                                          : EditAddressDialog::EditSendingAddress,
353                                   this);
354             dlg.setModel(addressBook);
355             dlg.loadRow(idx);
356             dlg.exec();
357         }
358         else
359         {
360             // Add sending address
361             EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
362                                   this);
363             dlg.setModel(addressBook);
364             dlg.setAddress(address);
365             dlg.exec();
366         }
367     }
368 }
369
370 void TransactionView::showDetails()
371 {
372     if(!transactionView->selectionModel())
373         return;
374     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
375     if(!selection.isEmpty())
376     {
377         TransactionDescDialog dlg(selection.at(0));
378         dlg.exec();
379     }
380 }
381
382 QWidget *TransactionView::createDateRangeWidget()
383 {
384     dateRangeWidget = new QFrame();
385     dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
386     dateRangeWidget->setContentsMargins(1,1,1,1);
387     QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
388     layout->setContentsMargins(0,0,0,0);
389     layout->addSpacing(23);
390     layout->addWidget(new QLabel(tr("Range:")));
391
392     dateFrom = new QDateTimeEdit(this);
393     dateFrom->setDisplayFormat("dd/MM/yy");
394     dateFrom->setCalendarPopup(true);
395     dateFrom->setMinimumWidth(100);
396     dateFrom->setDate(QDate::currentDate().addDays(-7));
397     layout->addWidget(dateFrom);
398     layout->addWidget(new QLabel(tr("to")));
399
400     dateTo = new QDateTimeEdit(this);
401     dateTo->setDisplayFormat("dd/MM/yy");
402     dateTo->setCalendarPopup(true);
403     dateTo->setMinimumWidth(100);
404     dateTo->setDate(QDate::currentDate());
405     layout->addWidget(dateTo);
406     layout->addStretch();
407
408     // Hide by default
409     dateRangeWidget->setVisible(false);
410
411     // Notify on change
412     connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
413     connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
414
415     return dateRangeWidget;
416 }
417
418 void TransactionView::dateRangeChanged()
419 {
420     if(!transactionProxyModel)
421         return;
422     transactionProxyModel->setDateRange(
423             QDateTime(dateFrom->date()),
424             QDateTime(dateTo->date()).addDays(1));
425 }