]> git.lyx.org Git - features.git/commitdiff
Warn if buffer is switched with unapplied document changes (#9369)
authorJuergen Spitzmueller <spitz@lyx.org>
Sat, 10 Dec 2022 13:28:46 +0000 (14:28 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Sat, 10 Dec 2022 13:28:46 +0000 (14:28 +0100)
src/frontends/qt/GuiDocument.cpp
src/frontends/qt/GuiDocument.h

index 6da810f64890e011f61bbe6dcff7ac74a9142bbd..d1937a1adeb32fe39399f22a324a71e91108970e 100644 (file)
@@ -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();
index 5d06226b238ea2febed890f6cb451643421ad646..745afc399889ab5d61da9d77ca63052fa8d720e6 100644 (file)
@@ -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<modInfoStruct> 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_;
 };