From d377eedf52e9c382ab474088a6168ceebe8c204d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 9 Jan 2022 07:08:39 +0530 Subject: [PATCH] Work on using icon themes --- .../calibre-default-dark/index.theme | 23 +++++++++++ .../calibre-default-light/index.theme | 23 +++++++++++ src/calibre/gui2/__init__.py | 40 +++++++++++++++---- .../progress_indicator/QProgressIndicator.cpp | 6 +-- 4 files changed, 79 insertions(+), 13 deletions(-) create mode 100644 resources/icon-themes/calibre-default-dark/index.theme create mode 100644 resources/icon-themes/calibre-default-light/index.theme diff --git a/resources/icon-themes/calibre-default-dark/index.theme b/resources/icon-themes/calibre-default-dark/index.theme new file mode 100644 index 0000000000..72fc6819af --- /dev/null +++ b/resources/icon-themes/calibre-default-dark/index.theme @@ -0,0 +1,23 @@ +[Icon Theme] +Name=calibre default light icons +Comment=icons for calibre in light mode + +[base] +Size=128 +MinSize=16 +MaxSize=256 + +[base/devices] +Size=128 +MinSize=16 +MaxSize=256 + +[base/plugins] +Size=128 +MinSize=16 +MaxSize=256 + +[base/mimetypes] +Size=128 +MinSize=16 +MaxSize=256 diff --git a/resources/icon-themes/calibre-default-light/index.theme b/resources/icon-themes/calibre-default-light/index.theme new file mode 100644 index 0000000000..72fc6819af --- /dev/null +++ b/resources/icon-themes/calibre-default-light/index.theme @@ -0,0 +1,23 @@ +[Icon Theme] +Name=calibre default light icons +Comment=icons for calibre in light mode + +[base] +Size=128 +MinSize=16 +MaxSize=256 + +[base/devices] +Size=128 +MinSize=16 +MaxSize=256 + +[base/plugins] +Size=128 +MinSize=16 +MaxSize=256 + +[base/mimetypes] +Size=128 +MinSize=16 +MaxSize=256 diff --git a/src/calibre/gui2/__init__.py b/src/calibre/gui2/__init__.py index 83e96c2dc5..3ca3111972 100644 --- a/src/calibre/gui2/__init__.py +++ b/src/calibre/gui2/__init__.py @@ -47,13 +47,37 @@ except AttributeError: NO_URL_FORMATTING = getattr(QUrl, 'None') +icons_subdirs = ('devices', 'plugins', 'mimetypes') + + +def set_icon_paths(): + paths = [] + for main_dir in (os.path.join(config_dir, 'resources', 'images'), os.path.dirname(I(icons_subdirs[0], allow_user_override=False))): + if os.path.exists(main_dir): + paths.append(main_dir) + for subdir in icons_subdirs: + q = os.path.join(main_dir, subdir) + if os.path.exists(q): + paths.append(q) + QIcon.setFallbackSearchPaths(paths + list(QIcon.fallbackSearchPaths())) + default_theme_path = P('icon-themes', allow_user_override=False) + paths = [default_theme_path] + user_theme_path = P('icon-themes') + if user_theme_path != default_theme_path: + paths.insert(0, user_theme_path) + QIcon.setThemeSearchPaths(paths) + + def load_qicon(name): if isinstance(name, QIcon): return name if not name: return QIcon() if not os.path.isabs(name): - name = I(name) + parts = name.split('/') + if len(parts) == 2 and parts[0] in icons_subdirs: + name = parts[1] + return QIcon.fromTheme(os.path.splitext(name)[0]) return QIcon(name) @@ -947,6 +971,7 @@ class Application(QApplication): QApplication.setDesktopFileName(override_program_name) QApplication.setAttribute(Qt.ApplicationAttribute.AA_ShareOpenGLContexts, True) # needed for webengine QApplication.__init__(self, qargs) + set_icon_paths() sh = self.styleHints() if hasattr(sh, 'setShowShortcutsInContextMenus'): sh.setShowShortcutsInContextMenus(True) @@ -1149,15 +1174,19 @@ class Application(QApplication): if self.is_dark_theme: ss += 'QMenu { border: 1px solid palette(shadow); }' self.setStyleSheet(ss) + self.update_icon_theme() self.palette_changed.emit() + def update_icon_theme(self): + name = 'calibre-default-' + ('dark' if self.is_dark_theme else 'light') + QIcon.setThemeName(name) + def stylesheet_for_line_edit(self, is_error=False): return 'QLineEdit { border: 2px solid %s; border-radius: 3px }' % ( '#FF2400' if is_error else '#50c878') def load_calibre_style(self): icon_map = self.__icon_map_memory_ = {} - pcache = {} for k, v in { 'DialogYesButton': 'ok.png', 'DialogNoButton': 'window-close.png', @@ -1178,16 +1207,11 @@ class Application(QApplication): 'ToolBarHorizontalExtensionButton': 'v-ellipsis.png', 'ToolBarVerticalExtensionButton': 'h-ellipsis.png', }.items(): - if v not in pcache: - pcache[v] = I(v) - # if not os.path.exists(pcache[v]): raise ValueError(pcache[v]) - icon_map[getattr(QStyle.StandardPixmap, 'SP_'+k).value] = pcache[v] + icon_map[getattr(QStyle.StandardPixmap, 'SP_'+k).value] = v.rpartition('.')[0] transient_scroller = 0 if ismacos: from calibre_extensions.cocoa import transient_scroller transient_scroller = transient_scroller() - icon_map[(QStyle.StandardPixmap.SP_CustomBase.value & 0xf0000000) + 1] = I('close-for-light-theme.png') - icon_map[(QStyle.StandardPixmap.SP_CustomBase.value & 0xf0000000) + 2] = I('close-for-dark-theme.png') self.calibre_style = style = self.pi.CalibreStyle(transient_scroller) style.set_icon_map(icon_map) self.setStyle(style) diff --git a/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp b/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp index b97485c15a..50a02347d7 100644 --- a/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp +++ b/src/calibre/gui2/progress_indicator/QProgressIndicator.cpp @@ -157,11 +157,7 @@ int CalibreStyle::styleHint(StyleHint hint, const QStyleOption *option, const QW } QIcon CalibreStyle::standardIcon(StandardPixmap standardIcon, const QStyleOption * option, const QWidget * widget) const { - if (standardIcon == QStyle::SP_DialogCloseButton) { - bool is_dark_theme = QApplication::instance()->property("is_dark_theme").toBool(); - return QIcon(icon_map.value(QStyle::SP_CustomBase + (is_dark_theme ? 2 : 1))); - } - if (icon_map.contains(standardIcon)) return QIcon(icon_map.value(standardIcon)); + if (icon_map.contains(standardIcon)) return QIcon::fromTheme(icon_map.value(standardIcon)); return QProxyStyle::standardIcon(standardIcon, option, widget); }