ESC Key
Daniel
xracoonx at gmx.de
Mon Jan 23 10:51:19 UTC 2023
On 2023-01-23 00:29, Richard Kimberly Heck wrote:
> On 1/22/23 10:36, racoon wrote:
>> On 2023-01-22 13:44, Daniel wrote:
>>> On 2023-01-22 10:25, Evan Langlois wrote:
>>>> Is there some way to reconfigure LyX to not have ESC cancel a dialog
>>>> or at least ask me first? I tried to remove the keybinding for esc in
>>>> the preferences, but that didn't help. The issue is that I use vi, so
>>>> when editing the preamble my hand just hits the esc key to save out of
>>>> habit. All my changes are then gone without a "Hey you changed stuff!
>>>> Are you sure?"
>>>
>>> I don't think there is a way. And I am not sure it is a common enough
>>> case to allow for a preference or so.
>>>
>>> But it sounds like you would be satisfied with LyX asking whether to
>>> discard changes made to the preamble before cancelling the dialog. I
>>> think that is generally a good idea. After all, it is like editing your
>>> document where we also don't just let the user close the window without
>>> asking.
>>>
>>> Daniel
>>
>> Attached is a simple patch that avoids the dialog being closed when the
>> Esc key is pressed. The patch presupposes the source code patch to
>> ticket #12577 (https://www.lyx.org/trac/ticket/12577).
>>
>> As suggested above, at least additionally, there should also be a
>> warning when source codes where changed whether the dialog should be
>> closed. But that is not implemented with this patch.
>
> Probalby the better solution is the other one you mentioned: If there
> are changes, do not close without asking first. This could be
> implemented quite generally, I suspect, though the GuiDialog class (and
> whatever one is the other one---remember we have two parallel dialog
> classes for no terribly good reason).
>
> Riki
I guess the greatest loss is with changes in the preamble (and maybe
local layout). So, I would restrict asking for changes in that.
Otherwise, it might just be annoying.
The attached patch does that. However, I changed a function into a
virtual which made me a bit uncomfortable about possible side effects
(there are also a couple of override warnings triggered, obviously, that
haven't corrected either) since I am not fluent with these concepts.
The main challenge is to handle the cancel button, the window close
button and the escape key the same way. It seems to works well as I
implemented it. Maybe someone else could check whether it looks correct.
Daniel
-------------- next part --------------
From 4af7fa2c0236eaa96e361104589e59770ec34076 Mon Sep 17 00:00:00 2001
From: Daniel Ramoeller <d.lyx at web.de>
Date: Mon, 23 Jan 2023 11:43:56 +0100
Subject: [PATCH] Ask to discard preamble
---
src/frontends/qt/GuiDialog.cpp | 17 ++++++++++++++++-
src/frontends/qt/GuiDialog.h | 12 +++++++++++-
src/frontends/qt/GuiDocument.cpp | 16 ++++++++++++++++
src/frontends/qt/GuiDocument.h | 2 ++
4 files changed, 45 insertions(+), 2 deletions(-)
diff --git a/src/frontends/qt/GuiDialog.cpp b/src/frontends/qt/GuiDialog.cpp
index 70d086c7f8..6fe8b36ce7 100644
--- a/src/frontends/qt/GuiDialog.cpp
+++ b/src/frontends/qt/GuiDialog.cpp
@@ -37,10 +37,25 @@ GuiDialog::GuiDialog(GuiView & lv, QString const & name, QString const & title)
}
+void GuiDialog::keyPressEvent(QKeyEvent * event)
+{
+ if (event->key() == Qt::Key_Escape) {
+ // Let the esc key trigger the close event so we can handle it uniformly
+ close();
+ return;
+ }
+ QDialog::keyPressEvent(event);
+}
+
+
void GuiDialog::closeEvent(QCloseEvent * ev)
{
+ setCloseStopped(false);
slotClose();
- ev->accept();
+ if (closeStopped())
+ ev->ignore();
+ else
+ ev->accept();
}
diff --git a/src/frontends/qt/GuiDialog.h b/src/frontends/qt/GuiDialog.h
index 160357dbc6..2d2af2be75 100644
--- a/src/frontends/qt/GuiDialog.h
+++ b/src/frontends/qt/GuiDialog.h
@@ -42,6 +42,9 @@ public:
QWidget * asQWidget() override { return this; }
QWidget const * asQWidget() const override { return this; }
+protected:
+ void keyPressEvent(QKeyEvent * event) override;
+
public Q_SLOTS:
/** \name Buttons
* These methods are publicly accessible because they are invoked
@@ -58,7 +61,7 @@ public Q_SLOTS:
// AutoApply checkbox clicked
void slotAutoApply();
// Close button clicked or closed from WindowManager
- void slotClose();
+ virtual void slotClose();
// A collectiong slot for QDialogButtonBox
void slotButtonBox(QAbstractButton *);
///
@@ -77,6 +80,9 @@ public:
// Set whether to stop the apply process
void setApplyStopped(bool stop) { apply_stopped_ = stop; };
+ // Set whether to stop the close process
+ void setCloseStopped(bool stop) { close_stopped_ = stop; };
+
/** \name Dialog Components
* Methods to access the various components making up a dialog.
*/
@@ -122,6 +128,10 @@ private:
/// stop the apply process?
bool applyStopped() { return apply_stopped_; };
bool apply_stopped_;
+
+ /// stop the apply process?
+ bool closeStopped() { return close_stopped_; };
+ bool close_stopped_;
};
diff --git a/src/frontends/qt/GuiDocument.cpp b/src/frontends/qt/GuiDocument.cpp
index 76b08132b3..c97c7bf2df 100644
--- a/src/frontends/qt/GuiDocument.cpp
+++ b/src/frontends/qt/GuiDocument.cpp
@@ -1903,6 +1903,22 @@ void GuiDocument::slotOK()
}
+void GuiDocument::slotClose()
+{
+ QString preamble = toqstr(bp_.preamble);
+ if (preamble != preambleModule->preambleTE->document()->toPlainText()) {
+ int const ret = Alert::prompt(_("Preamble changed"),
+ _("Do you want to discard changes to the preamble?"),
+ 0, 1, _("&Discard"), _("&Cancel"));
+ if (ret == 1) {
+ setCloseStopped(true);
+ return;
+ }
+ }
+ GuiDialog::slotClose();
+}
+
+
void GuiDocument::slotButtonBox(QAbstractButton * button)
{
switch (buttonBox->standardButton(button)) {
diff --git a/src/frontends/qt/GuiDocument.h b/src/frontends/qt/GuiDocument.h
index ef143ab638..a6bb7a7924 100644
--- a/src/frontends/qt/GuiDocument.h
+++ b/src/frontends/qt/GuiDocument.h
@@ -98,6 +98,8 @@ public Q_SLOTS:
void slotOK();
// Apply button clicked
void slotApply();
+ // Close/cancel button clicked
+ void slotClose() override;
void slotButtonBox(QAbstractButton *);
private Q_SLOTS:
--
2.24.3 (Apple Git-128)
More information about the lyx-devel
mailing list