本文整理汇总了Python中PyQt5.QtGui.QTextCursor.block方法的典型用法代码示例。如果您正苦于以下问题:Python QTextCursor.block方法的具体用法?Python QTextCursor.block怎么用?Python QTextCursor.block使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtGui.QTextCursor
的用法示例。
在下文中一共展示了QTextCursor.block方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: actionTriggered
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [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
示例2: cursors
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [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
示例3: _cursor_moved
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [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)
示例4: cursor_position
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [as 别名]
def cursor_position(self, position):
line, column = position
line = min(line, self.line_count() - 1)
column = min(column, len(self.line_text(line)))
cursor = QTextCursor(self.document().findBlockByNumber(line))
cursor.setPosition(cursor.block().position() + column,
QTextCursor.MoveAnchor)
self.setTextCursor(cursor)
示例5: indent_line
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [as 别名]
def indent_line(self, line_no, indent_length):
block = self._get_block(line_no)
cursor = QTextCursor(block)
cursor.joinPreviousEditBlock()
cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
if indent_length < 0:
for i in range(-indent_length):
cursor.deleteChar()
else:
cursor.insertText(" " * indent_length)
if indent_length:
cursor.movePosition(QTextCursor.StartOfBlock, QTextCursor.MoveAnchor)
line = unicode(cursor.block().text())
if len(line) and line[0] == " ":
cursor.movePosition(QTextCursor.NextWord, QTextCursor.MoveAnchor)
self.editview.setTextCursor(cursor)
cursor.endEditBlock()
示例6: lineNumberAreaPaintEvent
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [as 别名]
def lineNumberAreaPaintEvent(self, event):
painter = QPainter(self.lineNumberArea)
painter.fillRect(event.rect(), colorValues['lineNumberArea'])
cursor = QTextCursor(self.document())
cursor.movePosition(QTextCursor.Start)
atEnd = False
while not atEnd:
rect = self.cursorRect(cursor)
block = cursor.block()
if block.isVisible():
number = str(cursor.blockNumber() + 1)
painter.setPen(colorValues['lineNumberAreaText'])
painter.drawText(0, rect.top(), self.lineNumberArea.width()-2,
self.fontMetrics().height(), Qt.AlignRight, number)
cursor.movePosition(QTextCursor.EndOfBlock)
atEnd = cursor.atEnd()
if not atEnd:
cursor.movePosition(QTextCursor.NextBlock)
示例7: _cursor_moved
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [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)
示例8: paintEvent
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [as 别名]
def paintEvent(self, event):
if not globalSettings.lineNumbersEnabled:
return QWidget.paintEvent(self, event)
painter = QPainter(self)
painter.fillRect(event.rect(), colorValues['lineNumberArea'])
cursor = QTextCursor(self.editor.document())
cursor.movePosition(QTextCursor.Start)
atEnd = False
if globalSettings.relativeLineNumbers:
relativeTo = self.editor.textCursor().blockNumber()
else:
relativeTo = -1
while not atEnd:
rect = self.editor.cursorRect(cursor)
block = cursor.block()
if block.isVisible():
number = str(cursor.blockNumber() - relativeTo).replace('-', '−')
painter.setPen(colorValues['lineNumberAreaText'])
painter.drawText(0, rect.top(), self.width() - 2,
self.fontMetrics().height(), Qt.AlignRight, number)
cursor.movePosition(QTextCursor.EndOfBlock)
atEnd = cursor.atEnd()
if not atEnd:
cursor.movePosition(QTextCursor.NextBlock)
示例9: cmdJoinLines
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [as 别名]
def cmdJoinLines(self, cmd, repeatLineCount=None):
if repeatLineCount is not None:
self._selectRangeForRepeat(repeatLineCount)
start, end = self._selectedLinesRange()
count = end - start
if not count: # nothing to join
return
self._saveLastEditLinesCmd(cmd, end - start + 1)
cursor = QTextCursor(self._qpart.document().findBlockByNumber(start))
with self._qpart:
for _ in range(count):
cursor.movePosition(QTextCursor.EndOfBlock)
cursor.movePosition(QTextCursor.NextCharacter, QTextCursor.KeepAnchor)
self.moveToFirstNonSpace(cursor, QTextCursor.KeepAnchor)
nonEmptyBlock = cursor.block().length() > 1
cursor.removeSelectedText()
if nonEmptyBlock:
cursor.insertText(' ')
self._qpart.setTextCursor(cursor)
示例10: Log
# 需要导入模块: from PyQt5.QtGui import QTextCursor [as 别名]
# 或者: from PyQt5.QtGui.QTextCursor import block [as 别名]
class Log(QTextBrowser):
"""Widget displaying output from a Job."""
def __init__(self, parent=None):
super(Log, self).__init__(parent)
self.setOpenLinks(False)
self.cursor = QTextCursor(self.document())
self._types = job.ALL
self._lasttype = None
self._formats = self.logformats()
def setMessageTypes(self, types):
"""Set the types of Job output to display.
types is a constant bitmask from job, like job.STDOUT etc.
By default job.ALL is used.
"""
self._types = types
def messageTypes(self):
"""Return the set message types (job.ALL by default)."""
return self._types
def connectJob(self, job):
"""Gives us the output from the Job (past and upcoming)."""
for msg, type in job.history():
self.write(msg, type)
job.output.connect(self.write)
def textFormat(self, type):
"""Returns a QTextFormat() for the given type."""
return self._formats[type]
def write(self, message, type):
"""Writes the given message with the given type to the log.
The keepScrolledDown context manager is used to scroll the log further
down if it was scrolled down at that moment.
If two messages of a different type are written after each other a newline
is inserted if otherwise the message would continue on the same line.
"""
if type & self._types:
with self.keepScrolledDown():
changed = type != self._lasttype
self._lasttype = type
if changed and self.cursor.block().text() and not message.startswith('\n'):
self.cursor.insertText('\n')
self.writeMessage(message, type)
def writeMessage(self, message, type):
"""Inserts the given message in the text with the textformat belonging to type."""
self.cursor.insertText(message, self.textFormat(type))
@contextlib.contextmanager
def keepScrolledDown(self):
"""Performs a function, ensuring the log stays scrolled down if it was scrolled down on start."""
vbar = self.verticalScrollBar()
scrolleddown = vbar.value() == vbar.maximum()
try:
yield
finally:
if scrolleddown:
vbar.setValue(vbar.maximum())
def logformats(self):
"""Returns a dictionary with QTextCharFormats for the different types of messages.
Besides the STDOUT, STDERR, NEUTRAL, FAILURE and SUCCESS formats there is also
a "link" format, that looks basically the same as the output formats, but blueish
and underlined, to make parts of the output (e.g. filenames) look clickable.
"""
textColor = QApplication.palette().color(QPalette.WindowText)
successColor = qutil.addcolor(textColor, 0, 128, 0) # more green
failureColor = qutil.addcolor(textColor, 128, 0, 0) # more red
linkColor = qutil.addcolor(textColor, 0, 0, 128) # more blue
stdoutColor = qutil.addcolor(textColor, 64, 64, 0) # more purple
s = QSettings()
s.beginGroup("log")
outputFont = QFont(s.value("fontfamily", "monospace", str))
outputFont.setPointSizeF(s.value("fontsize", 9.0, float))
output = QTextCharFormat()
output.setFont(outputFont)
# enable zooming the log font size
output.setProperty(QTextFormat.FontSizeAdjustment, 0)
stdout = QTextCharFormat(output)
stdout.setForeground(stdoutColor)
stderr = QTextCharFormat(output)
link = QTextCharFormat(output)
link.setForeground(linkColor)
link.setFontUnderline(True)
status = QTextCharFormat()
status.setFontWeight(QFont.Bold)
#.........这里部分代码省略.........