def __init__(self, parent=None): super(pythonHighlighter, self).__init__(parent) quotationFormat = QTextCharFormat() quotationFormat.setForeground(Qt.darkGreen) self.setRule("\".*\"", quotationFormat) functionFormat = QTextCharFormat() functionFormat.setFontItalic(True) functionFormat.setForeground(Qt.blue) self.setRule("\\b[A-Za-z0-9_]+(?=\\()", functionFormat) keywordFormat = QTextCharFormat() keywordFormat.setForeground(Qt.darkBlue) keywordFormat.setFontWeight(QFont.Bold) pythonKeywordPatterns = ["\\band\\b", "\\bdel\\b", "\\bfrom\\b", "\\bnot\\b", "\\bwhile\\b", "\\bas\\b", "\\belif\\b", "\\bglobal\\b", "\\bor\\b", "\\bwith\\b", "\\bassert\\b", "\\belse\\b", "\\bif\\b", "\\bpass\\b", "\\byield\\b", "\\bbreak\\b", "\\bexcept\\b", "\\bimport\\b", "\\bprint\\b", "\\bclass\\b", "\\bexec\\b", "\\bin\\b", "\\braise\\b", "\\bcontinue\\b", "\\bfinally\\b", "\\bis\\b", "\\breturn\\b", "\\bdef\\b", "\\bfor\\b", "\\blambda\\b", "\\btry\\b"] for pattern in pythonKeywordPatterns: self.setRule(pattern, keywordFormat)
def __init__(self, parent=None): self.keywordFormat = QTextCharFormat() self.keywordFormat.setForeground(QBrush(Qt.blue)) self.keywordFormat.setFontWeight(QFont.Bold) self.stringFormat = QTextCharFormat() self.stringFormat.setForeground(QBrush(Qt.darkGreen)) self.defFormat = QTextCharFormat() self.defFormat.setForeground(QBrush(Qt.black)) self.defFormat.setFontWeight(QFont.Bold) self.commentFormat = QTextCharFormat() self.commentFormat.setForeground(QBrush(Qt.lightGray)) self.decoratorFormat = QTextCharFormat() self.decoratorFormat.setForeground(QBrush(Qt.darkGray)) self.keywords = list(keyword.kwlist) self.rules = [(QRegExp(r"\b%s\b" % kwd), self.keywordFormat) for kwd in self.keywords] + \ [(QRegExp(r"\bdef\s+([A-Za-z_]+[A-Za-z0-9_]+)\s*\("), self.defFormat), (QRegExp(r"\bclass\s+([A-Za-z_]+[A-Za-z0-9_]+)\s*\("), self.defFormat), (QRegExp(r"'.*'"), self.stringFormat), (QRegExp(r'".*"'), self.stringFormat), (QRegExp(r"#.*"), self.commentFormat), (QRegExp(r"@[A-Za-z_]+[A-Za-z0-9_]+"), self.decoratorFormat)] self.multilineStart = QRegExp(r"(''')|" + r'(""")') self.multilineEnd = QRegExp(r"(''')|" + r'(""")') QSyntaxHighlighter.__init__(self, parent)
def updateFonts(self, font): self.base_format.setFont(font) self.empty_format = QTextCharFormat(self.base_format) #self.empty_format.setFontPointSize(font.pointSize()/4.0) self.keywordFormat = QTextCharFormat(self.base_format) self.keywordFormat.setForeground(QColor(scheme.syntax_keyword)) self.keywordFormat.setFontWeight(QFont.Bold) self.builtinFormat = QTextCharFormat(self.base_format) self.builtinFormat.setForeground(QColor(scheme.syntax_builtin)) self.magicFormat = QTextCharFormat(self.base_format) self.magicFormat.setForeground(QColor(scheme.syntax_magic)) #self.qtFormat = QTextCharFormat(self.base_format) #self.qtFormat.setForeground(QColor(scheme.syntax_qt)) ##self.qtFormat.setFontWeight(QFont.Bold) self.selfFormat = QTextCharFormat(self.base_format) self.selfFormat.setForeground(QColor(scheme.syntax_self)) #self.selfFormat.setFontItalic(True) self.singleLineCommentFormat = QTextCharFormat(self.base_format) self.singleLineCommentFormat.setForeground( QColor(scheme.syntax_comment)) self.singleLineCommentFormat.setFontItalic(True) self.multiLineStringFormat = QTextCharFormat(self.base_format) self.multiLineStringFormat.setForeground(QColor(scheme.syntax_string)) #self.multiLineStringFormat.setBackground(QBrush(Qt.green)) self.quotationFormat1 = QTextCharFormat(self.base_format) self.quotationFormat1.setForeground(QColor(scheme.syntax_string)) self.quotationFormat2 = QTextCharFormat(self.base_format) self.quotationFormat2.setForeground(QColor(scheme.syntax_string)) self.numFormat = QTextCharFormat(self.base_format) self.numFormat.setForeground(QColor(scheme.syntax_number))
def __init__(self, parent=None): super(DiffsMenu, self).__init__(parent) self.ui = Ui_Diffs() self.ui.setupUi(self) self.ui.actionCopyPath = QtGui.QAction("Copy path", None, triggered=self.copyPath) self.ui.treeResults.addAction(self.ui.actionCopyPath) self.folder1 = None self.folder2 = None self.files = None self.files_nodupes = None self.files_missing = None self.saved_diffs = {} self.format1 = QTextCharFormat() self.format1.setBackground(QColor(255, 224, 224)) self.format2 = QTextCharFormat() self.format2.setBackground(QColor(224, 240, 255)) self.menu_name = "Diffs" self.format_plain = QTextCharFormat()
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 __init__(self, parent=None): QSyntaxHighlighter.__init__(self, parent) self.parent = parent sqlKeyword = QTextCharFormat() sqlOperator = QTextCharFormat() self.highlightingRules = [] #Keywords sqlKeyword.setFontWeight(QFont.Bold) sqlKeyword.setForeground(Qt.blue) sqlKeywords = ["AND", "OR", "LIKE"] for word in sqlKeywords: regExp = QRegExp("\\b" + word + "\\b", Qt.CaseInsensitive) rule = HighlightingRule(regExp, sqlKeyword) self.highlightingRules.append(rule) #Comparison Operators sqlOperator.setForeground(Qt.magenta) sqlOperators = ["<", ">", "="] for operator in sqlOperators: regExp = QRegExp("\\W" + operator + "\\W", Qt.CaseInsensitive) rule = HighlightingRule(regExp, sqlOperator) self.highlightingRules.append(rule)
def load(self, scheme): """Load the settings for the scheme. Called on init.""" s = QSettings() s.beginGroup("fontscolors/" + scheme) # load font defaultfont = "Lucida Console" if os.name == "nt" else "monospace" self.font = QFont(s.value("fontfamily", defaultfont, type(""))) self.font.setPointSizeF(s.value("fontsize", 10.0, float)) # load base colors s.beginGroup("basecolors") for name in baseColors: if s.contains(name): self.baseColors[name] = QColor(s.value(name, "", type(""))) else: self.baseColors[name] = baseColorDefaults[name]() s.endGroup() # get the list of supported styles from ly.colorize all_styles = ly.colorize.default_mapping() default_styles = set() for group, styles in all_styles: d = self._inherits[group] = {} for style in styles: if style.base: default_styles.add(style.base) d[style.name] = style.base default_scheme = ly.colorize.default_scheme # load default styles s.beginGroup("defaultstyles") for name in default_styles: self.defaultStyles[name] = f = QTextCharFormat() css = default_scheme[None].get(name) if css: css2fmt(css, f) s.beginGroup(name) self.loadTextFormat(f, s) s.endGroup() s.endGroup() # load specific styles s.beginGroup("allstyles") for group, styles in all_styles: self.allStyles[group]= {} s.beginGroup(group) for style in styles: self.allStyles[group][style.name] = f = QTextCharFormat() css = default_scheme[group].get(style.name) if css: css2fmt(css, f) s.beginGroup(style.name) self.loadTextFormat(f, s) s.endGroup() s.endGroup() s.endGroup()
def __init__(self, parent): super(OutputWidget, self).__init__(parent) self._parent = parent self.setReadOnly(True) self.maxValue = 0 self.actualValue = 0 #traceback pattern self.patLink = re.compile(r'(\s)*File "(.*?)", line \d.+') #formats font = settings.FONT self.plain_format = QTextCharFormat() self.plain_format.setFont(font) self.plain_format.setForeground( QBrush( QColor( resources.CUSTOM_SCHEME.get( "editor-text", resources.COLOR_SCHEME["editor-text"])))) self.error_format = QTextCharFormat() self.error_format.setFont(font) self.error_format.setAnchor(True) self.error_format.setForeground( QColor( resources.CUSTOM_SCHEME.get( "pep8-underline", resources.COLOR_SCHEME["pep8-underline"]))) self.error_format.setBackground( QColor( resources.CUSTOM_SCHEME.get( "error-underline", resources.COLOR_SCHEME["error-underline"]))) self.error_format.setToolTip(self.tr("Click to show the source")) self.error_format2 = QTextCharFormat() self.error_format2.setAnchor(True) self.error_format2.setFont(font) self.error_format2.setForeground( QBrush( QColor( resources.CUSTOM_SCHEME.get( "error-underline", resources.COLOR_SCHEME["error-underline"])))) self.connect(self, SIGNAL("blockCountChanged(int)"), self._scroll_area) css = 'QPlainTextEdit {color: %s; background-color: %s;' \ 'selection-color: %s; selection-background-color: %s;}' \ % (resources.CUSTOM_SCHEME.get('editor-text', resources.COLOR_SCHEME['editor-text']), resources.CUSTOM_SCHEME.get('editor-background', resources.COLOR_SCHEME['editor-background']), resources.CUSTOM_SCHEME.get('editor-selection-color', resources.COLOR_SCHEME['editor-selection-color']), resources.CUSTOM_SCHEME.get('editor-selection-background', resources.COLOR_SCHEME['editor-selection-background'])) self.setStyleSheet(css)
def initializeFormats(): baseFormat = QTextCharFormat() baseFormat.setFontFamily(Config["fontfamily"]) baseFormat.setFontPointSize(Config["fontsize"]) for name in ("normal", "keyword", "builtin", "constant", "decorator", "comment", "string", "number", "error", "pyqt"): format = QTextCharFormat(baseFormat) format.setForeground( QColor(Config["%sfontcolor" % name])) if Config["%sfontbold" % name]: format.setFontWeight(QFont.Bold) format.setFontItalic(Config["%sfontitalic" % name]) PythonHighlighter.Formats[name] = format
def __init__(self, parent=None): global reWord super(wordHighLighter, self).__init__(parent) # store a local reference to the global regex self.wordMatch = reWord # Initialize text formats to apply to words from various lists. # - Scanno candidates get a light lilac background. self.scannoFormat = QTextCharFormat() self.scannoFormat.setBackground(QBrush(QColor("#EBD7E6"))) # Set the style for misspelt words. We underline in red using the # well-known wavy red underline, the same on all platforms. self.misspeltFormat = QTextCharFormat() self.misspeltFormat.setUnderlineStyle(QTextCharFormat.WaveUnderline) self.misspeltFormat.setUnderlineColor(QColor("red"))
def __init__(self, parent=None): super(ReferenceHighlighter, self).__init__(parent) self.format = QTextCharFormat() self.format.setForeground(QColor(0, 0, 255)) self.references = []
def onMatchNumberChange(self, matchNumber): # Set default format on the whole text before highlighting the selected # match. document = self.matchText.document() cursor = QTextCursor(document) cursor.movePosition(QTextCursor.End, QTextCursor.KeepAnchor) cursor.setCharFormat(QTextCharFormat()) search = self.getSearchText() for i, match in enumerate(self.regex.finditer(search)): if i + 1 == matchNumber: break else: assert False, ("We didn't find a match?! (RE=%r, text=%r" % (self.regex.pattern, search)) self.formatMatchedText(document, match) model = self.groupsView.model() model.clear() # Create a reversed self.regex.groupindex dictionnary groupsIndexes = dict( (v, k) for (k, v) in self.regex.groupindex.iteritems()) for i in range(1, self.regex.groups + 1): groupName = groupsIndexes.get(i, "") groupValue = match.group(i) model.append((groupName, groupValue))
def __init__(self, doc): QSyntaxHighlighter.__init__(self, doc) commentFormat = QTextCharFormat() commentFormat.setFontItalic(True) commentFormat.setFontWeight(0) commentFormat.setForeground(QColor('grey')) self.commentFormat = commentFormat mysqlObjectFormat = QTextCharFormat() mysqlObjectFormat.setFontItalic(True) mysqlObjectFormat.setFontWeight(0) mysqlObjectFormat.setForeground(QColor().fromRgb(128, 128, 0)) self.mysqlObjectFormat = mysqlObjectFormat self.mysqlObjectRegex = re.compile('(`.+?`)')
def colorizeDiff(self, diffinfos): cursor = self.textCursor() cursor.setPosition(QTextCursor.Start) text = self.toPlainText() keys = diffinfos.keys() keys.sort() for offset in keys: difflen = diffinfos[offset] pos = offset + (offset / 16) # count = offset / 16 # print "offset ", offset, " count ", count count = difflen + (((offset + difflen) / 16) - (offset / 16)) # count = difflen cursor.setPosition(pos, QTextCursor.MoveAnchor) # print "L", l, " len ", pos + difflen cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor, count) format = QTextCharFormat() format.setFont(self.sfont) format.setForeground(QBrush(QColor(Qt.red))) cursor.setCharFormat(format) cursor.setPosition(QTextCursor.Start, QTextCursor.MoveAnchor)
def make_pre_block(self): cursor = self.textCursor() block_format = cursor.blockFormat() if block_format.nonBreakableLines(): block_format.setNonBreakableLines(False) cursor.setBlockFormat(block_format) char_format = QTextCharFormat() char_format.setFontFixedPitch(False) cursor.setCharFormat(char_format) else: block_format.setNonBreakableLines(True) cursor.setBlockFormat(block_format) char_format = QTextCharFormat() char_format.setFontFixedPitch(True) cursor.setCharFormat(char_format)
def finish_execution(self, exitCode, exitStatus): """Print a message and hide the input line when the execution ends.""" self.lblInput.hide() self.input.hide() format_ = QTextCharFormat() format_.setAnchor(True) self.output.textCursor().insertText('\n\n') if exitStatus == QProcess.NormalExit: format_.setForeground( QBrush( QColor( resources.CUSTOM_SCHEME.get( "keyword", resources.COLOR_SCHEME["keyword"])))) self.output.textCursor().insertText( self.tr("Execution Successful!"), format_) else: format_.setForeground( QBrush( QColor( resources.CUSTOM_SCHEME.get( "error-underline", resources.COLOR_SCHEME["error-underline"])))) self.output.textCursor().insertText( self.tr("Execution Interrupted"), format_) self.output.textCursor().insertText('\n\n') self.__post_execution()
def update_char_format(baseformat, color=None, background=None, weight=None, italic=None, underline=None, font=None): """ Return a copy of `baseformat` :class:`QTextCharFormat` with updated color, weight, background and font properties. """ charformat = QTextCharFormat(baseformat) if color is not None: charformat.setForeground(color) if background is not None: charformat.setBackground(background) if font is not None: charformat.setFont(font) else: font = update_font(baseformat.font(), weight, italic, underline) charformat.setFont(font) return charformat
def setDateBackground(self, color): brush = QBrush(color) frmt = QTextCharFormat() frmt.setBackground(brush) return frmt
def css2fmt(d, f=None): """Convert a css dictionary to a QTextCharFormat.""" if f is None: f = QTextCharFormat() v = d.get('font-style') if v: f.setFontItalic(v in ('oblique', 'italic')) v = d.get('font-weight') if v: if v == 'bold': f.setFontWeight(QFont.Bold) elif v == 'normal': f.setFontWeight(QFont.Normal) elif v.isdigit(): f.setFontWeight(int(v) / 10) v = d.get('color') if v: f.setForeground(QColor(v)) v = d.get('background') if v: f.setBackground(QColor(v)) v = d.get('text-decoration') if v: f.setFontUnderline(v == 'underline') v = d.get('text-decoration-color') if v: f.setUnderlineColor(QColor(v)) return f
def __post_execution_message(self): """Print post execution message.""" self.output.textCursor().insertText('\n\n') format_ = QTextCharFormat() format_.setAnchor(True) format_.setForeground(Qt.green) self.output.textCursor().insertText( self.tr("Post Execution Script Successfully executed."), format_)
def setBold(self): format = QTextCharFormat() if self.boldAct.isChecked(): weight = QFont.Bold else: weight = QFont.Normal format.setFontWeight(weight) self.setFormat(format)
def __init__(self, textboxdoc, valid_list_of_commands): QSyntaxHighlighter.__init__(self, textboxdoc) self.valid_syntax = "|".join( [command.regexp_str for command in valid_list_of_commands]) self.my_expression = QRegExp(self.valid_syntax) #define a blue font format for valid commands self.valid = QTextCharFormat() self.valid.setForeground(Qt.black) #define a bold red font format for invalid commands self.invalid = QTextCharFormat() self.invalid.setFontWeight(QFont.Bold) self.invalid.setForeground(Qt.red) #define a blue font format for valid parameters self.valid_value = QTextCharFormat() self.valid_value.setFontWeight(QFont.Bold) #self.valid_value.setForeground(QColor.fromRgb(255,85,0)) self.valid_value.setForeground(Qt.blue)
def updateDisplay(self): data = self.data[self.scheme.currentScheme()] with qutil.signalsBlocked(self.fontChooser, self.fontSize): self.fontChooser.setCurrentFont(data.font) self.fontSize.setValue(data.font.pointSizeF()) with qutil.signalsBlocked(self): # update base colors for name in textformats.baseColors: self.baseColorsWidget.color[name].setColor(data.baseColors[name]) # update base colors for whole treewidget p = QApplication.palette() p.setColor(QPalette.Base, data.baseColors['background']) p.setColor(QPalette.Text, data.baseColors['text']) p.setColor(QPalette.Highlight, data.baseColors['selectionbackground']) p.setColor(QPalette.HighlightedText, data.baseColors['selectiontext']) self.tree.setPalette(p) def setItemTextFormat(item, f): font = QFont(data.font) if f.hasProperty(QTextFormat.ForegroundBrush): item.setForeground(0, f.foreground().color()) else: item.setForeground(0, data.baseColors['text']) if f.hasProperty(QTextFormat.BackgroundBrush): item.setBackground(0, f.background().color()) else: item.setBackground(0, QBrush()) font.setWeight(f.fontWeight()) font.setItalic(f.fontItalic()) font.setUnderline(f.fontUnderline()) item.setFont(0, font) # update looks of default styles for name in textformats.defaultStyles: setItemTextFormat(self.defaultStyles[name], data.defaultStyles[name]) # update looks of all the specific styles for group, styles in ly.colorize.default_mapping(): children = self.allStyles[group][1] for name, inherit, clss in styles: f = QTextCharFormat(data.defaultStyles[inherit]) if inherit else QTextCharFormat() f.merge(data.allStyles[group][name]) setItemTextFormat(children[name], f)
def mkformat(self, fg=None, bg=None, bold=False): fmt = QTextCharFormat() if fg: fmt.setForeground(fg) if bg: fmt.setBackground(bg) if bold: fmt.setFontWeight(QFont.Bold) return fmt
def set_font(self, font): """Set shell styles font""" self.set_pythonshell_font(font) cursor = self.textCursor() cursor.select(QTextCursor.Document) charformat = QTextCharFormat() charformat.setFontFamily(font.family()) charformat.setFontPointSize(font.pointSize()) cursor.mergeCharFormat(charformat)
def format(color, style=''): """Return a QTextCharFormat with the given attributes. """ c = QColor(color) #c.setNamedColor(color) f = QTextCharFormat() f.setForeground(c) return f
def __init__(self, parent): super(OutputWidget, self).__init__(parent) self._parent = parent self.setReadOnly(True) #traceback pattern self.patLink = re.compile(r'(\s)*File "(.*?)", line \d.+') #formats font = settings.FONT self.plain_format = QTextCharFormat() self.plain_format.setFont(font) self.plain_format.setForeground( QBrush(QColor(resources.CUSTOM_SCHEME.get( "Default", resources.COLOR_SCHEME["Default"])))) self.error_format = QTextCharFormat() self.error_format.setFont(font) self.error_format.setAnchor(True) self.error_format.setForeground(QColor(resources.CUSTOM_SCHEME.get( "Pep8Underline", resources.COLOR_SCHEME["Pep8Underline"]))) self.error_format.setBackground(QColor(resources.CUSTOM_SCHEME.get( "ErrorUnderline", resources.COLOR_SCHEME["ErrorUnderline"]))) self.error_format.setToolTip(self.tr("Click to show the source")) self.error_format2 = QTextCharFormat() self.error_format2.setAnchor(True) self.error_format2.setFont(font) self.error_format2.setForeground( QBrush( QColor(resources.CUSTOM_SCHEME.get( "ErrorUnderline", resources.COLOR_SCHEME["ErrorUnderline"])))) self.connect(self, SIGNAL("blockCountChanged(int)"), lambda: self.moveCursor(QTextCursor.End)) css = 'QPlainTextEdit {color: %s; background-color: %s;' \ 'selection-color: %s; selection-background-color: %s;}' \ % (resources.CUSTOM_SCHEME.get('editor-text', resources.COLOR_SCHEME['Default']), resources.CUSTOM_SCHEME.get('EditorBackground', resources.COLOR_SCHEME['EditorBackground']), resources.CUSTOM_SCHEME.get('EditorSelectionColor', resources.COLOR_SCHEME['EditorSelectionColor']), resources.CUSTOM_SCHEME.get('EditorSelectionBackground', resources.COLOR_SCHEME['EditorSelectionBackground'])) self.setStyleSheet(css)
def __init__(self, parent): QPlainTextEdit.__init__(self, parent) self._parent = parent self.setReadOnly(True) self.maxValue = 0 self.actualValue = 0 #traceback pattern self.patLink = re.compile(r'(\s)*File "(.*?)", line \d.+') #formats self.plain_format = QTextCharFormat() self.error_format = QTextCharFormat() self.error_format.setAnchor(True) self.error_format.setFontUnderline(True) self.error_format.setUnderlineStyle(QTextCharFormat.SingleUnderline) self.error_format.setUnderlineColor(Qt.red) self.error_format.setForeground(Qt.blue) self.error_format.setToolTip(self.tr("Click to show the source")) self.connect(self, SIGNAL("blockCountChanged(int)"), self._scroll_area)
def toggleBold(self): #self.setFontWeight(QFont.Normal if self.fontWeight() > QFont.Normal else QFont.Bold) if self.which_header(): return bold = self.fontWeight() > QFont.Normal cursor = self.textCursor() char_format = QTextCharFormat() char_format.setFont(self.font) char_format.setFontWeight(QFont.Normal if bold else QFont.Bold) cursor.setCharFormat(char_format)
def toggleItalic(self): if self.which_header(): return #self.setFontItalic(not self.fontItalic()) italic = self.fontItalic() cursor = self.textCursor() char_format = QTextCharFormat() char_format.setFont(self.font) char_format.setFontItalic(not italic) cursor.setCharFormat(char_format)