本文整理汇总了Python中PyQt5.QtGui.QTextCursor.setPosition方法的典型用法代码示例。如果您正苦于以下问题:Python QTextCursor.setPosition方法的具体用法?Python QTextCursor.setPosition怎么用?Python QTextCursor.setPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtGui.QTextCursor
的用法示例。
在下文中一共展示了QTextCursor.setPosition方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _cursor_moved
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [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: spanner_positions
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def spanner_positions(cursor):
"""Return a list with 0 to 2 QTextCursor instances.
At the first cursor a starting spanner item can be inserted, at the
second an ending item.
"""
c = lydocument.cursor(cursor)
if cursor.hasSelection():
partial = ly.document.INSIDE
else:
# just select until the end of the current line
c.select_end_of_block()
partial = ly.document.OUTSIDE
items = list(ly.rhythm.music_items(c, partial=partial))
if cursor.hasSelection():
del items[1:-1]
else:
del items[2:]
positions = []
for i in items:
c = QTextCursor(cursor.document())
c.setPosition(i.end)
positions.append(c)
return positions
示例3: update_minimap_doc
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def update_minimap_doc(self, position, charsRemoved, charsAdded):
def select_blocks(first_block, last_block):
first_block_pos = first_block.position()
doc_cursor.setPosition(first_block_pos)
last_block_pos = last_block.position()
doc_cursor.setPosition(last_block_pos, QTextCursor.KeepAnchor)
doc_cursor.movePosition(
QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
print("selected text :", doc_cursor.selectedText())
return doc_cursor.selectedText()
doc_cursor = QTextCursor(self._doc)
minimap_cursor = QTextCursor(self._minimap_doc)
# IF one same block is modified
if self._minimap_doc.blockCount() == self._doc.blockCount():
doc_cursor.setPosition(position)
doc_cursor.select(QTextCursor.BlockUnderCursor)
minimap_cursor.setPosition(position)
minimap_cursor.select(QTextCursor.BlockUnderCursor)
minimap_cursor.insertFragment(doc_cursor.selection())
# TODO: if the doc is modified on more than one block but resulting in
# the same count of blocks (right now only the first block would be
# updated)
else:
# ELSE
doc_cursor.select(QTextCursor.Document)
minimap_cursor.select(QTextCursor.Document)
minimap_cursor.insertFragment(doc_cursor.selection())
示例4: doTextEdit
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def doTextEdit(self, url, setCursor=False):
"""Process a textedit link and either highlight
the corresponding source code or set the
cursor to it.
"""
t = textedit.link(url)
# Only process textedit links
if not t:
return False
filename = util.normpath(t.filename)
doc = self.document(filename, setCursor)
if doc:
cursor = QTextCursor(doc)
b = doc.findBlockByNumber(t.line - 1)
p = b.position() + t.column
cursor.setPosition(p)
cursors = pointandclick.positions(cursor)
# Do highlighting if the document is active
if cursors and doc == self.mainwindow().currentDocument():
import viewhighlighter
view = self.mainwindow().currentView()
viewhighlighter.highlighter(view).highlight(self._highlightFormat, cursors, 2, 0)
# set the cursor and bring the document to front
if setCursor:
mainwindow = self.mainwindow()
mainwindow.setTextCursor(cursor)
import widgets.blink
widgets.blink.Blinker.blink_cursor(mainwindow.currentView())
self.mainwindow().setCurrentDocument(doc)
mainwindow.activateWindow()
mainwindow.currentView().setFocus()
return True
示例5: actionTriggered
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [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
示例6: keyPressEvent
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def keyPressEvent(self, event):
#utilities.printKeyEvent(event)
kkey = int( int(event.modifiers()) & C.KEYPAD_MOD_CLEAR) | int(event.key())
if kkey in C.KEYS_EDITOR :
event.accept() # yes, this is one we handle
if kkey in C.KEYS_FIND :
# ^f, ^g, etc. -- just pass them straight to the Find panel
self.editFindKey.emit(kkey)
elif kkey in C.KEYS_ZOOM :
self.setFont( fonts.scale(kkey, self.font()) )
self.my_book.save_font_size(self.font().pointSize())
elif kkey in C.KEYS_BOOKMARKS :
# Something to do with a bookmark. They are kept in the Book
# because they are read and written in the metadata.
mark_number = int(event.key()) - 0x31 # number in 0..8
mark_list = self.my_book.bookmarks # quick reference to the list
if kkey in C.KEYS_MARK_SET : # alt-1..9, set bookmark
# Set a bookmark to the current edit selection
mark_list[mark_number] = QTextCursor(self.textCursor())
self.my_book.metadata_modified(True, C.MD_MOD_FLAG)
elif kkey in C.KEYS_MARK : # ctl-1..9, go to mark
# Move to the save position including a saved selection
if mark_list[mark_number] is not None :
self.parent().center_this(mark_list[mark_number])
else : # shft-ctl-1..9, go to mark, extending selection
if mark_list[mark_number] is not None:
pos = mark_list[mark_number].position()
tc = QTextCursor(self.textCursor())
tc.setPosition(pos, QTextCursor.KeepAnchor)
self.setTextCursor(tc)
self.ensureCursorVisible()
else: # not a key for the editor, pass it on.
event.ignore()
super().keyPressEvent(event)
示例7: pixmap
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def pixmap(cursor, num_lines=6, scale=0.8):
"""Return a QPixmap displaying the selected lines of the document.
If the cursor has no selection, num_lines are drawn.
By default the text is drawn 0.8 * the normal font size. You can change
that by supplying the scale parameter.
"""
block = cursor.document().findBlock(cursor.selectionStart())
c2 = QTextCursor(block)
if cursor.hasSelection():
c2.setPosition(cursor.selectionEnd(), QTextCursor.KeepAnchor)
c2.movePosition(QTextCursor.EndOfBlock, QTextCursor.KeepAnchor)
else:
c2.movePosition(QTextCursor.NextBlock, QTextCursor.KeepAnchor, num_lines)
data = textformats.formatData('editor')
doc = QTextDocument()
font = QFont(data.font)
font.setPointSizeF(font.pointSizeF() * scale)
doc.setDefaultFont(font)
doc.setPlainText(c2.selection().toPlainText())
if metainfo.info(cursor.document()).highlighting:
highlighter.highlight(doc, state=tokeniter.state(block))
size = doc.size().toSize() + QSize(8, -4)
pix = QPixmap(size)
pix.fill(data.baseColors['background'])
doc.drawContents(QPainter(pix))
return pix
示例8: test_indentLessWithSelection
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def test_indentLessWithSelection(self):
self.document.setPlainText(' foo\n bar\nbaz')
cursor = QTextCursor(self.document)
cursor.setPosition(5)
cursor.setPosition(11, QTextCursor.KeepAnchor)
documentIndentLess(self.document, cursor, self.settings)
self.assertEqual('foo\nbar\nbaz', self.document.toPlainText())
示例9: articulation_positions
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def articulation_positions(cursor):
"""Returns a list of positions where an articulation can be added.
Every position is given as a QTextCursor instance.
If the cursor has a selection, all positions in the selection are returned.
"""
c = lydocument.cursor(cursor)
if not cursor.hasSelection():
# just select until the end of the current line
c.select_end_of_block()
rests = True
partial = ly.document.OUTSIDE
else:
rests = False
partial = ly.document.INSIDE
positions = []
for item in ly.rhythm.music_items(c, partial):
if not rests and item.tokens and isinstance(item.tokens[0], ly.lex.lilypond.Rest):
continue
csr = QTextCursor(cursor.document())
csr.setPosition(item.end)
positions.append(csr)
if not cursor.hasSelection():
break # leave if first found, that's enough
return positions
示例10: setLine
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def setLine(self, line):
cursor = QTextCursor(self.document())
cursor.movePosition(QTextCursor.End)
cursor.setPosition(self.newPromptPos, QTextCursor.KeepAnchor)
cursor.removeSelectedText()
cursor.insertText(line)
self.setTextCursor(cursor)
示例11: cursors
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def cursors(self):
"""Cursors for rectangular selection.
1 cursor for every line
"""
cursors = []
if self._start is not None:
startLine, startVisibleCol = self._start
currentLine, currentCol = self._qpart.cursorPosition
if abs(startLine - currentLine) > self._MAX_SIZE or \
abs(startVisibleCol - currentCol) > self._MAX_SIZE:
# Too big rectangular selection freezes the GUI
self._qpart.userWarning.emit('Rectangular selection area is too big')
self._start = None
return []
currentBlockText = self._qpart.textCursor().block().text()
currentVisibleCol = self._realToVisibleColumn(currentBlockText, currentCol)
for lineNumber in range(min(startLine, currentLine),
max(startLine, currentLine) + 1):
block = self._qpart.document().findBlockByNumber(lineNumber)
cursor = QTextCursor(block)
realStartCol = self._visibleToRealColumn(block.text(), startVisibleCol)
realCurrentCol = self._visibleToRealColumn(block.text(), currentVisibleCol)
if realStartCol is None:
realStartCol = block.length() # out of range value
if realCurrentCol is None:
realCurrentCol = block.length() # out of range value
cursor.setPosition(cursor.block().position() + min(realStartCol, block.length() - 1))
cursor.setPosition(cursor.block().position() + min(realCurrentCol, block.length() - 1),
QTextCursor.KeepAnchor)
cursors.append(cursor)
return cursors
示例12: scan_pages
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def scan_pages(self):
global re_line_sep
# first page is Arabic starting at 1
rule = C.FolioRuleSet
fmt = C.FolioFormatArabic
nbr = 1
self.explicit_formats = {0}
for qtb in self.document.all_blocks() :
m = re_line_sep.match(qtb.text())
if m :
# capture the image filename
fname = m.group(1)
if m.group(3) is not None :
# record proofers as a list, omitting the
# null element caused by the leading '\'
plist = m.group(3).split('\\')[1:]
else :
# sep. line with no proofers, minimal list
plist = ['']
qtc = QTextCursor(self.document)
qtc.setPosition(qtb.position())
self.cursor_list.append(qtc)
self.filename_list.append(fname)
self.folio_list.append( [rule,fmt,nbr] )
self.proofers_list.append(plist)
# remaining pages are ditto, add 1, next number
rule = C.FolioRuleAdd1
fmt = C.FolioFormatSame
nbr += 1
if 0 < len(self.cursor_list) : # we found at least 1
self.my_book.metadata_modified(True, C.MD_MOD_FLAG)
self._active = True
self._add_stopper()
示例13: indent_selection
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def indent_selection(self):
def indent_block(block):
cursor = QTextCursor(block)
indentation = self.__block_indentation(block)
cursor.setPosition(block.position() + len(indentation))
cursor.insertText(self.text())
cursor = self._neditor.textCursor()
start_block = self._neditor.document().findBlock(
cursor.selectionStart())
end_block = self._neditor.document().findBlock(
cursor.selectionEnd())
with self._neditor:
if start_block != end_block:
stop_block = end_block.next()
# Indent multiple lines
block = start_block
while block != stop_block:
indent_block(block)
block = block.next()
new_cursor = QTextCursor(start_block)
new_cursor.setPosition(
end_block.position() + len(end_block.text()),
QTextCursor.KeepAnchor)
self._neditor.setTextCursor(new_cursor)
else:
# Indent one line
indent_block(start_block)
示例14: cut_assign
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def cut_assign(cursor):
"""Cuts selected text and assigns it to a LilyPond variable."""
# ask the variable name
name = inputdialog.getText(None, _("Cut and Assign"), _(
"Please enter the name for the variable to assign the selected "
"text to:"), regexp="[A-Za-z]+")
if not name:
return
cursortools.strip_selection(cursor)
# determine state at cursor
block = cursortools.block(cursor)
state = tokeniter.state(block)
for t in tokeniter.partition(cursor).left:
state.follow(t)
mode = ""
for p in state.parsers():
if isinstance(p, ly.lex.lilypond.ParseInputMode):
if isinstance(p, ly.lex.lilypond.ParseLyricMode):
mode = " \\lyricmode"
elif isinstance(p, ly.lex.lilypond.ParseChordMode):
mode = " \\chordmode"
elif isinstance(p, ly.lex.lilypond.ParseFigureMode):
mode = " \\figuremode"
elif isinstance(p, ly.lex.lilypond.ParseDrumMode):
mode = " \\drummode"
break
# find insertion place:
found = False
while block.previous().isValid():
block = block.previous()
state = tokeniter.state(block)
if isinstance(state.parser(), ly.lex.lilypond.ParseGlobal):
found = True
break
tokens = tokeniter.tokens(block)
for t in tokens:
if isinstance(t, ly.lex.lilypond.Name):
found = True
break
elif not isinstance(t, (ly.lex.Space, ly.lex.Comment)):
break
if found:
break
insert = QTextCursor(block)
text = cursor.selection().toPlainText()
space = '\n' if '\n' in text else ' '
text = ''.join((name, ' =', mode, ' {', space, text, space, '}\n\n'))
with cursortools.compress_undo(cursor):
cursor.insertText('\\' + name)
pos = insert.selectionStart()
insert.insertText(text)
if metainfo.info(cursor.document()).auto_indent:
insert.setPosition(pos, QTextCursor.KeepAnchor)
with cursortools.compress_undo(insert, True):
indent.re_indent(insert)
示例15: test_indentLess
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import setPosition [as 别名]
def test_indentLess(self):
self.document.setPlainText(' foo')
cursor = QTextCursor(self.document)
cursor.setPosition(10)
documentIndentLess(self.document, cursor, self.settings)
self.assertEqual(' foo', self.document.toPlainText())
documentIndentLess(self.document, cursor, self.settings)
self.assertEqual('foo', self.document.toPlainText())