Add page break markers to the QTextDocument format class

This commit is contained in:
Veronica Berglyd Olsen
2024-11-07 17:52:01 +01:00
parent 2018023dc7
commit 0c659422b7
2 changed files with 63 additions and 28 deletions
+34 -4
View File
@@ -41,7 +41,7 @@ from novelwriter.formats.tokenizer import HEADINGS, Tokenizer
from novelwriter.types import ( from novelwriter.types import (
QtAlignAbsolute, QtAlignCenter, QtAlignJustify, QtAlignLeft, QtAlignRight, QtAlignAbsolute, QtAlignCenter, QtAlignJustify, QtAlignLeft, QtAlignRight,
QtKeepAnchor, QtMoveAnchor, QtPageBreakAfter, QtPageBreakBefore, QtKeepAnchor, QtMoveAnchor, QtPageBreakAfter, QtPageBreakBefore,
QtTransparent, QtVAlignNormal, QtVAlignSub, QtVAlignSuper QtPropLineHeight, QtTransparent, QtVAlignNormal, QtVAlignSub, QtVAlignSuper
) )
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@@ -75,6 +75,7 @@ class ToQTextDocument(Tokenizer):
self._init = False self._init = False
self._bold = QFont.Weight.Bold self._bold = QFont.Weight.Bold
self._normal = QFont.Weight.Normal self._normal = QFont.Weight.Normal
self._newPage = False
self._pageSize = QPageSize(QPageSize.PageSizeId.A4) self._pageSize = QPageSize(QPageSize.PageSizeId.A4)
self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0) self._pageMargins = QMarginsF(20.0, 20.0, 20.0, 20.0)
@@ -102,6 +103,11 @@ class ToQTextDocument(Tokenizer):
self._pageMargins = QMarginsF(left, top, right, bottom) self._pageMargins = QMarginsF(left, top, right, bottom)
return return
def setShowNewPage(self, state: bool) -> None:
"""Add markers for page breaks."""
self._newPage = state
return
## ##
# Class Methods # Class Methods
## ##
@@ -149,8 +155,6 @@ class ToQTextDocument(Tokenizer):
# Text Formats # Text Formats
# ============ # ============
QtPropLineHeight = QTextBlockFormat.LineHeightTypes.ProportionalHeight
self._blockFmt = QTextBlockFormat() self._blockFmt = QTextBlockFormat()
self._blockFmt.setTopMargin(self._mText[0]) self._blockFmt.setTopMargin(self._mText[0])
self._blockFmt.setBottomMargin(self._mText[1]) self._blockFmt.setBottomMargin(self._mText[1])
@@ -184,7 +188,6 @@ class ToQTextDocument(Tokenizer):
bFmt.setTopMargin(self._mSep[0]) bFmt.setTopMargin(self._mSep[0])
bFmt.setBottomMargin(self._mSep[1]) bFmt.setBottomMargin(self._mSep[1])
if tStyle is not None:
if tStyle & BlockFmt.LEFT: if tStyle & BlockFmt.LEFT:
bFmt.setAlignment(QtAlignLeft) bFmt.setAlignment(QtAlignLeft)
elif tStyle & BlockFmt.RIGHT: elif tStyle & BlockFmt.RIGHT:
@@ -195,6 +198,7 @@ class ToQTextDocument(Tokenizer):
bFmt.setAlignment(QtAlignJustify) bFmt.setAlignment(QtAlignJustify)
if tStyle & BlockFmt.PBB: if tStyle & BlockFmt.PBB:
self._insertNewPageMarker(cursor)
bFmt.setPageBreakPolicy(QtPageBreakBefore) bFmt.setPageBreakPolicy(QtPageBreakBefore)
if tStyle & BlockFmt.PBA: if tStyle & BlockFmt.PBA:
bFmt.setPageBreakPolicy(QtPageBreakAfter) bFmt.setPageBreakPolicy(QtPageBreakAfter)
@@ -228,6 +232,9 @@ class ToQTextDocument(Tokenizer):
newBlock(cursor, bFmt) newBlock(cursor, bFmt)
cursor.insertText(nwUnicode.U_NBSP, self._charFmt) cursor.insertText(nwUnicode.U_NBSP, self._charFmt)
if tStyle & BlockFmt.PBA:
self._insertNewPageMarker(cursor)
self._document.blockSignals(False) self._document.blockSignals(False)
return return
@@ -386,6 +393,29 @@ class ToQTextDocument(Tokenizer):
return return
def _insertNewPageMarker(self, cursor: QTextCursor) -> None:
"""Insert a new page marker."""
if self._newPage:
cursor.insertHtml("<hr width='100%'>")
hFmt = cursor.blockFormat()
hFmt.setBottomMargin(0.0)
hFmt.setLineHeight(75.0, QtPropLineHeight)
cursor.setBlockFormat(hFmt)
bFmt = QTextBlockFormat(self._blockFmt)
bFmt.setAlignment(QtAlignCenter)
bFmt.setTopMargin(0.0)
bFmt.setLineHeight(75.0, QtPropLineHeight)
cFmt = QTextCharFormat(self._charFmt)
cFmt.setFontPointSize(0.75*self._textFont.pointSizeF())
cFmt.setForeground(self._theme.comment)
newBlock(cursor, bFmt)
cursor.insertText(self._project.localLookup("New Page"), cFmt)
return
def _genHeadStyle(self, hType: BlockTyp, hKey: str, rFmt: QTextBlockFormat) -> T_TextStyle: def _genHeadStyle(self, hType: BlockTyp, hKey: str, rFmt: QTextBlockFormat) -> T_TextStyle:
"""Generate a heading style set.""" """Generate a heading style set."""
mTop, mBottom = self._mHead.get(hType, (0.0, 0.0)) mTop, mBottom = self._mHead.get(hType, (0.0, 0.0))
+7 -2
View File
@@ -24,7 +24,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations from __future__ import annotations
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from PyQt5.QtGui import QColor, QFont, QPainter, QTextCharFormat, QTextCursor, QTextFormat from PyQt5.QtGui import (
QColor, QFont, QPainter, QTextBlockFormat, QTextCharFormat, QTextCursor,
QTextFormat
)
from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QSizePolicy, QStyle from PyQt5.QtWidgets import QDialog, QDialogButtonBox, QSizePolicy, QStyle
# Qt Alignment Flags # Qt Alignment Flags
@@ -48,11 +51,13 @@ QtVAlignNormal = QTextCharFormat.VerticalAlignment.AlignNormal
QtVAlignSub = QTextCharFormat.VerticalAlignment.AlignSubScript QtVAlignSub = QTextCharFormat.VerticalAlignment.AlignSubScript
QtVAlignSuper = QTextCharFormat.VerticalAlignment.AlignSuperScript QtVAlignSuper = QTextCharFormat.VerticalAlignment.AlignSuperScript
# Qt Page Break # Qt Text Formats
QtPageBreakBefore = QTextFormat.PageBreakFlag.PageBreak_AlwaysBefore QtPageBreakBefore = QTextFormat.PageBreakFlag.PageBreak_AlwaysBefore
QtPageBreakAfter = QTextFormat.PageBreakFlag.PageBreak_AlwaysAfter QtPageBreakAfter = QTextFormat.PageBreakFlag.PageBreak_AlwaysAfter
QtPropLineHeight = QTextBlockFormat.LineHeightTypes.ProportionalHeight
# Qt Painter Types # Qt Painter Types
QtTransparent = QColor(0, 0, 0, 0) QtTransparent = QColor(0, 0, 0, 0)