def _reset_document_tree_model(self, extra_expand=None): dtm = self.document_tree_model dt = self.document_tree # get all the paths to the files inside the expanded directories so # we can expand the tree again after we rebuild it expanded_dirs = [] expanded_files = [] def get_expanded(model, path, iterrow): if dt.row_expanded(path): expanded_dirs.append(dtm.get_value(iterrow, 1)) dtm.foreach(get_expanded) if expanded_dirs: for v in self.views: location = v.document.location if location: dirname = os.path.dirname(location) + os.path.sep if dirname in expanded_dirs: expanded_files.append(location) # now clear the tree dtm.clear() # add all the document locations into the tree root = RootDirNode() for v in self.views: if v.document.location: root.add(v.document.location) else: # add unsaved documents to the top row = (v.document.description, v.document.description, True) dtm.append(None, row) root.collapse() node_dict = {} # hmm.. should we keep this around? def add(node, parent_path): if parent_path: parent = node_dict[parent_path] else: parent = None name = node.path.replace(parent_path, '', 1) path = node.path is_file = isinstance(node, FileNode) node_dict[path] = dtm.append(parent, (name, path, is_file)) if root.dirs or root.files: root.walk(add) if extra_expand: expanded_files.append(extra_expand) # expand up to all the files we had expanded before, put the cursor on # the selected file #doc = self.textarea.view.document def set_expanded(model, path, iterrow): filepath = dtm.get_value(iterrow, 1) if filepath in expanded_files: iterparent = dtm.iter_parent(iterrow) path = dtm.get_path(iterparent) dt.expand_to_path(path) dtm.foreach(set_expanded)
def test_collapse(): root = RootDirNode() root.add(u'/home/leroux/test.txt') root.add(u'/home/leroux/projects/ni/hello.py') root.add(u'/home/leroux/projects/ni/sub/folder/filename.py') root.collapse() paths = [] def callback(node, parent_path): paths.append(node.path) root.walk(callback) assert len(paths) == 6