diff --git a/src/qt/coincontroldialog.h b/src/qt/coincontroldialog.h --- a/src/qt/coincontroldialog.h +++ b/src/qt/coincontroldialog.h @@ -43,21 +43,21 @@ Q_OBJECT public: - explicit CoinControlDialog(const PlatformStyle *platformStyle, + explicit CoinControlDialog(CCoinControl &coin_control, WalletModel *model, + const PlatformStyle *platformStyle, QWidget *parent = nullptr); ~CoinControlDialog(); - void setModel(WalletModel *model); - // static because also called from sendcoinsdialog - static void updateLabels(WalletModel *, QDialog *); + static void updateLabels(CCoinControl &m_coin_control, WalletModel *, + QDialog *); static QList payAmounts; - static CCoinControl *coinControl(); static bool fSubtractFeeFromAmount; private: Ui::CoinControlDialog *ui; + CCoinControl &m_coin_control; WalletModel *model; int sortColumn; Qt::SortOrder sortOrder; diff --git a/src/qt/coincontroldialog.cpp b/src/qt/coincontroldialog.cpp --- a/src/qt/coincontroldialog.cpp +++ b/src/qt/coincontroldialog.cpp @@ -45,9 +45,12 @@ return QTreeWidgetItem::operator<(other); } -CoinControlDialog::CoinControlDialog(const PlatformStyle *_platformStyle, +CoinControlDialog::CoinControlDialog(CCoinControl &coin_control, + WalletModel *_model, + const PlatformStyle *_platformStyle, QWidget *parent) - : QDialog(parent), ui(new Ui::CoinControlDialog), model(nullptr), + : QDialog(parent), ui(new Ui::CoinControlDialog), + m_coin_control(coin_control), model(_model), platformStyle(_platformStyle) { ui->setupUi(this); @@ -167,6 +170,12 @@ } GUIUtil::handleCloseWindowShortcut(this); + + if (_model->getOptionsModel() && _model->getAddressTableModel()) { + updateView(); + updateLabelLocked(); + CoinControlDialog::updateLabels(m_coin_control, _model, this); + } } CoinControlDialog::~CoinControlDialog() { @@ -178,16 +187,6 @@ delete ui; } -void CoinControlDialog::setModel(WalletModel *_model) { - this->model = _model; - - if (_model && _model->getOptionsModel() && _model->getAddressTableModel()) { - updateView(); - updateLabelLocked(); - CoinControlDialog::updateLabels(_model, this); - } -} - // ok button void CoinControlDialog::buttonBoxClicked(QAbstractButton *button) { if (ui->buttonBox->buttonRole(button) == QDialogButtonBox::AcceptRole) { @@ -217,9 +216,9 @@ ui->treeWidget->setEnabled(true); if (state == Qt::Unchecked) { // just to be sure - coinControl()->UnSelectAll(); + m_coin_control.UnSelectAll(); } - CoinControlDialog::updateLabels(model, this); + CoinControlDialog::updateLabels(m_coin_control, model, this); } // context menu @@ -412,18 +411,18 @@ COutPoint outpoint = buildOutPoint(item); if (item->checkState(COLUMN_CHECKBOX) == Qt::Unchecked) { - coinControl()->UnSelect(outpoint); + m_coin_control.UnSelect(outpoint); } else if (item->isDisabled()) { // locked (this happens if "check all" through parent node) item->setCheckState(COLUMN_CHECKBOX, Qt::Unchecked); } else { - coinControl()->Select(outpoint); + m_coin_control.Select(outpoint); } // selection changed -> update labels if (ui->treeWidget->isEnabled()) { // do not update on every click for (un)select all - CoinControlDialog::updateLabels(model, this); + CoinControlDialog::updateLabels(m_coin_control, model, this); } } } @@ -440,7 +439,8 @@ } } -void CoinControlDialog::updateLabels(WalletModel *model, QDialog *dialog) { +void CoinControlDialog::updateLabels(CCoinControl &m_coin_control, + WalletModel *model, QDialog *dialog) { if (!model) { return; } @@ -469,7 +469,7 @@ unsigned int nQuantity = 0; std::vector vCoinControl; - coinControl()->ListSelected(vCoinControl); + m_coin_control.ListSelected(vCoinControl); size_t i = 0; for (const auto &out : model->wallet().getCoins(vCoinControl)) { @@ -481,7 +481,7 @@ // when selected are spent elsewhere, like rpc or another computer const COutPoint &output = vCoinControl[i++]; if (out.is_spent) { - coinControl()->UnSelect(output); + m_coin_control.UnSelect(output); continue; } @@ -529,7 +529,7 @@ } // Fee - nPayFee = model->wallet().getMinimumFee(nBytes, *coinControl()); + nPayFee = model->wallet().getMinimumFee(nBytes, m_coin_control); if (nPayAmount > Amount::zero()) { nChange = nAmount - nPayAmount; @@ -646,11 +646,6 @@ } } -CCoinControl *CoinControlDialog::coinControl() { - static CCoinControl coin_control; - return &coin_control; -} - COutPoint CoinControlDialog::buildOutPoint(const QTreeWidgetItem *item) { TxId txid; txid.SetHex(item->data(COLUMN_ADDRESS, TxIdRole).toString().toStdString()); @@ -781,7 +776,7 @@ // disable locked coins if (model->wallet().isLockedCoin(output)) { // just to be sure - coinControl()->UnSelect(output); + m_coin_control.UnSelect(output); itemOutput->setDisabled(true); itemOutput->setIcon( COLUMN_CHECKBOX, @@ -789,7 +784,7 @@ } // set checkbox - if (coinControl()->IsSelected(output)) { + if (m_coin_control.IsSelected(output)) { itemOutput->setCheckState(COLUMN_CHECKBOX, Qt::Checked); } } diff --git a/src/qt/sendcoinsdialog.h b/src/qt/sendcoinsdialog.h --- a/src/qt/sendcoinsdialog.h +++ b/src/qt/sendcoinsdialog.h @@ -12,6 +12,7 @@ #include #include +class CCoinControl; class ClientModel; class PlatformStyle; class SendCoinsEntry; @@ -63,6 +64,7 @@ Ui::SendCoinsDialog *ui; ClientModel *clientModel; WalletModel *model; + std::unique_ptr m_coin_control; std::unique_ptr m_current_transaction; bool fNewRecipientAllowed; bool fFeeMinimized; diff --git a/src/qt/sendcoinsdialog.cpp b/src/qt/sendcoinsdialog.cpp --- a/src/qt/sendcoinsdialog.cpp +++ b/src/qt/sendcoinsdialog.cpp @@ -33,7 +33,8 @@ SendCoinsDialog::SendCoinsDialog(const PlatformStyle *_platformStyle, WalletModel *_model, QWidget *parent) : QDialog(parent), ui(new Ui::SendCoinsDialog), clientModel(nullptr), - model(_model), fNewRecipientAllowed(true), fFeeMinimized(true), + model(_model), m_coin_control(new CCoinControl), + fNewRecipientAllowed(true), fFeeMinimized(true), platformStyle(_platformStyle) { ui->setupUi(this); @@ -252,16 +253,10 @@ std::make_unique(recipients); WalletModel::SendCoinsReturn prepareStatus; - // Always use a CCoinControl instance, use the CoinControlDialog instance if - // CoinControl has been enabled - CCoinControl ctrl; - if (model->getOptionsModel()->getCoinControlFeatures()) { - ctrl = *CoinControlDialog::coinControl(); - } - - updateCoinControlState(ctrl); + updateCoinControlState(*m_coin_control); - prepareStatus = model->prepareTransaction(*m_current_transaction, ctrl); + prepareStatus = + model->prepareTransaction(*m_current_transaction, *m_coin_control); // process prepareStatus and on error generate message shown to user processSendCoinsReturn(prepareStatus, @@ -496,7 +491,7 @@ } if (!send_failure) { accept(); - CoinControlDialog::coinControl()->UnSelectAll(); + m_coin_control->UnSelectAll(); coinControlUpdateLabels(); } fNewRecipientAllowed = true; @@ -507,7 +502,7 @@ m_current_transaction.reset(); // Clear coin control settings - CoinControlDialog::coinControl()->UnSelectAll(); + m_coin_control->UnSelectAll(); ui->checkBoxCoinControlChange->setChecked(false); ui->lineEditCoinControlChange->clear(); coinControlUpdateLabels(); @@ -725,17 +720,11 @@ } void SendCoinsDialog::useAvailableBalance(SendCoinsEntry *entry) { - // Get CCoinControl instance if CoinControl is enabled or create a new one. - CCoinControl coin_control; - if (model->getOptionsModel()->getCoinControlFeatures()) { - coin_control = *CoinControlDialog::coinControl(); - } - // Include watch-only for wallets without private key - coin_control.fAllowWatchOnly = model->wallet().privateKeysDisabled(); + m_coin_control->fAllowWatchOnly = model->wallet().privateKeysDisabled(); // Calculate available amount to send. - Amount amount = model->wallet().getAvailableBalance(coin_control); + Amount amount = model->wallet().getAvailableBalance(*m_coin_control); for (int i = 0; i < ui->entries->count(); ++i) { SendCoinsEntry *e = qobject_cast(ui->entries->itemAt(i)->widget()); @@ -792,11 +781,10 @@ return; } - CCoinControl coin_control; - updateCoinControlState(coin_control); + updateCoinControlState(*m_coin_control); // Explicitly use only fee estimation rate for smart fee labels - coin_control.m_feerate.reset(); - CFeeRate feeRate(model->wallet().getMinimumFee(1000, coin_control)); + m_coin_control->m_feerate.reset(); + CFeeRate feeRate(model->wallet().getMinimumFee(1000, *m_coin_control)); ui->labelSmartFee->setText( BitcoinUnits::formatWithUnit(model->getOptionsModel()->getDisplayUnit(), @@ -868,7 +856,7 @@ // coin control features disabled if (!checked && model) { - CoinControlDialog::coinControl()->SetNull(); + m_coin_control->SetNull(); } coinControlUpdateLabels(); @@ -876,8 +864,7 @@ // Coin Control: button inputs -> show actual coin control dialog void SendCoinsDialog::coinControlButtonClicked() { - CoinControlDialog dlg(platformStyle); - dlg.setModel(model); + CoinControlDialog dlg(*m_coin_control, model, platformStyle); dlg.exec(); coinControlUpdateLabels(); } @@ -885,7 +872,7 @@ // Coin Control: checkbox custom change address void SendCoinsDialog::coinControlChangeChecked(int state) { if (state == Qt::Unchecked) { - CoinControlDialog::coinControl()->destChange = CNoDestination(); + m_coin_control->destChange = CNoDestination(); ui->labelCoinControlChangeLabel->clear(); } else { // use this to re-validate an already entered address @@ -899,7 +886,7 @@ void SendCoinsDialog::coinControlChangeEdited(const QString &text) { if (model && model->getAddressTableModel()) { // Default to no change address until verified - CoinControlDialog::coinControl()->destChange = CNoDestination(); + m_coin_control->destChange = CNoDestination(); ui->labelCoinControlChangeLabel->setStyleSheet("QLabel{color:red;}"); const CTxDestination dest = @@ -928,7 +915,7 @@ QMessageBox::Cancel); if (btnRetVal == QMessageBox::Yes) { - CoinControlDialog::coinControl()->destChange = dest; + m_coin_control->destChange = dest; } else { ui->lineEditCoinControlChange->setText(""); ui->labelCoinControlChangeLabel->setStyleSheet( @@ -949,7 +936,7 @@ ui->labelCoinControlChangeLabel->setText(tr("(no label)")); } - CoinControlDialog::coinControl()->destChange = dest; + m_coin_control->destChange = dest; } } } @@ -961,7 +948,7 @@ return; } - updateCoinControlState(*CoinControlDialog::coinControl()); + updateCoinControlState(*m_coin_control); // set pay amounts CoinControlDialog::payAmounts.clear(); @@ -978,9 +965,9 @@ } } - if (CoinControlDialog::coinControl()->HasSelected()) { + if (m_coin_control->HasSelected()) { // actual coin control calculation - CoinControlDialog::updateLabels(model, this); + CoinControlDialog::updateLabels(*m_coin_control, model, this); // show coin control stats ui->labelCoinControlAutomaticallySelected->hide();