def __init__(self, ui, model=None): PageTreeView.__init__(self, ui) self.set_name('zim-tags-pagelist') self._tag_filter = None if model: self.set_model(model)
def do_initialize_popup(self, menu): ''' Disable some items in popup menu for only tagged pages mode. Although it is possible to leave it as is but it is not recommended to create or change page names since not all pages are shown. ''' model = self.get_model() if not isinstance(model, gtk.TreeModelFilter): PageTreeView.do_initialize_popup(self, menu) return path = self.get_selected_path() or Path(':') item = gtk.MenuItem(_('Open in New Window')) item.connect('activate', lambda o: self.ui.open_new_window(path)) menu.append(item) menu.append(gtk.SeparatorMenuItem()) item = gtk.ImageMenuItem('gtk-copy') item.connect('activate', lambda o: self.do_copy()) menu.append(item) self.populate_popup_expand_collapse(menu) menu.show_all()
def set_model(self, model, _show_tagged=False): ''' Set the model to be used. :param _show_tagged: if True enable tagged mode. ''' # disconnect previous model oldmodel = self.get_model() if oldmodel: if isinstance(oldmodel, gtk.TreeModelFilter): oldmodel = oldmodel.get_model() oldmodel.disconnect_index() if _show_tagged: model = self._init_modelfilter(model) PageTreeView.set_model(self, model)
def set_model(self, model, _show_tagged = False): ''' Set the model to be used. :param _show_tagged: if True enable tagged mode. ''' # disconnect previous model oldmodel = self.get_model() if oldmodel: if isinstance(oldmodel, gtk.TreeModelFilter): oldmodel = oldmodel.get_model() oldmodel.disconnect_index() if _show_tagged: model = self._init_modelfilter(model) PageTreeView.set_model(self, model)
def set_current_page(self, path, vivificate=False): ''' Ensure that parent with tagged pages will autoexpand if clicked on some of its children. Also added new view options. :param path: a notebook L{Path} object for the page. ''' model = self.get_model() if model is None: return None # index not yet initialized ... if self.view == 'collapse': # Collapse all other pages and expand only current page. self.collapse_all() if (not isinstance(model, gtk.TreeModelFilter)) or model.get_treepath(path): return PageTreeView.set_current_page(self, path, vivificate) # Path may be invisible due to modelfilter. if path.parent and (not path.parent.isroot) and (self.view != 'disable'): # Expand parent path if it is available. parent_treepath = model.get_treepath(path.parent) if parent_treepath: self.expand_to_path(parent_treepath) return None
def set_current_page(self, path, vivificate = False): ''' Ensure that parent with tagged pages will autoexpand if clicked on some of its children. Also added new view options. :param path: a notebook L{Path} object for the page. ''' model = self.get_model() if model is None: return None # index not yet initialized ... if self.view == 'collapse': # Collapse all other pages and expand only current page. self.collapse_all() if (not isinstance(model, gtk.TreeModelFilter)) or model.get_treepath(path): return PageTreeView.set_current_page(self, path, vivificate) # Path may be invisible due to modelfilter. if path.parent and (not path.parent.isroot) and (self.view != 'disable'): # Expand parent path if it is available. parent_treepath = model.get_treepath(path.parent) if parent_treepath: self.expand_to_path(parent_treepath) return None
def set_model(self, model, filter=None): '''Set the model to be used''' # disconnect previous model oldmodel = self.get_model() if oldmodel: childmodel = oldmodel.get_model() childmodel.disconnect_index() # Filter is also provided here, just to make it more efficient to # set model and filter in one go without need for refilter self._set_tag_filter(filter) # set new model def func(model, iter): index = self.ui.notebook.index if self._tag_filter is None: return True # no filtering else: iter = model.get_user_data(iter) if len(iter.treepath) > model.filter_depth: return True # deeper levels are not filtered at all else: if isinstance(iter, PageTreeTagIter): # -> tag return iter.indextag in self._tag_filter[1] # show filtered tags else: # PageTreeIter -> page tags = frozenset(index.list_tags(iter.indexpath)) return tags >= self._tag_filter[0] # match all selected tags filtermodel = model.filter_new(root = None) filtermodel.set_visible_func(func) # HACK add some methods and attributes # (can not subclass gtk.TreeModelFilter because it lacks a constructor) def get_indexpath(treeiter): childiter = filtermodel.convert_iter_to_child_iter(treeiter) if childiter: return model.get_indexpath(childiter) else: return None def get_treepath(path): for treepath in model.get_treepaths(path): filtered = filtermodel.convert_child_path_to_path(treepath) if not filtered is None: return filtered else: return None def get_treepaths(path): treepaths = model.get_treepaths(path) if treepaths: treepaths = map(filtermodel.convert_child_path_to_path, treepaths) return tuple(t for t in treepaths if not t is None) else: return () filtermodel.get_indexpath = get_indexpath filtermodel.get_treepath = get_treepath filtermodel.get_treepaths = get_treepaths filtermodel.index = model.index filtermodel.select_page = model.select_page PageTreeView.set_model(self, filtermodel)
def set_model(self, model, filter=None): '''Set the model to be used''' # disconnect previous model oldmodel = self.get_model() if oldmodel: childmodel = oldmodel.get_model() childmodel.disconnect_index() # Filter is also provided here, just to make it more efficient to # set model and filter in one go without need for refilter self._set_tag_filter(filter) # set new model def func(model, iter): index = self.ui.notebook.index if self._tag_filter is None: return True # no filtering else: iter = model.get_user_data(iter) if len(iter.treepath) > model.filter_depth: return True # deeper levels are not filtered at all else: if isinstance(iter, PageTreeTagIter): # -> tag return iter.indextag in self._tag_filter[ 1] # show filtered tags else: # PageTreeIter -> page tags = frozenset(index.list_tags(iter.indexpath)) return tags >= self._tag_filter[ 0] # match all selected tags filtermodel = model.filter_new(root=None) filtermodel.set_visible_func(func) # HACK add some methods and attributes # (can not subclass gtk.TreeModelFilter because it lacks a constructor) def get_indexpath(treeiter): childiter = filtermodel.convert_iter_to_child_iter(treeiter) if childiter: return model.get_indexpath(childiter) else: return None def get_treepath(path): for treepath in model.get_treepaths(path): filtered = filtermodel.convert_child_path_to_path(treepath) if not filtered is None: return filtered else: return None def get_treepaths(path): treepaths = model.get_treepaths(path) if treepaths: treepaths = map(filtermodel.convert_child_path_to_path, treepaths) return tuple(t for t in treepaths if not t is None) else: return () filtermodel.get_indexpath = get_indexpath filtermodel.get_treepath = get_treepath filtermodel.get_treepaths = get_treepaths filtermodel.index = model.index filtermodel.select_page = model.select_page PageTreeView.set_model(self, filtermodel)
def runTest(self): '''Test PageTreeStore index interface''' # This is one big test instead of seperate sub tests because in the # subclass we generate a file based notebook in setUp, and we do not # want to do that many times self.index.update() treestore = PageTreeStore(self.index) self.assertEqual(treestore.get_flags(), 0) self.assertEqual(treestore.get_n_columns(), 5) treeview = PageTreeView(None) # just run hidden to check errors treeview.set_model(treestore) n = treestore.on_iter_n_children() self.assertTrue(n > 0) n = treestore.iter_n_children(None) self.assertTrue(n > 0) # Quick check for basic methods path = treestore.on_get_iter((0,)) self.assertTrue(isinstance(path, IndexPath) and not path.isroot) basename = treestore.on_get_value(path, 0) self.assertTrue(len(basename) > 0) self.assertEqual(treestore.get_treepath(path), (0,)) path2 = treestore.on_iter_children() self.assertEqual(path2, path) self.assertTrue(treestore.on_get_iter((20,20,20,20,20)) is None) self.assertRaises( ValueError, treestore.get_treepath, Path('nonexisting')) # Now walk through the whole notebook testing the API # with nested pages and stuff path = [] for page in self.notebook.walk(): names = page.name.split(':') if len(names) > len(path): path.append(0) # always increment by one elif len(names) < len(path): while len(names) < len(path): path.pop() path[-1] += 1 else: path[-1] += 1 #~ print '>>', page, path iter = treestore.get_iter(tuple(path)) indexpath = treestore.get_indexpath(iter) #~ print '>>>', indexpath self.assertEqual(indexpath, page) self.assertEqual( treestore.get_value(iter, 0), page.basename) self.assertEqual( treestore.get_path(iter), tuple(path)) if indexpath.haschildren: self.assertTrue(treestore.iter_has_child(iter)) child = treestore.iter_children(iter) self.assertTrue(not child is None) child = treestore.iter_nth_child(iter, 0) self.assertTrue(not child is None) parent = treestore.iter_parent(child) self.assertEqual( treestore.get_indexpath(parent), page) childpath = treestore.get_path(child) self.assertEqual( childpath, tuple(path) + (0,)) n = treestore.iter_n_children(iter) for i in range(1, n): child = treestore.iter_next(child) childpath = treestore.get_path(child) self.assertEqual( childpath, tuple(path) + (i,)) child = treestore.iter_next(child) self.assertTrue(child is None) else: self.assertTrue(not treestore.iter_has_child(iter)) child = treestore.iter_children(iter) self.assertTrue(child is None) child = treestore.iter_nth_child(iter, 0) self.assertTrue(child is None) # Check if all the signals go OK del treestore self.index.flush() treestore = PageTreeStore(self.index) self.index.update()