Exemplo n.º 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'

        # Use flags for case match
        flags = QTextDocument.FindFlags()
        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 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)
Exemplo n.º 2
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()
Exemplo n.º 3
0
 def __init__(self, parent=None, flags=Qt.Dialog):
     # Base class constructor.
     super(FindDialog, self).__init__(parent, flags)
     # Prepare the GUI.
     self.setupUi(self)
     # data members
     self._text = QString()
     self._findFlags = QTextDocument.FindFlags()
     self._wrappedSearch = True
     self._updateCheckBoxes()
Exemplo n.º 4
0
 def setFindFlags(self, value):
     if type(value) is QTextDocument.FindFlags:
         self._findFlags = value
     else:
         self._findFlags = QTextDocument.FindFlags()
         for bit in (QTextDocument.FindBackward,
                     QTextDocument.FindCaseSensitively,
                     QTextDocument.FindWholeWords):
             if value & int(bit):
                 self._findFlags |= bit
     self._updateCheckBoxes()
Exemplo n.º 5
0
 def find_match(self, word, flags, findNext=False):
     flags = QTextDocument.FindFlags(flags)
     if findNext:
         self.moveCursor(QTextCursor.NoMove, QTextCursor.KeepAnchor)
     else:
         self.moveCursor(QTextCursor.StartOfWord, QTextCursor.KeepAnchor)
     found = self.find(word, flags)
     if not found:
         cursor = self.textCursor()
         self.moveCursor(QTextCursor.Start)
         found = self.find(word, flags)
         if not found:
             self.setTextCursor(cursor)
Exemplo n.º 6
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])
Exemplo n.º 7
0
 def _textAndFindFlags(self, backward=False):
     text = self.findComboBox.currentText()
     flags = QTextDocument.FindFlags()
     if backward:
         flags |= QTextDocument.FindBackward
     if not self.ignoreCaseCheckBox.isChecked():
         flags |= QTextDocument.FindCaseSensitively
     if self.flagsComboBox.currentIndex() == self.FULL_WORD:
         flags |= QTextDocument.FindWholeWords
     elif self.flagsComboBox.currentIndex() == self.STARTS_WITH:
         text = QRegExp("\\b%s" % text)
         caseSensitive = not self.ignoreCaseCheckBox.isChecked(
         ) and Qt.CaseSensitive or Qt.CaseInsensitive
         text.setCaseSensitivity(caseSensitive)
     return text, flags
Exemplo n.º 8
0
    def replace_match(self,
                      wordOld,
                      wordNew,
                      flags,
                      all=False,
                      selection=False):
        """Find if searched text exists and replace it with new one.
        If there is a selection just doit inside it and exit.
        """
        tc = self.textCursor()
        if selection and tc.hasSelection():
            start, end = tc.selectionStart(), tc.selectionEnd()
            text = tc.selectedText()
            old_len = len(text)
            max_replace = -1  # all
            text = text.replace(wordOld, wordNew, max_replace)
            new_len = len(text)
            tc.insertText(text)
            offset = new_len - old_len
            self.__set_selection_from_pair(start, end + offset)
            return

        flags = QTextDocument.FindFlags(flags)
        self.moveCursor(QTextCursor.NoMove, QTextCursor.KeepAnchor)

        cursor = self.textCursor()
        cursor.beginEditBlock()

        self.moveCursor(QTextCursor.Start)
        replace = True
        while (replace or all):
            result = False
            result = self.find(wordOld, flags)

            if result:
                tc = self.textCursor()
                if tc.hasSelection():
                    tc.insertText(wordNew)
            else:
                break
            replace = False

        cursor.endEditBlock()
Exemplo n.º 9
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()
Exemplo n.º 10
0
    def replace_match(self, wordOld, wordNew, flags, all=False):
        flags = QTextDocument.FindFlags(flags)
        self.moveCursor(QTextCursor.NoMove, QTextCursor.KeepAnchor)

        cursor = self.textCursor()
        cursor.beginEditBlock()

        self.moveCursor(QTextCursor.Start)
        replace = True
        while (replace or all):
            result = False
            result = self.find(wordOld, flags)

            if result:
                tc = self.textCursor()
                if tc.hasSelection():
                    tc.insertText(wordNew)
            else:
                break
            replace = False

        cursor.endEditBlock()
Exemplo n.º 11
0
    def find(self, flags=0):
        """
        Looks throught the text document based on the current criteria.  The \
        inputed flags will be merged with the generated search flags.
        
        :param      flags | <QTextDocument.FindFlag>
        
        :return     <bool> | success
        """
        # check against the web and text views
        if (not (self._textEdit or self._webView)):
            fg = QColor('darkRed')
            bg = QColor('red').lighter(180)

            palette = self.palette()
            palette.setColor(palette.Text, fg)
            palette.setColor(palette.Base, bg)

            self._searchEdit.setPalette(palette)
            self._searchEdit.setToolTip('No Text Edit is linked.')

            return False

        if (self._caseSensitiveCheckbox.isChecked()):
            flags |= QTextDocument.FindCaseSensitively

        if (self._textEdit and self._wholeWordsCheckbox.isChecked()):
            flags |= QTextDocument.FindWholeWords

        terms = self._searchEdit.text()
        if (terms != self._lastText):
            self._lastCursor = QTextCursor()

        if (self._regexCheckbox.isChecked()):
            terms = QRegExp(terms)

        palette = self.palette()

        # search on a text edit
        if (self._textEdit):
            cursor = self._textEdit.document().find(
                terms, self._lastCursor, QTextDocument.FindFlags(flags))
            found = not cursor.isNull()
            self._lastCursor = cursor
            self._textEdit.setTextCursor(cursor)

        elif (QWebPage):
            flags = QWebPage.FindFlags(flags)
            flags |= QWebPage.FindWrapsAroundDocument

            found = self._webView.findText(terms, flags)

        self._lastText = self._searchEdit.text()

        if (not terms or found):
            fg = palette.color(palette.Text)
            bg = palette.color(palette.Base)
        else:
            fg = QColor('darkRed')
            bg = QColor('red').lighter(180)

        palette.setColor(palette.Text, fg)
        palette.setColor(palette.Base, bg)

        self._searchEdit.setPalette(palette)

        return found