Example #1
0
    def find(self, what, *args):
        """Perform a search
        arg[0] ->  QTextDocument.FindCaseSensitively
        arg[1] ->  QTextDocument.FindWholeWords
        arg[2] ->  QTextDocument.FindBackward
        arg[3] ->  QTextDocument.RegEx
        """
        print('find called: "%s" (%s)' % (what, args))

        if not args:
            flags = QTextDocument.FindFlags()
        elif len(args) == 1 and type(args[0]) == QTextDocument.FindFlags:
            flags = args[0]
        else:
            flags = QTextDocument.FindFlags()
            # Use flags for case match
            if args[0]==True:
                flags = flags |  QTextDocument.FindCaseSensitively
            if args[1]==True:
                flags = flags |  QTextDocument.FindWholeWords
            if args[2]==True:
                flags = flags |  QTextDocument.FindBackward

        cursor = self.textCursor()
        if len(args) >= 4 and args[3]==True:
            cursor = self.document().find(QRegExp(what),
                                       cursor , flags)
        else:
            cursor = self.document().find(what,
                                       cursor, flags)

        if not cursor.isNull():
            self.setTextCursor(cursor)

        return not cursor.isNull()
Example #2
0
 def __findPrevNextQTextEdit(self, backwards):
     """
     Private method to to search the associated edit widget of
     type QTextEdit.
     
     @param backwards flag indicating a backwards search
     @type bool
     @return flag indicating the search result
     @rtype bool
     """
     if backwards:
         flags = QTextDocument.FindFlags(QTextDocument.FindBackward)
     else:
         flags = QTextDocument.FindFlags()
     if self.caseCheckBox.isChecked():
         flags |= QTextDocument.FindCaseSensitively
     if self.wordCheckBox.isChecked():
         flags |= QTextDocument.FindWholeWords
     
     ok = self.__textedit.find(self.findtextCombo.currentText(), flags)
     if not ok:
         # wrap around once
         cursor = self.__textedit.textCursor()
         if backwards:
             moveOp = QTextCursor.End        # move to end of document
         else:
             moveOp = QTextCursor.Start      # move to start of document
         cursor.movePosition(moveOp)
         self.__textedit.setTextCursor(cursor)
         ok = self.__textedit.find(self.findtextCombo.currentText(), flags)
     
     return ok
Example #3
0
    def replace(self, what, new, *args):
        """Replace the first occurence of a search
        arg[0] ->  QTextDocument.FindCaseSensitively
        arg[1] ->  QTextDocument.FindWholeWords
        arg[2] ->  QTextDocument.FindBackward
        arg[3] ->  QTextDocument.RegEx
        """
        # Use flags for case match
        flags =  QTextDocument.FindFlags()
        if args[0]:
            flags = flags| QTextDocument.FindCaseSensitively
        if args[1]:
            flags = flags| QTextDocument.FindWholeWords
        if args[2]:
            flags = flags| QTextDocument.FindBackward

        # Beginning of undo block
        pcursor = self.textCursor()
        pcursor.beginEditBlock()

        cursor = self.textCursor()

        # Replace
        # self is the QTextEdit
        if args[3]==True:
            cursor = self.document().find( QRegExp(what),
                                    cursor, flags)
        else:
            cursor = self.document().find(what, cursor, flags)
        if not cursor.isNull():
            if cursor.hasSelection():
                cursor.insertText(new)

        # Mark end of undo block
        pcursor.endEditBlock()
Example #4
0
 def find_text(self,
               search,
               cs=False,
               wo=False,
               backward=False,
               find_next=True):
     flags = QTextDocument.FindFlags()
     if cs:
         flags = QTextDocument.FindCaseSensitively
     if wo:
         flags |= QTextDocument.FindWholeWords
     if backward:
         flags |= QTextDocument.FindBackward
     if find_next or backward:
         self.moveCursor(QTextCursor.NoMove)
     else:
         self.moveCursor(QTextCursor.StartOfWord)
     found = self.find(search, flags)
     if not found:
         cursor = self.textCursor()
         if backward:
             self.moveCursor(QTextCursor.End)
         else:
             self.moveCursor(QTextCursor.Start)
         found = self.find(search, flags)
         if not found:
             self.setTextCursor(cursor)
