Update qdocument class to work with merged code from main

This commit is contained in:
Veronica Berglyd Olsen
2024-05-23 19:02:27 +02:00
parent 818f145b62
commit 6d84942746
2 changed files with 15 additions and 29 deletions
+13 -28
View File
@@ -42,6 +42,7 @@ from novelwriter.types import (
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
T_TextStyle = tuple[QTextBlockFormat, QTextCharFormat] T_TextStyle = tuple[QTextBlockFormat, QTextCharFormat]
T_TextParFormat = tuple[str, QTextCharFormat]
class ToQTextDocument(Tokenizer): class ToQTextDocument(Tokenizer):
@@ -61,21 +62,16 @@ class ToQTextDocument(Tokenizer):
return return
def initDocument(self, font: QFont | None = None) -> None: def initDocument(self) -> None:
"""Initialise all computed values of the document.""" """Initialise all computed values of the document."""
if not font:
font = QFont()
font.setFamily(self._textFont)
font.setPointSize(self._textSize)
self._document.setUndoRedoEnabled(False) self._document.setUndoRedoEnabled(False)
self._document.blockSignals(True) self._document.blockSignals(True)
self._document.clear() self._document.clear()
self._document.setDefaultFont(font) self._document.setDefaultFont(self._textFont)
qMetric = QFontMetrics(font) qMetric = QFontMetrics(self._textFont)
mScale = qMetric.height() mScale = qMetric.height()
fPt = font.pointSizeF() fPt = self._textFont.pointSizeF()
self._mHead = { self._mHead = {
self.T_TITLE: (mScale * self._marginTitle[0], mScale * self._marginTitle[1]), self.T_TITLE: (mScale * self._marginTitle[0], mScale * self._marginTitle[1]),
@@ -139,10 +135,6 @@ class ToQTextDocument(Tokenizer):
else: else:
cursor.setBlockFormat(bFmt) cursor.setBlockFormat(bFmt)
pText = []
pFmt = None
lineSep = "\n" if self._keepBreaks else " "
for tType, _, tText, tFormat, tStyle in self._tokens: for tType, _, tText, tFormat, tStyle in self._tokens:
# Styles # Styles
@@ -172,13 +164,10 @@ class ToQTextDocument(Tokenizer):
if tStyle & self.A_IND_R: if tStyle & self.A_IND_R:
bFmt.setRightMargin(self._blockIndent) bFmt.setRightMargin(self._blockIndent)
if tType == self.T_EMPTY: if tType == self.T_TEXT:
if pText and pFmt is not None: newBlock(bFmt)
tTemp = (lineSep.join(pText)).rstrip(" ") tTemp, cFmt = self._formatText(tText, tFormat, self._defaultChar)
newBlock(pFmt) cursor.insertText(tTemp, cFmt)
cursor.insertText(tTemp, self._defaultChar)
pText = []
pFmt = None
elif tType in self.L_HEADINGS: elif tType in self.L_HEADINGS:
bFmt, cFmt = self._genHeadStyle(tType, bFmt) bFmt, cFmt = self._genHeadStyle(tType, bFmt)
@@ -187,16 +176,11 @@ class ToQTextDocument(Tokenizer):
elif tType == self.T_SEP: elif tType == self.T_SEP:
newBlock(bFmt) newBlock(bFmt)
cursor.insertText(tTemp, self._defaultChar) cursor.insertText(tText, self._defaultChar)
elif tType == self.T_SKIP: elif tType == self.T_SKIP:
newBlock(bFmt) newBlock(bFmt)
elif tType == self.T_TEXT:
if pFmt is None:
pFmt = bFmt
pText.append(self._formatText(tText, tFormat).rstrip())
elif tType == self.T_SYNOPSIS and self._doSynopsis: elif tType == self.T_SYNOPSIS and self._doSynopsis:
pass pass
@@ -238,9 +222,10 @@ class ToQTextDocument(Tokenizer):
# Internal Functions # Internal Functions
## ##
def _formatText(self, text: str, tFmt: T_Formats) -> str: def _formatText(self, text: str, tFmt: T_Formats, dFmt: QTextCharFormat) -> T_TextParFormat:
"""Apply formatting tags to text.""" """Apply formatting tags to text."""
temp = text temp = text
fmt = QTextCharFormat(dFmt)
# for pos, fmt, data in reversed(tFmt): # for pos, fmt, data in reversed(tFmt):
# md = "" # md = ""
# if fmt == self.FMT_FNOTE: # if fmt == self.FMT_FNOTE:
@@ -253,7 +238,7 @@ class ToQTextDocument(Tokenizer):
# else: # else:
# md = tags.get(fmt, "") # md = tags.get(fmt, "")
# temp = f"{temp[:pos]}{md}{temp[pos:]}" # temp = f"{temp[:pos]}{md}{temp[pos:]}"
return temp return temp, fmt
def _formatKeywords(self, text: str, style: int) -> str: def _formatKeywords(self, text: str, style: int) -> str:
"""Apply Markdown formatting to keywords.""" """Apply Markdown formatting to keywords."""
+2 -1
View File
@@ -203,7 +203,8 @@ class GuiDocViewer(QTextBrowser):
sPos = self.verticalScrollBar().value() sPos = self.verticalScrollBar().value()
qDoc = ToQTextDocument(SHARED.project) qDoc = ToQTextDocument(SHARED.project)
qDoc.initDocument(CONFIG.textFont) qDoc.setFont(CONFIG.textFont)
qDoc.initDocument()
qDoc.setKeywords(True) qDoc.setKeywords(True)
qDoc.setComments(CONFIG.viewComments) qDoc.setComments(CONFIG.viewComments)
qDoc.setSynopsis(CONFIG.viewSynopsis) qDoc.setSynopsis(CONFIG.viewSynopsis)