Added unicode constants class, and support for non-breaking spaces, which aren't preserved on save.

This commit is contained in:
Veronica K. B. Olsen
2019-10-29 11:35:40 +01:00
parent 2c63d1fbf2
commit 146756a395
6 changed files with 111 additions and 35 deletions
+30 -6
View File
@@ -23,7 +23,7 @@ from nw.project.document import NWDoc
from nw.gui.dochighlight import GuiDocHighlighter
from nw.gui.wordcounter import WordCounter
from nw.tools.spellcheck import NWSpellCheck
from nw.constants import nwFiles
from nw.constants import nwFiles, nwUnicode
from nw.enum import nwDocAction, nwAlert
logger = logging.getLogger(__name__)
@@ -316,9 +316,26 @@ class GuiDocEditor(QTextEdit):
to know whether we had a selection prior to triggering the _docChange slot, as we do not
want to trigger autoreplace on selections. Autoreplace on selections messes with undo/redo
history.
We also need to intercept the Shift key modifier for certain key combinations that modifies
standard keys like enter and space. However, we don't want to spend a lot of time in this
function as it is triggered on every keypress when typing.
"""
self.hasSelection = self.textCursor().hasSelection()
QTextEdit.keyPressEvent(self, keyEvent)
if keyEvent.modifiers() == Qt.ShiftModifier:
theKey = keyEvent.key()
if theKey == Qt.Key_Return:
self._insertHardBreak()
elif theKey == Qt.Key_Enter:
self._insertHardBreak()
elif theKey == Qt.Key_Space:
self._insertNonBreakingSpace()
else:
QTextEdit.keyPressEvent(self, keyEvent)
else:
QTextEdit.keyPressEvent(self, keyEvent)
return
##
@@ -332,6 +349,13 @@ class GuiDocEditor(QTextEdit):
theCursor.endEditBlock()
return
def _insertNonBreakingSpace(self):
theCursor = self.textCursor()
theCursor.beginEditBlock()
theCursor.insertText(nwUnicode.U_NBSP)
theCursor.endEditBlock()
return
def _openSpellContext(self):
self._openContextMenu(self.cursorRect().center())
return
@@ -442,15 +466,15 @@ class GuiDocEditor(QTextEdit):
elif self.mainConf.doReplaceDash and theTwo == "--":
theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 2)
theCursor.insertText("\u2013")
theCursor.insertText(nwUnicode.U_NDASH)
elif self.mainConf.doReplaceDash and theTwo == "\u2013-":
elif self.mainConf.doReplaceDash and theTwo == nwUnicode.U_NDASH+"-":
theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 2)
theCursor.insertText("\u2014")
theCursor.insertText(nwUnicode.U_MDASH)
elif self.mainConf.doReplaceDots and theThree == "...":
theCursor.movePosition(QTextCursor.Left, QTextCursor.KeepAnchor, 3)
theCursor.insertText("\u2026")
theCursor.insertText(nwUnicode.U_HELLIP)
return