PPCoin: Qt: display coinstake as single 'mint by stake' transaction
[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_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::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("Mint by stake"), TransactionFilterProxy::TYPE(TransactionRecord::StakeMint));
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 = 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     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::AddressRole);
305 }
306
307 void TransactionView::copyLabel()
308 {
309     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::LabelRole);
310 }
311
312 void TransactionView::copyAmount()
313 {
314     GUIUtil::copyEntryData(transactionView, 0, TransactionTableModel::FormattedAmountRole);
315 }
316
317 void TransactionView::editLabel()
318 {
319     if(!transactionView->selectionModel() ||!model)
320         return;
321     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
322     if(!selection.isEmpty())
323     {
324         AddressTableModel *addressBook = model->getAddressTableModel();
325         if(!addressBook)
326             return;
327         QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
328         if(address.isEmpty())
329         {
330             // If this transaction has no associated address, exit
331             return;
332         }
333         // Is address in address book? Address book can miss address when a transaction is
334         // sent from outside the UI.
335         int idx = addressBook->lookupAddress(address);
336         if(idx != -1)
337         {
338             // Edit sending / receiving address
339             QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
340             // Determine type of address, launch appropriate editor dialog type
341             QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
342
343             EditAddressDialog dlg(type==AddressTableModel::Receive
344                                          ? EditAddressDialog::EditReceivingAddress
345                                          : EditAddressDialog::EditSendingAddress,
346                                   this);
347             dlg.setModel(addressBook);
348             dlg.loadRow(idx);
349             dlg.exec();
350         }
351         else
352         {
353             // Add sending address
354             EditAddressDialog dlg(EditAddressDialog::NewSendingAddress,
355                                   this);
356             dlg.setModel(addressBook);
357             dlg.setAddress(address);
358             dlg.exec();
359         }
360     }
361 }
362
363 void TransactionView::showDetails()
364 {
365     if(!transactionView->selectionModel())
366         return;
367     QModelIndexList selection = transactionView->selectionModel()->selectedRows();
368     if(!selection.isEmpty())
369     {
370         TransactionDescDialog dlg(selection.at(0));
371         dlg.exec();
372     }
373 }
374
375 QWidget *TransactionView::createDateRangeWidget()
376 {
377     dateRangeWidget = new QFrame();
378     dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
379     dateRangeWidget->setContentsMargins(1,1,1,1);
380     QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
381     layout->setContentsMargins(0,0,0,0);
382     layout->addSpacing(23);
383     layout->addWidget(new QLabel(tr("Range:")));
384
385     dateFrom = new QDateTimeEdit(this);
386     dateFrom->setDisplayFormat("dd/MM/yy");
387     dateFrom->setCalendarPopup(true);
388     dateFrom->setMinimumWidth(100);
389     dateFrom->setDate(QDate::currentDate().addDays(-7));
390     layout->addWidget(dateFrom);
391     layout->addWidget(new QLabel(tr("to")));
392
393     dateTo = new QDateTimeEdit(this);
394     dateTo->setDisplayFormat("dd/MM/yy");
395     dateTo->setCalendarPopup(true);
396     dateTo->setMinimumWidth(100);
397     dateTo->setDate(QDate::currentDate());
398     layout->addWidget(dateTo);
399     layout->addStretch();
400
401     // Hide by default
402     dateRangeWidget->setVisible(false);
403
404     // Notify on change
405     connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
406     connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
407
408     return dateRangeWidget;
409 }
410
411 void TransactionView::dateRangeChanged()
412 {
413     if(!transactionProxyModel)
414         return;
415     transactionProxyModel->setDateRange(
416             QDateTime(dateFrom->date()),
417             QDateTime(dateTo->date()).addDays(1));
418 }