Add a dialog parent class that handles escape key presses

This commit is contained in:
Veronica Berglyd Olsen
2024-05-27 22:53:30 +02:00
parent 206ca6040e
commit f7cdc7da0c
11 changed files with 49 additions and 39 deletions
+13 -3
View File
@@ -31,7 +31,7 @@ from enum import Enum
from typing import TYPE_CHECKING
from PyQt5.QtCore import QSize, Qt
from PyQt5.QtGui import QWheelEvent
from PyQt5.QtGui import QKeyEvent, QKeySequence, QWheelEvent
from PyQt5.QtWidgets import (
QApplication, QComboBox, QDialog, QDoubleSpinBox, QSpinBox, QToolButton,
QWidget
@@ -43,7 +43,17 @@ if TYPE_CHECKING: # pragma: no cover
from novelwriter.guimain import GuiMain
class NToolDialog(QDialog):
class NDialog(QDialog):
def keyPressEvent(self, event: QKeyEvent) -> None:
"""Overload keyPressEvent and forward escape to close."""
if event.matches(QKeySequence.StandardKey.Cancel):
self.close()
event.ignore()
return
class NToolDialog(NDialog):
def __init__(self, parent: GuiMain) -> None:
super().__init__(parent=parent)
@@ -62,7 +72,7 @@ class NToolDialog(QDialog):
return
class NNonBlockingDialog(QDialog):
class NNonBlockingDialog(NDialog):
def __init__(self, parent: QWidget | None = None) -> None:
super().__init__(parent=parent)