Make the editor a plain text editor again

This commit is contained in:
Veronica Berglyd Olsen
2023-09-06 17:47:25 +02:00
parent 8275963811
commit 2b98e0d4d2
3 changed files with 30 additions and 29 deletions
+9 -8
View File
@@ -42,11 +42,13 @@ from PyQt5.QtCore import (
) )
from PyQt5.QtGui import ( from PyQt5.QtGui import (
QColor, QCursor, QFont, QFontMetrics, QKeyEvent, QKeySequence, QMouseEvent, QColor, QCursor, QFont, QFontMetrics, QKeyEvent, QKeySequence, QMouseEvent,
QPalette, QPixmap, QResizeEvent, QTextBlock, QTextCursor, QTextDocument, QTextOption QPalette, QPixmap, QResizeEvent, QTextBlock, QTextCursor, QTextDocument,
QTextOption
) )
from PyQt5.QtWidgets import ( from PyQt5.QtWidgets import (
QAction, qApp, QFrame, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMenu, QAction, QFrame, QGridLayout, QHBoxLayout, QLabel, QLineEdit, QMenu,
QPushButton, QShortcut, QTextEdit, QToolBar, QToolButton, QWidget QPlainTextEdit, QPushButton, QShortcut, QToolBar, QToolButton, QWidget,
qApp
) )
from novelwriter import CONFIG, SHARED from novelwriter import CONFIG, SHARED
@@ -63,7 +65,7 @@ if TYPE_CHECKING: # pragma: no cover
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class GuiDocEditor(QTextEdit): class GuiDocEditor(QPlainTextEdit):
"""Gui Widget: Main Document Editor""" """Gui Widget: Main Document Editor"""
MOVE_KEYS = ( MOVE_KEYS = (
@@ -141,7 +143,6 @@ class GuiDocEditor(QTextEdit):
# Editor Settings # Editor Settings
self.setMinimumWidth(CONFIG.pxInt(300)) self.setMinimumWidth(CONFIG.pxInt(300))
self.setAcceptRichText(False)
self.setAutoFillBackground(True) self.setAutoFillBackground(True)
self.setFrameStyle(QFrame.NoFrame) self.setFrameStyle(QFrame.NoFrame)
@@ -678,9 +679,9 @@ class GuiDocEditor(QTextEdit):
lineIdx = line - 1 # Block index is 0 offset, lineNo is 1 offset lineIdx = line - 1 # Block index is 0 offset, lineNo is 1 offset
if lineIdx >= 0: if lineIdx >= 0:
theBlock = self.document().findBlockByLineNumber(lineIdx) block = self.document().findBlockByNumber(lineIdx)
if theBlock: if block:
self.setCursorPosition(theBlock.position()) self.setCursorPosition(block.position())
logger.debug("Cursor moved to line %d", line) logger.debug("Cursor moved to line %d", line)
return True return True
+1 -1
View File
@@ -44,7 +44,7 @@ def testGuiEditor_Init(qtbot, nwGUI, projPath, ipsumText, mockRnd):
buildTestProject(nwGUI, projPath) buildTestProject(nwGUI, projPath)
assert nwGUI.openDocument(C.hSceneDoc) assert nwGUI.openDocument(C.hSceneDoc)
nwGUI.docEditor.setText("### Lorem Ipsum\n\n%s" % ipsumText[0]) nwGUI.docEditor.setPlainText("### Lorem Ipsum\n\n%s" % ipsumText[0])
assert nwGUI.saveDocument() assert nwGUI.saveDocument()
# Check Defaults # Check Defaults
+20 -20
View File
@@ -212,7 +212,7 @@ def testGuiMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum):
# ================== # ==================
cleanText = "A single, short paragraph.\n\n" cleanText = "A single, short paragraph.\n\n"
nwGUI.docEditor.setText(cleanText) nwGUI.docEditor.setPlainText(cleanText)
assert nwGUI.docEditor.setCursorPosition(0) assert nwGUI.docEditor.setCursorPosition(0)
# Left Align # Left Align
@@ -247,7 +247,7 @@ def testGuiMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum):
# Other Checks # Other Checks
# Replace Quotes # Replace Quotes
nwGUI.docEditor.setText(( nwGUI.docEditor.setPlainText((
"### New Text\n\n" "### New Text\n\n"
"Text with 'single' quotes and 'tricky stuff's'.\n\n" "Text with 'single' quotes and 'tricky stuff's'.\n\n"
"Also text with \"double\" quotes which are \"less tricky\".\n\n" "Also text with \"double\" quotes which are \"less tricky\".\n\n"
@@ -270,7 +270,7 @@ def testGuiMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum):
) )
# Remove in-paragraph line breaks # Remove in-paragraph line breaks
nwGUI.docEditor.setText(( nwGUI.docEditor.setPlainText((
"### New Text\n\n" "### New Text\n\n"
"@char: Someone\n" "@char: Someone\n"
"@location: Somewhere\n\n" "@location: Somewhere\n\n"
@@ -288,7 +288,7 @@ def testGuiMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum):
"With another paragraph here.\n" "With another paragraph here.\n"
) )
nwGUI.docEditor.setText(( nwGUI.docEditor.setPlainText((
"### New Text\n\n" "### New Text\n\n"
"@char: Someone\n" "@char: Someone\n"
"@location: Somewhere\n\n" "@location: Somewhere\n\n"
@@ -314,7 +314,7 @@ def testGuiMenu_EditFormat(qtbot, monkeypatch, nwGUI, prjLipsum):
assert not nwGUI.docEditor.docAction(nwDocAction.NO_ACTION) assert not nwGUI.docEditor.docAction(nwDocAction.NO_ACTION)
# Test Invalid Formats # Test Invalid Formats
nwGUI.docEditor.setText(( nwGUI.docEditor.setPlainText((
"### New Text\n\n" "### New Text\n\n"
"@tag: Bod\n\n" "@tag: Bod\n\n"
"Text with 'single' quotes and 'tricky stuff's'.\n\n" "Text with 'single' quotes and 'tricky stuff's'.\n\n"
@@ -541,43 +541,43 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd):
# Insert Keywords # Insert Keywords
# =============== # ===============
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.TAG_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.TAG_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.TAG_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.TAG_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.POV_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.POV_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.POV_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.POV_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.FOCUS_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.FOCUS_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.FOCUS_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.FOCUS_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.CHAR_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.CHAR_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.CHAR_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.CHAR_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.PLOT_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.PLOT_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.PLOT_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.PLOT_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.TIME_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.TIME_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.TIME_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.TIME_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.WORLD_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.WORLD_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.WORLD_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.WORLD_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.OBJECT_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.OBJECT_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.OBJECT_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.OBJECT_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.ENTITY_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.ENTITY_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.ENTITY_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.ENTITY_KEY
nwGUI.docEditor.setText("Stuff") nwGUI.docEditor.setPlainText("Stuff")
nwGUI.mainMenu.mInsKWItems[nwKeyWords.CUSTOM_KEY][0].activate(QAction.Trigger) nwGUI.mainMenu.mInsKWItems[nwKeyWords.CUSTOM_KEY][0].activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.CUSTOM_KEY assert nwGUI.docEditor.getText() == "Stuff\n%s: " % nwKeyWords.CUSTOM_KEY
@@ -592,22 +592,22 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd):
# Insert Special Comments # Insert Special Comments
# ======================= # =======================
nwGUI.docEditor.setText("Stuff\n") nwGUI.docEditor.setPlainText("Stuff\n")
nwGUI.mainMenu.aInsSynopsis.activate(QAction.Trigger) nwGUI.mainMenu.aInsSynopsis.activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "Stuff\n% Synopsis: \n" assert nwGUI.docEditor.getText() == "Stuff\n% Synopsis: \n"
# Insert Break or Space # Insert Break or Space
# ===================== # =====================
nwGUI.docEditor.setText("### Stuff\n") nwGUI.docEditor.setPlainText("### Stuff\n")
nwGUI.mainMenu.aInsNewPage.activate(QAction.Trigger) nwGUI.mainMenu.aInsNewPage.activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "[NEW PAGE]\n### Stuff\n" assert nwGUI.docEditor.getText() == "[NEW PAGE]\n### Stuff\n"
nwGUI.docEditor.setText("### Stuff\n") nwGUI.docEditor.setPlainText("### Stuff\n")
nwGUI.mainMenu.aInsVSpaceS.activate(QAction.Trigger) nwGUI.mainMenu.aInsVSpaceS.activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "[VSPACE]\n### Stuff\n" assert nwGUI.docEditor.getText() == "[VSPACE]\n### Stuff\n"
nwGUI.docEditor.setText("### Stuff\n") nwGUI.docEditor.setPlainText("### Stuff\n")
nwGUI.mainMenu.aInsVSpaceM.activate(QAction.Trigger) nwGUI.mainMenu.aInsVSpaceM.activate(QAction.Trigger)
assert nwGUI.docEditor.getText() == "[VSPACE:2]\n### Stuff\n" assert nwGUI.docEditor.getText() == "[VSPACE:2]\n### Stuff\n"
@@ -637,7 +637,7 @@ def testGuiMenu_Insert(qtbot, monkeypatch, nwGUI, fncPath, projPath, mockRnd):
# Open the document from before, and add some text to it # Open the document from before, and add some text to it
nwGUI.openDocument(C.hSceneDoc) nwGUI.openDocument(C.hSceneDoc)
nwGUI.docEditor.setText("Bar") nwGUI.docEditor.setPlainText("Bar")
assert nwGUI.docEditor.getText() == "Bar" assert nwGUI.docEditor.getText() == "Bar"
# The document isn't empty, so the message box should pop # The document isn't empty, so the message box should pop