LCOV - code coverage report
Current view: top level - src/qt - coincontroldialog.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 1 408 0.2 %
Date: 2020-09-26 01:30:44 Functions: 1 31 3.2 %

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

Generated by: LCOV version 1.15