Qt: don't save shader choice until OK/Apply is clicked

This commit is contained in:
Adam Higerd 2024-11-19 16:50:12 -06:00 committed by Vicki Pfau
parent f930184efb
commit a2e7e5b902
3 changed files with 36 additions and 12 deletions

View File

@ -428,6 +428,7 @@ void SettingsView::setShaderSelector(ShaderSelector* shaderSelector) {
} }
m_shader = shaderSelector; m_shader = shaderSelector;
QObject::connect(this, &SettingsView::saveSettingsRequested, m_shader, &ShaderSelector::saveSettings); QObject::connect(this, &SettingsView::saveSettingsRequested, m_shader, &ShaderSelector::saveSettings);
QObject::connect(m_ui.buttonBox, &QDialogButtonBox::rejected, m_shader, &ShaderSelector::revert);
if (shaderSelector) { if (shaderSelector) {
addPage(tr("Shaders"), m_shader, Page::SHADERS); addPage(tr("Shaders"), m_shader, Page::SHADERS);
} else { } else {

View File

@ -50,7 +50,25 @@ ShaderSelector::~ShaderSelector() {
} }
void ShaderSelector::saveSettings() { void ShaderSelector::saveSettings() {
emit saved(); QString oldPath = config->getOption("shader");
if (oldPath != m_shaderPath) {
if (m_shaderPath.isEmpty()) {
clearShader(true);
} else {
loadShader(m_shaderPath, true);
}
}
emit saveSettingsRequested();
}
void ShaderSelector::revert() {
QString shaderPath = m_config->getOption("shader");
if (shaderPath.isEmpty()) {
clearShader();
} else {
loadShader(shaderPath);
emit reset();
}
} }
void ShaderSelector::clear() { void ShaderSelector::clear() {
@ -78,7 +96,7 @@ void ShaderSelector::selectShader() {
} }
} }
void ShaderSelector::loadShader(const QString& path) { void ShaderSelector::loadShader(const QString& path, bool saveToSettings) {
VDir* shader = VFileDevice::openDir(path); VDir* shader = VFileDevice::openDir(path);
if (!shader) { if (!shader) {
shader = VFileDevice::openArchive(path); shader = VFileDevice::openArchive(path);
@ -89,14 +107,18 @@ void ShaderSelector::loadShader(const QString& path) {
m_display->setShaders(shader); m_display->setShaders(shader);
shader->close(shader); shader->close(shader);
m_shaderPath = path; m_shaderPath = path;
m_config->setOption("shader", m_shaderPath); if (saveToSettings) {
m_config->setOption("shader", path);
}
} }
void ShaderSelector::clearShader() { void ShaderSelector::clearShader(bool saveToSettings) {
m_display->clearShaders(); m_display->clearShaders();
refreshShaders();
m_shaderPath = ""; m_shaderPath = "";
if (saveToSettings) {
m_config->setOption("shader", m_shaderPath); m_config->setOption("shader", m_shaderPath);
}
refreshShaders();
} }
void ShaderSelector::refreshShaders() { void ShaderSelector::refreshShaders() {
@ -121,7 +143,7 @@ void ShaderSelector::refreshShaders() {
m_ui.author->clear(); m_ui.author->clear();
} }
disconnect(this, &ShaderSelector::saved, 0, 0); disconnect(this, &ShaderSelector::saveSettingsRequested, 0, 0);
disconnect(this, &ShaderSelector::reset, 0, 0); disconnect(this, &ShaderSelector::reset, 0, 0);
disconnect(this, &ShaderSelector::resetToDefault, 0, 0); disconnect(this, &ShaderSelector::resetToDefault, 0, 0);
@ -160,7 +182,7 @@ void ShaderSelector::addUniform(QGridLayout* settings, const QString& section, c
connect(f, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), [value](double v) { connect(f, static_cast<void (QDoubleSpinBox::*)(double)>(&QDoubleSpinBox::valueChanged), [value](double v) {
*value = v; *value = v;
}); });
connect(this, &ShaderSelector::saved, [this, section, name, f]() { connect(this, &ShaderSelector::saveSettingsRequested, [this, section, name, f]() {
m_config->setQtOption(name, f->value(), section); m_config->setQtOption(name, f->value(), section);
}); });
connect(this, &ShaderSelector::reset, [this, section, name, f]() { connect(this, &ShaderSelector::reset, [this, section, name, f]() {
@ -194,7 +216,7 @@ void ShaderSelector::addUniform(QGridLayout* settings, const QString& section, c
connect(i, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [value](int v) { connect(i, static_cast<void (QSpinBox::*)(int)>(&QSpinBox::valueChanged), [value](int v) {
*value = v; *value = v;
}); });
connect(this, &ShaderSelector::saved, [this, section, name, i]() { connect(this, &ShaderSelector::saveSettingsRequested, [this, section, name, i]() {
m_config->setQtOption(name, i->value(), section); m_config->setQtOption(name, i->value(), section);
}); });
connect(this, &ShaderSelector::reset, [this, section, name, i]() { connect(this, &ShaderSelector::reset, [this, section, name, i]() {

View File

@ -31,15 +31,16 @@ public slots:
void saveSettings(); void saveSettings();
void refreshShaders(); void refreshShaders();
void clear(); void clear();
void revert();
private slots: private slots:
void selectShader(); void selectShader();
void loadShader(const QString& path); void loadShader(const QString& path, bool saveToSettings = false);
void clearShader(); void clearShader(bool saveToSettings = false);
void buttonPressed(QAbstractButton*); void buttonPressed(QAbstractButton*);
signals: signals:
void saved(); void saveSettingsRequested();
void reset(); void reset();
void resetToDefault(); void resetToDefault();