def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() if style.objectName() == 'oxygen': painter.translate(textRect.topLeft() + QPoint(5, -9)) else: painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options) painter.save() # Adjustments for the file switcher if hasattr(options.widget, 'files_list'): if style.objectName() in ['oxygen', 'qtcurve', 'breeze']: if options.widget.files_list: painter.translate(textRect.topLeft() + QPoint(4, -9)) else: painter.translate(textRect.topLeft()) else: if options.widget.files_list: painter.translate(textRect.topLeft() + QPoint(4, 4)) else: painter.translate(textRect.topLeft() + QPoint(2, 4)) else: painter.translate(textRect.topLeft() + QPoint(0, -3)) doc.documentLayout().draw(painter, ctx) painter.restore()
def test_HtmlSH_unclosed_commend(): txt = '-->' doc = QTextDocument(txt) sh = HtmlSH(doc, color_scheme='Spyder') sh.rehighlightBlock(doc.firstBlock()) res = [(0, 3, 'normal')] compare_formats(doc.firstBlock().layout().additionalFormats(), res, sh)
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) doc = QTextDocument() text = options.text doc.setHtml(text) doc.setDocumentMargin(0) # This needs to be an empty string to avoid the overlapping the # normal text of the QTreeWidgetItem options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None) painter.save() painter.translate(textRect.topLeft()) painter.setClipRect(textRect.translated(-textRect.topLeft())) doc.documentLayout().draw(painter, ctx) painter.restore()
def sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(options.text) return QSize(doc.idealWidth(), doc.size().height() - 2)
def sizeHint(self, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setHtml(options.text) doc.setTextWidth(options.rect.width()) size = QSize(int(doc.idealWidth()), int(doc.size().height())) return size
def _get_height(self): """ Return the expected height of this item's text, including the text margins. """ doc = QTextDocument() doc.setHtml('<hr>') doc.setDocumentMargin(self._PADDING) return doc.size().height()
def _prepare_text_document(self, option, index): # This logic must be shared between paint and sizeHint for consistency options = QStyleOptionViewItem(option) self.initStyleOption(options, index) doc = QTextDocument() doc.setDocumentMargin(self._margin) doc.setHtml(options.text) return options, doc
def _get_height(self): """ Return the expected height of this item's text, including the text margins. """ doc = QTextDocument() try: doc.setHtml('<span style="font-size:{}pt">Title</span>' .format(self._styles['title_font_size'])) except KeyError: doc.setHtml('<span>Title</span>') doc.setDocumentMargin(self._PADDING) return doc.size().height()
def get_text_width(text) -> int: """Return the width required to render ``text`` (including rich text elements).""" if qtpy.PYSIDE2: from qtpy.QtGui import Qt as _Qt else: from qtpy.QtCore import Qt as _Qt if _Qt.mightBeRichText(text): doc = QTextDocument() doc.setHtml(text) return doc.size().width() else: fm = QFontMetrics(QFont("", 0)) return fm.boundingRect(text).width() + 5
def get_legend_items(self): plot = self.plot() if plot is None: return [] text_items = [] for item in plot.get_items(): if not isinstance(item, CurveItem) or not self.include_item(item): continue text = QTextDocument() text.setDefaultFont(self.font) text.setDefaultStyleSheet("div { color: %s; }" % self.color) text.setHtml("<div>%s</div>" % item.curveparam.label) text_items.append((text, item.pen(), item.brush(), item.symbol())) return text_items
def setPlainText(self, p_str): # FixMe: Keep a reference to old QTextDocuments form previously loaded # files. This is needed to prevent garbage collection which results in a # seg fault if the document is discarded while still being highlighted. self.old_docs.append(self.document()) doc = QTextDocument() doc.setDocumentLayout(QPlainTextDocumentLayout(doc)) doc.setPlainText(p_str) self.setDocument(doc) self.margin.updateWidth() # start syntax heightening self.gCodeHighlighter = GcodeSyntaxHighlighter(self)
def test_HtmlSH_basic(): txt = '<p style="color:red;">Foo <!--comment--> bar.</p>' doc = QTextDocument(txt) sh = HtmlSH(doc, color_scheme='Spyder') sh.rehighlightBlock(doc.firstBlock()) # Expected result as list of tuples (begin, length, format) res = [(0, 2, 'builtin'), # |<p| (2, 6, 'keyword'), # | style| (8, 1, 'normal'), # | | (9, 12, 'string'), # |"color:red;"| (21, 1, 'builtin'), # |>| (22, 4, 'normal'), # |Foo | (26, 14, 'comment'), # |<!--comment-->| (40, 5, 'normal'), # | bar.| (45, 4, 'builtin')] # |</p>| compare_formats(doc.firstBlock().layout().additionalFormats(), res, sh)
def paint(self, painter, option, index): options = QStyleOptionViewItem(option) self.initStyleOption(options, index) style = (QApplication.style() if options.widget is None else options.widget.style()) # Set background color for selected and hovered items. # Inspired by: # - https://stackoverflow.com/a/43253004/438386 # - https://stackoverflow.com/a/27274233/438386 # This is commented for now until we find a way to correctly colorize # the entire line with a single color. # if options.state & QStyle.State_Selected: # # This only applies when the selected item doesn't have focus # if not (options.state & QStyle.State_HasFocus): # options.palette.setBrush( # QPalette.Highlight, # QBrush(self._background_color) # ) if options.state & QStyle.State_MouseOver: painter.fillRect(option.rect, self._background_color) # Set text doc = QTextDocument() text = options.text doc.setHtml(text) doc.setDocumentMargin(0) # This needs to be an empty string to avoid overlapping the # normal text of the QTreeWidgetItem options.text = "" style.drawControl(QStyle.CE_ItemViewItem, options, painter) ctx = QAbstractTextDocumentLayout.PaintContext() textRect = style.subElementRect(QStyle.SE_ItemViewItemText, options, None) painter.save() painter.translate(textRect.topLeft() + QPoint(0, 4)) doc.documentLayout().draw(painter, ctx) painter.restore()
class TestFoldScopeHelper(object): test_case = """# -*- coding: utf-8 -*- def my_add(): a = 1 b = 2 return a + b """ doc = QTextDocument(test_case) sh = PythonSH(doc, color_scheme='Spyder') sh.fold_detector = IndentFoldDetector() sh.rehighlightBlock(doc.firstBlock()) block = doc.firstBlock() block = block.next() TextBlockHelper.set_fold_trigger(block, True) fold_scope = FoldScope(block) oed = block.userData().oedata def test_fold_scope_helper(self): fsh = cfd.FoldScopeHelper(None, None) assert isinstance(fsh, cfd.FoldScopeHelper) def test_fold_scope_helper_str(self): fsh = cfd.FoldScopeHelper(self.fold_scope, self.oed) assert "my_add" in str(fsh) def test_fold_scope_helper_str_with_parents(self): fsh = cfd.FoldScopeHelper(self.fold_scope, self.oed) fsh.parents = ["fake parent list!"] assert "parents:" in str(fsh) def test_fold_scope_helper_repr(self): fsh = cfd.FoldScopeHelper(self.fold_scope, self.oed) assert "(at 0x" in repr(fsh) def test_fold_scope_helper_properties(self): fsh = cfd.FoldScopeHelper(self.fold_scope, self.oed) assert fsh.range == (1, 4) assert fsh.start_line == 1 assert fsh.end_line == 4 assert fsh.name == "my_add" assert fsh.line == 1 assert fsh.def_type == OED.FUNCTION_TOKEN
def test_Markdown_basic(): txt = "Some __random__ **text** with ~~different~~ [styles](link_url)" doc = QTextDocument(txt) sh = MarkdownSH(doc, color_scheme='Spyder') sh.rehighlightBlock(doc.firstBlock()) res = [(0, 5, 'normal'), # |Some| (5, 10, 'italic'), # |__random__| (15, 1, 'normal'), # | | (16, 8, 'strong'), # |**text**| (24, 6, 'normal'), # |with| (30, 13, 'italic'), # |~~diferents~~| (43, 1, 'normal'), # | | (44, 8, 'string'), # |[styles]| (52, 1, 'normal'), # |(| (53, 8, 'string'), # |(link_url)| (61, 1, 'normal'), # || ] compare_formats(doc.firstBlock().layout().additionalFormats(), res, sh)
def test_python_string_prefix(): if PY3: prefixes = ("r", "u", "R", "U", "f", "F", "fr", "Fr", "fR", "FR", "rf", "rF", "Rf", "RF", "b", "B", "br", "Br", "bR", "BR", "rb", "rB", "Rb", "RB") else: prefixes = ("r", "u", "ur", "R", "U", "UR", "Ur", "uR", "b", "B", "br", "Br", "bR", "BR") for prefix in prefixes: txt = "[%s'test', %s'''test''']" % (prefix, prefix) doc = QTextDocument(txt) sh = PythonSH(doc, color_scheme='Spyder') sh.rehighlightBlock(doc.firstBlock()) offset = len(prefix) res = [(0, 1, 'normal'), # |[| (1, 6 + offset, 'string'), # |{prefix}'test'| (7 + offset, 2, 'normal'), # |, | (9 + offset, 10 + offset, 'string'), # |{prefix}'''test'''| (19 + 2*offset, 1, 'normal')] # | | compare_formats(doc.firstBlock().layout().additionalFormats(), res, sh)
def __init__(self, *args, **kwargs): """Frame used in css styling with fade in and fade out effect.""" super(FrameContentHover, self).__init__(*args, **kwargs) self.current_opacity = 0 self.max_frame = 100 self.max_opacity = 0.95 self.button_text = None self.label_icon = None self.label_text = None self.summary = '' # Widgets self.text_document = QTextDocument(self) self.timeline = QTimeLine(500) font = QFont() if sys.platform == 'darwin': font.setPointSize(12) elif os.name == 'nt': font.setPointSize(9) else: font.setPointSize(9) self.text_document.setDefaultFont(font) self.text_document.setMaximumBlockCount(5) self.text_document.setDocumentMargin(10) # Setup self.setWindowFlags(Qt.FramelessWindowHint) self.setAutoFillBackground(True) self.setWindowFlags(Qt.FramelessWindowHint) self.setMinimumSize(self.widget_size()) self.timeline.setFrameRange(0, self.max_frame) # Signals self.timeline.frameChanged.connect(self.set_opacity)
def __init__(self, text=None, labelparam=None): self.text_string = "" if text is None else text self.text = QTextDocument() super(LabelItem, self).__init__(labelparam)