Example #5
0
 def searchPrev(self, txt, caseSensitive, wholeWord, regexp):
     """
     Public method to search the previous occurrence of the given text.
     
     @param txt text to search for
     @type str
     @param caseSensitive flag indicating to perform a case sensitive
         search
     @type bool
     @param wholeWord flag indicating to search for whole words
         only
     @type bool
     @param regexp flag indicating a regular expression search
     @type bool
     """
     self.__lastSearch = (txt, caseSensitive, wholeWord, regexp)
     flags = QTextDocument.FindFlags(QTextDocument.FindBackward)
     if caseSensitive:
         flags |= QTextDocument.FindCaseSensitively
     if wholeWord:
         flags |= QTextDocument.FindWholeWords
     if regexp:
         ok = self.find(
             QRegExp(
                 txt,
                 Qt.CaseSensitive if caseSensitive else Qt.CaseInsensitive),
             flags)
     else:
         ok = self.find(txt, flags)
     self.searchStringFound.emit(ok)
Example #6
0
    def find_matches(self,
                     search,
                     case_sensitive=False,
                     whole_word=False,
                     backward=False,
                     find_next=True):
        flags = QTextDocument.FindFlags()
        if case_sensitive:
            flags = QTextDocument.FindCaseSensitively
        if whole_word:
            flags |= QTextDocument.FindWholeWords
        if backward:
            flags |= QTextDocument.FindBackward
        if find_next or backward:
            self.moveCursor(QTextCursor.NoMove)
        else:
            self.moveCursor(QTextCursor.StartOfWord)
        found = self.find(search, flags)
        cursor = self.textCursor()
        if not found:
            # Original cursor is saved for restore later
            cursor = self.textCursor()
            # Move cursor to beginning of document to search again
            if backward:
                self.moveCursor(QTextCursor.End)
            else:
                self.moveCursor(QTextCursor.Start)
            found = self.find(search, flags)
            if not found:
                # self.clear_extra_selections()
                # Restore cursor
                self.setTextCursor(cursor)
                self.clear_extra_selections('searchs')
                return 0, []
        index, results = self._get_find_index_results(search, case_sensitive,
                                                      whole_word)
        # TODO: obtain line numbers for highlight in scrollbar
        # TODO: cambiar el 2
        # FIXME: clear its ok?
        self.clear_extra_selections('searchs')

        if len(search) > 2:
            ss = []
            append = ss.append
            results = results[:500]
            for start, end in results:
                s = extra_selection.ExtraSelection(self.textCursor(),
                                                   start_pos=start,
                                                   end_pos=end)
                s.set_full_width()
                c = QColor('yellow')
                c.setAlpha(40)
                s.set_background(c)
                s.set_outline('gray')
                append(s)
            self.add_extra_selections('searchs', ss)

        return index, results
Example #7
0
 def _makeFind(self, back: bool = False) -> int:
     flags = QTextDocument.FindFlags()
     if self.matchCase.isChecked():
         flags |= QTextDocument.FindCaseSensitively
     if self.wholeWords.isChecked():
         flags |= QTextDocument.FindWholeWords
     if back:
         flags |= QTextDocument.FindBackward
     return flags
Example #8
0
	def find(self, back=False, replace=False):
		flags = QTextDocument.FindFlags()
		if back:
			flags |= QTextDocument.FindBackward
		if self.csBox.isChecked():
			flags |= QTextDocument.FindCaseSensitively
		text = self.searchEdit.text()
		replaceText = self.replaceEdit.text() if replace else None
		found = self.currentTab.find(text, flags, replaceText=replaceText)
		self.setSearchEditColor(found)
