fix: warn on preset save when plugin not found (#16929)

* fix: warn on preset save when plugin not found

* refactor: build the unsaved-changes save config in one place

* Fixes clang-tidy

---------

Co-authored-by: Lam Wei Lun <weilun.lam@gmail.com>
This commit is contained in:
Ian ChuaandLam Wei Lun authored and GitHub committed 2026-10-28 13:02:05 +08:00
1 parent 3322f6101c
commit 3a0517831f
15 files changed
+411 -66

No files matched your search

+69 -4
View File
@@ -24,6 +24,7 @@
#include <set>
#include <iterator>
#include <string>
#include <utility>
#include <vector>
#include <boost/algorithm/string.hpp>
@@ -78,6 +79,8 @@ namespace GUI {
wxDEFINE_EVENT(EVT_DIFF_DIALOG_TRANSFER, SimpleEvent);
static PresetCollection* get_preset_collection(Preset::Type type, PresetBundle* preset_bundle = nullptr);
// ----------------------------------------------------------------------------
// ModelNode: a node inside DiffModel
@@ -848,7 +851,8 @@ UnsavedChangesDialog::UnsavedChangesDialog(const wxString &caption, const wxStri
}
UnsavedChangesDialog::UnsavedChangesDialog(Preset::Type type, PresetCollection *dependent_presets, const std::string &new_selected_preset, bool no_transfer)
: m_new_selected_preset_name(new_selected_preset)
: m_dependent_presets(dependent_presets)
, m_new_selected_preset_name(new_selected_preset)
, DPIDialog(static_cast<wxWindow *>(wxGetApp().mainframe),
wxID_ANY,
_L("Transfer or discard changes"),
@@ -870,8 +874,13 @@ inline int UnsavedChangesDialog::ShowModal()
auto choise = wxGetApp().app_config->get(choise_key);
long result = 0;
if ((m_buttons & REMEMBER_CHOISE) && !choise.empty() && wxString(choise).ToLong(&result) && (1 << result) & (m_buttons | DONT_SAVE)) {
m_exit_action = Action(result);
return 0;
const Action remembered = Action(result);
// A remembered Save would otherwise skip the unresolved-plugin warning in save(). Show the
// dialog when the presets being saved need it; remembered Transfer/Discard keep the shortcut.
if (remembered != Action::Save || !selected_save_needs_plugin_warning()) {
m_exit_action = remembered;
return 0;
}
}
int r = DPIDialog::ShowModal();
if (r != wxID_CANCEL && dynamic_cast<::CheckBox*>(FindWindowById(wxID_APPLY))->GetValue()) {
@@ -1176,6 +1185,30 @@ void UnsavedChangesDialog::close(Action action)
this->EndModal(wxID_CLOSE);
}
DynamicPrintConfig UnsavedChangesDialog::make_save_config(PresetCollection& presets)
{
DynamicPrintConfig config = presets.get_edited_preset().config;
const std::vector<std::string> unselected_options = get_unselected_options(presets.type());
if (!unselected_options.empty())
config.apply_only(presets.get_selected_preset().config, unselected_options);
return config;
}
bool UnsavedChangesDialog::selected_save_needs_plugin_warning()
{
if (m_dependent_presets != nullptr)
return has_unresolved_plugin_capabilities(make_save_config(*m_dependent_presets));
const PrinterTechnology printer_technology = wxGetApp().preset_bundle->printers.get_edited_preset().printer_technology();
for (Tab* tab : wxGetApp().tabs_list)
if (tab->supports_printer_technology(printer_technology) && tab->current_preset_is_dirty()) {
PresetCollection* presets = tab->get_presets();
if (presets != nullptr && has_unresolved_plugin_capabilities(make_save_config(*presets)))
return true;
}
return false;
}
bool UnsavedChangesDialog::save(PresetCollection* dependent_presets, bool show_save_preset_dialog/* = true*/)
{
names_and_types.clear();
@@ -1188,6 +1221,14 @@ bool UnsavedChangesDialog::save(PresetCollection* dependent_presets, bool show_s
// for system/default/external presets we should take an edited name
//BBS: add project embedded preset logic and refine is_external
bool save_to_project = false;
// Warn before the save-name dialog: accepting that dialog can delete an overwritten preset,
// so a Cancel must be decided before it. Transfer reaches save() with
// show_save_preset_dialog == false and must not warn.
if (show_save_preset_dialog &&
!confirm_save_with_unresolved_plugins(this, make_save_config(*dependent_presets), SaveSubject::Preset))
return false;
if (!preset.can_overwrite()) {
//if (preset.is_system || preset.is_default || preset.is_external) {
SavePresetDialog save_dlg(this, preset.type);
@@ -1226,6 +1267,30 @@ bool UnsavedChangesDialog::save(PresetCollection* dependent_presets, bool show_s
}
// One Save can write several presets. Build each exact partial config the same way
// PresetBundle::save_changes_for_preset will, and warn once before any SavePresetDialog can
// delete an overwritten preset. Transfer/Discard must not warn.
if (show_save_preset_dialog && !names_and_types.empty()) {
std::vector<std::unique_ptr<DynamicPrintConfig>> configs;
std::vector<SaveConfigToCheck> checks;
checks.reserve(names_and_types.size());
for (const PresetData& nt : names_and_types) {
PresetCollection* presets = get_preset_collection(nt.type, wxGetApp().preset_bundle);
if (!presets)
continue;
auto config = std::make_unique<DynamicPrintConfig>(make_save_config(*presets));
checks.push_back({from_u8(nt.name), config.get()});
configs.emplace_back(std::move(config));
}
if (!checks.empty()) {
const bool confirmed = checks.size() == 1
? confirm_save_with_unresolved_plugins(this, *checks.front().config, SaveSubject::Preset)
: confirm_save_presets_with_unresolved_plugins(this, checks);
if (!confirmed)
return false;
}
}
if (show_save_preset_dialog && !types_for_save.empty()) {
SavePresetDialog save_dlg(this, types_for_save);
if (save_dlg.ShowModal() != wxID_OK) {
@@ -1774,7 +1839,7 @@ FullCompareDialog::FullCompareDialog(const wxString& option_name, const wxString
}
static PresetCollection* get_preset_collection(Preset::Type type, PresetBundle* preset_bundle = nullptr) {
static PresetCollection* get_preset_collection(Preset::Type type, PresetBundle* preset_bundle) {
if (!preset_bundle)
preset_bundle = wxGetApp().preset_bundle;
return type == Preset::Type::TYPE_PRINTER ? &preset_bundle->printers :