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 hline(self): """ Insert horizontal line """ # Tag HR is not correctly displayed in QTextview cur_char_fmt = self._text_edit_cursor.charFormat() cur_block_fmt = self._text_edit_cursor.blockFormat() if bool(self._text_edit_cursor.currentTable()): self._text_edit_cursor.insertBlock(cur_block_fmt, cur_char_fmt) block_fmt = QTextBlockFormat() block_fmt.setTopMargin(5) block_fmt.setBottomMargin(5) block_fmt.setAlignment(Qt.AlignLeft) block_fmt.setBackground(QBrush(QColor("#C1C1C1"))) char_format = QTextCharFormat() char_format.setFont(QFont("Arial", 1)) self._text_edit_cursor.insertBlock(block_fmt, char_format) self._text_edit_cursor.insertText(" ") self._text_edit_cursor.insertBlock(cur_block_fmt, cur_char_fmt) self.change(self._text_edit_cursor)