From: Juergen Spitzmueller Date: Sat, 10 Dec 2022 13:28:46 +0000 (+0100) Subject: Warn if buffer is switched with unapplied document changes (#9369) X-Git-Url: https://git.lyx.org/gitweb/?a=commitdiff_plain;h=7f1cb306f942709601314f3e396f0e01defdc45d;p=features.git Warn if buffer is switched with unapplied document changes (#9369) --- diff --git a/src/frontends/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp index 6da810f648..d1937a1ade 100644 --- a/src/frontends/qt/GuiDocument.cpp +++ b/src/frontends/qt/GuiDocument.cpp @@ -783,7 +783,8 @@ void LocalLayout::editExternal() { GuiDocument::GuiDocument(GuiView & lv) : GuiDialog(lv, "document", qt_("Document Settings")), biblioChanged_(false), nonModuleChanged_(false), - modulesChanged_(false), shellescapeChanged_(false) + modulesChanged_(false), shellescapeChanged_(false), + switchback_(false), prompted_(false) { setupUi(this); @@ -1783,6 +1784,39 @@ GuiDocument::GuiDocument(GuiView & lv) void GuiDocument::onBufferViewChanged() { + if (switchback_) { + // We are just switching back. Nothing to do. + switchback_ = false; + return; + } + BufferView const * view = bufferview(); + string const new_filename = view ? view->buffer().absFileName() : string(); + // If we switched buffer really and the previous file name is different to + // the current one, we ask on unapplied changes (#9369) + // FIXME: This is more complicated than it should be. Why do we need these to cycles? + // And ideally, we should propose to apply without having to switch back + // (e.g., via a LFUN_BUFFER_PARAMS_APPLY_OTHER) + if (!prev_buffer_filename_.empty() && prev_buffer_filename_ != new_filename + && buttonBox->button(QDialogButtonBox::Apply)->isEnabled()) { + // Only ask if we haven't yet in this cycle + int const ret = prompted_ ? 3 : Alert::prompt(_("Unapplied changes"), + _("Some changes in the previous document were not yet applied.\n" + "Do you want to switch back and apply them?"), + 1, 1, _("Yes, &Switch Back"), _("No, &Dismiss Changes")); + if (ret == 0) { + // Switch to previous buffer view and apply + switchback_ = true; + // Record that we have asked. + prompted_ = true; + lyx::dispatch(FuncRequest(LFUN_BUFFER_SWITCH, prev_buffer_filename_)); + return; + } else if (ret == 3) { + // We are in the second cycle. Set back. + prompted_ = false; + return; + } + } + if (isVisibleView()) initialiseParams(""); } @@ -4906,6 +4940,7 @@ bool GuiDocument::initialiseParams(string const &) paramsToDialog(); return true; } + prev_buffer_filename_ = view->buffer().absFileName(); bp_ = view->buffer().params(); loadModuleInfo(); updateAvailableModules(); diff --git a/src/frontends/qt/GuiDocument.h b/src/frontends/qt/GuiDocument.h index 5d06226b23..745afc3998 100644 --- a/src/frontends/qt/GuiDocument.h +++ b/src/frontends/qt/GuiDocument.h @@ -334,6 +334,9 @@ private: void filterModules(QString const & string); /// BufferParams bp_; + /// Store buffer filename for the case we switch with + /// unapplied changes. + std::string prev_buffer_filename_; /// List of names of available modules std::list moduleNames_; /// @@ -348,6 +351,12 @@ private: bool modulesChanged_; /// Track if the shellescape param changed bool shellescapeChanged_; + /// Track if we switch back to a buffer due to unapplied + /// changes + bool switchback_; + /// Track whether we prompted the user about unapplied + /// changes + bool prompted_; };