本文整理汇总了Python中PyQt5.QtGui.QTextCursor.position方法的典型用法代码示例。如果您正苦于以下问题:Python QTextCursor.position方法的具体用法?Python QTextCursor.position怎么用?Python QTextCursor.position使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtGui.QTextCursor
的用法示例。
在下文中一共展示了QTextCursor.position方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _cursor_moved
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def _cursor_moved(self):
tc = QTextCursor(self.Editor.textCursor()) # copy of cursor
self.ColNumber.setText( str( tc.positionInBlock() ) )
tb = tc.block()
if tb == self.last_text_block :
return # still on same line, nothing more to do
# Fill in line-number widget, line #s are origin-1
self.LineNumber.setText( str( tb.blockNumber()+1 ) )
# Fill in the image name and folio widgets
pn = self.page_model.page_index(tc.position())
if pn is not None : # the page model has info on this position
self.ImageFilename.setText(self.page_model.filename(pn))
self.Folio.setText(self.page_model.folio_string(pn))
else: # no image data, or cursor is above page 1
self.ImageFilename.setText('')
self.Folio.setText('')
# Change the current-line "extra selection" to the new current line.
# Note: the cursor member of the extra selection may not have a
# selection. If it does, the current line highlight disappears. The
# cursor "tc" may or may not have a selection; to make sure, we clone
# it and remove any selection from it.
cltc = QTextCursor(tc)
cltc.setPosition(tc.position(),QTextCursor.MoveAnchor)
# Set the cloned cursor into the current line extra selection object.
self.current_line_sel.cursor = cltc
# Re-assign the list of extra selections to force update of display.
self.Editor.setExtraSelections(self.extra_sel_list)
示例2: unindent
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def unindent(self):
cursor = self._neditor.textCursor()
start_block = self._neditor.document().findBlock(
cursor.selectionStart())
end_block = self._neditor.document().findBlock(
cursor.selectionEnd())
position = cursor.position()
if position == 0:
return
if start_block != end_block:
# Unindent multiple lines
block = start_block
stop_block = end_block.next()
while block != stop_block:
indentation = self.__block_indentation(block)
if indentation.endswith(self.text()):
cursor = QTextCursor(block)
cursor.setPosition(block.position() + len(indentation))
cursor.setPosition(cursor.position() - self.WIDTH,
QTextCursor.KeepAnchor)
cursor.removeSelectedText()
block = block.next()
else:
# Unindent one line
indentation = self.__block_indentation(start_block)
cursor = QTextCursor(start_block)
cursor.setPosition(start_block.position() + len(indentation))
cursor.setPosition(cursor.position() - self.WIDTH,
QTextCursor.KeepAnchor)
cursor.removeSelectedText()
示例3: insert_snippet
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def insert_snippet(text, cursor, variables):
"""Inserts a normal text snippet.
After the insert, the cursor points to the end of the inserted snippet.
If this function returns a cursor it must be set as the cursor for the view
after the snippet has been inserted.
"""
exp_base = expand.Expander(cursor)
evs = [] # make a list of events, either text or a constant
for text, key in snippets.expand(text):
if text:
evs.append(text)
if key == '$':
evs.append('$')
elif key:
# basic variables
func = getattr(exp_base, key, None)
if func:
evs.append(func())
selectionUsed = expand.SELECTION in evs
# do the padding if 'selection: strip;' is used
if selectionUsed and 'strip' in variables.get('selection', ''):
space = '\n' if '\n' in cursor.selection().toPlainText() else ' '
# change whitespace in previous and next piece of text
i = evs.index(expand.SELECTION)
for j in range(i-1, -i, -1):
if evs[j] not in expand.constants:
evs[j] = evs[j].rstrip() + space
break
for j in range(i+1, len(evs)):
if evs[j] not in expand.constants:
evs[j] = space + evs[j].lstrip()
break
# now insert the text
ins = QTextCursor(cursor)
selectionUsed and ins.setPosition(cursor.selectionStart())
a, c = -1, -1
for e in evs:
if e == expand.ANCHOR:
a = ins.position()
elif e == expand.CURSOR:
c = ins.position()
elif e == expand.SELECTION:
ins.setPosition(cursor.selectionEnd())
else:
ins.insertText(e)
cursor.setPosition(ins.position())
# return a new cursor if requested
if (a, c) != (-1, -1):
new = QTextCursor(cursor)
if a != -1:
new.setPosition(a)
if c != -1:
new.setPosition(c, QTextCursor.KeepAnchor if a != -1 else QTextCursor.MoveAnchor)
return new
示例4: actionTriggered
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def actionTriggered(self, name):
# convert arpeggio_normal to arpeggioNormal, etc.
name = _arpeggioTypes[name]
cursor = self.mainwindow().textCursor()
# which arpeggio type is last used?
lastused = '\\arpeggioNormal'
types = set(_arpeggioTypes.values())
block = cursor.block()
while block.isValid():
s = types.intersection(tokeniter.tokens(block))
if s:
lastused = s.pop()
break
block = block.previous()
# where to insert
c = lydocument.cursor(cursor)
c.select_end_of_block()
with cursortools.compress_undo(cursor):
for item in ly.rhythm.music_items(c, partial=ly.document.OUTSIDE):
c = QTextCursor(cursor.document())
c.setPosition(item.end)
c.insertText('\\arpeggio')
if name != lastused:
cursortools.strip_indent(c)
indent = c.block().text()[:c.position()-c.block().position()]
c.insertText(name + '\n' + indent)
# just pick the first place
return
示例5: keep_selection
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def keep_selection(cursor, edit=None):
"""Performs operations inside the selection and restore the selection afterwards.
If edit is given, call setTextCursor(cursor) on the Q(Plain)TextEdit afterwards.
"""
start, end, pos = cursor.selectionStart(), cursor.selectionEnd(), cursor.position()
cur2 = QTextCursor(cursor)
cur2.setPosition(end)
try:
yield
finally:
if pos == start:
cursor.setPosition(cur2.position())
cursor.setPosition(start, QTextCursor.KeepAnchor)
else:
cursor.setPosition(start)
cursor.setPosition(cur2.position(), QTextCursor.KeepAnchor)
if edit:
edit.setTextCursor(cursor)
示例6: read_pages
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def read_pages(self, section, value, version):
valid_rule = {C.FolioRuleAdd1,C.FolioRuleSet,C.FolioRuleSkip}
valid_fmt = {C.FolioFormatArabic,C.FolioFormatLCRom,C.FolioFormatUCRom,C.FolioFormatSame}
last_fmt = C.FolioFormatArabic
last_pos = -1 # ensure monotonically increasing positions
self.clear()
if not isinstance(value, list) :
pagedata_logger.error('{} metadata must be a list of lists, ignoring it'.format(C.MD_PT))
return
for row in value:
try:
# throws exception if not exactly 6 items
[P, fn, pfrs, rule, fmt, nbr] = row
P = int(P) # exception if not valid numeric
tc = QTextCursor(self.document)
tc.setPosition(P) # no effect if P negative or too big
if (tc.position() != P) or (P < last_pos) :
raise ValueError("Invalid document position")
last_pos = P
rule = int(rule) # exceptions if not numeric
fmt = int(fmt)
nbr = int(nbr)
if not ( (rule in valid_rule) and (fmt in valid_fmt) and (nbr >= 0) ) :
raise ValueError("Invalid folio info")
# All looks good, do permanent things
self.cursor_list.append(tc)
self.filename_list.append(fn)
self.folio_list.append( [rule, fmt, nbr] )
if fmt != C.FolioFormatSame :
self.explicit_formats.add(len(self.folio_list)-1)
# get list of proofer strings, dropping opening null string
# due to leading backslash. If it is only '\\' the result
# is the list [''].
plist = pfrs.split('\\')[1:]
self.proofers_list.append(plist)
except Exception as thing:
pagedata_logger.error('Invalid row of page metadata: '+thing.args[0])
pagedata_logger.error(' ignoring {}'.format(row))
if 0 < len(self.filename_list) :
self._active = True
self._add_stopper()
self.PagesUpdated.emit()
示例7: insert_snippet
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def insert_snippet(self):
cursor = self._editor.word_under_cursor()
prefix = cursor.selectedText()
# pos = cursor.position()
# cursor.movePosition(QTextCursor.StartOfWord)
# start_pos = cursor.position()
# cursor.setPosition(pos, QTextCursor.KeepAnchor)
copy = QTextCursor(cursor)
copy.movePosition(QTextCursor.StartOfWord)
start = copy.position()
self._current_snippet = self.snippets.get(prefix)
if self._current_snippet is not None:
self.active = True
cursor.removeSelectedText()
cursor.insertText(self._current_snippet.body)
self._highlight(start)
else:
self.active = False
示例8: _cursor_moved
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def _cursor_moved(self):
tc = QTextCursor(self.Editor.textCursor()) # copy of cursor
self.ColNumber.setText( str( tc.positionInBlock() ) )
tb = tc.block()
if tb == self.last_text_block :
return # still on same line, nothing more to do
# Fill in line-number widget, line #s are origin-1
self.LineNumber.setText( str( tb.blockNumber()+1 ) )
# Fill in the image name and folio widgets
pn = self.page_model.page_index(tc.position())
if pn is not None : # the page model has info on this position
self.ImageFilename.setText(self.page_model.filename(pn))
self.Folio.setText(self.page_model.folio_string(pn))
else: # no image data, or cursor is above page 1
self.ImageFilename.setText('')
self.Folio.setText('')
# Change the extra selection to the current line. The cursor needs
# to have no selection. Yes, we are here because the cursor moved,
# but that doesn't mean no-selection; it might have moved because
# of a shift-click extending the selection.
#xtc.clearSelection()
self.current_line_sel.cursor = tc
self.Editor.setExtraSelections(self.extra_sel_list)
示例9: read_pages
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import position [as 别名]
def read_pages(self, stream, sentinel, vers, parm):
valid_rule = {C.FolioRuleAdd1,C.FolioRuleSet,C.FolioRuleSkip}
valid_fmt = {C.FolioFormatArabic,C.FolioFormatLCRom,C.FolioFormatUCRom,C.FolioFormatSame}
last_fmt = C.FolioFormatArabic
last_pos = -1 # ensure monotonically increasing positions
for line in metadata.read_to(stream, sentinel):
try:
# throws exception if not exactly 6 items
(P, fn, pfrs, rule, fmt, nbr) = line.split(' ')
P = int(P) # throws exception if not valid int string
tc = QTextCursor(self.document)
tc.setPosition(P) # no effect if P negative or too big
if (tc.position() != P) or (P < last_pos) :
raise ValueError("Invalid document position")
last_pos = P
rule = int(rule)
fmt = int(fmt)
nbr = int(nbr)
if not ( (rule in valid_rule) and (fmt in valid_fmt) and (nbr >= 0) ) :
raise ValueError("Invalid folio info")
# All looks good, do permanent things
self.cursor_list.append(tc)
self.filename_list.append(fn)
self.folio_list.append( [rule, fmt, nbr] )
if fmt != C.FolioFormatSame :
self.explicit_formats.add(len(self.folio_list)-1)
# get list of proofer strings, dropping opening null string
# due to leading backslash. If it is only '\\' the result
# is the list [''].
plist = pfrs.replace(C.UNICODE_EN_SPACE,' ').split('\\')[1:]
self.proofers_list.append(plist)
except Exception as thing:
pagedata_logger.error('invalid line of page metadata: '+thing.args[0])
pagedata_logger.error(' ignoring "'+line+'"')
if 0 < len(self.filename_list) :
self._active = True
self._add_stopper()