From ec2da3a509484a4f306101a8c67ab741e5fd4f6b Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Sun, 5 Nov 2023 12:09:33 +0100 Subject: [PATCH] Prevent data loss when closing LyX while document dialog has unapplied changes (#12955) Entails new strings, but I think the severity allows for that. --- src/frontends/qt/Dialog.h | 2 ++ src/frontends/qt/DialogView.cpp | 2 ++ src/frontends/qt/DialogView.h | 1 + src/frontends/qt/DockView.h | 1 + src/frontends/qt/GuiDialog.cpp | 2 ++ src/frontends/qt/GuiDialog.h | 1 + src/frontends/qt/GuiDocument.cpp | 21 +++++++++++++++++++-- src/frontends/qt/GuiDocument.h | 1 + src/frontends/qt/GuiView.cpp | 1 + 9 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/frontends/qt/Dialog.h b/src/frontends/qt/Dialog.h index be9b3232f7..6968aea803 100644 --- a/src/frontends/qt/Dialog.h +++ b/src/frontends/qt/Dialog.h @@ -267,6 +267,8 @@ protected: /// To be called when the buffer view has changed virtual void onBufferViewChanged() = 0; /// + virtual void onClosing(int) = 0; + /// void connectToNewInset(); private: diff --git a/src/frontends/qt/DialogView.cpp b/src/frontends/qt/DialogView.cpp index a5e411dedb..2d778f27eb 100644 --- a/src/frontends/qt/DialogView.cpp +++ b/src/frontends/qt/DialogView.cpp @@ -25,6 +25,8 @@ DialogView::DialogView(GuiView & lv, QString const & name, QString const & title { connect(&lv, SIGNAL(bufferViewChanged()), this, SLOT(onBufferViewChanged())); + connect(&lv, SIGNAL(closing(int)), + this, SLOT(onClosing(int))); // remove question marks from Windows dialogs setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); diff --git a/src/frontends/qt/DialogView.h b/src/frontends/qt/DialogView.h index 514242e926..81347bde5a 100644 --- a/src/frontends/qt/DialogView.h +++ b/src/frontends/qt/DialogView.h @@ -48,6 +48,7 @@ protected: protected Q_SLOTS: void onBufferViewChanged() override {} + void onClosing(int) override {} }; } // namespace frontend diff --git a/src/frontends/qt/DockView.h b/src/frontends/qt/DockView.h index aef0c8281a..1a6295446e 100644 --- a/src/frontends/qt/DockView.h +++ b/src/frontends/qt/DockView.h @@ -56,6 +56,7 @@ public: protected Q_SLOTS: void onBufferViewChanged() override {} + void onClosing(int) override {} }; } // namespace frontend diff --git a/src/frontends/qt/GuiDialog.cpp b/src/frontends/qt/GuiDialog.cpp index 186d9ea3a0..491cbeca34 100644 --- a/src/frontends/qt/GuiDialog.cpp +++ b/src/frontends/qt/GuiDialog.cpp @@ -34,6 +34,8 @@ GuiDialog::GuiDialog(GuiView & lv, QString const & name, QString const & title) { connect(&lv, SIGNAL(bufferViewChanged()), this, SLOT(onBufferViewChanged())); + connect(&lv, SIGNAL(closing(int)), + this, SLOT(onClosing(int))); // remove question marks from Windows dialogs setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); diff --git a/src/frontends/qt/GuiDialog.h b/src/frontends/qt/GuiDialog.h index bd50df067b..a1681afb53 100644 --- a/src/frontends/qt/GuiDialog.h +++ b/src/frontends/qt/GuiDialog.h @@ -66,6 +66,7 @@ public Q_SLOTS: protected Q_SLOTS: void onBufferViewChanged() override {} + void onClosing(int) override {} public: /** Check whether we may apply our data. diff --git a/src/frontends/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp index b16e8923bc..bd87d9a38e 100644 --- a/src/frontends/qt/GuiDocument.cpp +++ b/src/frontends/qt/GuiDocument.cpp @@ -1849,6 +1849,23 @@ GuiDocument::GuiDocument(GuiView & lv) } +void GuiDocument::onClosing(int const id) +{ + if (!guiApp || !guiApp->currentView() + || guiApp->currentView()->id() != id + || !bc().policy().buttonStatus(ButtonPolicy::RESTORE)) + // notthing to do + return; + + int const ret = Alert::prompt(_("Unapplied changes"), + _("Some changes in the document were not yet applied.\n" + "Do you want to apply them before closing?"), + 1, 1, _("Yes, &Apply"), _("No, &Dismiss Changes")); + if (ret == 0) + slotOK(); +} + + void GuiDocument::onBufferViewChanged() { if (switchback_) { @@ -1868,8 +1885,8 @@ void GuiDocument::onBufferViewChanged() // 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")); + "Do you want to switch back in order to apply them or dismiss the changes?"), + 1, 1, _("&Switch Back"), _("&Dismiss Changes")); if (ret == 0) { // Switch to previous buffer view and apply switchback_ = true; diff --git a/src/frontends/qt/GuiDocument.h b/src/frontends/qt/GuiDocument.h index 5b531943cb..c7d0cc06f8 100644 --- a/src/frontends/qt/GuiDocument.h +++ b/src/frontends/qt/GuiDocument.h @@ -96,6 +96,7 @@ public: public Q_SLOTS: void onBufferViewChanged() override; + void onClosing(int) override; // OK button clicked void slotOK(); // Apply button clicked diff --git a/src/frontends/qt/GuiView.cpp b/src/frontends/qt/GuiView.cpp index 47f8230d02..79ffc0a207 100644 --- a/src/frontends/qt/GuiView.cpp +++ b/src/frontends/qt/GuiView.cpp @@ -1311,6 +1311,7 @@ void GuiView::closeEvent(QCloseEvent * close_event) // it can happen that this event arrives without selecting the view, // e.g. when clicking the close button on a background window. setFocus(); + Q_EMIT closing(id_); if (!closeWorkAreaAll()) { closing_ = false; close_event->ignore(); -- 2.39.5