def _find(self, textEdit=None, backward=False): if textEdit is None: textEdit = self.textEdit found = False if textEdit is not None: cursor = textEdit.textCursor() text, flags = self._textAndFindFlags(backward=backward) position = cursor.position() cursor = textEdit.document().find(text, cursor, flags) if not cursor.isNull(): textEdit.setTextCursor(cursor) found = True elif self.wrapAroundCheckBox.isChecked(): cursor = QTextCursor(textEdit.textCursor()) cursor.movePosition(backward and QTextCursor.End or QTextCursor.Start) cursor = textEdit.document().find(text, cursor, flags) if not cursor.isNull(): if position == cursor.position(): pass #todo textEdit.setTextCursor(cursor) found = True self.writeSettings() if not found: QApplication.beep() self.feedbackLabel.setText(self.tr("Not found")) else: self.clearFeedback()
def slotCursorPositionChanged(self): """Called whenever the cursor position changes. Highlights matching characters if the cursor is at one of them. """ cursor = self.edit().textCursor() block = cursor.block() text = block.text() # try both characters at the cursor col = cursor.position() - block.position() end = col + 1 col = max(0, col - 1) for c in text[col:end]: if c in self.matchPairs: break col += 1 else: self.clear() return # the cursor is at a character from matchPairs i = self.matchPairs.index(c) cursor.setPosition(block.position() + col) # find the matching character new = QTextCursor(cursor) if i & 1: # look backward match = self.matchPairs[i - 1] flags = QTextDocument.FindBackward else: # look forward match = self.matchPairs[i + 1] flags = QTextDocument.FindFlags() new.movePosition(QTextCursor.Right) # search, also nesting rx = QRegExp(QRegExp.escape(c) + "|" + QRegExp.escape(match)) nest = 0 while nest >= 0: new = cursor.document().find(rx, new, flags) if new.isNull(): self.clear() return nest += 1 if new.selectedText() == c else -1 cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor) self.highlight([cursor, new])
def slotCursorPositionChanged(self): """Called whenever the cursor position changes. Highlights matching characters if the cursor is at one of them. """ cursor = self.edit().textCursor() block = cursor.block() text = block.text() # try both characters at the cursor col = cursor.position() - block.position() end = col + 1 col = max(0, col - 1) for c in text[col:end]: if c in self.matchPairs: break col += 1 else: self.clear() return # the cursor is at a character from matchPairs i = self.matchPairs.index(c) cursor.setPosition(block.position() + col) # find the matching character new = QTextCursor(cursor) if i & 1: # look backward match = self.matchPairs[i-1] flags = QTextDocument.FindBackward else: # look forward match = self.matchPairs[i+1] flags = QTextDocument.FindFlags() new.movePosition(QTextCursor.Right) # search, also nesting rx = QRegExp(QRegExp.escape(c) + '|' + QRegExp.escape(match)) nest = 0 while nest >= 0: new = cursor.document().find(rx, new, flags) if new.isNull(): self.clear() return nest += 1 if new.selectedText() == c else -1 cursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor) self.highlight([cursor, new])