def _connect(self): PageTreeStore._connect(self) def on_tag_changed(o, tag, path, _): ''' If a tag is added to a page or removed from a page emit signal to update. ''' self._flush() treepath = self.get_treepath(path) treeiter = self.get_iter(treepath) self._pagenames_cache.pop(path.name, None) self.emit('row-changed', treepath, treeiter) def on_iconlist_changed(o, path): '''If an icon is changed update cache.''' self._flush() self._pagenames_cache.pop(path.name, None) self.connectto_all(self.index, ( ('tag-inserted', on_tag_changed), ('tag-removed', on_tag_changed) )) self.connectto(self.index_ext, 'iconlist-changed', on_iconlist_changed)
def __init__(self, index, index_ext, show_tags, icons_for_tags): self.index_ext = index_ext self.icons_for_tags = icons_for_tags self.show_tags = show_tags # Cache pagenames with tags. self._pagenames_cache = {} self._clear_cache_scheduled = False PageTreeStore.__init__(self, index)
def on_get_value(self, iter, column): '''Returns the data for a specific column''' if isinstance(iter, PageTreeTagIter): tag = iter.indextag if column == NAME_COL: return tag.name elif column == PATH_COL: return tag elif column == EMPTY_COL: return tag == self.untagged elif column == STYLE_COL: if tag == self.untagged: return pango.STYLE_ITALIC else: return pango.STYLE_NORMAL elif column == FGCOLOR_COL: if tag == self.untagged: return self.EMPTY_COLOR else: return self.NORMAL_COLOR elif column == WEIGHT_COL: return pango.WEIGHT_NORMAL # TODO: use this property to show tags in current page? elif column == N_CHILD_COL: return '' ## Due to multiple tag filtering this result is no good.. #~ if tag == self.untagged: #~ return str(self.index.n_list_untagged_root_pages()) #~ else: #~ return str(self.index.n_list_tagged_pages(tag)) else: return PageTreeStore.on_get_value(self, iter, column)
def on_get_value(self, iter, column): '''Returns the data for a specific column''' if isinstance(iter, PageTreeTagIter): tag = iter.indextag if column == NAME_COL: return tag.name elif column == TIP_COL: return encode_markup_text(tag.name) elif column == PATH_COL: return tag elif column == EMPTY_COL: return tag == self.untagged elif column == STYLE_COL: if tag == self.untagged: return pango.STYLE_ITALIC else: return pango.STYLE_NORMAL elif column == FGCOLOR_COL: if tag == self.untagged: return self.EMPTY_COLOR else: return self.NORMAL_COLOR elif column == WEIGHT_COL: return pango.WEIGHT_NORMAL # TODO: use this property to show tags in current page? elif column == N_CHILD_COL: return '' ## Due to multiple tag filtering this result is no good.. #~ if tag == self.untagged: #~ return str(self.index.n_list_untagged_root_pages()) #~ else: #~ return str(self.index.n_list_tagged_pages(tag)) else: return PageTreeStore.on_get_value(self, iter, column)
def get_treepaths(self, path): '''Return all treepaths matching notebook path 'path' Default implementation assumes we are a non-duplicate treeview after all and uses L{PageTreeStore.get_treepath()}. @implementation: must be overloaded by subclasses that are real duplicate stores ''' return [PageTreeStore.get_treepath(self, path)]
def on_iter_n_children(self, iter): '''Returns the number of children in a namespace. As a special case, when iter is None the number of pages in the root namespace is given. ''' if iter is None: return self.index.n_list_all_pages() else: return PageTreeStore.on_iter_n_children(self, iter)
def on_iter_has_child(self, iter): '''Returns True if the iter has children''' if isinstance(iter, PageTreeTagIter): if iter.indextag == self.untagged: return self.index.n_list_untagged_root_pages() > 0 else: return self.index.n_list_tagged_pages(iter.indextag) > 0 else: return PageTreeStore.on_iter_has_child(self, iter)
def on_get_value(self, iter, column): '''Returns the data for a specific column''' if column == NAME_COL and self.show_full_page_name: # Show top level pages with full contex if len(iter.treepath) == 1: return iter.indexpath.name else: return iter.indexpath.basename else: return PageTreeStore.on_get_value(self, iter, column)
def on_iter_n_children(self, iter): '''Returns the number of children in a namespace. As a special case, when iter is None the number of tags is given. ''' if iter is None: return self.index.n_list_all_tags() + 1 # +1 to include untagged elif isinstance(iter, PageTreeTagIter): if iter.indextag == self.untagged: return self.index.n_list_untagged_root_pages() else: return self.index.n_list_tagged_pages(iter.indextag) else: return PageTreeStore.on_iter_n_children(self, iter)
def _connect(self): PageTreeStore._connect(self) def on_tag_changed(o, tag, path, _): ''' If a tag is added to a page or removed from a page emit signal to update. ''' self._flush() treepath = self.get_treepath(path) treeiter = self.get_iter(treepath) self._pagenames_cache.pop(path.name, None) self.emit('row-changed', treepath, treeiter) def on_iconlist_changed(o, path): '''If an icon is changed update cache.''' self._flush() self._pagenames_cache.pop(path.name, None) self.connectto_all(self.index, (('tag-inserted', on_tag_changed), ('tag-removed', on_tag_changed))) self.connectto(self.index_ext, 'iconlist-changed', on_iconlist_changed)
def reload_model(self): '''Re-initialize the treeview model. This is called when reloading the index to get rid of out-of-sync model errors without need to close the app first. ''' assert self.uistate['treeview'] in ('tagged', 'tags') tags = [t.name for t in self.tagcloud.get_tag_filter()] if tags: model = TaggedPageTreeStore(self.index, tags, self.uistate['show_full_page_name']) elif self.uistate['treeview'] == 'tags': model = TagsPageTreeStore(self.index, (), self.uistate['show_full_page_name']) else: model = PageTreeStore(self.index) self.treeview.set_model(model)
def on_get_value(self, iter, column): '''Returns the data for a specific column''' if isinstance(iter, PageTreeTagIter): tag = iter.indextag if column == NAME_COL: return tag.name elif column == TIP_COL: return encode_markup_text(tag.name) elif column == PATH_COL: return tag elif column == EMPTY_COL: return tag == self.untagged elif column == STYLE_COL: if tag == self.untagged: return pango.STYLE_ITALIC else: return pango.STYLE_NORMAL elif column == FGCOLOR_COL: if tag == self.untagged: return self.EMPTY_COLOR else: return self.NORMAL_COLOR elif column == WEIGHT_COL: return pango.WEIGHT_NORMAL # TODO: use this property to show tags in current page? elif column == N_CHILD_COL: return '' ## Due to multiple tag filtering this result is no good.. #~ if tag == self.untagged: #~ return str(self.index.n_list_untagged_root_pages()) #~ else: #~ return str(self.index.n_list_tagged_pages(tag)) else: if column == NAME_COL and self.show_full_page_name: # Show top level pages with full contex # top level tree is tags, so top level pages len(path) is 2 if len(iter.treepath) <= 2: return iter.indexpath.name else: return iter.indexpath.basename else: return PageTreeStore.on_get_value(self, iter, column)
def __init__(self, index): self._reverse_cache = {} self.untagged = IndexTag(_('untagged'), -1) # T: label for untagged pages in side pane PageTreeStore.__init__(self, index)
def _flush(self): self._reverse_cache = {} return PageTreeStore._flush(self)
def __init__(self, index): PageTreeStore.__init__(self, index) self._reverse_cache = {}
def __init__(self, index, show_full_page_name=True): PageTreeStore.__init__(self, index) self._reverse_cache = {} self.show_full_page_name = show_full_page_name
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()
def on_get_value(self, iter, column): ''' Modify to return new pagenames, tooltips and icons. Columns 'NAME_COL', 'TIP_COL' and 'ICON_COL' use cache, other columns works with default methods. ''' path = iter.indexpath if (column == NAME_COL) or (column == TIP_COL): try: return self._pagenames_cache[path.name][column] except KeyError: pass elif column == ICON_COL: try: return render_icon(self._pagenames_cache[path.name][column]) except KeyError: pass else: return PageTreeStore.on_get_value(self, iter, column) # Value is not in cache. # Find tags, icons and put values to cache. tags = [a.name for a in self.index.list_tags(path)] icon = self.index_ext.get_icon(path) if icon: icon = ICONS.get(icon, ICONS[NO_IMAGE]) elif tags: # Check icons for tags. _icons = [ self.icons_for_tags[a] for a in tags if a in self.icons_for_tags ] if _icons: # Check whether all icons are the same. if len(set(_icons)) > 1: icon = ICONS[SEVERAL_ICONS] else: icon = ICONS[_icons[0]] elif path.haschildren: icon = ICONS[FOLDER_TAGS_ICON] else: icon = ICONS[FILE_TAGS_ICON] else: if path.haschildren: icon = ICONS[FOLDER_ICON] else: icon = ICONS[FILE_ICON] if tags and self.show_tags: # show tags after page name name = '{} ({})'.format(path.basename, ', '.join(tags)) else: name = path.basename self._pagenames_cache[path.name] = { NAME_COL: name, TIP_COL: encode_markup_text(name), ICON_COL: icon } # Launch function to clear cache. if not self._clear_cache_scheduled: self._clear_pagenames_cache() return self.on_get_value(iter, column)
def __init__(self, index, show_full_page_name=True): self._reverse_cache = {} self.show_full_page_name = show_full_page_name self.untagged = IndexTag(_('untagged'), -1) # T: label for untagged pages in side pane PageTreeStore.__init__(self, index)
def on_get_value(self, iter, column): ''' Modify to return new pagenames, tooltips and icons. Columns 'NAME_COL', 'TIP_COL' and 'ICON_COL' use cache, other columns works with default methods. ''' path = iter.indexpath if (column == NAME_COL) or (column == TIP_COL): try: return self._pagenames_cache[path.name][column] except KeyError: pass elif column == ICON_COL: try: return render_icon(self._pagenames_cache[path.name][column]) except KeyError: pass else: return PageTreeStore.on_get_value(self, iter, column) # Value is not in cache. # Find tags, icons and put values to cache. tags = [a.name for a in self.index.list_tags(path)] icon = self.index_ext.get_icon(path) if icon: icon = ICONS.get(icon, ICONS[NO_IMAGE]) elif tags: # Check icons for tags. _icons = [self.icons_for_tags[a] for a in tags if a in self.icons_for_tags] if _icons: # Check whether all icons are the same. if len( set(_icons) ) > 1: icon = ICONS[SEVERAL_ICONS] else: icon = ICONS[_icons[0]] elif path.haschildren: icon = ICONS[FOLDER_TAGS_ICON] else: icon = ICONS[FILE_TAGS_ICON] else: if path.haschildren: icon = ICONS[FOLDER_ICON] else: icon = ICONS[FILE_ICON] if tags and self.show_tags: # show tags after page name name = '{} ({})'.format(path.basename, ', '.join(tags)) else: name = path.basename self._pagenames_cache[path.name] = {NAME_COL: name, TIP_COL: encode_markup_text(name), ICON_COL: icon} # Launch function to clear cache. if not self._clear_cache_scheduled: self._clear_pagenames_cache() return self.on_get_value(iter, column)