Show correct values for average rating of hierarchical items in the Tag Browser

This commit is contained in:
Kovid Goyal 2015-11-23 12:54:57 +05:30
parent 9f9655d812
commit b4f84a8fb4
2 changed files with 35 additions and 18 deletions

View File

@ -72,7 +72,7 @@ class TagTreeItem(object): # {{{
self.icon_state_map[0] = data.icon self.icon_state_map[0] = data.icon
self.tag = data self.tag = data
self.tooltip = (tooltip + ' ') if tooltip else '' self.tooltip = tooltip or ''
def break_cycles(self): def break_cycles(self):
del self.parent del self.parent
@ -96,6 +96,25 @@ class TagTreeItem(object): # {{{
child.parent = self child.parent = self
self.children.append(child) self.children.append(child)
@property
def average_rating(self):
if self.type != self.TAG:
return 0
if not self.tag.is_hierarchical:
return self.tag.avg_rating
if not self.children:
return self.tag.avg_rating # leaf node, avg_rating is correct
total = num = 0
for child in self.children:
r = child.average_rating
if r:
total += 1
num += r
if self.tag.avg_rating:
total += 1
num += self.tag.avg_rating
return num/float(total)
def data(self, role): def data(self, role):
if role == Qt.UserRole: if role == Qt.UserRole:
return self return self
@ -116,8 +135,8 @@ class TagTreeItem(object): # {{{
return self.icon return self.icon
if role == Qt.FontRole: if role == Qt.FontRole:
return bf() return bf()
if role == Qt.ToolTipRole and self.tooltip is not None: if role == Qt.ToolTipRole:
return (self.tooltip) return self.tooltip
if role == DRAG_IMAGE_ROLE: if role == DRAG_IMAGE_ROLE:
return self.icon return self.icon
return None return None
@ -126,13 +145,11 @@ class TagTreeItem(object): # {{{
tag = self.tag tag = self.tag
if tag.use_sort_as_name: if tag.use_sort_as_name:
name = tag.sort name = tag.sort
tt_author = True
else: else:
if not tag.is_hierarchical: if not tag.is_hierarchical:
name = tag.original_name name = tag.original_name
else: else:
name = tag.name name = tag.name
tt_author = False
if role == Qt.DisplayRole: if role == Qt.DisplayRole:
count = len(self.id_set) count = len(self.id_set)
count = count if count > 0 else tag.count count = count if count > 0 else tag.count
@ -145,15 +162,14 @@ class TagTreeItem(object): # {{{
if role == Qt.DecorationRole: if role == Qt.DecorationRole:
return self.icon_state_map[tag.state] return self.icon_state_map[tag.state]
if role == Qt.ToolTipRole: if role == Qt.ToolTipRole:
if tt_author: tt = [self.tooltip] if self.tooltip else []
if tag.tooltip is not None: tt.append('%s:%s' % (tag.category, tag.original_name))
return ('(%s) %s'%(tag.name, tag.tooltip)) ar = self.average_rating
else: if ar:
return (tag.name) tt.append(_('Average rating for books in this category: %.1f') % ar)
if tag.tooltip: elif self.type == self.TAG and ar is not None:
return (self.tooltip + tag.tooltip) tt.append(_('Books in this category are unrated'))
else: return '\n'.join(tt)
return (self.tooltip)
if role == DRAG_IMAGE_ROLE: if role == DRAG_IMAGE_ROLE:
return self.icon_state_map[0] return self.icon_state_map[0]
return None return None
@ -172,7 +188,7 @@ class TagTreeItem(object): # {{{
name = tag.name name = tag.name
count = len(self.id_set) count = len(self.id_set)
count = count if count > 0 else tag.count count = count if count > 0 else tag.count
rating = tag.avg_rating or 0 rating = self.average_rating
if rating: if rating:
rating = ',rating=%.1f' % rating rating = ',rating=%.1f' % rating
return fmt % (name, count, rating or '') return fmt % (name, count, rating or '')

View File

@ -42,13 +42,14 @@ class TagDelegate(QStyledItemDelegate): # {{{
painter.restore() painter.restore()
if item.type != TagTreeItem.TAG: if item.type != TagTreeItem.TAG:
return return
if (item.tag.state == 0 and config['show_avg_rating'] and if item.tag.state == 0 and config['show_avg_rating']:
item.tag.avg_rating is not None): rating = item.average_rating
if rating is None:
return
r = style.subElementRect(style.SE_ItemViewItemDecoration, r = style.subElementRect(style.SE_ItemViewItemDecoration,
option, widget) option, widget)
icon = option.icon icon = option.icon
painter.save() painter.save()
rating = item.tag.avg_rating
nr = r.adjusted(0, 0, 0, 0) nr = r.adjusted(0, 0, 0, 0)
nr.setBottom(r.bottom()-int(r.height()*(rating/5.0))) nr.setBottom(r.bottom()-int(r.height()*(rating/5.0)))
painter.setClipRect(nr) painter.setClipRect(nr)