Example #9
0
 def searchtext(self):
     #QTextDocument.FindFlags()  cursorForward 
     #QTextDocument.FindBackward()
     flag = QTextDocument.FindFlags()   #ๅๅ‘ๆœ็ดข
     searchtextexpress= self.lineEdit_searchexpression.text()
     if searchtextexpress is not "":
         print(searchtextexpress)
         print(self.textBrowser_testcommand.cursor())
         self.textBrowser_testcommand.find(searchtextexpress,QTextDocument.FindFlag())
         print(self.textBrowser_testcommand.cursor())
Example #10
0
    def replace_match(self,
                      word_old,
                      word_new,
                      cs=False,
                      wo=False,
                      all_words=False,
                      selection=False):
        """
        Find if searched text exists and replace it with new one.
        If there is a selection just do it inside it and exit
        """

        cursor = self.textCursor()
        if selection:
            if not cursor.hasSelection():
                return
            start, end = cursor.selectionStart(), cursor.selectionEnd()
            text = cursor.selectedText()
            old_len = len(text)
            text = text.replace(word_old, word_new)
            new_len = len(text)
            cursor.insertText(text)
            # Set selection
            cursor.setPosition(start)
            cursor.setPosition(end + new_len - old_len, QTextCursor.KeepAnchor)
            self.setTextCursor(cursor)
            return

        # Replace once
        cursor.insertText(word_new)
        if not all_words:
            return

        # Replace all
        flags = QTextDocument.FindFlags()
        if wo:
            flags |= QTextDocument.FindWholeWords
        if cs:
            flags |= QTextDocument.FindCaseSensitively

        self.moveCursor(QTextCursor.Start)

        cursor.beginEditBlock()

        while all_words:
            found = self.find(word_old, flags)
            if found:
                cursor = self.textCursor()
                if cursor.hasSelection():
                    cursor.insertText(word_new)
            else:
                break
        # Set cursor on last replace text
        self.setTextCursor(cursor)
        cursor.endEditBlock()
Example #11
0
    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])
Example #12
0
	def replaceAll(self, text, replaceText):
		cursor = self.editBox.textCursor()
		cursor.beginEditBlock()
		cursor.movePosition(QTextCursor.Start)
		flags = QTextDocument.FindFlags()
		cursor = lastCursor = self.editBox.document().find(text, cursor, flags)
		while not cursor.isNull():
			cursor.insertText(replaceText)
			lastCursor = cursor
			cursor = self.editBox.document().find(text, cursor, flags)
		if not lastCursor.isNull():
			lastCursor.movePosition(QTextCursor.Left, QTextCursor.MoveAnchor, len(replaceText))
			lastCursor.movePosition(QTextCursor.Right, QTextCursor.KeepAnchor, len(replaceText))
			self.editBox.setTextCursor(lastCursor)
		self.editBox.textCursor().endEditBlock()
		return not lastCursor.isNull()
Example #13
0
 def searchPrev(self, txt, caseSensitive, wholeWord):
     """
     Public method to search the previous occurrence of the given text.
     
     @param txt text to search for (string)
     @param caseSensitive flag indicating case sensitivity (boolean)
     @param wholeWord flag indicating a search for the whole word (boolean)
     """
     self.__lastSearch = (txt, caseSensitive, wholeWord)
     flags = QTextDocument.FindFlags(QTextDocument.FindBackward)
     if caseSensitive:
         flags |= QTextDocument.FindCaseSensitively
     if wholeWord:
         flags |= QTextDocument.FindWholeWords
     ok = self.find(txt, flags)
     self.searchStringFound.emit(ok)
