Master Core  v0.0.9 - 2abfd2849db8ba7a83957c64eb976b406713c123
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
transactionview.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2013 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "transactionview.h"
6 
7 #include "addresstablemodel.h"
8 #include "bitcoinunits.h"
9 #include "csvmodelwriter.h"
10 #include "editaddressdialog.h"
11 #include "guiutil.h"
12 #include "optionsmodel.h"
13 #include "transactiondescdialog.h"
14 #include "transactionfilterproxy.h"
15 #include "transactionrecord.h"
16 #include "transactiontablemodel.h"
17 #include "walletmodel.h"
18 
19 #include "ui_interface.h"
20 
21 #include <QComboBox>
22 #include <QDateTimeEdit>
23 #include <QDesktopServices>
24 #include <QDoubleValidator>
25 #include <QHBoxLayout>
26 #include <QHeaderView>
27 #include <QLabel>
28 #include <QLineEdit>
29 #include <QMenu>
30 #include <QPoint>
31 #include <QScrollBar>
32 #include <QSignalMapper>
33 #include <QTableView>
34 #include <QUrl>
35 #include <QVBoxLayout>
36 
38  QWidget(parent), model(0), transactionProxyModel(0),
39  transactionView(0)
40 {
41  // Build filter row
42  setContentsMargins(0,0,0,0);
43 
44  QHBoxLayout *hlayout = new QHBoxLayout();
45  hlayout->setContentsMargins(0,0,0,0);
46 #ifdef Q_OS_MAC
47  hlayout->setSpacing(5);
48  hlayout->addSpacing(26);
49 #else
50  hlayout->setSpacing(0);
51  hlayout->addSpacing(23);
52 #endif
53 
54  dateWidget = new QComboBox(this);
55 #ifdef Q_OS_MAC
56  dateWidget->setFixedWidth(121);
57 #else
58  dateWidget->setFixedWidth(120);
59 #endif
60  dateWidget->addItem(tr("All"), All);
61  dateWidget->addItem(tr("Today"), Today);
62  dateWidget->addItem(tr("This week"), ThisWeek);
63  dateWidget->addItem(tr("This month"), ThisMonth);
64  dateWidget->addItem(tr("Last month"), LastMonth);
65  dateWidget->addItem(tr("This year"), ThisYear);
66  dateWidget->addItem(tr("Range..."), Range);
67  hlayout->addWidget(dateWidget);
68 
69  typeWidget = new QComboBox(this);
70 #ifdef Q_OS_MAC
71  typeWidget->setFixedWidth(121);
72 #else
73  typeWidget->setFixedWidth(120);
74 #endif
75 
76  typeWidget->addItem(tr("All"), TransactionFilterProxy::ALL_TYPES);
84 
85  hlayout->addWidget(typeWidget);
86 
87  addressWidget = new QLineEdit(this);
88 #if QT_VERSION >= 0x040700
89  addressWidget->setPlaceholderText(tr("Enter address or label to search"));
90 #endif
91  hlayout->addWidget(addressWidget);
92 
93  amountWidget = new QLineEdit(this);
94 #if QT_VERSION >= 0x040700
95  amountWidget->setPlaceholderText(tr("Min amount"));
96 #endif
97 #ifdef Q_OS_MAC
98  amountWidget->setFixedWidth(97);
99 #else
100  amountWidget->setFixedWidth(100);
101 #endif
102  amountWidget->setValidator(new QDoubleValidator(0, 1e20, 8, this));
103  hlayout->addWidget(amountWidget);
104 
105  QVBoxLayout *vlayout = new QVBoxLayout(this);
106  vlayout->setContentsMargins(0,0,0,0);
107  vlayout->setSpacing(0);
108 
109  QTableView *view = new QTableView(this);
110  vlayout->addLayout(hlayout);
111  vlayout->addWidget(createDateRangeWidget());
112  vlayout->addWidget(view);
113  vlayout->setSpacing(0);
114  int width = view->verticalScrollBar()->sizeHint().width();
115  // Cover scroll bar width with spacing
116 #ifdef Q_OS_MAC
117  hlayout->addSpacing(width+2);
118 #else
119  hlayout->addSpacing(width);
120 #endif
121  // Always show scroll bar
122  view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
123  view->setTabKeyNavigation(false);
124  view->setContextMenuPolicy(Qt::CustomContextMenu);
125 
126  transactionView = view;
127 
128  // Actions
129  QAction *copyAddressAction = new QAction(tr("Copy address"), this);
130  QAction *copyLabelAction = new QAction(tr("Copy label"), this);
131  QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
132  QAction *copyTxIDAction = new QAction(tr("Copy transaction ID"), this);
133  QAction *editLabelAction = new QAction(tr("Edit label"), this);
134  QAction *showDetailsAction = new QAction(tr("Show transaction details"), this);
135 
136  contextMenu = new QMenu();
137  contextMenu->addAction(copyAddressAction);
138  contextMenu->addAction(copyLabelAction);
139  contextMenu->addAction(copyAmountAction);
140  contextMenu->addAction(copyTxIDAction);
141  contextMenu->addAction(editLabelAction);
142  contextMenu->addAction(showDetailsAction);
143 
144  mapperThirdPartyTxUrls = new QSignalMapper(this);
145 
146  // Connect actions
147  connect(mapperThirdPartyTxUrls, SIGNAL(mapped(QString)), this, SLOT(openThirdPartyTxUrl(QString)));
148 
149  connect(dateWidget, SIGNAL(activated(int)), this, SLOT(chooseDate(int)));
150  connect(typeWidget, SIGNAL(activated(int)), this, SLOT(chooseType(int)));
151  connect(addressWidget, SIGNAL(textChanged(QString)), this, SLOT(changedPrefix(QString)));
152  connect(amountWidget, SIGNAL(textChanged(QString)), this, SLOT(changedAmount(QString)));
153 
154  connect(view, SIGNAL(doubleClicked(QModelIndex)), this, SIGNAL(doubleClicked(QModelIndex)));
155  connect(view, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(contextualMenu(QPoint)));
156 
157  connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
158  connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
159  connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
160  connect(copyTxIDAction, SIGNAL(triggered()), this, SLOT(copyTxID()));
161  connect(editLabelAction, SIGNAL(triggered()), this, SLOT(editLabel()));
162  connect(showDetailsAction, SIGNAL(triggered()), this, SLOT(showDetails()));
163 }
164 
166 {
167  this->model = model;
168  if(model)
169  {
171  transactionProxyModel->setSourceModel(model->getTransactionTableModel());
172  transactionProxyModel->setDynamicSortFilter(true);
173  transactionProxyModel->setSortCaseSensitivity(Qt::CaseInsensitive);
174  transactionProxyModel->setFilterCaseSensitivity(Qt::CaseInsensitive);
175 
176  transactionProxyModel->setSortRole(Qt::EditRole);
177 
178  transactionView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
180  transactionView->setAlternatingRowColors(true);
181  transactionView->setSelectionBehavior(QAbstractItemView::SelectRows);
182  transactionView->setSelectionMode(QAbstractItemView::ExtendedSelection);
183  transactionView->setSortingEnabled(true);
184  transactionView->sortByColumn(TransactionTableModel::Status, Qt::DescendingOrder);
185  transactionView->verticalHeader()->hide();
186 
191 
193 
194  if (model->getOptionsModel())
195  {
196  // Add third party transaction URLs to context menu
197  QStringList listUrls = model->getOptionsModel()->getThirdPartyTxUrls().split("|", QString::SkipEmptyParts);
198  for (int i = 0; i < listUrls.size(); ++i)
199  {
200  QString host = QUrl(listUrls[i].trimmed(), QUrl::StrictMode).host();
201  if (!host.isEmpty())
202  {
203  QAction *thirdPartyTxUrlAction = new QAction(host, this); // use host as menu item label
204  if (i == 0)
205  contextMenu->addSeparator();
206  contextMenu->addAction(thirdPartyTxUrlAction);
207  connect(thirdPartyTxUrlAction, SIGNAL(triggered()), mapperThirdPartyTxUrls, SLOT(map()));
208  mapperThirdPartyTxUrls->setMapping(thirdPartyTxUrlAction, listUrls[i].trimmed());
209  }
210  }
211  }
212  }
213 }
214 
216 {
218  return;
219  QDate current = QDate::currentDate();
220  dateRangeWidget->setVisible(false);
221  switch(dateWidget->itemData(idx).toInt())
222  {
223  case All:
227  break;
228  case Today:
230  QDateTime(current),
232  break;
233  case ThisWeek: {
234  // Find last Monday
235  QDate startOfWeek = current.addDays(-(current.dayOfWeek()-1));
237  QDateTime(startOfWeek),
239 
240  } break;
241  case ThisMonth:
243  QDateTime(QDate(current.year(), current.month(), 1)),
245  break;
246  case LastMonth:
248  QDateTime(QDate(current.year(), current.month()-1, 1)),
249  QDateTime(QDate(current.year(), current.month(), 1)));
250  break;
251  case ThisYear:
253  QDateTime(QDate(current.year(), 1, 1)),
255  break;
256  case Range:
257  dateRangeWidget->setVisible(true);
259  break;
260  }
261 }
262 
264 {
266  return;
268  typeWidget->itemData(idx).toInt());
269 }
270 
271 void TransactionView::changedPrefix(const QString &prefix)
272 {
274  return;
276 }
277 
278 void TransactionView::changedAmount(const QString &amount)
279 {
281  return;
282  qint64 amount_parsed = 0;
283  if(BitcoinUnits::parse(model->getOptionsModel()->getDisplayUnit(), amount, &amount_parsed))
284  {
285  transactionProxyModel->setMinAmount(amount_parsed);
286  }
287  else
288  {
290  }
291 }
292 
294 {
295  // CSV is currently the only supported format
296  QString filename = GUIUtil::getSaveFileName(this,
297  tr("Export Transaction History"), QString(),
298  tr("Comma separated file (*.csv)"), NULL);
299 
300  if (filename.isNull())
301  return;
302 
303  CSVModelWriter writer(filename);
304 
305  // name, column, role
307  writer.addColumn(tr("Confirmed"), 0, TransactionTableModel::ConfirmedRole);
308  writer.addColumn(tr("Date"), 0, TransactionTableModel::DateRole);
309  writer.addColumn(tr("Type"), TransactionTableModel::Type, Qt::EditRole);
310  writer.addColumn(tr("Label"), 0, TransactionTableModel::LabelRole);
311  writer.addColumn(tr("Address"), 0, TransactionTableModel::AddressRole);
312  writer.addColumn(tr("Amount"), 0, TransactionTableModel::FormattedAmountRole);
313  writer.addColumn(tr("ID"), 0, TransactionTableModel::TxIDRole);
314 
315  if(!writer.write()) {
316  emit message(tr("Exporting Failed"), tr("There was an error trying to save the transaction history to %1.").arg(filename),
318  }
319  else {
320  emit message(tr("Exporting Successful"), tr("The transaction history was successfully saved to %1.").arg(filename),
322  }
323 }
324 
325 void TransactionView::contextualMenu(const QPoint &point)
326 {
327  QModelIndex index = transactionView->indexAt(point);
328  if(index.isValid())
329  {
330  contextMenu->exec(QCursor::pos());
331  }
332 }
333 
335 {
337 }
338 
340 {
342 }
343 
345 {
347 }
348 
350 {
352 }
353 
355 {
356  if(!transactionView->selectionModel() ||!model)
357  return;
358  QModelIndexList selection = transactionView->selectionModel()->selectedRows();
359  if(!selection.isEmpty())
360  {
361  AddressTableModel *addressBook = model->getAddressTableModel();
362  if(!addressBook)
363  return;
364  QString address = selection.at(0).data(TransactionTableModel::AddressRole).toString();
365  if(address.isEmpty())
366  {
367  // If this transaction has no associated address, exit
368  return;
369  }
370  // Is address in address book? Address book can miss address when a transaction is
371  // sent from outside the UI.
372  int idx = addressBook->lookupAddress(address);
373  if(idx != -1)
374  {
375  // Edit sending / receiving address
376  QModelIndex modelIdx = addressBook->index(idx, 0, QModelIndex());
377  // Determine type of address, launch appropriate editor dialog type
378  QString type = modelIdx.data(AddressTableModel::TypeRole).toString();
379 
380  EditAddressDialog dlg(
384  dlg.setModel(addressBook);
385  dlg.loadRow(idx);
386  dlg.exec();
387  }
388  else
389  {
390  // Add sending address
392  this);
393  dlg.setModel(addressBook);
394  dlg.setAddress(address);
395  dlg.exec();
396  }
397  }
398 }
399 
401 {
402  if(!transactionView->selectionModel())
403  return;
404  QModelIndexList selection = transactionView->selectionModel()->selectedRows();
405  if(!selection.isEmpty())
406  {
407  TransactionDescDialog dlg(selection.at(0));
408  dlg.exec();
409  }
410 }
411 
413 {
414  if(!transactionView || !transactionView->selectionModel())
415  return;
416  QModelIndexList selection = transactionView->selectionModel()->selectedRows(0);
417  if(!selection.isEmpty())
418  QDesktopServices::openUrl(QUrl::fromUserInput(url.replace("%s", selection.at(0).data(TransactionTableModel::TxHashRole).toString())));
419 }
420 
422 {
423  dateRangeWidget = new QFrame();
424  dateRangeWidget->setFrameStyle(QFrame::Panel | QFrame::Raised);
425  dateRangeWidget->setContentsMargins(1,1,1,1);
426  QHBoxLayout *layout = new QHBoxLayout(dateRangeWidget);
427  layout->setContentsMargins(0,0,0,0);
428  layout->addSpacing(23);
429  layout->addWidget(new QLabel(tr("Range:")));
430 
431  dateFrom = new QDateTimeEdit(this);
432  dateFrom->setDisplayFormat("dd/MM/yy");
433  dateFrom->setCalendarPopup(true);
434  dateFrom->setMinimumWidth(100);
435  dateFrom->setDate(QDate::currentDate().addDays(-7));
436  layout->addWidget(dateFrom);
437  layout->addWidget(new QLabel(tr("to")));
438 
439  dateTo = new QDateTimeEdit(this);
440  dateTo->setDisplayFormat("dd/MM/yy");
441  dateTo->setCalendarPopup(true);
442  dateTo->setMinimumWidth(100);
443  dateTo->setDate(QDate::currentDate());
444  layout->addWidget(dateTo);
445  layout->addStretch();
446 
447  // Hide by default
448  dateRangeWidget->setVisible(false);
449 
450  // Notify on change
451  connect(dateFrom, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
452  connect(dateTo, SIGNAL(dateChanged(QDate)), this, SLOT(dateRangeChanged()));
453 
454  return dateRangeWidget;
455 }
456 
458 {
460  return;
462  QDateTime(dateFrom->date()),
463  QDateTime(dateTo->date()).addDays(1));
464 }
465 
466 void TransactionView::focusTransaction(const QModelIndex &idx)
467 {
469  return;
470  QModelIndex targetIdx = transactionProxyModel->mapFromSource(idx);
471  transactionView->scrollTo(targetIdx);
472  transactionView->setCurrentIndex(targetIdx);
473  transactionView->setFocus();
474 }
475 
476 // We override the virtual resizeEvent of the QWidget to adjust tables column
477 // sizes as the tables width is proportional to the dialogs width.
478 void TransactionView::resizeEvent(QResizeEvent* event)
479 {
480  QWidget::resizeEvent(event);
482 }
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 &)
void setTypeFilter(quint32 modes)
QTableView * transactionView
AddressTableModel * getAddressTableModel()
Export a Qt table model to a CSV file.
void setAddressPrefix(const QString &addrPrefix)
static quint32 TYPE(int type)
QDateTimeEdit * dateTo
const char * url
Definition: rpcconsole.cpp:35
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:137
void changedAmount(const QString &amount)
void setMinAmount(qint64 minimum)
Date and time this transaction was created.
TransactionTableModel * getTransactionTableModel()
int getDisplayUnit()
Definition: optionsmodel.h:58
Qt model of the address book in the core.
TransactionFilterProxy * transactionProxyModel
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 Bitcoin wallet from Qt view code.
Definition: walletmodel.h:96
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:254
static const QString Receive
Specifies receive address.
Dialog for editing an address and associated information.
static bool parse(int unit, const QString &value, qint64 *val_out)
Parse string to coin amount.
QFrame * dateRangeWidget
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:241
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:69
QComboBox * dateWidget
QString getThirdPartyTxUrls()
Definition: optionsmodel.h:60