def do_insert_link(self, *args): link, name, is_image = self.ask_link() if not link: return url = self.parse_link(link) if url.isValid(): url = unicode_type(url.toString(NO_URL_FORMATTING)) self.focus_self() with self.editing_cursor() as c: if is_image: c.insertImage(url) else: oldfmt = QTextCharFormat(c.charFormat()) fmt = QTextCharFormat() fmt.setAnchor(True) fmt.setAnchorHref(url) fmt.setForeground(QBrush(self.palette().color(QPalette.Link))) if name or not c.hasSelection(): c.mergeCharFormat(fmt) c.insertText(name or url) else: pos, anchor = c.position(), c.anchor() start, end = min(pos, anchor), max(pos, anchor) for i in range(start, end): cur = self.textCursor() cur.setPosition(i), cur.setPosition(i + 1, c.KeepAnchor) cur.mergeCharFormat(fmt) c.setPosition(c.position()) c.setCharFormat(oldfmt) else: error_dialog(self, _('Invalid URL'), _('The url %r is invalid') % link, show=True)
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 do_insert_link(self, *args): link, name, is_image = self.ask_link() if not link: return url = self.parse_link(link) if url.isValid(): url = unicode_type(url.toString(NO_URL_FORMATTING)) self.focus_self() with self.editing_cursor() as c: if is_image: c.insertImage(url) else: name = name or url fmt = QTextCharFormat() fmt.setAnchor(True) fmt.setAnchorHref(url) fmt.setFontUnderline(True) fmt.setForeground(QBrush(QColor('blue'))) prev_fmt = c.charFormat() c.mergeCharFormat(fmt) c.insertText(url) c.setCharFormat(prev_fmt) else: error_dialog(self, _('Invalid URL'), _('The url %r is invalid') % link, show=True)
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 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_color(self): col = QColorDialog.getColor(Qt.black, self, _('Choose foreground color'), QColorDialog.ShowAlphaChannel) if col.isValid(): fmt = QTextCharFormat() fmt.setForeground(QBrush(col)) with self.editing_cursor() as c: c.mergeCharFormat(fmt)
def __init__( self, pattern, color, style ): if isinstance(pattern,basestring): self.pattern = re.compile(pattern) else: self.pattern=pattern charfmt = QTextCharFormat() brush = QBrush(color, style) charfmt.setForeground(brush) self.highlight = charfmt
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 __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 _refreshTextFormat(self): if not self._webView: return textFormat = [] # typedef QList<QTextLayout::FormatRange> if self._webView.url().isEmpty(): hostName = QUrl(self.text()).host() else: hostName = self._webView.url().host() if hostName: hostPos = self.text().find(hostName) if hostPos > 0: format_ = QTextCharFormat() palette = self.palette() color = Colors.mid(palette.color(QPalette.Base), palette.color(QPalette.Text), 1, 1) format_.setForeground(color) schemePart = QTextLayout.FormatRange() schemePart.start = 0 schemePart.length = hostPos schemePart.format = format_ hostPart = QTextLayout.FormatRange() hostPart.start = hostPos hostPart.length = len(hostName) remainingPart = QTextLayout.FormatRange() remainingPart.start = hostPos + len(hostName) remainingPart.length = len(self.text()) - remainingPart.start remainingPart.format = format_ textFormat.append(schemePart) textFormat.append(hostPart) textFormat.append(remainingPart) self.setTextFormat(textFormat)
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 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()