本文整理汇总了Python中calibre.gui2.tweak_book.editor.syntax.base.SyntaxHighlighter.setDocument方法的典型用法代码示例。如果您正苦于以下问题:Python SyntaxHighlighter.setDocument方法的具体用法?Python SyntaxHighlighter.setDocument怎么用?Python SyntaxHighlighter.setDocument使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类calibre.gui2.tweak_book.editor.syntax.base.SyntaxHighlighter
的用法示例。
在下文中一共展示了SyntaxHighlighter.setDocument方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TextEdit
# 需要导入模块: from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.syntax.base.SyntaxHighlighter import setDocument [as 别名]
class TextEdit(QPlainTextEdit):
def __init__(self, parent=None):
QPlainTextEdit.__init__(self, parent)
self.highlighter = SyntaxHighlighter(self)
self.apply_settings()
self.setMouseTracking(True)
self.cursorPositionChanged.connect(self.highlight_cursor_line)
self.blockCountChanged[int].connect(self.update_line_number_area_width)
self.updateRequest.connect(self.update_line_number_area)
self.line_number_area = LineNumbers(self)
@dynamic_property
def is_modified(self):
''' True if the document has been modified since it was loaded or since
the last time is_modified was set to False. '''
def fget(self):
return self.document().isModified()
def fset(self, val):
self.document().setModified(bool(val))
return property(fget=fget, fset=fset)
def sizeHint(self):
return self.size_hint
def apply_settings(self, prefs=None): # {{{
prefs = prefs or tprefs
self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.NoWrap)
theme = THEMES.get(prefs['editor_theme'], None)
if theme is None:
theme = THEMES[default_theme()]
self.apply_theme(theme)
def apply_theme(self, theme):
self.theme = theme
pal = self.palette()
pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg'))
pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg'))
pal.setColor(pal.Text, theme_color(theme, 'Normal', 'fg'))
pal.setColor(pal.Highlight, theme_color(theme, 'Visual', 'bg'))
pal.setColor(pal.HighlightedText, theme_color(theme, 'Visual', 'fg'))
self.setPalette(pal)
self.tooltip_palette = pal = QPalette()
pal.setColor(pal.ToolTipBase, theme_color(theme, 'Tooltip', 'bg'))
pal.setColor(pal.ToolTipText, theme_color(theme, 'Tooltip', 'fg'))
self.line_number_palette = pal = QPalette()
pal.setColor(pal.Base, theme_color(theme, 'LineNr', 'bg'))
pal.setColor(pal.Text, theme_color(theme, 'LineNr', 'fg'))
pal.setColor(pal.BrightText, theme_color(theme, 'LineNrC', 'fg'))
font = self.font()
ff = tprefs['editor_font_family']
if ff is None:
ff = default_font_family()
font.setFamily(ff)
font.setPointSize(tprefs['editor_font_size'])
self.tooltip_font = QFont(font)
self.tooltip_font.setPointSize(font.pointSize() - 1)
self.setFont(font)
self.highlighter.apply_theme(theme)
w = self.fontMetrics()
self.number_width = max(map(lambda x:w.width(str(x)), xrange(10)))
self.size_hint = QSize(100 * w.averageCharWidth(), 50 * w.height())
# }}}
def load_text(self, text, syntax='html'):
self.highlighter = {'html':HTMLHighlighter, 'css':CSSHighlighter, 'xml':XMLHighlighter}.get(syntax, SyntaxHighlighter)(self)
self.highlighter.apply_theme(self.theme)
self.highlighter.setDocument(self.document())
self.setPlainText(text)
def replace_text(self, text):
c = self.textCursor()
pos = c.position()
c.beginEditBlock()
c.clearSelection()
c.select(c.Document)
c.insertText(text)
c.endEditBlock()
c.setPosition(min(pos, len(text)))
self.setTextCursor(c)
self.ensureCursorVisible()
# Line numbers and cursor line {{{
def highlight_cursor_line(self):
sel = QTextEdit.ExtraSelection()
sel.format.setBackground(self.palette().alternateBase())
sel.format.setProperty(QTextFormat.FullWidthSelection, True)
sel.cursor = self.textCursor()
sel.cursor.clearSelection()
self.setExtraSelections([sel])
# Update the cursor line's line number in the line number area
try:
self.line_number_area.update(0, self.last_current_lnum[0], self.line_number_area.width(), self.last_current_lnum[1])
except AttributeError:
pass
block = self.textCursor().block()
top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
height = int(self.blockBoundingRect(block).height())
self.line_number_area.update(0, top, self.line_number_area.width(), height)
#.........这里部分代码省略.........
示例2: TextEdit
# 需要导入模块: from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.syntax.base.SyntaxHighlighter import setDocument [as 别名]
class TextEdit(PlainTextEdit):
def __init__(self, parent=None):
PlainTextEdit.__init__(self, parent)
self.saved_matches = {}
self.smarts = NullSmarts(self)
self.current_cursor_line = None
self.current_search_mark = None
self.highlighter = SyntaxHighlighter(self)
self.line_number_area = LineNumbers(self)
self.apply_settings()
self.setMouseTracking(True)
self.cursorPositionChanged.connect(self.highlight_cursor_line)
self.blockCountChanged[int].connect(self.update_line_number_area_width)
self.updateRequest.connect(self.update_line_number_area)
self.syntax = None
@dynamic_property
def is_modified(self):
''' True if the document has been modified since it was loaded or since
the last time is_modified was set to False. '''
def fget(self):
return self.document().isModified()
def fset(self, val):
self.document().setModified(bool(val))
return property(fget=fget, fset=fset)
def sizeHint(self):
return self.size_hint
def apply_settings(self, prefs=None): # {{{
prefs = prefs or tprefs
self.setLineWrapMode(QPlainTextEdit.WidgetWidth if prefs['editor_line_wrap'] else QPlainTextEdit.NoWrap)
theme = THEMES.get(prefs['editor_theme'], None)
if theme is None:
theme = THEMES[default_theme()]
self.apply_theme(theme)
w = self.fontMetrics()
self.space_width = w.width(' ')
self.setTabStopWidth(prefs['editor_tab_stop_width'] * self.space_width)
def apply_theme(self, theme):
self.theme = theme
pal = self.palette()
pal.setColor(pal.Base, theme_color(theme, 'Normal', 'bg'))
pal.setColor(pal.AlternateBase, theme_color(theme, 'CursorLine', 'bg'))
pal.setColor(pal.Text, theme_color(theme, 'Normal', 'fg'))
pal.setColor(pal.Highlight, theme_color(theme, 'Visual', 'bg'))
pal.setColor(pal.HighlightedText, theme_color(theme, 'Visual', 'fg'))
self.setPalette(pal)
self.tooltip_palette = pal = QPalette()
pal.setColor(pal.ToolTipBase, theme_color(theme, 'Tooltip', 'bg'))
pal.setColor(pal.ToolTipText, theme_color(theme, 'Tooltip', 'fg'))
self.line_number_palette = pal = QPalette()
pal.setColor(pal.Base, theme_color(theme, 'LineNr', 'bg'))
pal.setColor(pal.Text, theme_color(theme, 'LineNr', 'fg'))
pal.setColor(pal.BrightText, theme_color(theme, 'LineNrC', 'fg'))
self.match_paren_format = theme_format(theme, 'MatchParen')
font = self.font()
ff = tprefs['editor_font_family']
if ff is None:
ff = default_font_family()
font.setFamily(ff)
font.setPointSize(tprefs['editor_font_size'])
self.tooltip_font = QFont(font)
self.tooltip_font.setPointSize(font.pointSize() - 1)
self.setFont(font)
self.highlighter.apply_theme(theme)
w = self.fontMetrics()
self.number_width = max(map(lambda x:w.width(str(x)), xrange(10)))
self.size_hint = QSize(100 * w.averageCharWidth(), 50 * w.height())
self.highlight_color = theme_color(theme, 'HighlightRegion', 'bg')
self.highlight_cursor_line()
# }}}
def load_text(self, text, syntax='html', process_template=False):
self.syntax = syntax
self.highlighter = get_highlighter(syntax)(self)
self.highlighter.apply_theme(self.theme)
self.highlighter.setDocument(self.document())
sclass = {'html':HTMLSmarts, 'xml':HTMLSmarts}.get(syntax, None)
if sclass is not None:
self.smarts = sclass(self)
self.setPlainText(unicodedata.normalize('NFC', text))
if process_template and QPlainTextEdit.find(self, '%CURSOR%'):
c = self.textCursor()
c.insertText('')
def replace_text(self, text):
c = self.textCursor()
pos = c.position()
c.beginEditBlock()
c.clearSelection()
c.select(c.Document)
c.insertText(unicodedata.normalize('NFC', text))
c.endEditBlock()
c.setPosition(min(pos, len(text)))
self.setTextCursor(c)
self.ensureCursorVisible()
#.........这里部分代码省略.........
示例3: TextEdit
# 需要导入模块: from calibre.gui2.tweak_book.editor.syntax.base import SyntaxHighlighter [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.syntax.base.SyntaxHighlighter import setDocument [as 别名]
#.........这里部分代码省略.........
c = self.textCursor()
c.clearSelection()
c.movePosition(c.Start)
c.movePosition(c.End, c.KeepAnchor)
return c.selectedText().replace(PARAGRAPH_SEPARATOR, '\n')
@pyqtSlot()
def copy(self):
# Workaround Qt replacing nbsp with normal spaces on copy
c = self.textCursor()
if not c.hasSelection():
return
md = QMimeData()
md.setText(self.selected_text)
QApplication.clipboard().setMimeData(md)
@pyqtSlot()
def cut(self):
# Workaround Qt replacing nbsp with normal spaces on copy
self.copy()
self.textCursor().removeSelectedText()
def selection_changed(self):
# Workaround Qt replacing nbsp with normal spaces on copy
clipboard = QApplication.clipboard()
if clipboard.supportsSelection() and self.textCursor().hasSelection():
md = QMimeData()
md.setText(self.selected_text)
clipboard.setMimeData(md, clipboard.Selection)
def load_text(self, text, syntax='html', process_template=False):
self.highlighter = {'html':HTMLHighlighter, 'css':CSSHighlighter, 'xml':XMLHighlighter}.get(syntax, SyntaxHighlighter)(self)
self.highlighter.apply_theme(self.theme)
self.highlighter.setDocument(self.document())
self.setPlainText(unicodedata.normalize('NFC', text))
if process_template and QPlainTextEdit.find(self, '%CURSOR%'):
c = self.textCursor()
c.insertText('')
def replace_text(self, text):
c = self.textCursor()
pos = c.position()
c.beginEditBlock()
c.clearSelection()
c.select(c.Document)
c.insertText(unicodedata.normalize('NFC', text))
c.endEditBlock()
c.setPosition(min(pos, len(text)))
self.setTextCursor(c)
self.ensureCursorVisible()
def go_to_line(self, lnum, col=None):
lnum = max(1, min(self.blockCount(), lnum))
c = self.textCursor()
c.clearSelection()
c.movePosition(c.Start)
c.movePosition(c.NextBlock, n=lnum - 1)
c.movePosition(c.StartOfLine)
c.movePosition(c.EndOfLine, c.KeepAnchor)
text = unicode(c.selectedText())
if col is None:
c.movePosition(c.StartOfLine)
lt = text.lstrip()
if text and lt and lt != text:
c.movePosition(c.NextWord)
else: