Python PyQt5.QtGui.QTextCharFormat() Examples
The following are 30
code examples of PyQt5.QtGui.QTextCharFormat().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
PyQt5.QtGui
, or try the search function
.
Example #1
Source File: sourcewindow.py From dcc with Apache License 2.0 | 10 votes |
def _get_format_from_style(self, token, style): """ Returns a QTextCharFormat for token by reading a Pygments style. """ result = QtGui.QTextCharFormat() for key, value in list(style.style_for_token(token).items()): if value: if key == 'color': result.setForeground(self._get_brush(value)) elif key == 'bgcolor': result.setBackground(self._get_brush(value)) elif key == 'bold': result.setFontWeight(QtGui.QFont.Bold) elif key == 'italic': result.setFontItalic(True) elif key == 'underline': result.setUnderlineStyle( QtGui.QTextCharFormat.SingleUnderline) elif key == 'sans': result.setFontStyleHint(QtGui.QFont.SansSerif) elif key == 'roman': result.setFontStyleHint(QtGui.QFont.Times) elif key == 'mono': result.setFontStyleHint(QtGui.QFont.TypeWriter) return result
Example #2
Source File: show_text_window.py From easygui_qt with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, parent=None): super(Highlighter, self).__init__(parent) keywordFormat = QtGui.QTextCharFormat() keywordFormat.setForeground(QtCore.Qt.blue) keywordFormat.setFontWeight(QtGui.QFont.Bold) keywordPatterns = ["\\b{}\\b".format(k) for k in keyword.kwlist] self.highlightingRules = [(QtCore.QRegExp(pattern), keywordFormat) for pattern in keywordPatterns] classFormat = QtGui.QTextCharFormat() classFormat.setFontWeight(QtGui.QFont.Bold) self.highlightingRules.append((QtCore.QRegExp("\\bQ[A-Za-z]+\\b"), classFormat)) singleLineCommentFormat = QtGui.QTextCharFormat() singleLineCommentFormat.setForeground(QtCore.Qt.gray) self.highlightingRules.append((QtCore.QRegExp("#[^\n]*"), singleLineCommentFormat)) quotationFormat = QtGui.QTextCharFormat() quotationFormat.setForeground(QtCore.Qt.darkGreen) self.highlightingRules.append((QtCore.QRegExp("\".*\""), quotationFormat)) self.highlightingRules.append((QtCore.QRegExp("'.*'"), quotationFormat))
Example #3
Source File: manage_files.py From QualCoder with MIT License | 6 votes |
def highlight(self, fid, textEdit): """ Add coding and annotation highlights. """ cur = self.app.conn.cursor() sql = "select pos0,pos1 from annotation where fid=? union all select pos0,pos1 from code_text where fid=?" cur.execute(sql, [fid, fid]) annoted_coded = cur.fetchall() format_ = QtGui.QTextCharFormat() format_.setFontFamily(self.app.settings['font']) format_.setFontPointSize(self.app.settings['fontsize']) # remove formatting cursor = textEdit.textCursor() cursor.setPosition(0, QtGui.QTextCursor.MoveAnchor) cursor.setPosition(len(textEdit.toPlainText()), QtGui.QTextCursor.KeepAnchor) cursor.setCharFormat(format_) # add formatting for item in annoted_coded: cursor.setPosition(int(item[0]), QtGui.QTextCursor.MoveAnchor) cursor.setPosition(int(item[1]), QtGui.QTextCursor.KeepAnchor) format_.setFontUnderline(True) format_.setUnderlineColor(QtCore.Qt.red) cursor.setCharFormat(format_)
Example #4
Source File: cases.py From QualCoder with MIT License | 6 votes |
def highlight(self): """ Apply text highlighting to current file. Highlight text of selected case with red underlining. #format_.setForeground(QtGui.QColor("#990000")) """ if self.selected_file is None: return if self.selected_file['fulltext'] is None: return format_ = QtGui.QTextCharFormat() cursor = self.ui.textBrowser.textCursor() for item in self.case_text: try: cursor.setPosition(int(item['pos0']), QtGui.QTextCursor.MoveAnchor) cursor.setPosition(int(item['pos1']), QtGui.QTextCursor.KeepAnchor) format_.setFontUnderline(True) format_.setUnderlineColor(QtCore.Qt.red) cursor.setCharFormat(format_) except: msg = "highlight, text length " + str(len(self.ui.textBrowser.toPlainText())) msg += "\npos0:" + str(item['pos0']) + ", pos1:" + str(item['pos1']) logger.debug(msg)
Example #5
Source File: syntax.py From IDAngr with BSD 2-Clause "Simplified" License | 6 votes |
def format(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 # Syntax styles that can be shared by all languages
Example #6
Source File: sourcewindow.py From MARA_Framework with GNU Lesser General Public License v3.0 | 6 votes |
def _get_format_from_style(self, token, style): """ Returns a QTextCharFormat for token by reading a Pygments style. """ result = QtGui.QTextCharFormat() for key, value in style.style_for_token(token).items(): if value: if key == 'color': result.setForeground(self._get_brush(value)) elif key == 'bgcolor': result.setBackground(self._get_brush(value)) elif key == 'bold': result.setFontWeight(QtGui.QFont.Bold) elif key == 'italic': result.setFontItalic(True) elif key == 'underline': result.setUnderlineStyle( QtGui.QTextCharFormat.SingleUnderline) elif key == 'sans': result.setFontStyleHint(QtGui.QFont.SansSerif) elif key == 'roman': result.setFontStyleHint(QtGui.QFont.Times) elif key == 'mono': result.setFontStyleHint(QtGui.QFont.TypeWriter) return result
Example #7
Source File: hexteditor.py From guppy-proxy with MIT License | 6 votes |
def set_bytes(self, bs): with DisableUpdates(self.textedit): self.pretty_mode = False self.data = bs chunks = HextEditor._split_by_printables(bs) self.clear() cursor = QTextCursor(self.textedit.document()) cursor.beginEditBlock() try: cursor.select(QTextCursor.Document) cursor.setCharFormat(QTextCharFormat()) cursor.clearSelection() for chunk in chunks: if chr(chunk[0]) in qtprintable: cursor.insertText(chunk.decode()) else: for b in chunk: self._insert_byte(cursor, b) finally: cursor.endEditBlock() self.repaint() # needed to fix issue with py2app
Example #8
Source File: exp_syntax_highlighter.py From VUT-FIT-IFJ-2017-toolkit with GNU General Public License v3.0 | 5 votes |
def _setupFormat(self, color: QColor, fontSettings: QFont, colorIsForeground: bool = True) -> QTextCharFormat: pattern_format = QTextCharFormat() if color and colorIsForeground: pattern_format.setForeground(color) if color and (not colorIsForeground): pattern_format.setBackground(color) pattern_format.setFontItalic(fontSettings.italic()) pattern_format.setFontWeight(fontSettings.bold()) return pattern_format
Example #9
Source File: view_av.py From QualCoder with MIT License | 5 votes |
def highlight(self): """ Apply text highlighting to current file. If no colour has been assigned to a code, those coded text fragments are coloured gray. Each code text item contains: fid, date, pos0, pos1, seltext, cid, status, memo, name, owner. """ fmt = QtGui.QTextCharFormat() cursor = self.ui.textEdit.textCursor() # add coding highlights for item in self.code_text: cursor.setPosition(int(item['pos0']), QtGui.QTextCursor.MoveAnchor) cursor.setPosition(int(item['pos1']), QtGui.QTextCursor.KeepAnchor) color = "#F8E0E0" # default light red for fcode in self.codes: if fcode['cid'] == item['cid']: color = fcode['color'] fmt.setBackground(QtGui.QBrush(QtGui.QColor(color))) # highlight codes with memos - these are italicised if item['memo'] is not None and item['memo'] != "": fmt.setFontItalic(True) else: fmt.setFontItalic(False) fmt.setFontWeight(QtGui.QFont.Normal) cursor.setCharFormat(fmt) # add annotation marks - these are in bold for note in self.annotations: if note['fid'] == self.transcription[0]: cursor.setPosition(int(note['pos0']), QtGui.QTextCursor.MoveAnchor) cursor.setPosition(int(note['pos1']), QtGui.QTextCursor.KeepAnchor) formatB = QtGui.QTextCharFormat() formatB.setFontWeight(QtGui.QFont.Bold) cursor.mergeCharFormat(formatB)
Example #10
Source File: formatted_text_writer.py From VUT-FIT-IFJ-2017-toolkit with GNU General Public License v3.0 | 5 votes |
def _setupFormat(self, color: QColor) -> QTextCharFormat: pattern_format = QTextCharFormat() if color is not None: pattern_format.setForeground(color) pattern_format.setFontItalic(self._target.property("font").italic()) pattern_format.setFontWeight(self._target.property("font").bold()) return pattern_format
Example #11
Source File: log.py From eddy with GNU General Public License v3.0 | 5 votes |
def fmt(color): """ Return a QTextCharFormat with the given attributes. """ _color = QtGui.QColor() _color.setNamedColor(color) _format = QtGui.QTextCharFormat() _format.setForeground(_color) return _format
Example #12
Source File: sourcewindow.py From MARA_Framework with GNU Lesser General Public License v3.0 | 5 votes |
def _get_format(self, token): """ Returns a QTextCharFormat for token or None. """ if token in self._formats: return self._formats[token] result = self._get_format_from_style(token, self._style) self._formats[token] = result return result
Example #13
Source File: HighlightText.py From PyQt with GNU General Public License v3.0 | 5 votes |
def highlight(self): text = self.findText.text() # 输入框中的文字 if not text: return col = QColorDialog.getColor(self.textEdit.textColor(), self) if not col.isValid(): return # 恢复默认的颜色 cursor = self.textEdit.textCursor() cursor.select(QTextCursor.Document) cursor.setCharFormat(QTextCharFormat()) cursor.clearSelection() self.textEdit.setTextCursor(cursor) # 文字颜色 fmt = QTextCharFormat() fmt.setForeground(col) # 正则 expression = QRegExp(text) self.textEdit.moveCursor(QTextCursor.Start) cursor = self.textEdit.textCursor() # 循环查找设置颜色 pos = 0 index = expression.indexIn(self.textEdit.toPlainText(), pos) while index >= 0: cursor.setPosition(index) cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor, len(text)) cursor.mergeCharFormat(fmt) pos = index + expression.matchedLength() index = expression.indexIn(self.textEdit.toPlainText(), pos)
Example #14
Source File: CalendarQssStyle.py From PyQt with GNU General Public License v3.0 | 5 votes |
def __init__(self, *args, **kwargs): super(CalendarWidget, self).__init__(*args, **kwargs) # 隐藏左边的序号 self.setVerticalHeaderFormat(self.NoVerticalHeader) # 修改周六周日颜色 fmtGreen = QTextCharFormat() fmtGreen.setForeground(QBrush(Qt.green)) self.setWeekdayTextFormat(Qt.Saturday, fmtGreen) fmtOrange = QTextCharFormat() fmtOrange.setForeground(QBrush(QColor(252, 140, 28))) self.setWeekdayTextFormat(Qt.Sunday, fmtOrange)
Example #15
Source File: encoder.py From deen with Apache License 2.0 | 5 votes |
def search_highlight(self): """The function that will be called whenever the search area is submitted. It will search within the text_field and highlights matches.""" cursor = self.text_field.textCursor() char_format = QTextCharFormat() cursor.select(QTextCursor.Document) cursor.mergeCharFormat(char_format) cursor.clearSelection() char_format.setBackground(QBrush(QColor('yellow'))) regex = QRegularExpression(self.ui.search_area.text()) matches = regex.globalMatch(self.text_field.toPlainText()) _matches = [] while matches.hasNext(): _matches.append(matches.next()) self.search_matches = _matches self.ui.search_matches_label.setText('Matches: ' + str(len(self.search_matches))) self.ui.search_progress_bar.setRange(0, len(self.search_matches)) if len(self.search_matches) > 100: self.ui.search_progress_bar.show() match_count = 1 for match in self.search_matches: if match_count > 1000: # TODO: implement proper handling of > 1000 matches break self.ui.search_progress_bar.setValue(match_count) match_count += 1 cursor.setPosition(match.capturedStart()) cursor.setPosition(match.capturedEnd(), QTextCursor.KeepAnchor) cursor.mergeCharFormat(format) self.ui.search_progress_bar.hide()
Example #16
Source File: encoder.py From deen with Apache License 2.0 | 5 votes |
def clear_search_highlight(self, widget=None): """Reset any highlights set by the search function.""" widget = widget or self cursor = self.text_field.textCursor() cursor.select(QTextCursor.Document) char_format = QTextCharFormat() cursor.setCharFormat(char_format) widget.ui.search_area.clear() widget.ui.search_matches_label.setText('Matches: 0')
Example #17
Source File: hex.py From deen with Apache License 2.0 | 5 votes |
def selection_changed(self): # Check if items from the previous selection # are still selected. for sel in self.current_selection: _widget = self.item(sel[0], sel[1]) if not _widget.isSelected() and not _widget in self.selectedItems(): self.current_selection.remove(sel) ascii_widget = self.cellWidget(sel[0], self.columnCount() - 1) cursor = ascii_widget.textCursor() cursor.select(QTextCursor.Document) cursor.setCharFormat(QTextCharFormat()) cursor.clearSelection() ascii_widget.setTextCursor(cursor) for i in self.selectedItems(): if not (i.row(), i.column()) in self.current_selection: self.current_selection.append((i.row(), i.column())) ascii_widget = self.cellWidget(i.row(), self.columnCount() - 1) cursor = ascii_widget.textCursor() char_format = QTextCharFormat() cursor.select(QTextCursor.Document) cursor.mergeCharFormat(char_format) cursor.clearSelection() char_format.setBackground(QBrush(QColor('darkCyan'))) cursor.setPosition(i.column()) cursor.setPosition(i.column() + 1, QTextCursor.KeepAnchor) cursor.mergeCharFormat(char_format)
Example #18
Source File: stonix_log_viewer.py From stonix with GNU General Public License v2.0 | 5 votes |
def highlight_search_results(self): """ search through log output and highlight all matching search terms """ # get search field text and set up cursor searchterm = self.log_search_text.toPlainText() cursor = self.log_display_browser.textCursor() cursor.select(QtGui.QTextCursor.Document) cursor.setCharFormat(QtGui.QTextCharFormat()) # clear all highlighted items for each new search cursor.clearSelection() self.log_display_browser.moveCursor(QtGui.QTextCursor.End) # reset search results for each new search self.search_results = [] # search through log_display and highlight all matching search terms while self.log_display_browser.find(searchterm, QtGui.QTextDocument.FindBackward): result = self.log_display_browser.ExtraSelection() highlighter = QtGui.QColor(QtCore.Qt.yellow).lighter(160) result.format.setBackground(highlighter) result.format.setProperty(QtGui.QTextFormat.FullWidthSelection, QVariant(True)) result.cursor = self.log_display_browser.textCursor() result.cursor.clearSelection() self.search_results.append(result) self.log_display_browser.setExtraSelections(self.search_results) num_results = str(len(self.search_results)) self.log_search_results_label.setText("Search Results: " + num_results)
Example #19
Source File: syntax.py From hrdev with MIT License | 5 votes |
def highlightBlock(self, text): '''Highlight block.''' for pattern, hl_format in self._highlighting_rules: expression = QtCore.QRegExp(pattern) index = expression.indexIn(text) while index >= 0: length = expression.matchedLength() self.setFormat(index, length, hl_format) index = expression.indexIn(text, index + length) self.setCurrentBlockState(0) start_index = 0 if self.previousBlockState() != 1: start_index = self.comment_start_expression.indexIn(text) while start_index >= 0: end_index = self.comment_end_expression.indexIn(text, start_index) if end_index == -1: self.setCurrentBlockState(1) comment_length = text.length() - start_index else: comment_length = end_index - \ start_index + \ self.comment_end_expression.matchedLength() multi_line_comment_format = QtGui.QTextCharFormat() multiline_color = self.config_theme.get('tokens_highlight', 'quotation_color') multi_line_comment_format.setForeground(QtGui.QColor(multiline_color)) self.setFormat(start_index, comment_length, multi_line_comment_format) start_index = self.comment_start_expression.indexIn(text, start_index + comment_length) return
Example #20
Source File: cases.py From QualCoder with MIT License | 5 votes |
def unlight(self): """ Remove all text highlighting from current file. """ if self.selected_file is None: return if self.selected_file['fulltext'] is None: return cursor = self.ui.textBrowser.textCursor() try: cursor.setPosition(0, QtGui.QTextCursor.MoveAnchor) cursor.setPosition(len(self.selected_file['fulltext']) - 1, QtGui.QTextCursor.KeepAnchor) cursor.setCharFormat(QtGui.QTextCharFormat()) except Exception as e: logger.debug((str(e) + "\n unlight, text length" +str(len(self.textBrowser.toPlainText()))))
Example #21
Source File: code_text.py From QualCoder with MIT License | 5 votes |
def unlight(self): """ Remove all text highlighting from current file. """ if self.sourceText is None: return cursor = self.ui.textEdit.textCursor() cursor.setPosition(0, QtGui.QTextCursor.MoveAnchor) cursor.setPosition(len(self.sourceText) - 1, QtGui.QTextCursor.KeepAnchor) cursor.setCharFormat(QtGui.QTextCharFormat())
Example #22
Source File: code_text.py From QualCoder with MIT License | 5 votes |
def highlight(self): """ Apply text highlighting to current file. If no colour has been assigned to a code, those coded text fragments are coloured gray. Each code text item contains: fid, date, pos0, pos1, seltext, cid, status, memo, name, owner. """ if self.sourceText is not None: fmt = QtGui.QTextCharFormat() cursor = self.ui.textEdit.textCursor() # Add coding highlights codes = {x['cid']:x for x in self.codes} for item in self.code_text: cursor.setPosition(int(item['pos0']), QtGui.QTextCursor.MoveAnchor) cursor.setPosition(int(item['pos1']), QtGui.QTextCursor.KeepAnchor) color = codes.get(item['cid'],{}).get('color',"#F8E0E0") # default light red fmt.setBackground(QtGui.QBrush(QtGui.QColor(color))) # Highlight codes with memos - these are italicised if item['memo'] is not None and item['memo'] != "": fmt.setFontItalic(True) else: fmt.setFontItalic(False) fmt.setFontWeight(QtGui.QFont.Normal) cursor.setCharFormat(fmt) # Add annotation marks - these are in bold for note in self.annotations: if len(self.filename.keys()) > 0: # will be zero if using autocode and no file is loaded if note['fid'] == self.filename['id']: cursor.setPosition(int(note['pos0']), QtGui.QTextCursor.MoveAnchor) cursor.setPosition(int(note['pos1']), QtGui.QTextCursor.KeepAnchor) formatB = QtGui.QTextCharFormat() formatB.setFontWeight(QtGui.QFont.Bold) cursor.mergeCharFormat(formatB)
Example #23
Source File: sourcewindow.py From dcc with Apache License 2.0 | 5 votes |
def _get_format(self, token): """ Returns a QTextCharFormat for token or None. """ if token in self._formats: return self._formats[token] result = self._get_format_from_style(token, self._style) self._formats[token] = result return result
Example #24
Source File: syntax_highlighter.py From VUT-FIT-IFJ-2017-toolkit with GNU General Public License v3.0 | 5 votes |
def text_format(self) -> Union[QTextCharFormat, Sequence[QTextCharFormat], None]: return self._text_format
Example #25
Source File: annotations.py From Lector with GNU General Public License v3.0 | 5 votes |
def __init__(self): self.annotation_type = None self.annotation_components = None self.underline_styles = { 'Solid': QtGui.QTextCharFormat.SingleUnderline, 'Dashes': QtGui.QTextCharFormat.DashUnderline, 'Dots': QtGui.QTextCharFormat.DotLine, 'Wavy': QtGui.QTextCharFormat.WaveUnderline}
Example #26
Source File: contentwidgets.py From Lector with GNU General Public License v3.0 | 5 votes |
def clear_annotations(self): if not self.are_we_doing_images_only: cursor = self.pw.textCursor() cursor.setPosition(0) cursor.movePosition( QtGui.QTextCursor.End, QtGui.QTextCursor.KeepAnchor) previewCharFormat = QtGui.QTextCharFormat() previewCharFormat.setFontStyleStrategy( QtGui.QFont.PreferAntialias) cursor.setCharFormat(previewCharFormat) cursor.clearSelection() self.pw.setTextCursor(cursor)
Example #27
Source File: message_area.py From CHATIMUSMAXIMUS with GNU General Public License v3.0 | 5 votes |
def __init__(self, text_color=Qt.white, font=QtGui.QFont.DemiBold): super(_StandardTextFormat, self).__init__() self.setFontWeight(font) self.setForeground(text_color) self.setFontPointSize(13) self.setVerticalAlignment(QtGui.QTextCharFormat.AlignMiddle) # TODO: see `QTextEdit.setAlignment` for setting the time to the right
Example #28
Source File: QtGrapSyntax.py From grap with MIT License | 5 votes |
def format(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
Example #29
Source File: syntax_hl_log.py From execution-trace-viewer with MIT License | 5 votes |
def format(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 # Syntax styles
Example #30
Source File: texteditor.py From pychemqt with GNU General Public License v3.0 | 5 votes |
def font(self, family): format = QtGui.QTextCharFormat() format.setFontFamily(family) self.MergeFormat(format)