From 0a7cf449bc78e4c32eb086b2362d645a7d1b5303 Mon Sep 17 00:00:00 2001 From: Samuel Abels Date: Sun, 29 Jun 2025 12:57:32 +0200 Subject: [PATCH] CAM: Fix: Tools without icon now display the thumbnail from the FCStd file --- src/Mod/CAM/Path/Tool/shape/models/base.py | 10 ------- src/Mod/CAM/Path/Tool/shape/ui/shapebutton.py | 23 ++------------- src/Mod/CAM/Path/Tool/shape/ui/shapewidget.py | 29 +++++++++++++++++-- 3 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/Mod/CAM/Path/Tool/shape/models/base.py b/src/Mod/CAM/Path/Tool/shape/models/base.py index 2e06f5887f..7e8ab2bdc8 100644 --- a/src/Mod/CAM/Path/Tool/shape/models/base.py +++ b/src/Mod/CAM/Path/Tool/shape/models/base.py @@ -714,11 +714,6 @@ class ToolBitShape(Asset): self.icon = cast( ToolBitShapeIcon, cam_assets.get_or_none(f"toolbitshapesvg://{self.id}.svg") ) - if self.icon: - return self.icon - self.icon = cast( - ToolBitShapeIcon, cam_assets.get_or_none(f"toolbitshapesvg://{self.name.lower()}.svg") - ) if self.icon: return self.icon @@ -726,11 +721,6 @@ class ToolBitShape(Asset): self.icon = cast( ToolBitShapeIcon, cam_assets.get_or_none(f"toolbitshapepng://{self.id}.png") ) - if self.icon: - return self.icon - self.icon = cast( - ToolBitShapeIcon, cam_assets.get_or_none(f"toolbitshapepng://{self.name.lower()}.png") - ) if self.icon: return self.icon return None diff --git a/src/Mod/CAM/Path/Tool/shape/ui/shapebutton.py b/src/Mod/CAM/Path/Tool/shape/ui/shapebutton.py index 1b5979ded2..78e9591c0f 100644 --- a/src/Mod/CAM/Path/Tool/shape/ui/shapebutton.py +++ b/src/Mod/CAM/Path/Tool/shape/ui/shapebutton.py @@ -20,6 +20,7 @@ # * * # *************************************************************************** from PySide import QtGui, QtCore +from .shapewidget import ShapeWidget class ShapeButton(QtGui.QToolButton): @@ -27,10 +28,6 @@ class ShapeButton(QtGui.QToolButton): super(ShapeButton, self).__init__(parent) self.shape = shape - # Remove default text handling and use a custom layout - # self.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon) - # self.setText(f"{shape.label}\n{shape.id}") - self.vbox = QtGui.QVBoxLayout(self) self.vbox.setAlignment( QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter @@ -38,8 +35,7 @@ class ShapeButton(QtGui.QToolButton): self.vbox.setContentsMargins(0, 0, 0, 0) self.vbox.setSpacing(0) - self.icon_widget = QtGui.QLabel() - self.icon_widget.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) + self.icon_widget = ShapeWidget(self.shape, QtCore.QSize(71, 70)) self.label_widget = QtGui.QLabel(shape.label) self.label_widget.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter) @@ -60,22 +56,7 @@ class ShapeButton(QtGui.QToolButton): self.setFixedSize(128, 128) self.setBaseSize(128, 128) - # Adjust icon size to make space for text. - # Total height is 128. Let's allocate 70 for icon, 25 for label, 25 for ID. - self.icon_size = QtCore.QSize(71, 70) - # self.setIconSize(self.icon_size) # Removed as custom layout handles sizing - - self._update_icon() def set_text(self, text): # Update the text of the label widget self.label_widget.setText(text) - - def _update_icon(self): - icon = self.shape.get_icon() - if icon: - # Set the pixmap on the icon_widget QLabel - pixmap = icon.get_qpixmap(self.icon_size) - self.icon_widget.setPixmap(pixmap) - else: - self.icon_widget.clear() # Clear pixmap if no icon diff --git a/src/Mod/CAM/Path/Tool/shape/ui/shapewidget.py b/src/Mod/CAM/Path/Tool/shape/ui/shapewidget.py index 2bea5969f2..0829d44ce5 100644 --- a/src/Mod/CAM/Path/Tool/shape/ui/shapewidget.py +++ b/src/Mod/CAM/Path/Tool/shape/ui/shapewidget.py @@ -19,18 +19,32 @@ # * USA * # * * # *************************************************************************** +from typing import Optional from PySide import QtGui, QtCore +def _png2qpixmap(data, icon_size): + pixmap = QtGui.QPixmap() + pixmap.loadFromData(data, "PNG") + # Scale the pixmap if the requested size is different + if pixmap.size() != icon_size: + pixmap = pixmap.scaled( + icon_size, + QtCore.Qt.KeepAspectRatio, + QtCore.Qt.SmoothTransformation, + ) + return pixmap + + class ShapeWidget(QtGui.QWidget): - def __init__(self, shape, parent=None): + def __init__(self, shape, icon_size: Optional[QtCore.QSize] = None, parent=None): super(ShapeWidget, self).__init__(parent) self.layout = QtGui.QVBoxLayout(self) self.layout.setAlignment(QtCore.Qt.AlignHCenter) self.shape = shape ratio = self.devicePixelRatioF() - self.icon_size = QtCore.QSize(200 * ratio, 235 * ratio) + self.icon_size = icon_size or QtCore.QSize(200 * ratio, 235 * ratio) self.icon_widget = QtGui.QLabel() self.layout.addWidget(self.icon_widget) @@ -41,3 +55,14 @@ class ShapeWidget(QtGui.QWidget): if icon: pixmap = icon.get_qpixmap(self.icon_size) self.icon_widget.setPixmap(pixmap) + return + + thumbnail = self.shape.get_thumbnail() + if thumbnail: + ratio = self.devicePixelRatioF() + size = self.icon_size * ratio + pixmap = _png2qpixmap(thumbnail, size) + self.icon_widget.setPixmap(pixmap) + return + + self.icon_widget.clear() # Clear pixmap if no icon