Ejemplo n.º 1
0
    def update_search(self):
        self._search_timeout = 0
        from translate.tools.pogrep import GrepFilter
        self.filter = GrepFilter(
            searchstring=unicode(self.ent_search.get_text()),
            searchparts=('source', 'target'),
            ignorecase=not self.chk_casesensitive.get_active(),
            useregexp=self.chk_regex.get_active(),
            max_matches=self.MAX_RESULTS)
        store_units = self.storecursor.model.get_units()
        self.matches, indexes = self.filter.getmatches(store_units)
        self.matchcursor = Cursor(self.matches, range(len(self.matches)))

        logging.debug('Search text: %s (%d matches)' %
                      (self.ent_search.get_text(), len(indexes)))

        if indexes:
            self.ent_search.modify_base(gtk.STATE_NORMAL, self.default_base)
            self.ent_search.modify_text(gtk.STATE_NORMAL, self.default_text)

            self.storecursor.indices = indexes
            # Select initial match for in the current unit.
            match_index = 0
            selected_unit = self.storecursor.model[self.storecursor.index]
            for match in self.matches:
                if match.unit is selected_unit:
                    break
                match_index += 1
            self.matchcursor.index = match_index
        else:
            if self.ent_search.get_text():
                self.ent_search.modify_base(
                    gtk.STATE_NORMAL,
                    gtk.gdk.color_parse(current_theme['warning_bg']))
                self.ent_search.modify_text(gtk.STATE_NORMAL,
                                            gtk.gdk.color_parse('#fff'))
            else:
                self.ent_search.modify_base(gtk.STATE_NORMAL,
                                            self.default_base)
                self.ent_search.modify_text(gtk.STATE_NORMAL,
                                            self.default_text)

            self.filter.re_search = None
            # Act like the "Default" mode...
            self.storecursor.indices = self.storecursor.model.stats['total']
        self._highlight_matches()

        def grabfocus():
            curpos = self.ent_search.props.cursor_position
            self.ent_search.grab_focus()
            # that will select all text, so reset the cursor position
            self.ent_search.set_position(curpos)
            return False

        gobject.idle_add(grabfocus)