def test_search_tree_model(self):
        """
        Test bauble.utils.search_tree_model
        """
        import gtk
        model = gtk.TreeStore(str)

        # the rows that should be found
        to_find = []

        row = model.append(None, ['1'])
        model.append(row, ['1.1'])
        to_find.append(model.append(row, ['something']))
        model.append(row, ['1.3'])

        row = model.append(None, ['2'])
        to_find.append(model.append(row, ['something']))
        model.append(row, ['2.1'])

        to_find.append(model.append(None, ['something']))

        root = model.get_iter_root()
        results = utils.search_tree_model(model[root], 'something')
        self.assert_(sorted([model.get_path(r) for r in results]),
                     sorted(to_find))
Beispiel #2
0
    def on_entry_changed(entry, presenter):
        logger.debug('on_entry_changed(%s, %s)', entry, presenter)
        text = utils.utf8(entry.props.text)

        if not text and not required:
            presenter.remove_problem(PROBLEM, entry)
            on_select(None)
            return
        # see if the text matches a completion string
        comp = entry.get_completion()
        compl_model = comp.get_model()

        def _cmp(row, data):
            return utils.utf8(row[0]) == data

        found = utils.search_tree_model(compl_model, text, _cmp)
        if len(found) == 1:
            comp.emit('match-selected', compl_model, found[0])
            return True
        # if text looks like '(code) name', then split it into the two
        # parts, then see if the text matches exactly a code or name
        match = re_code_name_splitter.match(text)
        if match:
            code, name = match.groups()
        else:
            code = name = text
        codes = presenter.session.query(Location).\
            filter(utils.ilike(Location.code, '%s' % utils.utf8(code)))
        names = presenter.session.query(Location).\
            filter(utils.ilike(Location.name, '%s' % utils.utf8(name)))
        if codes.count() == 1:
            logger.debug('location matches code')
            location = codes.first()
            presenter.remove_problem(PROBLEM, entry)
            on_select(location)
        elif names.count() == 1:
            logger.debug('location matches name')
            location = names.first()
            presenter.remove_problem(PROBLEM, entry)
            on_select(location)
        else:
            logger.debug('location %s does not match anything' % text)
            presenter.add_problem(PROBLEM, entry)
        return True
 def on_test_expand_row(self, view, treeiter, path, data=None):
     '''
     Look up the table type of the selected row and if it has
     any children then add them to the row
     '''
     model = view.get_model()
     row = model.get_value(treeiter, 0)
     view.collapse_row(path)
     self.remove_children(model, treeiter)
     try:
         kids = self.row_meta[type(row)].get_children(row)
         if len(kids) == 0:
             return True
     except saexc.InvalidRequestError, e:
         logger.debug(utils.utf8(e))
         model = self.results_view.get_model()
         for found in utils.search_tree_model(model, row):
             model.remove(found)
         return True
Beispiel #4
0
 def on_test_expand_row(self, view, treeiter, path, data=None):
     '''
     Look up the table type of the selected row and if it has
     any children then add them to the row
     '''
     expand = False
     model = view.get_model()
     row = model.get_value(treeiter, 0)
     view.collapse_row(path)
     self.remove_children(model, treeiter)
     try:
         kids = self.view_meta[type(row)].get_children(row)
         if len(kids) == 0:
             return True
     except saexc.InvalidRequestError, e:
         logger.debug(utils.utf8(e))
         model = self.results_view.get_model()
         for found in utils.search_tree_model(model, row):
             model.remove(found)
         return True
        def on_changed(entry, *args):
            text = entry.get_text()
            comp = entry.get_completion()
            comp_model = comp.get_model()
            found = []
            if comp_model:
                # search the tree model to see if the text in the
                # entry matches one of the completions, if so then
                # emit the match-selected signal, this allows us to
                # type a match in the entry without having to select
                # it from the popup
                def _cmp(row, data):
                    return utils.utf8(row[0]) == text
                found = utils.search_tree_model(comp_model, text, _cmp)
                if len(found) == 1:
                    v = comp.get_model()[found[0]][0]
                    # only auto select if the full string has been entered
                    if text.lower() == utils.utf8(v).lower():
                        comp.emit('match-selected', comp.get_model(), found[0])
                    else:
                        found = None

            if text != '' and not found and PROBLEM not in self.problems:
                self.add_problem(PROBLEM, widget)
                on_select(None)

            key_length = widget.get_completion().props.minimum_key_length
            if (not comp_model and len(text)>key_length) or \
                    len(text) == key_length:
                add_completions(text)

            # if entry is empty select nothing and remove all problem
            if text == '':
                on_select(None)
                self.remove_problem(PROBLEM, widget)

            return True
Beispiel #6
0
def select_in_search_results(obj):
    """
    :param obj: the object the select
    @returns: a gtk.TreeIter to the selected row

    Search the tree model for obj if it exists then select it if not
    then add it and select it.

    The the obj is not in the model then we add it.
    """
    check(obj is not None, 'select_in_search_results: arg is None')
    view = bauble.gui.get_view()
    if not isinstance(view, SearchView):
        return None
    model = view.results_view.get_model()
    found = utils.search_tree_model(model, obj)
    row_iter = None
    if len(found) > 0:
        row_iter = found[0]
    else:
        row_iter = model.append(None, [obj])
        model.append(row_iter, ['-'])
    view.results_view.set_cursor(model.get_path(row_iter))
    return row_iter
Beispiel #7
0
def select_in_search_results(obj):
    """
    :param obj: the object the select
    @returns: a gtk.TreeIter to the selected row

    Search the tree model for obj if it exists then select it if not
    then add it and select it.

    The the obj is not in the model then we add it.
    """
    check(obj is not None, 'select_in_search_results: arg is None')
    view = bauble.gui.get_view()
    if not isinstance(view, SearchView):
        return None
    model = view.results_view.get_model()
    found = utils.search_tree_model(model, obj)
    row_iter = None
    if len(found) > 0:
        row_iter = found[0]
    else:
        row_iter = model.append(None, [obj])
        model.append(row_iter, ['-'])
    view.results_view.set_cursor(model.get_path(row_iter))
    return row_iter
Beispiel #8
0
 def remove():
     model = self.results_view.get_model()
     self.results_view.set_model(None)  # detach model
     for found in utils.search_tree_model(model, value):
         model.remove(found)
     self.results_view.set_model(model)
Beispiel #9
0
 def remove():
     model = self.results_view.get_model()
     self.results_view.set_model(None)  # detach model
     for found in utils.search_tree_model(model, value):
         model.remove(found)
     self.results_view.set_model(model)