本文整理汇总了Python中calibre.gui2.tweak_book.editor.text.TextEdit.setPlainText方法的典型用法代码示例。如果您正苦于以下问题:Python TextEdit.setPlainText方法的具体用法?Python TextEdit.setPlainText怎么用?Python TextEdit.setPlainText使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类calibre.gui2.tweak_book.editor.text.TextEdit
的用法示例。
在下文中一共展示了TextEdit.setPlainText方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Editor
# 需要导入模块: from calibre.gui2.tweak_book.editor.text import TextEdit [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.text.TextEdit import setPlainText [as 别名]
class Editor(QMainWindow):
modification_state_changed = pyqtSignal(object)
undo_redo_state_changed = pyqtSignal(object, object)
copy_available_state_changed = pyqtSignal(object)
data_changed = pyqtSignal(object)
def __init__(self, syntax, parent=None):
QMainWindow.__init__(self, parent)
if parent is None:
self.setWindowFlags(Qt.Widget)
self.syntax = syntax
self.editor = TextEdit(self)
self.setCentralWidget(self.editor)
self.editor.modificationChanged.connect(self.modification_state_changed.emit)
self.create_toolbars()
self.undo_available = False
self.redo_available = False
self.copy_available = self.cut_available = False
self.editor.undoAvailable.connect(self._undo_available)
self.editor.redoAvailable.connect(self._redo_available)
self.editor.textChanged.connect(self._data_changed)
self.editor.copyAvailable.connect(self._copy_available)
@dynamic_property
def data(self):
def fget(self):
ans = unicode(self.editor.toPlainText())
if self.syntax == 'html':
ans = xml_replace_entities(ans)
return ans.encode('utf-8')
def fset(self, val):
self.editor.load_text(val, syntax=self.syntax)
return property(fget=fget, fset=fset)
def get_raw_data(self):
return unicode(self.editor.toPlainText())
def replace_data(self, raw, only_if_different=True):
if isinstance(raw, bytes):
raw = raw.decode('utf-8')
current = self.get_raw_data() if only_if_different else False
if current != raw:
self.editor.replace_text(raw)
def set_focus(self):
self.editor.setFocus(Qt.OtherFocusReason)
def undo(self):
self.editor.undo()
def redo(self):
self.editor.redo()
@dynamic_property
def is_modified(self):
def fget(self):
return self.editor.is_modified
def fset(self, val):
self.editor.is_modified = val
return property(fget=fget, fset=fset)
def create_toolbars(self):
self.action_bar = b = self.addToolBar(_('File actions tool bar'))
b.setObjectName('action_bar') # Needed for saveState
for x in ('save', 'undo', 'redo'):
try:
b.addAction(actions['editor-%s' % x])
except KeyError:
pass
self.edit_bar = b = self.addToolBar(_('Edit actions tool bar'))
for x in ('cut', 'copy', 'paste'):
try:
b.addAction(actions['editor-%s' % x])
except KeyError:
pass
def break_cycles(self):
self.modification_state_changed.disconnect()
self.undo_redo_state_changed.disconnect()
self.copy_available_state_changed.disconnect()
self.data_changed.disconnect()
self.editor.undoAvailable.disconnect()
self.editor.redoAvailable.disconnect()
self.editor.modificationChanged.disconnect()
self.editor.textChanged.disconnect()
self.editor.copyAvailable.disconnect()
self.editor.setPlainText('')
def _data_changed(self):
self.data_changed.emit(self)
def _undo_available(self, available):
self.undo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _redo_available(self, available):
self.redo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
#.........这里部分代码省略.........
示例2: Editor
# 需要导入模块: from calibre.gui2.tweak_book.editor.text import TextEdit [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.text.TextEdit import setPlainText [as 别名]
#.........这里部分代码省略.........
for name in tuple('h%d' % d for d in range(1, 7)) + ('p',):
m.addAction(actions['rename-block-tag-%s' % name])
for name in tprefs.get('editor_common_toolbar', ()):
add_action(name, self.action_bar)
for name in tprefs.get('editor_%s_toolbar' % self.syntax, ()):
add_action(name, self.tools_bar)
if self.syntax == 'html':
self.format_bar.clear()
for name in tprefs['editor_format_toolbar']:
add_action(name, self.format_bar)
self.restore_state()
def break_cycles(self):
for x in ('modification_state_changed', 'word_ignored', 'link_clicked', 'smart_highlighting_updated'):
try:
getattr(self, x).disconnect()
except TypeError:
pass # in case this signal was never connected
self.undo_redo_state_changed.disconnect()
self.copy_available_state_changed.disconnect()
self.cursor_position_changed.disconnect()
self.data_changed.disconnect()
self.editor.undoAvailable.disconnect()
self.editor.redoAvailable.disconnect()
self.editor.modificationChanged.disconnect()
self.editor.textChanged.disconnect()
self.editor.copyAvailable.disconnect()
self.editor.cursorPositionChanged.disconnect()
self.editor.link_clicked.disconnect()
self.editor.smart_highlighting_updated.disconnect()
self.editor.setPlainText('')
self.editor.smarts = None
self.editor.request_completion = None
def _modification_state_changed(self):
self.is_synced_to_container = self.is_modified
self.modification_state_changed.emit(self.is_modified)
def _data_changed(self):
self.is_synced_to_container = False
self.data_changed.emit(self)
def _undo_available(self, available):
self.undo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _redo_available(self, available):
self.redo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _copy_available(self, available):
self.copy_available = self.cut_available = available
self.copy_available_state_changed.emit(available)
def _cursor_position_changed(self, *args):
self.cursor_position_changed.emit()
@property
def cursor_position(self):
c = self.editor.textCursor()
char = ''
col = c.positionInBlock()
if not c.atStart():
示例3: Editor
# 需要导入模块: from calibre.gui2.tweak_book.editor.text import TextEdit [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.text.TextEdit import setPlainText [as 别名]
#.........这里部分代码省略.........
self.edit_bar = b = self.addToolBar(_('Edit actions tool bar'))
for x in ('cut', 'copy', 'paste'):
b.addAction(actions['editor-%s' % x])
self.tools_bar = b = self.addToolBar(_('Editor tools'))
if self.syntax == 'html':
b.addAction(actions['fix-html-current'])
if self.syntax in {'xml', 'html', 'css'}:
b.addAction(actions['pretty-current'])
if self.syntax in {'html', 'css'}:
b.addAction(actions['insert-image'])
if self.syntax == 'html':
self.format_bar = b = self.addToolBar(_('Format text'))
for x in ('bold', 'italic', 'underline', 'strikethrough', 'subscript', 'superscript', 'color', 'background-color'):
b.addAction(actions['format-text-%s' % x])
ac = b.addAction(QIcon(I('format-text-heading.png')), _('Change paragraph to heading'))
m = QMenu()
ac.setMenu(m)
b.widgetForAction(ac).setPopupMode(QToolButton.InstantPopup)
for name in tuple('h%d' % d for d in range(1, 7)) + ('p',):
m.addAction(actions['rename-block-tag-%s' % name])
def break_cycles(self):
self.modification_state_changed.disconnect()
self.undo_redo_state_changed.disconnect()
self.copy_available_state_changed.disconnect()
self.cursor_position_changed.disconnect()
self.data_changed.disconnect()
self.editor.undoAvailable.disconnect()
self.editor.redoAvailable.disconnect()
self.editor.modificationChanged.disconnect()
self.editor.textChanged.disconnect()
self.editor.copyAvailable.disconnect()
self.editor.cursorPositionChanged.disconnect()
self.editor.setPlainText('')
self.editor.smarts = None
def _modification_state_changed(self):
self.is_synced_to_container = self.is_modified
self.modification_state_changed.emit(self.is_modified)
def _data_changed(self):
self.is_synced_to_container = False
self.data_changed.emit(self)
def _undo_available(self, available):
self.undo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _redo_available(self, available):
self.redo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _copy_available(self, available):
self.copy_available = self.cut_available = available
self.copy_available_state_changed.emit(available)
def _cursor_position_changed(self, *args):
self.cursor_position_changed.emit()
@property
def cursor_position(self):
c = self.editor.textCursor()
char = ''
if not c.atStart():
c.clearSelection()
c.setPosition(c.position()-1, c.KeepAnchor)
示例4: FunctionEditor
# 需要导入模块: from calibre.gui2.tweak_book.editor.text import TextEdit [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.text.TextEdit import setPlainText [as 别名]
class FunctionEditor(Dialog):
def __init__(self, func_name='', parent=None):
self._func_name = func_name
Dialog.__init__(self, _('Create/edit a function'), 'edit-sr-func', parent=parent)
def setup_ui(self):
self.l = l = QVBoxLayout(self)
self.h = h = QHBoxLayout()
l.addLayout(h)
self.la1 = la = QLabel(_('F&unction name:'))
h.addWidget(la)
self.fb = fb = FunctionBox(self)
la.setBuddy(fb)
h.addWidget(fb, stretch=10)
self.la3 = la = QLabel(_('&Code:'))
self.source_code = TextEdit(self)
self.source_code.load_text('', 'python')
la.setBuddy(self.source_code)
l.addWidget(la), l.addWidget(self.source_code)
if self._func_name:
self.fb.setText(self._func_name)
func = functions().get(self._func_name)
if func is not None:
self.source_code.setPlainText(func.source or ('\n' + EMPTY_FUNC))
else:
self.source_code.setPlainText('\n' + EMPTY_FUNC)
self.la2 = la = QLabel(_(
'For help with creating functions, see the <a href="%s">User Manual</a>') %
localize_user_manual_link('https://manual.calibre-ebook.com/function_mode.html'))
la.setOpenExternalLinks(True)
l.addWidget(la)
l.addWidget(self.bb)
def sizeHint(self):
fm = QFontMetrics(self.font())
return QSize(fm.averageCharWidth() * 120, 600)
@property
def func_name(self):
return self.fb.text().strip()
@property
def source(self):
return self.source_code.toPlainText()
def accept(self):
if not self.func_name:
return error_dialog(self, _('Must specify name'), _(
'You must specify a name for this function.'), show=True)
source = self.source
try:
mod = compile_code(source, self.func_name)
except Exception as err:
return error_dialog(self, _('Invalid python code'), _(
'The code you created is not valid python code, with error: %s') % err, show=True)
if not callable(mod.get('replace')):
return error_dialog(self, _('No replace function'), _(
'You must create a python function named replace in your code'), show=True)
user_functions[self.func_name] = source
functions(refresh=True)
refresh_boxes()
Dialog.accept(self)
示例5: Editor
# 需要导入模块: from calibre.gui2.tweak_book.editor.text import TextEdit [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.text.TextEdit import setPlainText [as 别名]
#.........这里部分代码省略.........
@property
def has_marked_text(self):
return self.editor.current_search_mark is not None
@dynamic_property
def is_modified(self):
def fget(self):
return self.editor.is_modified
def fset(self, val):
self.editor.is_modified = val
return property(fget=fget, fset=fset)
def create_toolbars(self):
self.action_bar = b = self.addToolBar(_('File actions tool bar'))
b.setObjectName('action_bar') # Needed for saveState
for x in ('undo', 'redo'):
b.addAction(actions['editor-%s' % x])
self.edit_bar = b = self.addToolBar(_('Edit actions tool bar'))
for x in ('cut', 'copy', 'paste'):
b.addAction(actions['editor-%s' % x])
self.tools_bar = b = self.addToolBar(_('Editor tools'))
if self.syntax == 'html':
b.addAction(actions['fix-html-current'])
if self.syntax in {'xml', 'html', 'css'}:
b.addAction(actions['pretty-current'])
def break_cycles(self):
self.modification_state_changed.disconnect()
self.undo_redo_state_changed.disconnect()
self.copy_available_state_changed.disconnect()
self.cursor_position_changed.disconnect()
self.data_changed.disconnect()
self.editor.undoAvailable.disconnect()
self.editor.redoAvailable.disconnect()
self.editor.modificationChanged.disconnect()
self.editor.textChanged.disconnect()
self.editor.copyAvailable.disconnect()
self.editor.cursorPositionChanged.disconnect()
self.editor.setPlainText('')
def _modification_state_changed(self):
self.is_synced_to_container = self.is_modified
self.modification_state_changed.emit(self.is_modified)
def _data_changed(self):
self.is_synced_to_container = False
self.data_changed.emit(self)
def _undo_available(self, available):
self.undo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _redo_available(self, available):
self.redo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _copy_available(self, available):
self.copy_available = self.cut_available = available
self.copy_available_state_changed.emit(available)
def _cursor_position_changed(self, *args):
self.cursor_position_changed.emit()
@property
def cursor_position(self):
c = self.editor.textCursor()
return (c.blockNumber() + 1, c.positionInBlock())
def cut(self):
self.editor.cut()
def copy(self):
self.editor.copy()
def go_to_line(self, line, col=None):
self.editor.go_to_line(line, col=col)
def paste(self):
if not self.editor.canPaste():
return error_dialog(self, _('No text'), _(
'There is no suitable text in the clipboard to paste.'), show=True)
self.editor.paste()
def contextMenuEvent(self, ev):
ev.ignore()
def fix_html(self):
if self.syntax == 'html':
from calibre.ebooks.oeb.polish.pretty import fix_html
self.editor.replace_text(fix_html(current_container(), unicode(self.editor.toPlainText())).decode('utf-8'))
return True
return False
def pretty_print(self, name):
from calibre.ebooks.oeb.polish.pretty import pretty_html, pretty_css, pretty_xml
if self.syntax in {'css', 'html', 'xml'}:
func = {'css':pretty_css, 'xml':pretty_xml}.get(self.syntax, pretty_html)
self.editor.replace_text(func(current_container(), name, unicode(self.editor.toPlainText())).decode('utf-8'))
return True
return False
示例6: Editor
# 需要导入模块: from calibre.gui2.tweak_book.editor.text import TextEdit [as 别名]
# 或者: from calibre.gui2.tweak_book.editor.text.TextEdit import setPlainText [as 别名]
#.........这里部分代码省略.........
m = ac.m = QMenu()
ac.setMenu(m)
bar.widgetForAction(ac).setPopupMode(QToolButton.InstantPopup)
for name in tuple('h%d' % d for d in range(1, 7)) + ('p',):
m.addAction(actions['rename-block-tag-%s' % name])
for name in tprefs.get('editor_%s_toolbar' % self.syntax, ()):
add_action(name, self.tools_bar)
if self.syntax == 'html':
self.format_bar.clear()
for name in tprefs['editor_format_toolbar']:
add_action(name, self.format_bar)
def break_cycles(self):
try:
self.modification_state_changed.disconnect()
except TypeError:
pass # in case this signal was never connected
try:
self.word_ignored.disconnect()
except TypeError:
pass # in case this signal was never connected
self.undo_redo_state_changed.disconnect()
self.copy_available_state_changed.disconnect()
self.cursor_position_changed.disconnect()
self.data_changed.disconnect()
self.editor.undoAvailable.disconnect()
self.editor.redoAvailable.disconnect()
self.editor.modificationChanged.disconnect()
self.editor.textChanged.disconnect()
self.editor.copyAvailable.disconnect()
self.editor.cursorPositionChanged.disconnect()
self.editor.setPlainText('')
self.editor.smarts = None
def _modification_state_changed(self):
self.is_synced_to_container = self.is_modified
self.modification_state_changed.emit(self.is_modified)
def _data_changed(self):
self.is_synced_to_container = False
self.data_changed.emit(self)
def _undo_available(self, available):
self.undo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _redo_available(self, available):
self.redo_available = available
self.undo_redo_state_changed.emit(self.undo_available, self.redo_available)
def _copy_available(self, available):
self.copy_available = self.cut_available = available
self.copy_available_state_changed.emit(available)
def _cursor_position_changed(self, *args):
self.cursor_position_changed.emit()
@property
def cursor_position(self):
c = self.editor.textCursor()
char = ''
col = c.positionInBlock()
if not c.atStart():
c.clearSelection()