Fabcoin Core  0.16.2
P2P Digital Currency
coincontroldialog.cpp
Go to the documentation of this file.
1 // Copyright (c) 2011-2017 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <coincontroldialog.h>
6 #include <ui_coincontroldialog.h>
7 
8 #include <addresstablemodel.h>
9 #include <fabcoinunits.h>
10 #include <guiutil.h>
11 #include <optionsmodel.h>
12 #include <platformstyle.h>
13 #include <txmempool.h>
14 #include <walletmodel.h>
15 #include <styleSheet.h>
16 
17 #include <wallet/coincontrol.h>
18 #include <init.h>
19 #include <policy/fees.h>
20 #include <policy/policy.h>
21 #include <validation.h> // For mempool
22 #include <wallet/wallet.h>
23 
24 #include <QApplication>
25 #include <QCheckBox>
26 #include <QCursor>
27 #include <QDialogButtonBox>
28 #include <QFlags>
29 #include <QIcon>
30 #include <QSettings>
31 #include <QString>
32 #include <QTreeWidget>
33 #include <QTreeWidgetItem>
34 
35 QList<CAmount> CoinControlDialog::payAmounts;
38 
39 bool CCoinControlWidgetItem::operator<(const QTreeWidgetItem &other) const {
40  int column = treeWidget()->sortColumn();
42  return data(column, Qt::UserRole).toLongLong() < other.data(column, Qt::UserRole).toLongLong();
43  return QTreeWidgetItem::operator<(other);
44 }
45 
46 CoinControlDialog::CoinControlDialog(const PlatformStyle *_platformStyle, QWidget *parent) :
47  QDialog(parent),
48  ui(new Ui::CoinControlDialog),
49  model(0),
50  platformStyle(_platformStyle)
51 {
52  ui->setupUi(this);
53 
54  // Set stylesheet
55  SetObjectStyleSheet(ui->pushButtonSelectAll, StyleSheetNames::ButtonBlack);
56  SetObjectStyleSheet(ui->treeWidget, StyleSheetNames::TreeView);
57 
58  // context menu actions
59  QAction *copyAddressAction = new QAction(tr("Copy address"), this);
60  QAction *copyLabelAction = new QAction(tr("Copy label"), this);
61  QAction *copyAmountAction = new QAction(tr("Copy amount"), this);
62  copyTransactionHashAction = new QAction(tr("Copy transaction ID"), this); // we need to enable/disable this
63  lockAction = new QAction(tr("Lock unspent"), this); // we need to enable/disable this
64  unlockAction = new QAction(tr("Unlock unspent"), this); // we need to enable/disable this
65 
66  // context menu
67  contextMenu = new QMenu(this);
68  contextMenu->addAction(copyAddressAction);
69  contextMenu->addAction(copyLabelAction);
70  contextMenu->addAction(copyAmountAction);
72  contextMenu->addSeparator();
73  contextMenu->addAction(lockAction);
74  contextMenu->addAction(unlockAction);
75 
76  // context menu signals
77  connect(ui->treeWidget, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showMenu(QPoint)));
78  connect(copyAddressAction, SIGNAL(triggered()), this, SLOT(copyAddress()));
79  connect(copyLabelAction, SIGNAL(triggered()), this, SLOT(copyLabel()));
80  connect(copyAmountAction, SIGNAL(triggered()), this, SLOT(copyAmount()));
81  connect(copyTransactionHashAction, SIGNAL(triggered()), this, SLOT(copyTransactionHash()));
82  connect(lockAction, SIGNAL(triggered()), this, SLOT(lockCoin()));
83  connect(unlockAction, SIGNAL(triggered()), this, SLOT(unlockCoin()));
84 
85  // clipboard actions
86  QAction *clipboardQuantityAction = new QAction(tr("Copy quantity"), this);
87  QAction *clipboardAmountAction = new QAction(tr("Copy amount"), this);
88  QAction *clipboardFeeAction = new QAction(tr("Copy fee"), this);
89  QAction *clipboardAfterFeeAction = new QAction(tr("Copy after fee"), this);
90  QAction *clipboardBytesAction = new QAction(tr("Copy bytes"), this);
91  QAction *clipboardLowOutputAction = new QAction(tr("Copy dust"), this);
92  QAction *clipboardChangeAction = new QAction(tr("Copy change"), this);
93 
94  connect(clipboardQuantityAction, SIGNAL(triggered()), this, SLOT(clipboardQuantity()));
95  connect(clipboardAmountAction, SIGNAL(triggered()), this, SLOT(clipboardAmount()));
96  connect(clipboardFeeAction, SIGNAL(triggered()), this, SLOT(clipboardFee()));
97  connect(clipboardAfterFeeAction, SIGNAL(triggered()), this, SLOT(clipboardAfterFee()));
98  connect(clipboardBytesAction, SIGNAL(triggered()), this, SLOT(clipboardBytes()));
99  connect(clipboardLowOutputAction, SIGNAL(triggered()), this, SLOT(clipboardLowOutput()));
100  connect(clipboardChangeAction, SIGNAL(triggered()), this, SLOT(clipboardChange()));
101 
102  ui->labelCoinControlQuantity->addAction(clipboardQuantityAction);
103  ui->labelCoinControlAmount->addAction(clipboardAmountAction);
104  ui->labelCoinControlFee->addAction(clipboardFeeAction);
105  ui->labelCoinControlAfterFee->addAction(clipboardAfterFeeAction);
106  ui->labelCoinControlBytes->addAction(clipboardBytesAction);
107  ui->labelCoinControlLowOutput->addAction(clipboardLowOutputAction);
108  ui->labelCoinControlChange->addAction(clipboardChangeAction);
109 
110  // toggle tree/list mode
111  connect(ui->radioTreeMode, SIGNAL(toggled(bool)), this, SLOT(radioTreeMode(bool)));
112  connect(ui->radioListMode, SIGNAL(toggled(bool)), this, SLOT(radioListMode(bool)));
113 
114  // click on checkbox
115  connect(ui->treeWidget, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(viewItemChanged(QTreeWidgetItem*, int)));
116 
117  // click on header
118 #if QT_VERSION < 0x050000
119  ui->treeWidget->header()->setClickable(true);
120 #else
121  ui->treeWidget->header()->setSectionsClickable(true);
122 #endif
123  connect(ui->treeWidget->header(), SIGNAL(sectionClicked(int)), this, SLOT(headerSectionClicked(int)));
124 
125  // ok button
126  connect(ui->buttonBox, SIGNAL(clicked( QAbstractButton*)), this, SLOT(buttonBoxClicked(QAbstractButton*)));
127 
128  // (un)select all
129  connect(ui->pushButtonSelectAll, SIGNAL(clicked()), this, SLOT(buttonSelectAllClicked()));
130 
131  // change coin control first column label due Qt4 bug.
132  // see https://github.com/blockchaingate/fabcoin/issues/5716
133  ui->treeWidget->headerItem()->setText(COLUMN_CHECKBOX, QString());
134 
135  ui->treeWidget->setColumnWidth(COLUMN_CHECKBOX, 84);
136  ui->treeWidget->setColumnWidth(COLUMN_AMOUNT, 110);
137  ui->treeWidget->setColumnWidth(COLUMN_LABEL, 190);
138  ui->treeWidget->setColumnWidth(COLUMN_ADDRESS, 320);
139  ui->treeWidget->setColumnWidth(COLUMN_DATE, 130);
140  ui->treeWidget->setColumnWidth(COLUMN_CONFIRMATIONS, 110);
141  ui->treeWidget->setColumnHidden(COLUMN_TXHASH, true); // store transaction hash in this column, but don't show it
142  ui->treeWidget->setColumnHidden(COLUMN_VOUT_INDEX, true); // store vout index in this column, but don't show it
143 
144  // default view is sorted by amount desc
145  sortView(COLUMN_AMOUNT, Qt::DescendingOrder);
146 
147  // restore list mode and sortorder as a convenience feature
148  QSettings settings;
149  if (settings.contains("nCoinControlMode") && !settings.value("nCoinControlMode").toBool())
150  ui->radioTreeMode->click();
151  if (settings.contains("nCoinControlSortColumn") && settings.contains("nCoinControlSortOrder"))
152  sortView(settings.value("nCoinControlSortColumn").toInt(), ((Qt::SortOrder)settings.value("nCoinControlSortOrder").toInt()));
153 }
154 
156 {
157  QSettings settings;
158  settings.setValue("nCoinControlMode", ui->radioListMode->isChecked());
159  settings.setValue("nCoinControlSortColumn", sortColumn);
160  settings.setValue("nCoinControlSortOrder", (int)sortOrder);
161 
162  delete ui;
163 }
164 
166 {
167  this->model = _model;
168 
169  if(_model && _model->getOptionsModel() && _model->getAddressTableModel())
170  {
171  updateView();
173  CoinControlDialog::updateLabels(_model, this);
174  }
175 }
176 
177 // ok button
178 void CoinControlDialog::buttonBoxClicked(QAbstractButton* button)
179 {
180  if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole)
181  done(QDialog::Accepted); // closes the dialog
182 }
183 
184 // (un)select all
186 {
187  Qt::CheckState state = Qt::Checked;
188  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
189  {
190  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != Qt::Unchecked)
191  {
192  state = Qt::Unchecked;
193  break;
194  }
195  }
196  ui->treeWidget->setEnabled(false);
197  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
198  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) != state)
199  ui->treeWidget->topLevelItem(i)->setCheckState(COLUMN_CHECKBOX, state);
200  ui->treeWidget->setEnabled(true);
201  if (state == Qt::Unchecked)
202  coinControl->UnSelectAll(); // just to be sure
204 }
205 
206 // context menu
207 void CoinControlDialog::showMenu(const QPoint &point)
208 {
209  QTreeWidgetItem *item = ui->treeWidget->itemAt(point);
210  if(item)
211  {
212  contextMenuItem = item;
213 
214  // disable some items (like Copy Transaction ID, lock, unlock) for tree roots in context menu
215  if (item->text(COLUMN_TXHASH).length() == 64) // transaction hash is 64 characters (this means its a child node, so its not a parent node in tree mode)
216  {
217  copyTransactionHashAction->setEnabled(true);
218  if (model->isLockedCoin(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt()))
219  {
220  lockAction->setEnabled(false);
221  unlockAction->setEnabled(true);
222  }
223  else
224  {
225  lockAction->setEnabled(true);
226  unlockAction->setEnabled(false);
227  }
228  }
229  else // this means click on parent node in tree mode -> disable all
230  {
231  copyTransactionHashAction->setEnabled(false);
232  lockAction->setEnabled(false);
233  unlockAction->setEnabled(false);
234  }
235 
236  // show context menu
237  contextMenu->exec(QCursor::pos());
238  }
239 }
240 
241 // context menu action: copy amount
243 {
245 }
246 
247 // context menu action: copy label
249 {
250  if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_LABEL).length() == 0 && contextMenuItem->parent())
252  else
254 }
255 
256 // context menu action: copy address
258 {
259  if (ui->radioTreeMode->isChecked() && contextMenuItem->text(COLUMN_ADDRESS).length() == 0 && contextMenuItem->parent())
261  else
263 }
264 
265 // context menu action: copy transaction id
267 {
269 }
270 
271 // context menu action: lock coin
273 {
274  if (contextMenuItem->checkState(COLUMN_CHECKBOX) == Qt::Checked)
275  contextMenuItem->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
276 
277  COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
278  model->lockCoin(outpt);
279  contextMenuItem->setDisabled(true);
280  contextMenuItem->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
282 }
283 
284 // context menu action: unlock coin
286 {
287  COutPoint outpt(uint256S(contextMenuItem->text(COLUMN_TXHASH).toStdString()), contextMenuItem->text(COLUMN_VOUT_INDEX).toUInt());
288  model->unlockCoin(outpt);
289  contextMenuItem->setDisabled(false);
290  contextMenuItem->setIcon(COLUMN_CHECKBOX, QIcon());
292 }
293 
294 // copy label "Quantity" to clipboard
296 {
298 }
299 
300 // copy label "Amount" to clipboard
302 {
303  GUIUtil::setClipboard(ui->labelCoinControlAmount->text().left(ui->labelCoinControlAmount->text().indexOf(" ")));
304 }
305 
306 // copy label "Fee" to clipboard
308 {
309  GUIUtil::setClipboard(ui->labelCoinControlFee->text().left(ui->labelCoinControlFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
310 }
311 
312 // copy label "After fee" to clipboard
314 {
315  GUIUtil::setClipboard(ui->labelCoinControlAfterFee->text().left(ui->labelCoinControlAfterFee->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
316 }
317 
318 // copy label "Bytes" to clipboard
320 {
322 }
323 
324 // copy label "Dust" to clipboard
326 {
328 }
329 
330 // copy label "Change" to clipboard
332 {
333  GUIUtil::setClipboard(ui->labelCoinControlChange->text().left(ui->labelCoinControlChange->text().indexOf(" ")).replace(ASYMP_UTF8, ""));
334 }
335 
336 // treeview: sort
337 void CoinControlDialog::sortView(int column, Qt::SortOrder order)
338 {
339  sortColumn = column;
340  sortOrder = order;
341  ui->treeWidget->sortItems(column, order);
342  ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
343 }
344 
345 // treeview: clicked on header
347 {
348  if (logicalIndex == COLUMN_CHECKBOX) // click on most left column -> do nothing
349  {
350  ui->treeWidget->header()->setSortIndicator(sortColumn, sortOrder);
351  }
352  else
353  {
354  if (sortColumn == logicalIndex)
355  sortOrder = ((sortOrder == Qt::AscendingOrder) ? Qt::DescendingOrder : Qt::AscendingOrder);
356  else
357  {
358  sortColumn = logicalIndex;
359  sortOrder = ((sortColumn == COLUMN_LABEL || sortColumn == COLUMN_ADDRESS) ? Qt::AscendingOrder : Qt::DescendingOrder); // if label or address then default => asc, else default => desc
360  }
361 
363  }
364 }
365 
366 // toggle tree mode
368 {
369  if (checked && model)
370  updateView();
371 }
372 
373 // toggle list mode
375 {
376  if (checked && model)
377  updateView();
378 }
379 
380 // checkbox clicked by user
381 void CoinControlDialog::viewItemChanged(QTreeWidgetItem* item, int column)
382 {
383  if (column == COLUMN_CHECKBOX && item->text(COLUMN_TXHASH).length() == 64) // transaction hash is 64 characters (this means its a child node, so its not a parent node in tree mode)
384  {
385  COutPoint outpt(uint256S(item->text(COLUMN_TXHASH).toStdString()), item->text(COLUMN_VOUT_INDEX).toUInt());
386 
387  if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked)
388  coinControl->UnSelect(outpt);
389  else if (item->isDisabled()) // locked (this happens if "check all" through parent node)
390  item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
391  else
392  coinControl->Select(outpt);
393 
394  // selection changed -> update labels
395  if (ui->treeWidget->isEnabled()) // do not update on every click for (un)select all
397  }
398 
399  // TODO: Remove this temporary qt5 fix after Qt5.3 and Qt5.4 are no longer used.
400  // Fixed in Qt5.5 and above: https://bugreports.qt.io/browse/QTBUG-43473
401 #if QT_VERSION >= 0x050000
402  else if (column == COLUMN_CHECKBOX && item->childCount() > 0)
403  {
404  if (item->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked && item->child(0)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
405  item->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
406  }
407 #endif
408 }
409 
410 // shows count of locked unspent outputs
412 {
413  std::vector<COutPoint> vOutpts;
414  model->listLockedCoins(vOutpts);
415  if (vOutpts.size() > 0)
416  {
417  ui->labelLocked->setText(tr("(%1 locked)").arg(vOutpts.size()));
418  ui->labelLocked->setVisible(true);
419  }
420  else ui->labelLocked->setVisible(false);
421 }
422 
424 {
425  if (!model)
426  return;
427 
428  // nPayAmount
429  CAmount nPayAmount = 0;
430  bool fDust = false;
431  CMutableTransaction txDummy;
432  for (const CAmount &amount : CoinControlDialog::payAmounts)
433  {
434  nPayAmount += amount;
435 
436  if (amount > 0)
437  {
438  CTxOut txout(amount, (CScript)std::vector<unsigned char>(24, 0));
439  txDummy.vout.push_back(txout);
440  fDust |= IsDust(txout, ::dustRelayFee);
441  }
442  }
443 
444  CAmount nAmount = 0;
445  CAmount nPayFee = 0;
446  CAmount nAfterFee = 0;
447  CAmount nChange = 0;
448  unsigned int nBytes = 0;
449  unsigned int nBytesInputs = 0;
450  unsigned int nQuantity = 0;
451  bool fWitness = false;
452 
453  std::vector<COutPoint> vCoinControl;
454  std::vector<COutput> vOutputs;
455  coinControl->ListSelected(vCoinControl);
456  model->getOutputs(vCoinControl, vOutputs);
457 
458  for (const COutput& out : vOutputs) {
459  // unselect already spent, very unlikely scenario, this could happen
460  // when selected are spent elsewhere, like rpc or another computer
461  uint256 txhash = out.tx->GetHash();
462  COutPoint outpt(txhash, out.i);
463  if (model->isSpent(outpt))
464  {
465  coinControl->UnSelect(outpt);
466  continue;
467  }
468 
469  // Quantity
470  nQuantity++;
471 
472  // Amount
473  nAmount += out.tx->tx->vout[out.i].nValue;
474 
475  // Bytes
477  int witnessversion = 0;
478  std::vector<unsigned char> witnessprogram;
479  if (out.tx->tx->vout[out.i].scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram))
480  {
481  nBytesInputs += (32 + 4 + 1 + (107 / WITNESS_SCALE_FACTOR) + 4);
482  fWitness = true;
483  }
484  else if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, address))
485  {
486  CPubKey pubkey;
487  CKeyID *keyid = boost::get<CKeyID>(&address);
488  if (keyid && model->getPubKey(*keyid, pubkey))
489  {
490  nBytesInputs += (pubkey.IsCompressed() ? 148 : 180);
491  }
492  else
493  nBytesInputs += 148; // in all error cases, simply assume 148 here
494  }
495  else nBytesInputs += 148;
496  }
497 
498  // calculation
499  if (nQuantity > 0)
500  {
501  // Bytes
502  nBytes = nBytesInputs + ((CoinControlDialog::payAmounts.size() > 0 ? CoinControlDialog::payAmounts.size() + 1 : 2) * 34) + 10; // always assume +1 output for change here
503  if (fWitness)
504  {
505  // there is some fudging in these numbers related to the actual virtual transaction size calculation that will keep this estimate from being exact.
506  // usually, the result will be an overestimate within a couple of lius so that the confirmation dialog ends up displaying a slightly smaller fee.
507  // also, the witness stack size value is a variable sized integer. usually, the number of stack items will be well under the single byte var int limit.
508  nBytes += 2; // account for the serialized marker and flag bytes
509  nBytes += nQuantity; // account for the witness byte that holds the number of stack items for each input.
510  }
511 
512  // in the subtract fee from amount case, we can tell if zero change already and subtract the bytes, so that fee calculation afterwards is accurate
514  if (nAmount - nPayAmount == 0)
515  nBytes -= 34;
516 
517  // Fee
518  nPayFee = CWallet::GetMinimumFee(nBytes, *coinControl, ::mempool, ::feeEstimator, nullptr /* FeeCalculation */);
519 
520  if (nPayAmount > 0)
521  {
522  nChange = nAmount - nPayAmount;
524  nChange -= nPayFee;
525 
526  // Never create dust outputs; if we would, just add the dust to the fee.
527  if (nChange > 0 && nChange < MIN_CHANGE)
528  {
529  CTxOut txout(nChange, (CScript)std::vector<unsigned char>(24, 0));
530  if (IsDust(txout, ::dustRelayFee))
531  {
532  nPayFee += nChange;
533  nChange = 0;
535  nBytes -= 34; // we didn't detect lack of change above
536  }
537  }
538 
539  if (nChange == 0 && !CoinControlDialog::fSubtractFeeFromAmount)
540  nBytes -= 34;
541  }
542 
543  // after fee
544  nAfterFee = std::max<CAmount>(nAmount - nPayFee, 0);
545  }
546 
547  // actually update labels
548  int nDisplayUnit = FabcoinUnits::FAB;
549  if (model && model->getOptionsModel())
550  nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
551 
552  QLabel *l1 = dialog->findChild<QLabel *>("labelCoinControlQuantity");
553  QLabel *l2 = dialog->findChild<QLabel *>("labelCoinControlAmount");
554  QLabel *l3 = dialog->findChild<QLabel *>("labelCoinControlFee");
555  QLabel *l4 = dialog->findChild<QLabel *>("labelCoinControlAfterFee");
556  QLabel *l5 = dialog->findChild<QLabel *>("labelCoinControlBytes");
557  QLabel *l7 = dialog->findChild<QLabel *>("labelCoinControlLowOutput");
558  QLabel *l8 = dialog->findChild<QLabel *>("labelCoinControlChange");
559 
560  // enable/disable "dust" and "change"
561  dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setEnabled(nPayAmount > 0);
562  dialog->findChild<QLabel *>("labelCoinControlLowOutput") ->setEnabled(nPayAmount > 0);
563  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setEnabled(nPayAmount > 0);
564  dialog->findChild<QLabel *>("labelCoinControlChange") ->setEnabled(nPayAmount > 0);
565 
566  // stats
567  l1->setText(QString::number(nQuantity)); // Quantity
568  l2->setText(FabcoinUnits::formatWithUnit(nDisplayUnit, nAmount)); // Amount
569  l3->setText(FabcoinUnits::formatWithUnit(nDisplayUnit, nPayFee)); // Fee
570  l4->setText(FabcoinUnits::formatWithUnit(nDisplayUnit, nAfterFee)); // After Fee
571  l5->setText(((nBytes > 0) ? ASYMP_UTF8 : "") + QString::number(nBytes)); // Bytes
572  l7->setText(fDust ? tr("yes") : tr("no")); // Dust
573  l8->setText(FabcoinUnits::formatWithUnit(nDisplayUnit, nChange)); // Change
574  if (nPayFee > 0)
575  {
576  l3->setText(ASYMP_UTF8 + l3->text());
577  l4->setText(ASYMP_UTF8 + l4->text());
578  if (nChange > 0 && !CoinControlDialog::fSubtractFeeFromAmount)
579  l8->setText(ASYMP_UTF8 + l8->text());
580  }
581 
582  // turn label red when dust
583  l7->setStyleSheet((fDust) ? "color:red;" : "");
584 
585  // tool tips
586  QString toolTipDust = tr("This label turns red if any recipient receives an amount smaller than the current dust threshold.");
587 
588  // how many lius the estimated fee can vary per byte we guess wrong
589  double dFeeVary = (double)nPayFee / nBytes;
590 
591  QString toolTip4 = tr("Can vary +/- %1 liu(s) per input.").arg(dFeeVary);
592 
593  l3->setToolTip(toolTip4);
594  l4->setToolTip(toolTip4);
595  l7->setToolTip(toolTipDust);
596  l8->setToolTip(toolTip4);
597  dialog->findChild<QLabel *>("labelCoinControlFeeText") ->setToolTip(l3->toolTip());
598  dialog->findChild<QLabel *>("labelCoinControlAfterFeeText") ->setToolTip(l4->toolTip());
599  dialog->findChild<QLabel *>("labelCoinControlBytesText") ->setToolTip(l5->toolTip());
600  dialog->findChild<QLabel *>("labelCoinControlLowOutputText")->setToolTip(l7->toolTip());
601  dialog->findChild<QLabel *>("labelCoinControlChangeText") ->setToolTip(l8->toolTip());
602 
603  // Insufficient funds
604  QLabel *label = dialog->findChild<QLabel *>("labelCoinControlInsuffFunds");
605  if (label)
606  label->setVisible(nChange < 0);
607 }
608 
610 {
612  return;
613 
614  bool treeMode = ui->radioTreeMode->isChecked();
615 
616  ui->treeWidget->clear();
617  ui->treeWidget->setEnabled(false); // performance, otherwise updateLabels would be called for every checked checkbox
618  ui->treeWidget->setAlternatingRowColors(!treeMode);
619  QFlags<Qt::ItemFlag> flgCheckbox = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;
620  QFlags<Qt::ItemFlag> flgTristate = Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable | Qt::ItemIsTristate;
621 
622  int nDisplayUnit = model->getOptionsModel()->getDisplayUnit();
623 
624  std::map<QString, std::vector<COutput> > mapCoins;
625  model->listCoins(mapCoins);
626 
627  for (const std::pair<QString, std::vector<COutput>>& coins : mapCoins) {
628  CCoinControlWidgetItem *itemWalletAddress = new CCoinControlWidgetItem();
629  itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
630  QString sWalletAddress = coins.first;
631  QString sWalletLabel = model->getAddressTableModel()->labelForAddress(sWalletAddress);
632  if (sWalletLabel.isEmpty())
633  sWalletLabel = tr("(no label)");
634 
635  if (treeMode)
636  {
637  // wallet address
638  ui->treeWidget->addTopLevelItem(itemWalletAddress);
639 
640  itemWalletAddress->setFlags(flgTristate);
641  itemWalletAddress->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked);
642 
643  // label
644  itemWalletAddress->setText(COLUMN_LABEL, sWalletLabel);
645 
646  // address
647  itemWalletAddress->setText(COLUMN_ADDRESS, sWalletAddress);
648  }
649 
650  CAmount nSum = 0;
651  int nChildren = 0;
652  for (const COutput& out : coins.second) {
653  nSum += out.tx->tx->vout[out.i].nValue;
654  nChildren++;
655 
656  CCoinControlWidgetItem *itemOutput;
657  if (treeMode) itemOutput = new CCoinControlWidgetItem(itemWalletAddress);
658  else itemOutput = new CCoinControlWidgetItem(ui->treeWidget);
659  itemOutput->setFlags(flgCheckbox);
660  itemOutput->setCheckState(COLUMN_CHECKBOX,Qt::Unchecked);
661 
662  // address
663  CTxDestination outputAddress;
664  QString sAddress = "";
665  if(ExtractDestination(out.tx->tx->vout[out.i].scriptPubKey, outputAddress))
666  {
667  sAddress = QString::fromStdString(CFabcoinAddress(outputAddress).ToString());
668 
669  // if listMode or change => show fabcoin address. In tree mode, address is not shown again for direct wallet address outputs
670  if (!treeMode || (!(sAddress == sWalletAddress)))
671  itemOutput->setText(COLUMN_ADDRESS, sAddress);
672  }
673 
674  // label
675  if (!(sAddress == sWalletAddress)) // change
676  {
677  // tooltip from where the change comes from
678  itemOutput->setToolTip(COLUMN_LABEL, tr("change from %1 (%2)").arg(sWalletLabel).arg(sWalletAddress));
679  itemOutput->setText(COLUMN_LABEL, tr("(change)"));
680  }
681  else if (!treeMode)
682  {
683  QString sLabel = model->getAddressTableModel()->labelForAddress(sAddress);
684  if (sLabel.isEmpty())
685  sLabel = tr("(no label)");
686  itemOutput->setText(COLUMN_LABEL, sLabel);
687  }
688 
689  // amount
690  itemOutput->setText(COLUMN_AMOUNT, FabcoinUnits::format(nDisplayUnit, out.tx->tx->vout[out.i].nValue));
691  itemOutput->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)out.tx->tx->vout[out.i].nValue)); // padding so that sorting works correctly
692 
693  // date
694  itemOutput->setText(COLUMN_DATE, GUIUtil::dateTimeStr(out.tx->GetTxTime()));
695  itemOutput->setData(COLUMN_DATE, Qt::UserRole, QVariant((qlonglong)out.tx->GetTxTime()));
696 
697  // confirmations
698  itemOutput->setText(COLUMN_CONFIRMATIONS, QString::number(out.nDepth));
699  itemOutput->setData(COLUMN_CONFIRMATIONS, Qt::UserRole, QVariant((qlonglong)out.nDepth));
700 
701  // transaction hash
702  uint256 txhash = out.tx->GetHash();
703  itemOutput->setText(COLUMN_TXHASH, QString::fromStdString(txhash.GetHex()));
704 
705  // vout index
706  itemOutput->setText(COLUMN_VOUT_INDEX, QString::number(out.i));
707 
708  // disable locked coins
709  if (model->isLockedCoin(txhash, out.i))
710  {
711  COutPoint outpt(txhash, out.i);
712  coinControl->UnSelect(outpt); // just to be sure
713  itemOutput->setDisabled(true);
714  itemOutput->setIcon(COLUMN_CHECKBOX, platformStyle->SingleColorIcon(":/icons/lock_closed"));
715  }
716 
717  // set checkbox
718  if (coinControl->IsSelected(COutPoint(txhash, out.i)))
719  itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked);
720  }
721 
722  // amount
723  if (treeMode)
724  {
725  itemWalletAddress->setText(COLUMN_CHECKBOX, "(" + QString::number(nChildren) + ")");
726  itemWalletAddress->setText(COLUMN_AMOUNT, FabcoinUnits::format(nDisplayUnit, nSum));
727  itemWalletAddress->setData(COLUMN_AMOUNT, Qt::UserRole, QVariant((qlonglong)nSum));
728  }
729  }
730 
731  // expand all partially selected
732  if (treeMode)
733  {
734  for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
735  if (ui->treeWidget->topLevelItem(i)->checkState(COLUMN_CHECKBOX) == Qt::PartiallyChecked)
736  ui->treeWidget->topLevelItem(i)->setExpanded(true);
737  }
738 
739  // sort view
741  ui->treeWidget->setEnabled(true);
742 }
const PlatformStyle * platformStyle
void viewItemChanged(QTreeWidgetItem *, int)
CTxMemPool mempool
#define SetObjectStyleSheet(object, name)
Definition: styleSheet.h:10
void getOutputs(const std::vector< COutPoint > &vOutpoints, std::vector< COutput > &vOutputs)
int i
Definition: wallet.h:531
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:79
void lockCoin(COutPoint &output)
static CCoinControl * coinControl
const uint256 & GetHash() const
Definition: wallet.h:278
QRadioButton * radioTreeMode
std::string GetHex() const
Definition: uint256.cpp:21
void setupUi(QDialog *CoinControlDialog)
bool isLockedCoin(uint256 hash, unsigned int n) const
base58-encoded Fabcoin addresses.
Definition: base58.h:104
QString dateTimeStr(const QDateTime &date)
Definition: guiutil.cpp:80
CoinControlDialog(const PlatformStyle *platformStyle, QWidget *parent=0)
int64_t GetTxTime() const
Definition: wallet.cpp:1505
void ListSelected(std::vector< COutPoint > &vOutpoints) const
Definition: coincontrol.h:78
AddressTableModel * getAddressTableModel()
void UnSelect(const COutPoint &output)
Definition: coincontrol.h:68
#define ASYMP_UTF8
Coin Control Features.
Definition: coincontrol.h:16
static QString formatWithUnit(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string (with unit)
int64_t CAmount
Amount in lius (Can be negative)
Definition: amount.h:15
CBlockPolicyEstimator feeEstimator
Definition: validation.cpp:106
static QList< CAmount > payAmounts
QPushButton * pushButtonSelectAll
QIcon SingleColorIcon(const QString &filename) const
Colorize an icon (given filename) with the icon color.
QAction * copyTransactionHashAction
int nDepth
Definition: wallet.h:532
bool isSpent(const COutPoint &outpoint) const
Ui::CoinControlDialog * ui
CTransactionRef tx
Definition: wallet.h:211
void setClipboard(const QString &str)
Definition: guiutil.cpp:858
static QString format(int unit, const CAmount &amount, bool plussign=false, SeparatorStyle separators=separatorStandard)
Format as string.
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet, txnouttype *typeRet)
Definition: standard.cpp:268
CoinControlTreeWidget * treeWidget
void Select(const COutPoint &output)
Definition: coincontrol.h:63
uint256 uint256S(const char *str)
Definition: uint256.h:153
An encapsulated public key.
Definition: pubkey.h:39
bool getPubKey(const CKeyID &address, CPubKey &vchPubKeyOut) const
static void updateLabels(WalletModel *, QDialog *)
friend class CCoinControlWidgetItem
An output of a transaction.
Definition: transaction.h:131
int getDisplayUnit()
Definition: optionsmodel.h:70
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:18
std::vector< CTxOut > vout
Definition: transaction.h:393
void UnSelectAll()
Definition: coincontrol.h:73
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:171
256-bit opaque blob.
Definition: uint256.h:132
void setModel(WalletModel *model)
static CAmount GetMinimumFee(unsigned int nTxBytes, const CCoinControl &coin_control, const CTxMemPool &pool, const CBlockPolicyEstimator &estimator, FeeCalculation *feeCalc)
Estimate the minimum fee considering user set parameters and the required fee.
Definition: wallet.cpp:3114
static bool fSubtractFeeFromAmount
QTreeWidgetItem * contextMenuItem
QString labelForAddress(const QString &address) const
void listLockedCoins(std::vector< COutPoint > &vOutpts)
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:417
Interface to Fabcoin wallet from Qt view code.
Definition: walletmodel.h:103
void unlockCoin(COutPoint &output)
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
const CWalletTx * tx
Definition: wallet.h:530
static QString removeSpaces(QString text)
Definition: fabcoinunits.h:120
void sortView(int, Qt::SortOrder)
Qt::SortOrder sortOrder
bool IsDust(const CTxOut &txout, const CFeeRate &dustRelayFeeIn)
Definition: policy.cpp:52
A mutable version of CTransaction.
Definition: transaction.h:390
bool operator<(const QTreeWidgetItem &other) const
void buttonBoxClicked(QAbstractButton *)
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
QRadioButton * radioListMode
WalletModel * model
void showMenu(const QPoint &)
QDialogButtonBox * buttonBox
void listCoins(std::map< QString, std::vector< COutput > > &mapCoins) const
bool IsSelected(const COutPoint &output) const
Definition: coincontrol.h:58
uint8_t const * data
Definition: sha3.h:19
bool operator<(const ::CryptoPP::OID &lhs, const ::CryptoPP::OID &rhs)
Definition: asn.h:562
OptionsModel * getOptionsModel()
CFeeRate dustRelayFee
Definition: policy.cpp:251