def textStyle(self, styleIndex): cursor = self.textEdit.textCursor() if styleIndex: styleDict = { 1: QTextListFormat.ListDisc, 2: QTextListFormat.ListCircle, 3: QTextListFormat.ListSquare, 4: QTextListFormat.ListDecimal, 5: QTextListFormat.ListLowerAlpha, 6: QTextListFormat.ListUpperAlpha, 7: QTextListFormat.ListLowerRoman, 8: QTextListFormat.ListUpperRoman, } style = styleDict.get(styleIndex, QTextListFormat.ListDisc) cursor.beginEditBlock() blockFmt = cursor.blockFormat() listFmt = QTextListFormat() if cursor.currentList(): listFmt = cursor.currentList().format() else: listFmt.setIndent(blockFmt.indent() + 1) blockFmt.setIndent(0) cursor.setBlockFormat(blockFmt) listFmt.setStyle(style) cursor.createList(listFmt) cursor.endEditBlock() else: bfmt = QTextBlockFormat() bfmt.setObjectIndex(-1) cursor.mergeBlockFormat(bfmt)
def pressedReturn(self): """ enter or return key is pressed :return: returns nothing """ cursor = self.textCursor() listIn = cursor.currentList() # checks if cursor is in a list if cursor.currentList() is not None: cursor.select(QtGui.QTextCursor.BlockUnderCursor) text = cursor.selectedText() text = text.strip() if text == "": listIn.removeItem(listIn.count() - 1) return False currlist = listIn.format() style = currlist.style() cursor = self.textCursor() cursor.insertText("\n") listFormat = QTextListFormat() listFormat.setStyle(style) listFormat.setIndent(currlist.indent()) cursor.createList(listFormat) return False return True
def _text_style(self, index: int): styles = { 1: QTextListFormat.ListDisc, 2: QTextListFormat.ListCircle, 3: QTextListFormat.ListSquare, 4: QTextListFormat.ListDecimal, 5: QTextListFormat.ListLowerAlpha, 6: QTextListFormat.ListUpperAlpha, 7: QTextListFormat.ListLowerRoman, 8: QTextListFormat.ListUpperRoman } cursor = self.textEdit.textCursor() try: style = styles[index] except KeyError: style = None cursor.beginEditBlock() block_fmt = cursor.blockFormat() if style is None: block_fmt.setObjectIndex(-1) heading_level = index - 9 + 1 if index >= 9 else 0 block_fmt.setHeadingLevel(heading_level) cursor.setBlockFormat(block_fmt) size = 4 - heading_level if heading_level else 0 fmt = QTextCharFormat() fmt.setFontWeight(QFont.Bold if heading_level else QFont.Normal) fmt.setProperty(QTextFormat.FontSizeAdjustment, size) cursor.select(QTextCursor.LineUnderCursor) cursor.mergeCharFormat(fmt) self.textEdit.mergeCurrentCharFormat(fmt) else: list_fmt = QTextListFormat() if cursor.currentList(): list_fmt = cursor.currentList().format() else: list_fmt.setIndent(block_fmt.indent() + 1) block_fmt.setIndent(0) cursor.setBlockFormat(block_fmt) list_fmt.setStyle(style) cursor.createList(list_fmt) cursor.endEditBlock()
def moveListForward(self): """ helper function when tab is pressed in bullet list :return: returns nothing """ cursor = self.textCursor() listIn = cursor.currentList() currlist = listIn.format() style = currlist.style() # updates style to next bullet if style == -3: style = -1 else: style -= 1 listFormat = QTextListFormat() # adds indent listFormat.setIndent(currlist.indent() + 1) listFormat.setStyle(style) listIn.setFormat(listFormat)
def moveListBackward(self): """ helper function when shift-tab is pressed in bullet list :return: returns nothing """ cursor = self.textCursor() listIn = cursor.currentList() currlist = listIn.format() indent = currlist.indent() style = currlist.style() # checks if indent can be removed then removes if indent != 1: indent -= 1 # if indent can be removed get previous bullet style if style == -1: style = -3 else: style += 1 listFormat = QTextListFormat() listFormat.setIndent(indent) listFormat.setStyle(style) listIn.setFormat(listFormat)
def loadFormats(self): self.formats = {} stylesCSS = pkg_resources.resource_string(data.__name__, 'styles.css') print("styles.css file: {}".format(stylesCSS)) styleSheet = cssutils.parseString(stylesCSS) blockFormats = ['title[level="1"]', 'title[level="2"]', 'title[level="3"]', 'para', 'tip', 'warning', 'blockquote', 'programlisting[language="java"]', 'programlisting[language="cpp"]', 'programlisting[language="xml"]', 'programlisting[language="sql"]', 'programlisting[language="python"]', 'programlisting[language="bash"]', 'screen'] for cssKey in blockFormats: cssRule = self.simpleLookup(styleSheet, cssKey) # get the selector as a tuple of class and one attribute selector # (Can be extended later, but currently sufficient) m = re.match(r'^(\S*?)(?:\[(.*)="(.*)"])?$', cssKey) selector = m.groups() blockFmt = QTextBlockFormat() blockFmt.setProperty(QTextFormat.UserProperty, selector) value = self.getIntValue(cssRule, 'margin-top') if value: blockFmt.setTopMargin(value) value = self.getIntValue(cssRule, 'margin-right') if value: blockFmt.setRightMargin(value) value = self.getIntValue(cssRule, 'margin-bottom') if value: blockFmt.setBottomMargin(value) value = self.getIntValue(cssRule, 'margin-left') if value: blockFmt.setLeftMargin(value) value = self.getColorValue(cssRule, 'background-color') if value: blockFmt.setBackground(value) charFmt = QTextCharFormat() self.setCharFormatAttributes(cssRule, charFmt) fmt = Format(blockFmt, charFmt) value = self.getStringValue(cssRule, 'white-space') if value and value == 'pre': fmt.isPre = True self.formats[selector] = fmt ### List formats listFormats = ['itemizedlist[level="1"]', 'itemizedlist[level="2"]', 'itemizedlist[level="3"]', 'itemizedlist[level="4"]', 'orderedlist[level="1"]', 'orderedlist[level="2"]' , 'orderedlist[level="3"]', 'orderedlist[level="4"]'] for cssKey in listFormats: cssRule = self.simpleLookup(styleSheet, cssKey) indent = 0 m = re.match(r'^(\S*?)(?:\[(.*)="(.*)"])?$', cssKey) selector = m.groups() if selector[1] == 'level': indent = int(selector[2]) listFmt = QTextListFormat() listFmt.setProperty(QTextFormat.UserProperty, selector) listFmt.setIndent(indent) value = self.getStringValue(cssRule, 'list-style-type') if value: if value == 'disc': listFmt.setStyle(QTextListFormat.ListDisc) elif value == 'circle': listFmt.setStyle(QTextListFormat.ListCircle) elif value == 'square': listFmt.setStyle(QTextListFormat.ListSquare) elif value == 'decimal': listFmt.setStyle(QTextListFormat.ListDecimal) self.formats[selector] = Format(None, None, listFmt) ### Inline formats # Base format (?????) pcharFmt = QTextCharFormat() pcharFmt.setFontPointSize(10) pcharFmt.setFontFamily("Sans") inlineFormats = ['emphasis[role="highlight"]', 'emphasis', 'code', 'link', 'olink'] for cssKey in inlineFormats: cssRule = self.simpleLookup(styleSheet, cssKey) m = re.match(r'^(\S*?)(?:\[(.*)="(.*)"])?$', cssKey) selector = m.groups() charFmt = QTextCharFormat(pcharFmt) charFmt.setProperty(QTextFormat.UserProperty, selector) # TODO: better approach? if cssKey in ['link', 'olink']: charFmt.setAnchor(True) self.setCharFormatAttributes(cssRule, charFmt) self.formats[selector] = Format(None, charFmt) ### special formats charFmt = QTextCharFormat() cssRule = self.simpleLookup(styleSheet, 'searchMarker') self.setCharFormatAttributes(cssRule, charFmt) self.formats[('searchMarker', None, None)] = Format(None, charFmt)