def do_format_block(self): name = self.sender().block_name with self.editing_cursor() as c: bf = QTextBlockFormat() cf = QTextCharFormat() bcf = c.blockCharFormat() lvl = self.level_for_block_type(name) wt = QFont.Weight.Bold if lvl else None adjust = (0, 3, 2, 1, 0, -1, -1)[lvl] pos = None if not c.hasSelection(): pos = c.position() c.movePosition(QTextCursor.MoveOperation.StartOfBlock, QTextCursor.MoveMode.MoveAnchor) c.movePosition(QTextCursor.MoveOperation.EndOfBlock, QTextCursor.MoveMode.KeepAnchor) # margin values are taken from qtexthtmlparser.cpp hmargin = 0 if name == 'blockquote': hmargin = 40 tmargin = bmargin = 12 if name == 'h1': tmargin, bmargin = 18, 12 elif name == 'h2': tmargin, bmargin = 16, 12 elif name == 'h3': tmargin, bmargin = 14, 12 elif name == 'h4': tmargin, bmargin = 12, 12 elif name == 'h5': tmargin, bmargin = 12, 4 bf.setLeftMargin(hmargin), bf.setRightMargin(hmargin) bf.setTopMargin(tmargin), bf.setBottomMargin(bmargin) bf.setHeadingLevel(lvl) if adjust: bcf.setProperty(QTextFormat.Property.FontSizeAdjustment, adjust) cf.setProperty(QTextFormat.Property.FontSizeAdjustment, adjust) if wt: bcf.setProperty(QTextFormat.Property.FontWeight, wt) cf.setProperty(QTextFormat.Property.FontWeight, wt) c.setBlockCharFormat(bcf) c.mergeCharFormat(cf) c.mergeBlockFormat(bf) if pos is not None: c.setPosition(pos)
def check_spelling(text, tlen, fmt, locale, sfmt, store_locale): split_ans = [] ppos = 0 r, a = dictionaries.recognized, split_ans.append for start, length in split_into_words_and_positions(text, lang=locale.langcode): if start > ppos: a((start - ppos, fmt)) ppos = start + length recognized = r(text[start:ppos], locale) if recognized: a((length, fmt)) else: if store_locale: s = QTextCharFormat(sfmt) s.setProperty(SPELL_LOCALE_PROPERTY, locale) a((length, s)) else: a((length, sfmt)) if ppos < tlen: a((tlen - ppos, fmt)) return split_ans
def addNode(self, node): if type(node) == Paragraph: self.paraFormat = self.formatManager.getFormat(node.style) # NOTE: "The block char format is the format used when inserting # text at the beginning of an empty block." # See also below. self.cursor.insertBlock(self.paraFormat.getBlockFormat(), self.paraFormat.getCharFormat()) # self.cursor.insertFragment(QTextDocumentFragment.fromPlainText('')) if self.listLevel > 0: # TODO: use list style from list node - requires a stack, though ... listStyle = ('itemizedlist', 'level', str(self.listLevel)) newList = self.cursor.createList(self.formatManager.getFormat(listStyle).getListFormat()) for n in node.children: self.addNode(n) elif type(node) == List: self.listLevel += 1 for n in node.children: self.addNode(n) self.listLevel -= 1 elif type(node) is ImageFragment: imageObject = ImageObject() imagePath = os.path.join(self.contentPath, node.image) imageObject.setName(imagePath) imageObjectFormat = QTextCharFormat() imageObjectFormat.setObjectType(QTextFormat.UserObject + 1) imageObjectFormat.setProperty(QTextFormat.UserProperty + 1, imageObject) self.cursor.insertText('\ufffc', imageObjectFormat); elif type(node) is MathFragment: mathFormula = MathFormulaObject() mathFormula.setFormula(node.text) mathFormula.image = node.image # renderFormula() mathObjectFormat = QTextCharFormat() mathObjectFormat.setObjectType(QTextFormat.UserObject + 1) mathObjectFormat.setVerticalAlignment(QTextCharFormat.AlignMiddle) mathObjectFormat.setProperty(QTextFormat.UserProperty + 1, mathFormula) self.cursor.insertText('\ufffc', mathObjectFormat); elif type(node) is TextFragment: text = node.text.replace('\n', '\u2028') if node.href is not None: fmt = self.formatManager.getFormat(('link', None, None)) # TODO! charFmt = fmt.getCharFormat() charFmt.setAnchorHref(node.href) self.cursor.insertText(text, charFmt) else: # "The block char format is the format used when inserting text at the beginning of an empty block. # Hence, the block char format is only useful for the first fragment - # once a fragment is inserted with a different style, and afterwards # another fragment is inserted with no specific style, we need to reset # the char format to the block's char format explicitly! if node.style is not None: fmt = self.formatManager.getFormat(node.style) else: fmt = self.paraFormat self.cursor.insertText(text, fmt.getCharFormat())
def syntax_text_char_format(*args): ans = QTextCharFormat(*args) ans.setProperty(SYNTAX_PROPERTY, True) return ans
def spell_property(sfmt, locale): s = QTextCharFormat(sfmt) s.setProperty(SPELL_LOCALE_PROPERTY, locale) return s
def spell_property(sfmt, locale): s = QTextCharFormat(sfmt) s.setProperty(SPELL_LOCALE_PROPERTY, locale) s.mem = locale # ensure locale is not garbage collected return s