Tag browser: Fix preservation of state when recounting and the visible categories have changed. Fixes #1696596 [VL library tag browser category expansion not consistent](https://bugs.launchpad.net/calibre/+bug/1696596)

This commit is contained in:
Kovid Goyal 2017-06-14 12:53:46 +05:30
parent 3f0954f0c7
commit acff645c32
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 42 additions and 7 deletions

View File

@ -83,6 +83,14 @@ class TagTreeItem(object): # {{{
self.tooltip = tooltip or ''
@property
def name_id(self):
if self.type == self.CATEGORY:
return self.category_key + ':' + self.name
elif self.type == self.TAG:
return self.tag.original_name
return ''
def break_cycles(self):
del self.parent
del self.children
@ -104,10 +112,9 @@ class TagTreeItem(object): # {{{
if self.type == self.ROOT:
return 'ROOT'
if self.type == self.CATEGORY:
return 'CATEGORY:'+str(
self.name)+':%d'%len(getattr(self,
'children', []))
return 'TAG: %s'%self.tag.name
return 'CATEGORY(category_key={!r}, name={!r}, num_children={!r})'.format(
self.category_key, self.name, len(self.children))
return 'TAG(name=%r)'%self.tag.name
def row(self):
if self.parent is not None:
@ -1039,7 +1046,7 @@ class TagsModel(QAbstractItemModel): # {{{
return idx
def index_for_category(self, name):
for row, category in enumerate(self.category_nodes):
for row, category in enumerate(self.root_item.children):
if category.category_key == name:
return self.index(row, 0, QModelIndex())
@ -1203,6 +1210,31 @@ class TagsModel(QAbstractItemModel): # {{{
def supportedDropActions(self):
return Qt.CopyAction|Qt.MoveAction
def named_path_for_index(self, index):
ans = []
while index.isValid():
node = self.get_node(index)
if node is self.root_item:
break
ans.append(node.name_id)
index = self.parent(index)
return ans
def index_for_named_path(self, named_path):
parent = self.root_item
ipath = []
path = named_path[:]
while path:
q = path.pop()
for i, c in enumerate(parent.children):
if c.name_id == q:
ipath.append(i)
parent = c
break
else:
break
return self.index_for_path(ipath)
def path_for_index(self, index):
ans = []
while index.isValid():

View File

@ -770,7 +770,7 @@ class TagsView(QTreeView): # {{{
ci = self.currentIndex()
if not ci.isValid():
ci = self.indexAt(QPoint(10, 10))
path = self.model().path_for_index(ci) if self.is_visible(ci) else None
path = self.model().named_path_for_index(ci) if self.is_visible(ci) else None
expanded_categories, state_map = self.get_state()
self._model.rebuild_node_tree(state_map=state_map)
self.blockSignals(True)
@ -778,7 +778,10 @@ class TagsView(QTreeView): # {{{
idx = self._model.index_for_category(category)
if idx is not None and idx.isValid():
self.expand(idx)
self.show_item_at_path(path)
if path is not None:
index = self._model.index_for_named_path(path)
if index.isValid():
self.show_item_at_index(index)
self.blockSignals(False)
def show_item_at_path(self, path, box=False,