From 6e86fb0698cda203dfabe17051a6a47875fbe952 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Tue, 23 Feb 2021 11:01:33 +0530 Subject: [PATCH] Edit book: File browser: Show total size of items in category when hovering over category with mouse --- src/calibre/gui2/tweak_book/file_list.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/calibre/gui2/tweak_book/file_list.py b/src/calibre/gui2/tweak_book/file_list.py index 1d7a28ab20..babc66ff5d 100644 --- a/src/calibre/gui2/tweak_book/file_list.py +++ b/src/calibre/gui2/tweak_book/file_list.py @@ -166,14 +166,21 @@ class ItemDelegate(QStyledItemDelegate): # {{{ def paint(self, painter, option, index): top_level = not index.parent().isValid() hover = option.state & QStyle.StateFlag.State_MouseOver + cc = current_container() + + def safe_size(index): + try: + return cc.filesize(str(index.data(NAME_ROLE) or '')) + except OSError: + return 0 + if hover: if top_level: - suffix = '%s(%d)' % (NBSP, index.model().rowCount(index)) + count = index.model().rowCount(index) + total_size = human_readable(sum(safe_size(index.child(r, 0)) for r in range(count))) + suffix = f'{NBSP}{count}@{total_size}' else: - try: - suffix = NBSP + human_readable(current_container().filesize(unicode_type(index.data(NAME_ROLE) or ''))) - except EnvironmentError: - suffix = NBSP + human_readable(0) + suffix = NBSP + human_readable(safe_size(index)) br = painter.boundingRect(option.rect, Qt.AlignmentFlag.AlignRight|Qt.AlignmentFlag.AlignVCenter, suffix) if top_level and index.row() > 0: option.rect.adjust(0, 5, 0, 0)