Add page break markers to the QTextDocument format class
This commit is contained in:
@@ -41,7 +41,7 @@ from novelwriter.formats.tokenizer import HEADINGS, Tokenizer
|
||||
from novelwriter.types import (
|
||||
QtAlignAbsolute, QtAlignCenter, QtAlignJustify, QtAlignLeft, QtAlignRight,
|
||||
QtKeepAnchor, QtMoveAnchor, QtPageBreakAfter, QtPageBreakBefore,
|
||||
QtTransparent, QtVAlignNormal, QtVAlignSub, QtVAlignSuper
|
||||
QtPropLineHeight, QtTransparent, QtVAlignNormal, QtVAlignSub, QtVAlignSuper
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
@@ -75,6 +75,7 @@ class ToQTextDocument(Tokenizer):
|
||||
self._init = False
|
||||
self._bold = QFont.Weight.Bold
|
||||
self._normal = QFont.Weight.Normal
|
||||
self._newPage = False
|
||||
|
||||
self._pageSize = QPageSize(QPageSize.PageSizeId.A4)
|
||||
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)
|
||||
return
|
||||
|
||||
def setShowNewPage(self, state: bool) -> None:
|
||||
"""Add markers for page breaks."""
|
||||
self._newPage = state
|
||||
return
|
||||
|
||||
##
|
||||
# Class Methods
|
||||
##
|
||||
@@ -149,8 +155,6 @@ class ToQTextDocument(Tokenizer):
|
||||
# Text Formats
|
||||
# ============
|
||||
|
||||
QtPropLineHeight = QTextBlockFormat.LineHeightTypes.ProportionalHeight
|
||||
|
||||
self._blockFmt = QTextBlockFormat()
|
||||
self._blockFmt.setTopMargin(self._mText[0])
|
||||
self._blockFmt.setBottomMargin(self._mText[1])
|
||||
@@ -184,32 +188,32 @@ class ToQTextDocument(Tokenizer):
|
||||
bFmt.setTopMargin(self._mSep[0])
|
||||
bFmt.setBottomMargin(self._mSep[1])
|
||||
|
||||
if tStyle is not None:
|
||||
if tStyle & BlockFmt.LEFT:
|
||||
bFmt.setAlignment(QtAlignLeft)
|
||||
elif tStyle & BlockFmt.RIGHT:
|
||||
bFmt.setAlignment(QtAlignRight)
|
||||
elif tStyle & BlockFmt.CENTRE:
|
||||
bFmt.setAlignment(QtAlignCenter)
|
||||
elif tStyle & BlockFmt.JUSTIFY:
|
||||
bFmt.setAlignment(QtAlignJustify)
|
||||
if tStyle & BlockFmt.LEFT:
|
||||
bFmt.setAlignment(QtAlignLeft)
|
||||
elif tStyle & BlockFmt.RIGHT:
|
||||
bFmt.setAlignment(QtAlignRight)
|
||||
elif tStyle & BlockFmt.CENTRE:
|
||||
bFmt.setAlignment(QtAlignCenter)
|
||||
elif tStyle & BlockFmt.JUSTIFY:
|
||||
bFmt.setAlignment(QtAlignJustify)
|
||||
|
||||
if tStyle & BlockFmt.PBB:
|
||||
bFmt.setPageBreakPolicy(QtPageBreakBefore)
|
||||
if tStyle & BlockFmt.PBA:
|
||||
bFmt.setPageBreakPolicy(QtPageBreakAfter)
|
||||
if tStyle & BlockFmt.PBB:
|
||||
self._insertNewPageMarker(cursor)
|
||||
bFmt.setPageBreakPolicy(QtPageBreakBefore)
|
||||
if tStyle & BlockFmt.PBA:
|
||||
bFmt.setPageBreakPolicy(QtPageBreakAfter)
|
||||
|
||||
if tStyle & BlockFmt.Z_BTM:
|
||||
bFmt.setBottomMargin(0.0)
|
||||
if tStyle & BlockFmt.Z_TOP:
|
||||
bFmt.setTopMargin(0.0)
|
||||
if tStyle & BlockFmt.Z_BTM:
|
||||
bFmt.setBottomMargin(0.0)
|
||||
if tStyle & BlockFmt.Z_TOP:
|
||||
bFmt.setTopMargin(0.0)
|
||||
|
||||
if tStyle & BlockFmt.IND_L:
|
||||
bFmt.setLeftMargin(self._mIndent)
|
||||
if tStyle & BlockFmt.IND_R:
|
||||
bFmt.setRightMargin(self._mIndent)
|
||||
if tStyle & BlockFmt.IND_T:
|
||||
bFmt.setTextIndent(self._tIndent)
|
||||
if tStyle & BlockFmt.IND_L:
|
||||
bFmt.setLeftMargin(self._mIndent)
|
||||
if tStyle & BlockFmt.IND_R:
|
||||
bFmt.setRightMargin(self._mIndent)
|
||||
if tStyle & BlockFmt.IND_T:
|
||||
bFmt.setTextIndent(self._tIndent)
|
||||
|
||||
if tType in (BlockTyp.TEXT, BlockTyp.COMMENT, BlockTyp.KEYWORD):
|
||||
newBlock(cursor, bFmt)
|
||||
@@ -228,6 +232,9 @@ class ToQTextDocument(Tokenizer):
|
||||
newBlock(cursor, bFmt)
|
||||
cursor.insertText(nwUnicode.U_NBSP, self._charFmt)
|
||||
|
||||
if tStyle & BlockFmt.PBA:
|
||||
self._insertNewPageMarker(cursor)
|
||||
|
||||
self._document.blockSignals(False)
|
||||
|
||||
return
|
||||
@@ -386,6 +393,29 @@ class ToQTextDocument(Tokenizer):
|
||||
|
||||
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:
|
||||
"""Generate a heading style set."""
|
||||
mTop, mBottom = self._mHead.get(hType, (0.0, 0.0))
|
||||
|
||||
@@ -24,7 +24,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
from __future__ import annotations
|
||||
|
||||
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
|
||||
|
||||
# Qt Alignment Flags
|
||||
@@ -48,11 +51,13 @@ QtVAlignNormal = QTextCharFormat.VerticalAlignment.AlignNormal
|
||||
QtVAlignSub = QTextCharFormat.VerticalAlignment.AlignSubScript
|
||||
QtVAlignSuper = QTextCharFormat.VerticalAlignment.AlignSuperScript
|
||||
|
||||
# Qt Page Break
|
||||
# Qt Text Formats
|
||||
|
||||
QtPageBreakBefore = QTextFormat.PageBreakFlag.PageBreak_AlwaysBefore
|
||||
QtPageBreakAfter = QTextFormat.PageBreakFlag.PageBreak_AlwaysAfter
|
||||
|
||||
QtPropLineHeight = QTextBlockFormat.LineHeightTypes.ProportionalHeight
|
||||
|
||||
# Qt Painter Types
|
||||
|
||||
QtTransparent = QColor(0, 0, 0, 0)
|
||||
|
||||
Reference in New Issue
Block a user