diff --git a/src/controllers/controllerinputmappingtablemodel.cpp b/src/controllers/controllerinputmappingtablemodel.cpp index 7dec69fdf23..0fd42de58f9 100644 --- a/src/controllers/controllerinputmappingtablemodel.cpp +++ b/src/controllers/controllerinputmappingtablemodel.cpp @@ -47,6 +47,12 @@ void ControllerInputMappingTableModel::onPresetLoaded() { m_midiInputMappings = m_pMidiPreset->getInputMappings().values(); endInsertRows(); } + connect(this, + &QAbstractTableModel::dataChanged, + this, + [this]() { + m_pMidiPreset->setDirty(true); + }); } } diff --git a/src/controllers/controlleroutputmappingtablemodel.cpp b/src/controllers/controlleroutputmappingtablemodel.cpp index 025185db3fc..70284d70fcc 100644 --- a/src/controllers/controlleroutputmappingtablemodel.cpp +++ b/src/controllers/controlleroutputmappingtablemodel.cpp @@ -49,6 +49,12 @@ void ControllerOutputMappingTableModel::onPresetLoaded() { m_midiOutputMappings = m_pMidiPreset->getOutputMappings().values(); endInsertRows(); } + connect(this, + &QAbstractTableModel::dataChanged, + this, + [this]() { + m_pMidiPreset->setDirty(true); + }); } } diff --git a/src/controllers/dlgprefcontroller.cpp b/src/controllers/dlgprefcontroller.cpp index 879aaccc93d..1561a929859 100644 --- a/src/controllers/dlgprefcontroller.cpp +++ b/src/controllers/dlgprefcontroller.cpp @@ -496,7 +496,11 @@ void DlgPrefController::slotApply() { bEnabled = m_ui.chkEnabledDevice->isChecked(); if (m_pPreset->isDirty()) { - savePreset(); + if (savePreset()) { + // We might have saved the previous preset with a new name, + // so update the preset combobox. + enumeratePresets(m_pPreset->filePath()); + } } } m_ui.chkEnabledDevice->setChecked(bEnabled); @@ -562,12 +566,13 @@ void DlgPrefController::slotPresetSelected(int chosenIndex) { } applyPresetChanges(); + bool previousPresetSaved = false; if (m_pPreset && m_pPreset->isDirty()) { if (QMessageBox::question(this, tr("Mapping has been edited"), tr("Do you want to save the changes?")) == QMessageBox::Yes) { - savePreset(); + previousPresetSaved = savePreset(); } } @@ -578,17 +583,23 @@ void DlgPrefController::slotPresetSelected(int chosenIndex) { DEBUG_ASSERT(!pPreset->isDirty()); } - slotShowPreset(pPreset); + if (previousPresetSaved) { + // We might have saved the previous preset with a new name, so update + // the preset combobox. + enumeratePresets(presetPath); + } else { + slotShowPreset(pPreset); + } } -void DlgPrefController::savePreset() { +bool DlgPrefController::savePreset() { VERIFY_OR_DEBUG_ASSERT(m_pPreset) { - return; + return false; } if (!m_pPreset->isDirty()) { qDebug() << "Mapping is not dirty, no need to save it."; - return; + return false; } QString oldFilePath = m_pPreset->filePath(); @@ -634,7 +645,7 @@ void DlgPrefController::savePreset() { m_pOverwritePresets.insert(m_pPreset->filePath(), true); } } else if (overwriteMsgBox.close()) { - return; + return false; } } @@ -646,6 +657,11 @@ void DlgPrefController::savePreset() { newFilePath = oldFilePath; } else { presetName = askForPresetName(presetName); + if (presetName.isEmpty()) { + // QInputDialog was closed + qDebug() << "Mapping not saved, new name is empty"; + return false; + } newFilePath = presetNameToPath(m_pUserDir, presetName); m_pPreset->setName(presetName); qDebug() << "Mapping renamed to" << m_pPreset->name(); @@ -653,14 +669,14 @@ void DlgPrefController::savePreset() { if (!m_pPreset->savePreset(newFilePath)) { qDebug() << "Failed to save mapping as" << newFilePath; - return; + return false; } qDebug() << "Mapping saved as" << newFilePath; m_pPreset->setFilePath(newFilePath); m_pPreset->setDirty(false); - enumeratePresets(m_pPreset->filePath()); + return true; } QString DlgPrefController::askForPresetName(const QString& prefilledName) const { @@ -688,7 +704,8 @@ QString DlgPrefController::askForPresetName(const QString& prefilledName) const .remove(rxRemove) .trimmed(); if (!ok) { - continue; + // Return empty string if the dialog was canceled. Callers will deal with this. + return QString(); } if (presetName.isEmpty()) { QMessageBox::warning(nullptr, diff --git a/src/controllers/dlgprefcontroller.h b/src/controllers/dlgprefcontroller.h index 115191c0e64..fb0764877b3 100644 --- a/src/controllers/dlgprefcontroller.h +++ b/src/controllers/dlgprefcontroller.h @@ -73,7 +73,7 @@ class DlgPrefController : public DlgPreferencePage { QString presetPathFromIndex(int index) const; QString askForPresetName(const QString& prefilledName = QString()) const; void applyPresetChanges(); - void savePreset(); + bool savePreset(); void initTableView(QTableView* pTable); /// Set dirty state (i.e. changes have been made).