Skip to content

Commit

Permalink
refactor tool icon scaling; also use high DPI for PNGs #9
Browse files Browse the repository at this point in the history
  • Loading branch information
knipknap committed Feb 24, 2024
1 parent b3e71b4 commit 76f97ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 27 deletions.
5 changes: 4 additions & 1 deletion btl/qbtl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import sys
import argparse
from PySide import QtGui
from PySide import QtCore, QtGui
from btl import ToolDB, serializers
from btl.const import resource_dir
from btl.i18n import install_translator
Expand Down Expand Up @@ -34,6 +34,9 @@ def run():
serializer_cls = serializers.serializers[args.format]
serializer = serializer_cls(args.name)

QtGui.QApplication.setAttribute(QtCore.Qt.AA_ShareOpenGLContexts, True)
QtGui.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
QtGui.QApplication.setAttribute(QtCore.Qt.AA_UseHighDpiPixmaps, True)
app = QtGui.QApplication([])
install_translator(app)
window = LibraryUI(tool_db, serializer, standalone=True, parent=app)
Expand Down
17 changes: 5 additions & 12 deletions btl/ui/shapewidget.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from PySide import QtGui, QtSvg, QtCore
from .util import qpixmap_from_svg, qpixmap_from_png
from .util import get_pixmap_from_shape

class ShapeWidget(QtGui.QWidget):
def __init__ (self, shape, parent=None):
Expand All @@ -15,14 +15,7 @@ def __init__ (self, shape, parent=None):
self._update_icon()

def _update_icon(self):
icon_type, icon_bytes = self.shape.get_icon()
if not icon_type:
return
icon_ba = QtCore.QByteArray(icon_bytes)

if icon_type == 'svg':
icon = qpixmap_from_svg(icon_ba, self.icon_size, self.devicePixelRatio())
elif icon_type == 'png':
icon = qpixmap_from_png(icon_ba, self.icon_size)

self.icon_widget.setPixmap(icon)
ratio = self.devicePixelRatioF()
pixmap = get_pixmap_from_shape(self.shape, self.icon_size, ratio)
if pixmap:
self.icon_widget.setPixmap(pixmap)
17 changes: 5 additions & 12 deletions btl/ui/tablecell.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import FreeCAD
from PySide import QtGui, QtSvg, QtCore
from ..i18n import translate
from .util import qpixmap_from_svg, qpixmap_from_png
from .util import get_pixmap_from_shape

def isub(text, old, repl_pattern):
pattern = '|'.join(re.escape(o) for o in old)
Expand Down Expand Up @@ -113,17 +113,10 @@ def set_icon(self, pixmap):
self.hbox.insertWidget(1, self.icon_widget, 0)

def set_icon_from_shape(self, shape):
icon_type, icon_bytes = shape.get_icon()
if not icon_type:
return
icon_ba = QtCore.QByteArray(icon_bytes)

if icon_type == 'svg':
icon = qpixmap_from_svg(icon_ba, self.icon_size, self.devicePixelRatio())
elif icon_type == 'png':
icon = qpixmap_from_png(icon_ba, self.icon_size)

self.set_icon(icon)
ratio = self.devicePixelRatioF()
pixmap = get_pixmap_from_shape(shape, self.icon_size, ratio)
if pixmap:
self.set_icon(pixmap)

def contains_text(self, text):
for term in text.lower().split(' '):
Expand Down
18 changes: 16 additions & 2 deletions btl/ui/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def load_ui(ui_path, parent=None, custom_widgets=None):
ui_file.close()
return form

def qpixmap_from_png(byte_array, icon_size):
pixmap = QtGui.QPixmap(icon_size)
def qpixmap_from_png(byte_array, icon_size, ratio=1.0):
render_size = QtCore.QSize(icon_size.width()*ratio, icon_size.height()*ratio)
pixmap = QtGui.QPixmap(render_size)
pixmap.fill(QtGui.Qt.transparent)
pixmap.loadFromData(byte_array)
return pixmap.scaled(icon_size, QtCore.Qt.KeepAspectRatio)
Expand All @@ -33,3 +34,16 @@ def qpixmap_from_svg(byte_array, icon_size, ratio=1.0):
painter.end()

return QtGui.QPixmap.fromImage(image)

def get_pixmap_from_shape(shape, size, ratio):
icon_type, icon_bytes = shape.get_icon()
if not icon_type:
return None
icon_ba = QtCore.QByteArray(icon_bytes)

if icon_type == 'svg':
return qpixmap_from_svg(icon_ba, size, ratio)
elif icon_type == 'png':
return qpixmap_from_png(icon_ba, size, ratio)

return None

0 comments on commit 76f97ef

Please sign in to comment.