Skip to content

Commit

Permalink
High DPI support for QToolButton icons (on theme engine)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonoomph committed May 28, 2024
1 parent 9da3901 commit 170a51f
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/themes/base.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
import re

from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QColor, QIcon
from PyQt5.QtGui import QColor, QIcon, QPixmap, QPainter
from PyQt5.QtSvg import QSvgRenderer
from PyQt5.QtWidgets import QTabWidget, QWidget, QSizePolicy

from classes import ui_util
Expand All @@ -45,6 +46,17 @@ def __init__(self, app):
"""
self.app = app

def create_svg_icon(self, svg_path, size):
"""Create Dynamic High DPI icons"""
renderer = QSvgRenderer(svg_path)
image = QPixmap(size * self.app.devicePixelRatio())
image.fill(Qt.transparent)
painter = QPainter(image)
renderer.render(painter)
painter.end()
image.setDevicePixelRatio(self.app.devicePixelRatio())
return QIcon(image)

def get_color(self, class_name, property_name):
"""Return a QColor from a stylesheet class and property."""
# Regex to find the class and property with a color for more complex properties
Expand All @@ -71,7 +83,7 @@ def set_dock_margins(self, content_margins=None, layout_margins=None, object_nam
if child.objectName().startswith("dock") and child.objectName().endswith("Contents"):
# Set content margins on QDock* widget
child.setContentsMargins(*content_margins)
if child.objectName().startswith("dock") and child.layout() and layout_margins:
if child.layout() and layout_margins:
# Set content margins on the QDock Layout (which has additional margins)
child.layout().setContentsMargins(*layout_margins)

Expand All @@ -83,7 +95,8 @@ def set_toolbar_buttons(self, toolbar, icon_size=24, settings=None):
toolbar.clear()

# Set icon size
toolbar.setIconSize(QSize(icon_size, icon_size))
qsize_icon = QSize(icon_size, icon_size)
toolbar.setIconSize(qsize_icon)

for setting in settings:
# Button settings
Expand Down Expand Up @@ -119,7 +132,8 @@ def set_toolbar_buttons(self, toolbar, icon_size=24, settings=None):
toolbar.addAction(button_action)
button = toolbar.widgetForAction(button_action)
if button_icon:
button_action.setIcon(QIcon(button_icon))
qicon_instance = self.create_svg_icon(button_icon, qsize_icon)
button_action.setIcon(qicon_instance)
if button_style:
button.setToolButtonStyle(button_style)
if button_stylesheet:
Expand Down

0 comments on commit 170a51f

Please sign in to comment.