Пример #1
0
    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)
Пример #2
0
 def bulletList(self):
     """
     bullet list created on cursor location
     :return: returns nothing
     """
     logging.info("bullet list: created")
     style = QTextListFormat.ListDisc
     cursor = self.textCursor()
     cursor.select(QtGui.QTextCursor.LineUnderCursor)
     listFormat = QTextListFormat()
     listFormat.setStyle(style)
     cursor.createList(listFormat)
Пример #3
0
 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
Пример #4
0
    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)
Пример #5
0
    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()
Пример #6
0
 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)
Пример #7
0
 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)
Пример #8
0
 def test(self):
     # 选中文本字符
     cursor = self.textEdit.textCursor()  # 每次点击必须获取新的textCursor
     # 打印当前光标的位置
     print(cursor.position())
     cursor.setPosition(3, QTextCursor.KeepAnchor)
     self.textEdit.setTextCursor(cursor)
     self.textEdit.setFocus()
     print(cursor.selectedText())  # 打印选中的文本
     return None
     qc = QColor(255, 0, 0)
     self.textEdit.setTextBackgroundColor(qc)
     self.textEdit.setFocus()
     
     cursor = self.textEdit.textCursor()
     # 移动光标
     cursor.movePosition(QTextCursor.Left, n=1)
     # 移动完成之后,在编辑器上显示出来
     self.textEdit.setTextCursor(cursor)
     # 获取光标的焦点(未设置可以移动,但无法显示光标)
     self.textEdit.setFocus()
     
     cursor.insertText('hello')
     # 选中文本
     cursor.select(QTextCursor.WordUnderCursor)
     print(cursor.selectionStart(), cursor.selectionEnd())
     print(cursor.selectedText())
     # 插入一个表格(行,列)
     ttf = QTextTableFormat()
     ttf.setCellSpacing(10.0)
     ttf.setCellPadding(10.0)
     cursor.insertTable(2, 3, ttf)
     # 插入一个列表
     tlf = QTextListFormat()
     tlf.setStyle(QTextListFormat.ListDisc)
     cursor.insertList(tlf)
     cursor.insertList(QTextListFormat.ListDisc)
     # 利用文本光标插入纯文本或者HTML
     cursor.insertText('<h1>123</h1>')
     cursor.insertHtml('<h1>123</h1>')
     cursor.insertImage('./start.jpg')  # 暂时无法显示
     # 设置字体
     qfont = QFont()
     qfont.setFamily('微软雅黑')
     self.textEdit.setCurrentFont(qfont)  # 从当前光标处开始为微软雅黑
     # 设置文档标题
     self.textEdit.setDocumentTitle('第一次')
     self.textEdit.setFontFamily('微软雅黑')
     # 在文本末尾追加内容(文本类型自动识别)
     self.textEdit.append('123')
     self.textEdit.append('<h1>123</h1>')
     # 在光标处插入文本(需要手点确定光标位置)(不会覆盖之前的)
     self.textEdit.insertPlainText('123')
     self.textEdit.insertHtml('<h1>123</h1>')
     # 插入纯文本
     self.textEdit.setPlainText('<h1>123</h1>')
     # 插入HTML
     self.textEdit.setHtml('<h1>123</h1>')
     # 自动识别文本类型
     self.textEdit.setText('<h1>123</h1>')
     self.textEdit.setText('123')
Пример #9
0
 def getFormat(self, fmtId):
     result = self.formats.get(fmtId)
     if result is None:
         result = Format(QTextBlockFormat(), QTextCharFormat(), QTextListFormat())
     return result
Пример #10
0
    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)