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