Skip to content

Commit

Permalink
[Cleanup] Remove double save for StakeSplitThreshold in GUI optionsModel
Browse files Browse the repository at this point in the history
Also encapsulate SettingsWalletOptionsWidget methods by making
SettingsWidget a friend class
  • Loading branch information
random-zebra committed Apr 10, 2021
1 parent a81318f commit a66d3f9
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 72 deletions.
1 change: 0 additions & 1 deletion src/qt/optionsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,6 @@ void OptionsDialog::setMapper()
/* Wallet */
mapper->addMapping(ui->spendZeroConfChange, OptionsModel::SpendZeroConfChange);
mapper->addMapping(ui->coinControlFeatures, OptionsModel::CoinControlFeatures);
mapper->addMapping(ui->spinBoxStakeSplitThreshold, OptionsModel::StakeSplitThreshold);

/* Network */
mapper->addMapping(ui->mapPortUpnp, OptionsModel::MapPortUPnP);
Expand Down
39 changes: 0 additions & 39 deletions src/qt/optionsmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ void OptionsModel::Init()

// Ensure restart flag is unset on client startup
setRestartRequired(false);
setSSTChanged(false);

// These are Qt-only settings:

Expand Down Expand Up @@ -95,11 +94,6 @@ void OptionsModel::refreshDataView()
Q_EMIT dataChanged(index(0), index(rowCount(QModelIndex()) - 1));
}

void OptionsModel::refreshWalletDataView()
{
Q_EMIT dataChanged(index(StakeSplitThreshold), index(StakeSplitThreshold));
}

void OptionsModel::setMainDefaultOptions(QSettings& settings, bool reset)
{
// These are shared with the core or have a command-line parameter
Expand Down Expand Up @@ -264,8 +258,6 @@ QVariant OptionsModel::data(const QModelIndex& index, int role) const
return settings.value("bSpendZeroConfChange");
case ShowMasternodesTab:
return settings.value("fShowMasternodesTab");
case StakeSplitThreshold:
return QVariant(getSST());
#endif
case DisplayUnit:
return nDisplayUnit;
Expand Down Expand Up @@ -370,9 +362,6 @@ bool OptionsModel::setData(const QModelIndex& index, const QVariant& value, int
setRestartRequired(true);
}
break;
case StakeSplitThreshold:
setSST(value.toDouble());
break;
#endif
case DisplayUnit:
setDisplayUnit(value);
Expand Down Expand Up @@ -493,31 +482,3 @@ bool OptionsModel::isRestartRequired()
QSettings settings;
return settings.value("fRestartRequired", false).toBool();
}

void OptionsModel::setSST(double val)
{
QSettings settings;
double oldVal = getSST();
if (oldVal != val) {
settings.setValue("SSTVal", val);
setSSTChanged(true);
}
}

double OptionsModel::getSST() const
{
QSettings settings;
return settings.value("SSTVal", 0.0).toDouble();
}

void OptionsModel::setSSTChanged(bool fChanged)
{
QSettings settings;
return settings.setValue("fSSTChanged", fChanged);
}

bool OptionsModel::isSSTChanged()
{
QSettings settings;
return settings.value("fSSTChanged", false).toBool();
}
7 changes: 0 additions & 7 deletions src/qt/optionsmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ class OptionsModel : public QAbstractListModel
AnonymizePivxAmount, //int
ShowMasternodesTab, // bool
Listen, // bool
StakeSplitThreshold, // CAmount (LongLong)
ShowColdStakingScreen, // bool
OptionIDRowCount,
};
Expand All @@ -67,12 +66,8 @@ class OptionsModel : public QAbstractListModel
QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const;
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
void refreshDataView();
void refreshWalletDataView();
/** Updates current unit in memory, settings and emits displayUnitChanged(newUnit) signal */
void setDisplayUnit(const QVariant& value);
/* Stake split threshold*/
void setSST(double val);
double getSST() const;

/* Explicit getters */
bool isHideCharts() { return fHideCharts; }
Expand All @@ -88,8 +83,6 @@ class OptionsModel : public QAbstractListModel
/* Restart flag helper */
void setRestartRequired(bool fRequired);
bool isRestartRequired();
void setSSTChanged(bool fChanged);
bool isSSTChanged();
bool resetSettings;

bool isColdStakingScreenEnabled() { return showColdStakingScreen; }
Expand Down
17 changes: 16 additions & 1 deletion src/qt/pivx/settings/settingswalletoptionswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ void SettingsWalletOptionsWidget::onResetClicked()

