Skip to content

Commit 9d57c83

Browse files
committed
Move new method to the proper location
1 parent d83055f commit 9d57c83

File tree

2 files changed

+92
-88
lines changed

2 files changed

+92
-88
lines changed

spyder/utils/icon_manager.py

Lines changed: 3 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from qtpy.QtCore import QBuffer, QByteArray
1515
from qtpy.QtGui import QColor, QIcon, QImage, QPainter, QPixmap
1616
from qtpy.QtWidgets import QStyle, QWidget
17-
from qtpy.QtSvg import QSvgRenderer
1817

1918
# Local imports
2019
from spyder.config.manager import CONF
@@ -507,8 +506,9 @@ def _process_svg_icon(self, icon_path, resample):
507506

508507
# Process each size to ensure proper scaling on all displays
509508
for size in sizes:
510-
# Create the base pixmap for this size
511-
pixmap = self._render_colored_svg(
509+
# Create the base pixmap for this size using SVGColorize
510+
svg_colorizer = SVGColorize(icon_path)
511+
pixmap = svg_colorizer.render_colored_svg(
512512
paths, size, width, height, viewbox
513513
)
514514

@@ -528,91 +528,6 @@ def _process_svg_icon(self, icon_path, resample):
528528
# Any error, fall back to regular processing
529529
return self._process_regular_icon(icon_path, resample)
530530

531-
def _render_colored_svg(self, paths, size, width, height, viewbox=None):
532-
"""
533-
Render colored SVG paths to a pixmap.
534-
535-
Parameters
536-
----------
537-
paths : list
538-
List of path dictionaries with 'path_data' and 'color'
539-
size : int
540-
Size of the pixmap to create (used as the maximum dimension)
541-
width : int
542-
Original SVG width
543-
height : int
544-
Original SVG height
545-
viewbox : str or None
546-
SVG viewBox attribute if available
547-
548-
Returns
549-
-------
550-
QPixmap
551-
A pixmap with all paths rendered with their respective colors
552-
"""
553-
554-
# Calculate proper dimensions preserving aspect ratio
555-
aspect_ratio = width / height
556-
if width > height:
557-
# Width is larger, use size as width
558-
pixmap_width = size
559-
pixmap_height = int(size / aspect_ratio)
560-
else:
561-
# Height is larger or equal, use size as height
562-
pixmap_height = size
563-
pixmap_width = int(size * aspect_ratio)
564-
565-
# Create transparent pixmap for the icon with proper aspect ratio
566-
pixmap = QPixmap(pixmap_width, pixmap_height)
567-
pixmap.fill(QColor(0, 0, 0, 0)) # Transparent
568-
569-
# Painter for compositing all parts
570-
painter = QPainter(pixmap)
571-
painter.setRenderHint(QPainter.Antialiasing)
572-
573-
# Process each path
574-
for path_data in paths:
575-
path_d = path_data.get('path_data', '')
576-
color = QColor(path_data.get('color', self.MAIN_FG_COLOR))
577-
578-
if not path_d:
579-
continue
580-
581-
# Create a temporary SVG with just this path
582-
svg_template = (
583-
f'<svg xmlns="http://www.w3.org/2000/svg" '
584-
f'width="{width}" height="{height}"'
585-
)
586-
587-
# Add viewBox if available
588-
if viewbox:
589-
svg_template += f' viewBox="{viewbox}"'
590-
591-
svg_template += f'><path d="{path_d}"/></svg>'
592-
593-
# Render the path and apply color
594-
temp_bytes = QByteArray(svg_template.encode('utf-8'))
595-
temp_pixmap = QPixmap(pixmap_width, pixmap_height)
596-
temp_pixmap.fill(QColor(0, 0, 0, 0)) # Transparent
597-
598-
# Render the path
599-
temp_renderer = QSvgRenderer(temp_bytes)
600-
temp_painter = QPainter(temp_pixmap)
601-
temp_renderer.render(temp_painter)
602-
temp_painter.end()
603-
604-
# Apply color to the path
605-
temp_painter = QPainter(temp_pixmap)
606-
temp_painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
607-
temp_painter.fillRect(temp_pixmap.rect(), color)
608-
temp_painter.end()
609-
610-
# Composite this path onto the main pixmap
611-
painter.drawPixmap(0, 0, temp_pixmap)
612-
613-
# Finish compositing
614-
painter.end()
615-
return pixmap
616531

617532
def _create_disabled_pixmap(self, source_pixmap):
618533
"""

spyder/utils/svg_colorizer.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,92 @@ def get_colored_paths(cls, icon_path, theme_colors):
256256
return None
257257

258258
return icon.extract_colored_paths(theme_colors)
259+
260+
def render_colored_svg(self, paths, size, width, height, viewbox=None):
261+
"""
262+
Render colored SVG paths to a pixmap.
263+
264+
Parameters
265+
----------
266+
paths : list
267+
List of path dictionaries with 'path_data' and 'color'
268+
size : int
269+
Size of the pixmap to create (used as the maximum dimension)
270+
width : int
271+
Original SVG width
272+
height : int
273+
Original SVG height
274+
viewbox : str or None
275+
SVG viewBox attribute if available
276+
277+
Returns
278+
-------
279+
QPixmap
280+
A pixmap with all paths rendered with their respective colors
281+
"""
282+
from qtpy.QtCore import QByteArray
283+
from qtpy.QtGui import QColor, QPainter, QPixmap
284+
from qtpy.QtSvg import QSvgRenderer
285+
286+
# Calculate proper dimensions preserving aspect ratio
287+
aspect_ratio = width / height
288+
if width > height:
289+
# Width is larger, use size as width
290+
pixmap_width = size
291+
pixmap_height = int(size / aspect_ratio)
292+
else:
293+
# Height is larger or equal, use size as height
294+
pixmap_height = size
295+
pixmap_width = int(size * aspect_ratio)
296+
297+
# Create transparent pixmap for the icon with proper aspect ratio
298+
pixmap = QPixmap(pixmap_width, pixmap_height)
299+
pixmap.fill(QColor(0, 0, 0, 0)) # Transparent
300+
301+
# Painter for compositing all parts
302+
painter = QPainter(pixmap)
303+
painter.setRenderHint(QPainter.Antialiasing)
304+
305+
# Process each path
306+
for path_data in paths:
307+
path_d = path_data.get('path_data', '')
308+
color = QColor(path_data.get('color', '#FAFAFA'))
309+
310+
if not path_d:
311+
continue
312+
313+
# Create a temporary SVG with just this path
314+
svg_template = (
315+
f'<svg xmlns="http://www.w3.org/2000/svg" '
316+
f'width="{width}" height="{height}"'
317+
)
318+
319+
# Add viewBox if available
320+
if viewbox:
321+
svg_template += f' viewBox="{viewbox}"'
322+
323+
svg_template += f'><path d="{path_d}"/></svg>'
324+
325+
# Render the path and apply color
326+
temp_bytes = QByteArray(svg_template.encode('utf-8'))
327+
temp_pixmap = QPixmap(pixmap_width, pixmap_height)
328+
temp_pixmap.fill(QColor(0, 0, 0, 0)) # Transparent
329+
330+
# Render the path
331+
temp_renderer = QSvgRenderer(temp_bytes)
332+
temp_painter = QPainter(temp_pixmap)
333+
temp_renderer.render(temp_painter)
334+
temp_painter.end()
335+
336+
# Apply color to the path
337+
temp_painter = QPainter(temp_pixmap)
338+
temp_painter.setCompositionMode(QPainter.CompositionMode_SourceIn)
339+
temp_painter.fillRect(temp_pixmap.rect(), color)
340+
temp_painter.end()
341+
342+
# Composite this path onto the main pixmap
343+
painter.drawPixmap(0, 0, temp_pixmap)
344+
345+
# Finish compositing
346+
painter.end()
347+
return pixmap

0 commit comments

Comments
 (0)