class ConsoleFontStyle(object): def __init__(self, foregroundcolor, backgroundcolor, bold, italic, underline): self.foregroundcolor = foregroundcolor self.backgroundcolor = backgroundcolor self.bold = bold self.italic = italic self.underline = underline self.format = None def apply_style(self, font, is_default): self.format = QTextCharFormat() self.format.setFont(font) foreground = QColor(self.foregroundcolor) self.format.setForeground(foreground) background = QColor(self.backgroundcolor) self.format.setBackground(background) font = self.format.font() font.setBold(self.bold) font.setItalic(self.italic) font.setUnderline(self.underline) self.format.setFont(font)
class ConsoleFontStyle(object): def __init__(self, foregroundcolor, backgroundcolor, bold, italic, underline): self.foregroundcolor = foregroundcolor self.backgroundcolor = backgroundcolor self.bold = bold self.italic = italic self.underline = underline self.format = None def apply_style(self, font, is_default): self.format = QTextCharFormat() self.format.setFont(font) foreground = QColor(self.foregroundcolor) self.format.setForeground(foreground) background = QColor(self.backgroundcolor) self.format.setBackground(background) font = self.format.font() font.setBold(self.bold) font.setItalic(self.italic) font.setUnderline(self.underline) self.format.setFont(font)
class KeywordHighlighter(QSyntaxHighlighter): def __init__(self, document): QSyntaxHighlighter.__init__(self, document) self.clb = ConfigurationLineBuilder(ErtKeywords()) self.comment_format = QTextCharFormat() self.comment_format.setForeground(QColor(0, 128, 0)) self.comment_format.setFontItalic(True) self.keyword_format = QTextCharFormat() self.keyword_format.setForeground(QColor(200, 100, 0)) # self.keyword_format.setFontWeight(QFont.Bold) self.error_format = QTextCharFormat() # self.error_format.setForeground(QColor(255, 0, 0)) self.error_format.setUnderlineStyle(QTextCharFormat.WaveUnderline) self.error_format.setUnderlineColor(QColor(255, 0, 0)) self.search_format = QTextCharFormat() self.search_format.setBackground(QColor(220, 220, 220)) self.builtin_format = QTextCharFormat() self.builtin_format.setForeground(QColor(0, 170, 227)) self.search_string = "" def formatKeyword(self, keyword, validation_status): assert isinstance(keyword, Keyword) if keyword.hasKeywordDefinition(): keyword_format = QTextCharFormat(self.keyword_format) if not validation_status: keyword_format.merge(self.error_format) self.formatToken(keyword, keyword_format) else: self.formatToken(keyword, self.error_format) def highlightBlock(self, complete_block): try: block = unicode(complete_block) except NameError: block = complete_block self.clb.processLine(block) if self.clb.hasComment(): self.setFormat(self.clb.commentIndex(), len(block) - self.clb.commentIndex(), self.comment_format) if not self.clb.hasConfigurationLine(): count = len(block) if self.clb.hasComment(): count = self.clb.commentIndex() self.setFormat(0, count, self.error_format) if self.clb.hasConfigurationLine(): cl = self.clb.configurationLine() self.setCurrentBlockUserData(ConfigurationLineUserData(cl)) self.formatKeyword(cl.keyword(), cl.validationStatusForToken(cl.keyword())) arguments = cl.arguments() for argument in arguments: if not argument.hasArgumentDefinition(): pass elif argument.argumentDefinition().isBuiltIn(): self.formatToken(argument, self.builtin_format) if not cl.validationStatusForToken(argument): self.formatToken(argument, self.error_format) if self.search_string != "": for match in re.finditer("(%s)" % self.search_string, complete_block): self.setFormat(match.start(1), match.end(1) - match.start(1), self.search_format) def setSearchString(self, string): try: if self.search_string != unicode(string): self.search_string = unicode(string) self.rehighlight() except NameError: if self.search_string != string: self.search_string = string self.rehighlight() def formatToken(self, token, highlight_format): self.setFormat(token.fromIndex(), token.count(), highlight_format)
class QtANSIEscapeCodeHandler(ANSIEscapeCodeHandler): def __init__(self): ANSIEscapeCodeHandler.__init__(self) self.base_format = None self.current_format = None def set_color_scheme(self, foreground_color, background_color): """Set color scheme (foreground and background).""" if dark_color(foreground_color): self.default_foreground_color = 30 else: self.default_foreground_color = 37 if dark_color(background_color): self.default_background_color = 47 else: self.default_background_color = 40 def set_base_format(self, base_format): self.base_format = base_format def get_format(self): return self.current_format def set_style(self): """ Set font style with the following attributes: 'foreground_color', 'background_color', 'italic', 'bold' and 'underline' """ if self.current_format is None: assert self.base_format is not None self.current_format = QTextCharFormat(self.base_format) # Foreground color if self.foreground_color is None: qcolor = self.base_format.foreground() else: cstr = self.ANSI_COLORS[self.foreground_color - 30][self.intensity] qcolor = QColor(cstr) self.current_format.setForeground(qcolor) # Background color if self.background_color is None: qcolor = self.base_format.background() else: cstr = self.ANSI_COLORS[self.background_color - 40][self.intensity] qcolor = QColor(cstr) self.current_format.setBackground(qcolor) font = self.current_format.font() # Italic if self.italic is None: italic = self.base_format.fontItalic() else: italic = self.italic font.setItalic(italic) # Bold if self.bold is None: bold = self.base_format.font().bold() else: bold = self.bold font.setBold(bold) # Underline if self.underline is None: underline = self.base_format.font().underline() else: underline = self.underline font.setUnderline(underline) self.current_format.setFont(font)
class QtANSIEscapeCodeHandler(ANSIEscapeCodeHandler): def __init__(self): ANSIEscapeCodeHandler.__init__(self) self.base_format = None self.current_format = None def set_color_scheme(self, foreground_color, background_color): """Set color scheme (foreground and background).""" if dark_color(foreground_color): self.default_foreground_color = 30 else: self.default_foreground_color = 37 if dark_color(background_color): self.default_background_color = 47 else: self.default_background_color = 40 def set_base_format(self, base_format): self.base_format = base_format def get_format(self): return self.current_format def set_style(self): """ Set font style with the following attributes: 'foreground_color', 'background_color', 'italic', 'bold' and 'underline' """ if self.current_format is None: assert self.base_format is not None self.current_format = QTextCharFormat(self.base_format) # Foreground color if self.foreground_color is None: qcolor = self.base_format.foreground() else: cstr = self.ANSI_COLORS[self.foreground_color-30][self.intensity] qcolor = QColor(cstr) self.current_format.setForeground(qcolor) # Background color if self.background_color is None: qcolor = self.base_format.background() else: cstr = self.ANSI_COLORS[self.background_color-40][self.intensity] qcolor = QColor(cstr) self.current_format.setBackground(qcolor) font = self.current_format.font() # Italic if self.italic is None: italic = self.base_format.fontItalic() else: italic = self.italic font.setItalic(italic) # Bold if self.bold is None: bold = self.base_format.font().bold() else: bold = self.bold font.setBold(bold) # Underline if self.underline is None: underline = self.base_format.font().underline() else: underline = self.underline font.setUnderline(underline) self.current_format.setFont(font)