예제 #1
0
    def update_text(self):
        print "Updating Text"
        node = self._node
        if node == None:
            self.setText("")
            return

        vbar = self.TextBrowser.verticalScrollBar()
        st_val = vbar.value()
        print "vbar start", st_val

        if self.Radio_match.isChecked():
            html_list = researched_richtext.re_search_format_html(
                node.researched, show_replace=False, view_chars=self.view_chars
            )
        else:
            html_list = researched_richtext.re_search_format_html(
                node.researched, show_replace=True, view_chars=self.view_chars
            )

        self._html_list = html_list
        self.update_replaced(no_update_text=True)
        str_html = richtext.get_str_formated_html(html_list)
        # TODO: Get the screen to not jump on update
        #        hbar.triggerAction(QtGui.QAbstractSlider.setSliderPosition(pos))
        self.TextBrowser.setHtml(str_html)
        if st_val > vbar.maximum():
            vbar.setValue(vbar.maximum())
        else:
            vbar.setValue(st_val)

        visibile_str = richtext.get_str_formated_visible(html_list)
        if visibile_str != str(self.TextBrowser.toPlainText()):
            print "ERROR: visible string not equal to plain text"
예제 #2
0
    def update_formatting(self):
        self._disable_signals = True
        self.clear_error()

        rsearch_rtext = researched_richtext
        self._update = False
        # print 'Updating', time.time()
        qtpos = self.get_text_cursor_pos()  # visible pos
        # print 'Got pos', qtpos
        raw_html = self.getHtml()
        # we need to get the "True Position", i.e. the position without
        # our formats added in. I think this is the best way to do it
        deformated = richtext.deformat_html(
            raw_html, (richtext.KEEPIF["black-bold"], richtext.KEEPIF["red-underlined-bold"])
        )
        deformated_str = richtext.get_str_formated_true(deformated)

        #            assert(len(deformated_str) <= len(self.getText()))
        true_pos = richtext.get_position(deformated, visible_position=qtpos)[0]

        regexp = self.get_regexp()
        try:
            re.compile(regexp)
        except Exception as E:
            pass
        else:
            self.Replace_groups_model.set_groups(textools.get_regex_groups(regexp))

        #            import pprint
        #            pprint.pprint(self.Replace_groups_model.data)

        error = None
        # These slow it down alot and are not really useful. Just
        # display an error
        if regexp == ".":
            error = "'.' -- Matches everything, not displayed"
        elif regexp == "\w":
            error = "'\w' -- Matches all characters, not displayed"
        elif regexp == "":
            error = "'' -- Results not displayed, matches between every" " character."
        else:
            try:
                researched = textools.re_search(regexp, deformated_str)
                if len(researched) == 1 and type(researched[0]) == str:
                    error = "No Match Found"
            except re.sre_compile.error as E:
                error = str(E)
        if error:
            print error
            self.set_error(error)
            # believe it or not, setText will add formating!
            # have to explicitly set html
            self.setText(deformated_str)
            print "er setting pos", true_pos
            self.set_text_cursor_pos(true_pos, no_anchor=True)
            self._disable_signals = False
            return

        # Set the html to the correct values
        if self.Radio_match.isChecked():
            print "doing match"
            html_list = rsearch_rtext.re_search_format_html(researched)
        else:
            print "doing replace"
            rlist = self.get_replace()
            replaced = textools.re_search_replace(researched, rlist, preview=True)
            html_list = rsearch_rtext.re_search_format_html(replaced)

        raw_html = richtext.get_str_formated_html(html_list)
        self.setHtml(raw_html)

        visible_pos = richtext.get_position(html_list, true_position=true_pos)[1]
        print "new visible pos", visible_pos
        self.set_text_cursor_pos(visible_pos, no_anchor=True)

        self._researched = researched
        self._html_list = html_list
        self._disable_signals = False