void SettingsWalletOptionsWidget::setMapper(QDataWidgetMapper *mapper){
mapper->addMapping(ui->radioButtonSpend, OptionsModel::SpendZeroConfChange);
mapper->addMapping(ui->spinBoxStakeSplitThreshold, OptionsModel::StakeSplitThreshold);

// Network
mapper->addMapping(ui->checkBoxMap, OptionsModel::MapPortUPnP);
Expand All @@ -77,11 +76,27 @@ void SettingsWalletOptionsWidget::setMapper(QDataWidgetMapper *mapper){
mapper->addMapping(ui->lineEditPort, OptionsModel::ProxyPort);
}

void SettingsWalletOptionsWidget::setWalletModel(WalletModel* model)
{
PWidget::setWalletModel(model);
reloadWalletOptions();
}

void SettingsWalletOptionsWidget::reloadWalletOptions()
{
setSpinBoxStakeSplitThreshold(static_cast<double>(walletModel->getWalletStakeSplitThreshold()) / COIN);
}

void SettingsWalletOptionsWidget::setSpinBoxStakeSplitThreshold(double val)
{
ui->spinBoxStakeSplitThreshold->setValue(val);
}

double SettingsWalletOptionsWidget::getSpinBoxStakeSplitThreshold() const
{
return ui->spinBoxStakeSplitThreshold->value();
}

SettingsWalletOptionsWidget::~SettingsWalletOptionsWidget(){
delete ui;
}
14 changes: 11 additions & 3 deletions src/qt/pivx/settings/settingswalletoptionswidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ namespace Ui {
class SettingsWalletOptionsWidget;
}

class SettingsWidget;

class SettingsWalletOptionsWidget : public PWidget
{
Q_OBJECT
Expand All @@ -20,9 +22,6 @@ class SettingsWalletOptionsWidget : public PWidget
explicit SettingsWalletOptionsWidget(PIVXGUI* _window, QWidget *parent = nullptr);
~SettingsWalletOptionsWidget();

void setMapper(QDataWidgetMapper *mapper);
void setSpinBoxStakeSplitThreshold(double val);

Q_SIGNALS:
void saveSettings();
void discardSettings();
Expand All @@ -31,7 +30,16 @@ public Q_SLOTS:
void onResetClicked();

private:
friend class SettingsWidget;

Ui::SettingsWalletOptionsWidget *ui;

void setMapper(QDataWidgetMapper *mapper);
void setWalletModel(WalletModel* model);
void reloadWalletOptions();

void setSpinBoxStakeSplitThreshold(double val);
double getSpinBoxStakeSplitThreshold() const;
};

#endif // SETTINGSWALLETOPTIONSWIDGET_H
25 changes: 14 additions & 11 deletions src/qt/pivx/settings/settingswidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,19 +222,21 @@ void SettingsWidget::onResetAction()

void SettingsWidget::onSaveOptionsClicked()
{
// stake split threshold
const CAmount sstOld = walletModel->getWalletStakeSplitThreshold();
const CAmount sstNew = static_cast<CAmount>(settingsWalletOptionsWidget->getSpinBoxStakeSplitThreshold() * COIN);
if (sstNew != sstOld) {
const double stakeSplitMinimum = walletModel->getSSTMinimum();
if (sstNew != 0 && sstNew < static_cast<CAmount>(stakeSplitMinimum * COIN)) {
settingsWalletOptionsWidget->setSpinBoxStakeSplitThreshold(stakeSplitMinimum);
inform(tr("Stake Split too low, it shall be either >= %1 or equal to 0 (to disable stake splitting)").arg(stakeSplitMinimum));
return;
}
walletModel->setWalletStakeSplitThreshold(sstNew);
}

if (mapper->submit()) {
OptionsModel* optionsModel = this->clientModel->getOptionsModel();
if (optionsModel->isSSTChanged()) {
const double stakeSplitMinimum = walletModel->getSSTMinimum();
const double stakeSplit = optionsModel->getSST();
if (stakeSplit != 0 && stakeSplit < stakeSplitMinimum) {
settingsWalletOptionsWidget->setSpinBoxStakeSplitThreshold(stakeSplitMinimum);
inform(tr("Stake Split too low, it shall be either >= %1 or equal to 0 (to disable stake splitting)").arg(stakeSplitMinimum));
return;
}
walletModel->setWalletStakeSplitThreshold(static_cast<CAmount>(stakeSplit * COIN));
optionsModel->setSSTChanged(false);
}
if (optionsModel->isRestartRequired()) {
bool fAcceptRestart = openStandardDialog(tr("Restart required"), tr("Your wallet needs to be restarted to apply the changes\n"), tr("Restart Now"), tr("Restart Later"));

Expand Down Expand Up @@ -412,6 +414,7 @@ void SettingsWidget::onDiscardChanges()
return;
clientModel->getOptionsModel()->refreshDataView();
}
settingsWalletOptionsWidget->reloadWalletOptions();
}

void SettingsWidget::setMapper()
Expand Down
9 changes: 0 additions & 9 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ WalletModel::WalletModel(CWallet* wallet, OptionsModel* optionsModel, QObject* p
pollTimer = new QTimer(this);
connect(pollTimer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged);
pollTimer->start(MODEL_UPDATE_DELAY * 5);
loadWalletOptions();
subscribeToCoreSignals();
}

Expand All @@ -54,19 +53,11 @@ WalletModel::~WalletModel()
unsubscribeFromCoreSignals();
}

void WalletModel::loadWalletOptions()
{
// update optionsModel with wallet options
optionsModel->setSST(static_cast<double>(getWalletStakeSplitThreshold() / COIN));
optionsModel->refreshWalletDataView();
}

void WalletModel::resetWalletOptions(QSettings& settings)
{
setWalletStakeSplitThreshold(CWallet::DEFAULT_STAKE_SPLIT_THRESHOLD);
setWalletCustomFee(false, DEFAULT_TRANSACTION_FEE);
optionsModel->setWalletDefaultOptions(settings, false);
loadWalletOptions();
}

bool WalletModel::isTestNetwork() const
Expand Down
1 change: 0 additions & 1 deletion src/qt/walletmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@ class WalletModel : public QObject
TransactionTableModel* getTransactionTableModel();
RecentRequestsTableModel* getRecentRequestsTableModel();

void loadWalletOptions();
void resetWalletOptions(QSettings& settings);
bool isTestNetwork() const;
bool isRegTestNetwork() const;
Expand Down

0 comments on commit a66d3f9

Please sign in to comment.