From 30bd56ffb2d7c97f9e310d5b593e3d7abc43ffcc Mon Sep 17 00:00:00 2001 From: Loren Eteval Date: Mon, 28 Aug 2023 11:47:46 +0800 Subject: [PATCH] Add smart quotes for ZoomablePlainTextEdit Signed-off-by: Loren Eteval --- Furious/Widget/Widget.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Furious/Widget/Widget.py b/Furious/Widget/Widget.py index 445e5e0..6583a5f 100644 --- a/Furious/Widget/Widget.py +++ b/Furious/Widget/Widget.py @@ -10,7 +10,7 @@ from Furious.Utility.Utility import ( from Furious.Utility.Translator import Translatable, gettext as _ from PySide6 import QtCore -from PySide6.QtGui import QBrush, QColor, QFont +from PySide6.QtGui import QBrush, QColor, QFont, QTextCursor from PySide6.QtWidgets import ( QHeaderView, QInputDialog, @@ -321,12 +321,32 @@ class ZoomablePlainTextEdit(QPlainTextEdit): self.setTextCursor(cursor) + def smartQuotes(self, event): + cursor = self.textCursor() + + if cursor.block().text().endswith('"'): + # Do quote action + super().keyPressEvent(event) + else: + # Do quote action + super().keyPressEvent(event) + + # Add the other quote + cursor.insertText('"') + + # Move to middle + cursor.movePosition(QTextCursor.MoveOperation.Left) + + self.setTextCursor(cursor) + def keyPressEvent(self, event): if ( event.key() == QtCore.Qt.Key.Key_Return or event.key() == QtCore.Qt.Key.Key_Enter ): self.smartIndent(event) + elif event.key() == QtCore.Qt.Key.Key_QuoteDbl: + self.smartQuotes(event) else: super().keyPressEvent(event)