Anoncoin  0.9.4
P2P Digital Currency
transactionview.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Copyright (c) 2013-2014 The Anoncoin Core developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #include "transactionview.h"
7 
8 #include "addresstablemodel.h"
9 #include "anoncoinunits.h"
10 #include "csvmodelwriter.h"
11 #include "editaddressdialog.h"
12 #include "guiutil.h"
13 #include "optionsmodel.h"
14 #include "transactiondescdialog.h"
15 #include "transactionfilterproxy.h"
16 #include "transactionrecord.h"
17 #include "transactiontablemodel.h"
18 #include "walletmodel.h"
19 
20 #include "ui_interface.h"
21 
22 #include <QComboBox>
23 #include <QDateTimeEdit>
24 #include <QDesktopServices>
25 #include <QDoubleValidator>
26 #include <QHBoxLayout>
27 #include <QHeaderView>
28 #include <QLabel>
29 #include <QLineEdit>
30 #include <QMenu>
31 #include <QPoint>
32 #include <QScrollBar>
33 #include <QSignalMapper>
34 #include <QTableView>
35 #include <QUrl>
36 #include <QVBoxLayout>
37 
39  QWidget(parent), model(0), transactionProxyModel(0),
40  transactionView(0)
41 {
42  // Build filter row
43  setContentsMargins(0,0,0,0);
44 
45  QHBoxLayout *hlayout = new QHBoxLayout();
46  hlayout->setContentsMargins(0,0,0,0);
47 #ifdef Q_OS_MAC
48  hlayout->setSpacing(5);
49  hlayout->addSpacing(26);
50 #else
51  hlayout->setSpacing(0);
52  hlayout->addSpacing(23);
53 #endif
54 
55  watchOnlyWidget = new QComboBox(this);
56  watchOnlyWidget->setFixedWidth(24);
58  watchOnlyWidget->addItem(QIcon(":/icons/eye_plus"), "", TransactionFilterProxy::WatchOnlyFilter_Yes);
59  watchOnlyWidget->addItem(QIcon(":/icons/eye_minus"), "", TransactionFilterProxy::WatchOnlyFilter_No);
60  hlayout->addWidget(watchOnlyWidget);
61 
62  dateWidget = new QComboBox(this);
63 #ifdef Q_OS_MAC
64  dateWidget->setFixedWidth(121);
65 #else
66  dateWidget->setFixedWidth(120);
67 #endif
68  dateWidget->addItem(tr("All"), All);
69  dateWidget->addItem(tr("Today"), Today);
70  dateWidget->addItem(tr("This week"), ThisWeek);
71  dateWidget->addItem(tr("This month"), ThisMonth);
72  dateWidget->addItem(tr("Last month"), LastMonth);
73  dateWidget->addItem(tr("This year"), ThisYear);
74  dateWidget->addItem(tr("Range..."), Range);
75  hlayout->addWidget(dateWidget);
76 
77  typeWidget = new QComboBox(this);
78 #ifdef Q_OS_MAC
79  typeWidget->setFixedWidth(121);
80 #else
81  typeWidget->setFixedWidth(120);
82 #endif
83 
84  typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
92 
93  hlayout->addWidget(typeWidget);
94 
95  addressWidget = new QLineEdit(this);
96 #if QT_VERSION >= 0x040700
97  addressWidget->setPlaceholderText(tr("Enter address or label to search"));
98 #endif
99  hlayout->addWidget(addressWidget);
100 
101  amountWidget = new QLineEdit(this);
102 #if QT_VERSION >= 0x040700
103  amountWidget->setPlaceholderText(tr("Min amount"));
104 #endif
105 #ifdef Q_OS_MAC
106  amountWidget->setFixedWidth(97);
107 #else
108  amountWidget->setFixedWidth(100);
109 #endif
110  amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
111  hlayout->addWidget(amountWidget);
112 
113  QVBoxLayout *vlayout = new QVBoxLayout(this);
114  vlayout->setContentsMargins(0,0,0,0);
115  vlayout->setSpacing(0);
116 
117  QTableView *view = new QTableView(this);
118  vlayout->addLayout(hlayout);
119  vlayout->addWidget(createDateRangeWidget());
120  vlayout->addWidget(view);
121  vlayout->setSpacing(0);
122  int width = view->verticalScrollBar()->sizeHint().width();
123  // Cover scroll bar width with spacing
124 #ifdef Q_OS_MAC
125  hlayout->addSpacing(width+2);
126 #else
127  hlayout->addSpacing(width);
128 #endif
129  // Always show scroll bar
130  view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
131  view->setTabKeyNavigation(false);
132  view->setContextMenuPolicy(Qt::CustomContextMenu);
133 
134  transactionView = view;
135 
136  // Actions
137  QAction *copyAddressAction = new QAction(tr("Copy address"), this);
138  QAction *copyLabelAction = new QAction(tr("Copy label"), this);
139  QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
140  QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
141  QAction *editLabelAction = new QAction(tr("Edit label"), this);
142  QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
143 
144  contextMenu = new QMenu();
145  contextMenu->addAction(copyAddressAction);
146  contextMenu->addAction(copyLabelAction);
147  contextMenu->addAction(copyAmountAction);
148  contextMenu->addAction(copyTxIDAction);
149  contextMenu->addAction(editLabelAction);
150  contextMenu->addAction(showDetailsAction);
151 
152  mapperThirdPartyTxUrls = new QSignalMapper(this);
153 
154  // Connect actions
155  connect(mapperThirdPartyTxUrls, SIGNAL(mapped(QString)), this, SLOT(openThirdPartyTxUrl(QString)));
156 
157  connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
158  connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
159  connect(watchOnlyWidget, SIGNAL(activated(int)), this, SLOT(chooseWatchonly(int)));
160  connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
161  connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
162 
163  connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
164  connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
165 
166  connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
167  connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
168  connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
169  connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
170  connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
171  connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
172 }
173 
175 {
176  this->model = model;
177  if(model)
178  {
180  transactionProxyModel->setSourceModel(model->getTransactionTableModel());
181  transactionProxyModel->setDynamicSortFilter(true);
182  transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
183  transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
184 
185  transactionProxyModel->setSortRole(Qt::EditRole);
186 
187  transactionView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
189  transactionView->setAlternatingRowColors(true);
190  transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
191  transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
192  transactionView->setSortingEnabled(true);
193  transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
194  transactionView->verticalHeader()->hide();
195 
201 
203 
204  if (model->getOptionsModel())
205  {
206  // Add third party transaction URLs to context menu
207  QStringList listUrls = model->getOptionsModel()->getThirdPartyTxUrls().split("|", QString::SkipEmptyParts);
208  for (int i = 0; i < listUrls.size(); ++i)
209  {
210  QString host = QUrl(listUrls[i].trimmed(), QUrl::StrictMode).host();
211  if (!host.isEmpty())
212  {
213  QAction *thirdPartyTxUrlAction = new QAction(host, this); // use host as menu item label
214  if (i == 0)
215  contextMenu->addSeparator();
216  contextMenu->addAction(thirdPartyTxUrlAction);
217  connect(thirdPartyTxUrlAction, SIGNAL(triggered()), mapperThirdPartyTxUrls, SLOT(map()));
218  mapperThirdPartyTxUrls->setMapping(thirdPartyTxUrlAction, listUrls[i].trimmed());
219  }
220  }
221  }
222 
223  // show/hide column Watch-only
225 
226  // Watch-only signal
227  connect(model, SIGNAL(notifyWatchonlyChanged(bool)), this, SLOT(updateWatchOnlyColumn(bool)));
228  }
229 }
230 
232 {
234  return;
235  QDate current = QDate::currentDate();
236  dateRangeWidget->setVisible(false);
237  switch(dateWidget->itemData(idx).toInt())
238  {
239  case All:
243  break;
244  case Today:
246  QDateTime(current),
248  break;
249  case ThisWeek: {
250  // Find last Monday
251  QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
253  QDateTime(startOfWeek),
255 
256  } break;
257  case ThisMonth:
259  QDateTime(QDate(current.year(), current.month(), 1)),
261  break;
262  case LastMonth:
264  QDateTime(QDate(current.year(), current.month()-1, 1)),
265  QDateTime(QDate(current.year(), current.month(), 1)));
266  break;
267  case ThisYear:
269  QDateTime(QDate(current.year(), 1, 1)),
271  break;
272  case Range:
273  dateRangeWidget->setVisible(true);
275  break;
276  }
277 }
278 
280 {
282  return;
284  typeWidget->itemData(idx).toInt());
285 }
286 
288 {
290  return;
293 }
294 
295 void TransactionView::changedPrefix(const QString &prefix)
296 {
298  return;
300 }
301 
302 void TransactionView::changedAmount(const QString &amount)
303 {
305  return;
306  qint64 amount_parsed = 0;
307  if(AnoncoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
308  {
309  transactionProxyModel->setMinAmount(amount_parsed);
310  }
311  else
312  {
314  }
315 }
316 
318 {
319  // CSV is currently the only supported format
320  QString filename = GUIUtil::getSaveFileName(this,
321  tr("Export Transaction History"), QString(),
322  tr("Comma separated file (*.csv)"), NULL);
323 
324  if (filename.isNull())
325  return;
326 
327  CSVModelWriter writer(filename);
328 
329  // name, column, role
331  writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
332  if (model && model->haveWatchOnly())
333  writer.addColumn(tr("Watch-only"), TransactionTableModel::Watchonly);
334  writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
335  writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
336  writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
337  writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
339  writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
340 
341  if(!writer.write()) {
342  emit message(tr("Exporting Failed"), tr("There was an error trying to save the transaction history to %1.").arg(filename),
344  }
345  else {
346  emit message(tr("Exporting Successful"), tr("The transaction history was successfully saved to %1.").arg(filename),
348  }
349 }
350 
351 void TransactionView::contextualMenu(const QPoint &point)
352 {
353  QModelIndex index = transactionView->indexAt(point);
354  if(index.isValid())
355  {
356  contextMenu->exec(QCursor::pos());
357  }
358 }
359 
361 {
363 }
364 
366 {
368 }
369 
371 {
373 }
374 
376 {
378 }
379 
381 {
382  if(!transactionView->selectionModel() ||!model)
383  return;
384  QModelIndexList selection = transactionView->selectionModel()->selectedRows();
385  if(!selection.isEmpty())
386  {
387  AddressTableModel *addressBook = model->getAddressTableModel();
388  if(!addressBook)
389  return;
390  QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
391  if(address.isEmpty())
392  {
393  // If this transaction has no associated address, exit
394  return;
395  }
396  // Is address in address book? Address book can miss address when a transaction is
397  // sent from outside the UI.
398  int idx = addressBook->lookupAddress(address);
399  if(idx != -1)
400  {
401  // Edit sending / receiving address
402  QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
403  // Determine type of address, launch appropriate editor dialog type
404  QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
405 
406  EditAddressDialog dlg(
410  dlg.setModel(addressBook);
411  dlg.loadRow(idx);
412  dlg.exec();
413  }
414  else
415  {
416  // Add sending address
418  this);
419  dlg.setModel(addressBook);
420  dlg.setAddress(address);
421  dlg.exec();
422  }
423  }
424 }
425 
427 {
428  if(!transactionView->selectionModel())
429  return;
430  QModelIndexList selection = transactionView->selectionModel()->selectedRows();
431  if(!selection.isEmpty())
432  {
433  TransactionDescDialog dlg(selection.at(0));
434  dlg.exec();
435  }
436 }
437 
439 {
440  if(!transactionView || !transactionView->selectionModel())
441  return;
442  QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
443  if(!selection.isEmpty())
444  QDesktopServices::openUrl(QUrl::fromUserInput(url.replace("%s", selection.at(0).data(TransactionTableModel::TxHashRole).toString())));
445 }
446 
448 {
449  dateRangeWidget = new QFrame();
450  dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
451  dateRangeWidget->setContentsMargins(1,1,1,1);
452  QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
453  layout->setContentsMargins(0,0,0,0);
454  layout->addSpacing(23);
455  layout->addWidget(new QLabel(tr("Range:")));
456 
457  dateFrom = new QDateTimeEdit(this);
458  dateFrom->setDisplayFormat("dd/MM/yy");
459  dateFrom->setCalendarPopup(true);
460  dateFrom->setMinimumWidth(100);
461  dateFrom->setDate(QDate::currentDate().addDays(-7));
462  layout->addWidget(dateFrom);
463  layout->addWidget(new QLabel(tr("to")));
464 
465  dateTo = new QDateTimeEdit(this);
466  dateTo->setDisplayFormat("dd/MM/yy");
467  dateTo->setCalendarPopup(true);
468  dateTo->setMinimumWidth(100);
469  dateTo->setDate(QDate::currentDate());
470  layout->addWidget(dateTo);
471  layout->addStretch();
472 
473  // Hide by default
474  dateRangeWidget->setVisible(false);
475 
476  // Notify on change
477  connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
478  connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
479 
480  return dateRangeWidget;
481 }
482 
484 {
486  return;
488  QDateTime(dateFrom->date()),
489  QDateTime(dateTo->date()).addDays(1));
490 }
491 
492 void TransactionView::focusTransaction(const QModelIndex &idx)
493 {
495  return;
496  QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
497  transactionView->scrollTo(targetIdx);
498  transactionView->setCurrentIndex(targetIdx);
499  transactionView->setFocus();
500 }
501 
502 // We override the virtual resizeEvent of the QWidget to adjust tables column
503 // sizes as the tables width is proportional to the dialogs width.
504 void TransactionView::resizeEvent(QResizeEvent* event)
505 {
506  QWidget::resizeEvent(event);
508 }
509 
510 // show/hide column Watch-only
511 void TransactionView::updateWatchOnlyColumn(bool fHaveWatchOnly)
512 {
513  watchOnlyWidget->setVisible(fHaveWatchOnly);
514  transactionView->setColumnHidden(TransactionTableModel::Watchonly, !fHaveWatchOnly);
515 }
516 
void changedPrefix(const QString &prefix)
TransactionView(QWidget *parent=0)
void addColumn(const QString &title, int column, int role=Qt::EditRole)
QModelIndex index(int row, int column, const QModelIndex &parent) const
void openThirdPartyTxUrl(QString url)
QWidget * createDateRangeWidget()
Dialog showing transaction details.
int lookupAddress(const QString &address) const
void focusTransaction(const QModelIndex &)
TransactionRecord * index(int idx)
void setTypeFilter(quint32 modes)
QTableView * transactionView
static bool parse(int unit, const QString &value, qint64 *val_out)
Parse string to coin amount.
AddressTableModel * getAddressTableModel()
Export a Qt table model to a CSV file.
TransactionTableModel * parent
void chooseWatchonly(int idx)
void setAddressPrefix(const QString &addrPrefix)
static quint32 TYPE(int type)
QDateTimeEdit * dateTo
const char * url
Definition: rpcconsole.cpp:51
static QString getAmountColumnTitle(int unit)
Gets title for amount column including current display unit if optionsModel reference available */...
void setModel(AddressTableModel *model)
static const QDateTime MIN_DATE
Earliest date that can be represented (far in the past)
virtual void resizeEvent(QResizeEvent *event)
static const QDateTime MAX_DATE
Last date that can be represented (far in the future)
QSignalMapper * mapperThirdPartyTxUrls
void setDateRange(const QDateTime &from, const QDateTime &to)
void message(const QString &title, const QString &message, unsigned int style)
Fired when a message should be reported to the user.
Makes a QTableView last column feel as if it was being resized from its left border.
Definition: guiutil.h:140
void changedAmount(const QString &amount)
void setMinAmount(qint64 minimum)
Date and time this transaction was created.
void updateWatchOnlyColumn(bool fHaveWatchOnly)
TransactionTableModel * getTransactionTableModel()
int getDisplayUnit()
Definition: optionsmodel.h:87
void setWatchOnlyFilter(WatchOnlyFilter filter)
Qt model of the address book in the core.
TransactionFilterProxy * transactionProxyModel
QComboBox * watchOnlyWidget
void chooseDate(int idx)
void setModel(const QAbstractItemModel *model)
void setModel(WalletModel *model)
QLineEdit * amountWidget
QComboBox * typeWidget
QVariant data(const QModelIndex &index, int role) const
Filter the transaction list according to pre-specified rules.
void setAddress(const QString &address)
Interface to Anoncoin wallet from Qt view code.
Definition: walletmodel.h:97
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedSuffixOut)
Get save filename, mimics QFileDialog::getSaveFileName, except that it appends a default suffix when ...
Definition: guiutil.cpp:260
static const QString Receive
Specifies receive address.
Dialog for editing an address and associated information.
QFrame * dateRangeWidget
bool haveWatchOnly() const
Definition: walletmodel.cpp:84
Label of address related to transaction.
static const quint32 ALL_TYPES
Type filter bit field (all types)
QLineEdit * addressWidget
void contextualMenu(const QPoint &)
Formatted amount, without brackets when unconfirmed.
void copyEntryData(QAbstractItemView *view, int column, int role)
Copy a field of the currently selected entry of a view to the clipboard.
Definition: guiutil.cpp:247
void chooseType(int idx)
GUIUtil::TableViewLastColumnResizingFixer * columnResizingFixer
QDateTimeEdit * dateFrom
bool write()
Perform export of the model to CSV.
void doubleClicked(const QModelIndex &)
Type of address (Send or Receive)
WalletModel * model
OptionsModel * getOptionsModel()
Predefined combinations for certain default usage cases.
Definition: ui_interface.h:75
QComboBox * dateWidget
QString getThirdPartyTxUrls()
Definition: optionsmodel.h:88