def initializeFormats(self): Config = self.Config Config["fontfamily"] = "monospace" pal = QApplication.instance().palette() for name, color, bold, italic in ( ("normal", None, False, False), ("keyword", pal.color(pal.Link).name(), True, False), ("builtin", pal.color(pal.Link).name(), False, False), ("comment", "#007F00", False, True), ("string", "#808000", False, False), ("number", "#924900", False, False), ("lparen", None, True, True), ("rparen", None, True, True)): Config["%sfontcolor" % name] = color Config["%sfontbold" % name] = bold Config["%sfontitalic" % name] = italic baseFormat = QTextCharFormat() baseFormat.setFontFamily(Config["fontfamily"]) Config["fontsize"] = gprefs['gpm_template_editor_font_size'] baseFormat.setFontPointSize(Config["fontsize"]) for name in ("normal", "keyword", "builtin", "comment", "string", "number", "lparen", "rparen"): format = QTextCharFormat(baseFormat) col = Config["%sfontcolor" % name] if col: format.setForeground(QColor(col)) if Config["%sfontbold" % name]: format.setFontWeight(QFont.Bold) format.setFontItalic(Config["%sfontitalic" % name]) self.Formats[name] = format
def initializeFormats(self): Config = self.Config Config["fontfamily"] = "monospace" for name, color, bold, italic in ( ("normal", "#000000", False, False), ("keyword", "#000080", True, False), ("builtin", "#0000A0", False, False), ("comment", "#007F00", False, True), ("string", "#808000", False, False), ("number", "#924900", False, False), ("lparen", "#000000", True, True), ("rparen", "#000000", True, True)): Config["%sfontcolor" % name] = color Config["%sfontbold" % name] = bold Config["%sfontitalic" % name] = italic baseFormat = QTextCharFormat() baseFormat.setFontFamily(Config["fontfamily"]) Config["fontsize"] = gprefs['gpm_template_editor_font_size'] baseFormat.setFontPointSize(Config["fontsize"]) for name in ("normal", "keyword", "builtin", "comment", "string", "number", "lparen", "rparen"): format = QTextCharFormat(baseFormat) format.setForeground(QColor(Config["%sfontcolor" % name])) if Config["%sfontbold" % name]: format.setFontWeight(QFont.Bold) format.setFontItalic(Config["%sfontitalic" % name]) self.Formats[name] = format
def initializeFormats(cls): if cls.Formats: return baseFormat = QTextCharFormat() baseFormat.setFontFamily('monospace') baseFormat.setFontPointSize(11) for name, color, bold, italic in (("normal", "#000000", False, False), ("keyword", "#000080", True, False), ("builtin", "#0000A0", False, False), ("constant", "#0000C0", False, False), ("decorator", "#0000E0", False, False), ("comment", "#007F00", False, True), ("string", "#808000", False, False), ("number", "#924900", False, False), ("error", "#FF0000", False, False), ("pyqt", "#50621A", False, False)): format = QTextCharFormat(baseFormat) format.setForeground(QColor(color)) if bold: format.setFontWeight(QFont.Bold) format.setFontItalic(italic) cls.Formats[name] = format
def initializeFormats(cls): baseFormat = QTextCharFormat() baseFormat.setFontFamily('monospace') p = QApplication.instance().palette() for name, color, bold, italic in (("normal", None, False, False), ("keyword", p.color(p.Link).name(), True, False), ("builtin", p.color(p.Link).name(), False, False), ("constant", p.color(p.Link).name(), False, False), ("decorator", "#0000E0", False, False), ("comment", "#007F00", False, True), ("string", "#808000", False, False), ("number", "#924900", False, False), ("error", "#FF0000", False, False), ("pyqt", "#50621A", False, False)): fmt = QTextCharFormat(baseFormat) if color is not None: fmt.setForeground(QColor(color)) if bold: fmt.setFontWeight(QFont.Bold) if italic: fmt.setFontItalic(italic) cls.Formats[name] = fmt
def format_style(color, style=''): """Return a QTextCharFormat with the given attributes.""" _color = QColor() _color.setNamedColor(color) _format = QTextCharFormat() _format.setForeground(_color) if 'bold' in style: _format.setFontWeight(QFont.Bold) if 'italic' in style: _format.setFontItalic(True) return _format
def __init__( self, pattern, color, weight=QFont.Normal, style=Qt.SolidPattern, blocknum=0): if isinstance(pattern, string_types): self.pattern = re.compile(pattern) else: self.pattern=pattern charfmt = QTextCharFormat() brush = QBrush(color, style) charfmt.setForeground(brush) charfmt.setFontWeight(weight) self.highlight = charfmt self.blocknum=blocknum
def parse_text_formatting(text): pos = 0 tokens = [] for m in re.finditer(r'</?([a-zA-Z1-6]+)/?>', text): q = text[pos:m.start()] if q: tokens.append((False, q)) tokens.append((True, (m.group(1).lower(), '/' in m.group()[:2]))) pos = m.end() if tokens: if text[pos:]: tokens.append((False, text[pos:])) else: tokens = [(False, text)] ranges, open_ranges, text = [], [], [] offset = 0 for is_tag, tok in tokens: if is_tag: tag, closing = tok if closing: if open_ranges: r = open_ranges.pop() r[-1] = offset - r[-2] if r[-1] > 0: ranges.append(r) else: if tag in {'b', 'strong', 'i', 'em'}: open_ranges.append([tag, offset, -1]) else: offset += len(tok.replace('&', '&')) text.append(tok) text = ''.join(text) formats = [] for tag, start, length in chain(ranges, open_ranges): fmt = QTextCharFormat() if tag in {'b', 'strong'}: fmt.setFontWeight(QFont.Bold) elif tag in {'i', 'em'}: fmt.setFontItalic(True) else: continue if length == -1: length = len(text) - start if length > 0: r = QTextLayout.FormatRange() r.format = fmt r.start, r.length = start, length formats.append(r) return text, formats
def parse_text_formatting(text): pos = 0 tokens = [] for m in re.finditer(r'</?([a-zA-Z1-6]+)/?>', text): q = text[pos:m.start()] if q: tokens.append((False, q)) tokens.append((True, (m.group(1).lower(), '/' in m.group()[:2]))) pos = m.end() if tokens: if text[pos:]: tokens.append((False, text[pos:])) else: tokens = [(False, text)] ranges, open_ranges, text = [], [], [] offset = 0 for is_tag, tok in tokens: if is_tag: tag, closing = tok if closing: if open_ranges: r = open_ranges.pop() r[-1] = offset - r[-2] if r[-1] > 0: ranges.append(r) else: if tag in {'b', 'strong', 'i', 'em'}: open_ranges.append([tag, offset, -1]) else: offset += len(tok) text.append(tok) text = ''.join(text) formats = [] for tag, start, length in chain(ranges, open_ranges): fmt = QTextCharFormat() if tag in {'b', 'strong'}: fmt.setFontWeight(QFont.Bold) elif tag in {'i', 'em'}: fmt.setFontItalic(True) else: continue if length == -1: length = len(text) - start if length > 0: r = QTextLayout.FormatRange() r.format = fmt r.start, r.length = start, length formats.append(r) return text, formats
def __init__( self, pattern, color, weight=QFont.Normal, style=Qt.SolidPattern, blocknum=0): if isinstance(pattern,basestring): self.pattern = re.compile(pattern) else: self.pattern=pattern charfmt = QTextCharFormat() brush = QBrush(color, style) charfmt.setForeground(brush) charfmt.setFontWeight(weight) self.highlight = charfmt self.blocknum=blocknum
def __init__(self, controller): super().__init__() formc = QTextCharFormat() formc.setFontItalic(True) formc.setFontWeight(3) form = QTextBlockFormat() form.setLineHeight(200, 1) cur = self.textCursor() cur.insertText('', self.format_p) new_speech_signal = QObject() controller.speech.connect(self.new_speech_signal.emit) controller.speech.start() self.new_speech_signal.connect(cur.insertText)
def initializeFormats(cls): if cls.Formats: return baseFormat = QTextCharFormat() baseFormat.setFontFamily('monospace') baseFormat.setFontPointSize(11) for name, color, bold, italic in ( ("normal", "#000000", False, False), ("keyword", "#000080", True, False), ("builtin", "#0000A0", False, False), ("constant", "#0000C0", False, False), ("decorator", "#0000E0", False, False), ("comment", "#007F00", False, True), ("string", "#808000", False, False), ("number", "#924900", False, False), ("error", "#FF0000", False, False), ("pyqt", "#50621A", False, False)): format = QTextCharFormat(baseFormat) format.setForeground(QColor(color)) if bold: format.setFontWeight(QFont.Bold) format.setFontItalic(italic) cls.Formats[name] = format
def do_bold(self): with self.editing_cursor() as c: fmt = QTextCharFormat() fmt.setFontWeight( QFont.Bold if c.charFormat().fontWeight() != QFont.Bold else QFont.Normal) c.mergeCharFormat(fmt)
def setTheme(self, theme): self.theme = theme self.MARKDOWN_KWS_FORMAT = {} pal = self.parent.palette() pal.setColor(QPalette.Base, QColor(theme['background-color'])) self.parent.setPalette(pal) self.parent.setTextColor(QColor(theme['color'])) format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['bold']['color']))) format.setFontWeight(QFont.Bold if theme['bold']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['bold']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['Bold'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['bold']['color']))) format.setFontWeight(QFont.Bold if theme['bold']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['bold']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['uBold'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['emphasis']['color']))) format.setFontWeight(QFont.Bold if theme['emphasis']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['emphasis']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['Italic'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['emphasis']['color']))) format.setFontWeight(QFont.Bold if theme['emphasis']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['emphasis']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['uItalic'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['link']['color']))) format.setFontWeight(QFont.Bold if theme['link']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['link']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['Link'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['image']['color']))) format.setFontWeight(QFont.Bold if theme['image']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['image']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['Image'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['header']['color']))) format.setFontWeight(QFont.Bold if theme['header']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['header']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['Header'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['header']['color']))) format.setFontWeight(QFont.Bold if theme['header']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['header']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['HeaderAtx'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['unorderedlist']['color']))) format.setFontWeight(QFont.Bold if theme['unorderedlist'] ['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['unorderedlist']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['UnorderedList'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['orderedlist']['color']))) format.setFontWeight(QFont.Bold if theme['orderedlist']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['orderedlist']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['OrderedList'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['blockquote']['color']))) format.setFontWeight(QFont.Bold if theme['blockquote']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['blockquote']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['BlockQuote'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['codespan']['color']))) format.setFontWeight(QFont.Bold if theme['codespan']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['codespan']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['CodeSpan'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['codeblock']['color']))) format.setFontWeight(QFont.Bold if theme['codeblock']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['codeblock']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['CodeBlock'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['line']['color']))) format.setFontWeight(QFont.Bold if theme['line']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['line']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['HR'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['line']['color']))) format.setFontWeight(QFont.Bold if theme['line']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['line']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['eHR'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['html']['color']))) format.setFontWeight(QFont.Bold if theme['html']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['html']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['HTML'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['latex']['color']))) format.setFontWeight(QFont.Bold if theme['latex']['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['latex']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['LaTeX'] = format format = QTextCharFormat() format.setForeground(QBrush(QColor(theme['multilinecode']['color']))) format.setFontWeight(QFont.Bold if theme['multilinecode'] ['font-weight'] == 'bold' else QFont.Normal) format.setFontItalic(True if theme['multilinecode']['font-style'] == 'italic' else False) self.MARKDOWN_KWS_FORMAT['MultilineCode'] = format self.rehighlight()