Example #14
0
	def find(self, back=False):
		flags = QTextDocument.FindFlags()
		if back:
			flags |= QTextDocument.FindBackward
		if self.csBox.isChecked():
			flags |= QTextDocument.FindCaseSensitively
		text = self.searchEdit.text()
		editBox = self.editBoxes[self.ind]
		cursor = editBox.textCursor()
		newCursor = editBox.document().find(text, cursor, flags)
		if not newCursor.isNull():
			editBox.setTextCursor(newCursor)
			return self.setSearchEditColor(True)
		cursor.movePosition(QTextCursor.End if back else QTextCursor.Start)
		newCursor = editBox.document().find(text, cursor, flags)
		if not newCursor.isNull():
			editBox.setTextCursor(newCursor)
			return self.setSearchEditColor(True)
		self.setSearchEditColor(False)
Example #15
0
    def replaceAll(self,
                   findText,
                   replaceText,
                   syntaxCombo=None,
                   caseCheckBox=False,
                   wholeCheckBox=False):
        try:
            # Here I am just getting the replacement data
            # from my UI so it will be different for you

            old = findText
            print('old is ', old)
            new = replaceText
            print('new is ', new)

            # Beginning of undo block
            cursor = self.__textEditor.textCursor()
            cursor.beginEditBlock()

            # Use flags for case match
            flags = QTextDocument.FindFlags()
            if caseCheckBox:
                flags = flags | QTextDocument.FindCaseSensitively

            # Replace all we can
            while True:
                # self.editor is the QPlainTextEdit
                r = self.__textEditor.find(old, flags)
                if r:
                    qc = self.__textEditor.textCursor()
                    if qc.hasSelection():
                        qc.insertText(new)
                else:
                    break

            # Mark end of undo block
            cursor.endEditBlock()
            self.search(self.__findText, syntaxCombo)
            self.fixResFindValues()
            self.selectedText = False
            # self.fixFormat()
        except Exception as e:
            print("something  didn't work with replace all: ", e)
Example #16
0
    def replace_all(self, what, new, *args):
        """Replace all occurence of a search
        arg[0] ->  QTextDocument.FindCaseSensitively
        arg[1] ->  QTextDocument.FindWholeWords
        arg[2] ->  QTextDocument.FindBackward
        arg[3] ->  QTextDocument.RegEx
        """
        # Use flags for case match
        flags =  QTextDocument.FindFlags()
        if args[0]:
            flags = flags| QTextDocument.FindCaseSensitively
        if args[1]:
            flags = flags| QTextDocument.FindWholeWords
        if args[2]:
            flags = flags| QTextDocument.FindBackward

        # Beginning of undo block
        pcursor = self.textCursor()
        pcursor.beginEditBlock()

        #cursor at start as we replace from start
        cursor = self.textCursor()
        cursor.setPosition(0)

        # Replace all we can
        while True:
            # self is the QTextEdit
            if args[3]:
                cursor=self.document().find( QRegExp(what),
                                       cursor,flags)
            else:
                cursor=self.document().find(what,cursor,flags)
            if not cursor.isNull():
                if cursor.hasSelection():
                    cursor.insertText(new)
                else:
                    print('no selection')
            else:
                break

        # Mark end of undo block
        pcursor.endEditBlock()
Example #17
0
    def find_match(self,
                   search,
                   case_sensitive=False,
                   whole_word=False,
                   backward=False,
                   forward=False,
                   wrap_around=True):

        if not backward and not forward:
            self.moveCursor(QTextCursor.StartOfWord)

        flags = QTextDocument.FindFlags()
        if case_sensitive:
            flags |= QTextDocument.FindCaseSensitively
        if whole_word:
            flags |= QTextDocument.FindWholeWords
        if backward:
            flags |= QTextDocument.FindBackward

        cursor = self.textCursor()
        found = self.document().find(search, cursor, flags)
        if not found.isNull():
            self.setTextCursor(found)

        elif wrap_around:
            if not backward and not forward:
                cursor.movePosition(QTextCursor.Start)
            elif forward:
                cursor.movePosition(QTextCursor.Start)
            else:
                cursor.movePosition(QTextCursor.End)

            # Try again
            found = self.document().find(search, cursor, flags)
            if not found.isNull():
                self.setTextCursor(found)

        return not found.isNull()