def sort(self, ids): ids2pos = {} pos = 0 new_order = [] for record in self.group: ids2pos[record.id] = pos new_order.append(pos) pos += 1 pos = 0 for obj_id in ids: try: old_pos = ids2pos[obj_id] if old_pos != pos: new_order[old_pos] = pos pos += 1 except KeyError: continue self.group.sort(lambda x, y: cmp(new_order[ids2pos[x.id]], new_order[ids2pos[y.id]])) prev = None for record in self.group: if prev: prev.next[id(self.group)] = record prev = record if prev: prev.next[id(self.group)] = None path = gtk.TreePath() # XXX pygobject does not allow to create empty TreePath, # it is always a path of 0 # see: https://bugzilla.gnome.org/show_bug.cgi?id=770665 if hasattr(path, 'get_depth'): while path.get_depth(): path.up() self.rows_reordered(path, None, new_order)
def expand_nodes(self, nodes): model = self.treeview.get_model() # JCA : Manage always_expand attribute to force tree expansion if self.view_type == 'tree' and self.always_expand: group = model.group def get_all_sub_records(group, record, cur_expand_path, to_expand): if group is None: try: group = record.children_group( model.children_field, model.children_definitions) except AttributeError: return if group is None: return cur_expand_path.append(0) for i in range(len(group)): cur_expand_path[-1] = i to_expand += [list(cur_expand_path)] get_all_sub_records(None, group[i], cur_expand_path, to_expand) cur_expand_path.pop(-1) cur_expand_path = [] to_expand = [] get_all_sub_records(group, None, cur_expand_path, to_expand) for path in to_expand: tree_path = gtk.TreePath.new_from_indices(path) self.treeview.expand_to_path(tree_path) else: for node in nodes: expand_path = path_convert_id2pos(model, node) if expand_path: self.treeview.expand_to_path(gtk.TreePath(expand_path))
def set_cursor(self, new=False, reset_view=True): self.treeview.grab_focus() model = self.treeview.get_model() if self.screen.current_record and model: path = self.screen.current_record.get_index_path(model.group) if model.get_flags() & gtk.TREE_MODEL_LIST_ONLY: path = (path[0], ) focus_column = self.treeview.next_column(path, editable=new) if path[:-1]: self.treeview.expand_to_path(gtk.TreePath(path[:-1])) self.treeview.scroll_to_cell(path, focus_column, use_align=False) current_path = self.treeview.get_cursor()[0] selected_path = \ self.treeview.get_selection().get_selected_rows()[1] path = gtk.TreePath(path) if (current_path != path and path not in selected_path) or new: self.treeview.set_cursor(path, focus_column, new)
def select_nodes(self, nodes): selection = self.treeview.get_selection() if not nodes: return selection.unselect_all() scroll = False model = self.treeview.get_model() for node in nodes: path = path_convert_id2pos(model, node) if path: selection.select_path(gtk.TreePath(path)) if not scroll: self.treeview.scroll_to_cell(path) scroll = True
def get_expanded_paths(self, starting_path=None, starting_id_path=None): # Use id instead of position # because the position may change between load if not starting_path: starting_path = tuple() if not starting_id_path: starting_id_path = [] id_paths = [] model = self.treeview.get_model() if starting_path: iter_ = model.get_iter(gtk.TreePath(starting_path)) else: iter_ = None for path_idx in range(model.iter_n_children(iter_)): path = starting_path + (path_idx, ) expanded = self.treeview.row_expanded(gtk.TreePath(path)) if expanded: iter_ = model.get_iter(path) expanded_record = model.get_value(iter_, 0) id_path = starting_id_path + [expanded_record.id] id_paths.append(id_path) child_id_paths = self.get_expanded_paths(path, id_path) id_paths += child_id_paths return id_paths
def _action_cell_changed_cb(self, combo, path_string, new_iter, *etc): action_name = self.action_liststore.get_value( new_iter, self.action_liststore_value_column) iter = self.liststore.get_iter(path_string) self.liststore.set_value(iter, self.action_column, action_name) self.treeview.columns_autosize() # If we don't have a button-press name yet, edit that next bp_name = self.liststore.get_value(iter, self.bp_column) if bp_name is None: focus_col = self.treeview.get_column(self.bp_column) if gtk2compat.USE_GTK3: tree_path = gtk.TreePath(path_string) else: tree_path = path_string self.treeview.set_cursor_on_cell(tree_path, focus_col, None, True)
def sort(self, ids): old_idx = {record.id: i for i, record in enumerate(self.group)} new_idx = {id_: i for i, id_ in enumerate(ids)} self.group.sort(key=lambda r: new_idx.get(r.id)) new_order = [] prev = None for record in self.group: new_order.append(old_idx.get(record.id)) if prev: prev.next[id(self.group)] = record prev = record if prev: prev.next[id(self.group)] = None path = gtk.TreePath() # XXX pygobject does not allow to create empty TreePath, # it is always a path of 0 # see: https://bugzilla.gnome.org/show_bug.cgi?id=770665 if hasattr(path, 'get_depth'): while path.get_depth(): path.up() self.rows_reordered(path, None, new_order)
def expand_nodes(self, nodes): model = self.treeview.get_model() for node in nodes: expand_path = path_convert_id2pos(model, node) if expand_path: self.treeview.expand_to_path(gtk.TreePath(expand_path))