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