| 1 |
#!/usr/bin/env python3 |
| 2 |
"""Script to identify which SVG files trigger Qt warnings. |
| 3 |
|
| 4 |
This script scans all SVG files in the icon directories and loads them |
| 5 |
one by one, reporting which files cause Qt SVG warnings. |
| 6 |
""" |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import sys |
| 11 |
from pathlib import Path |
| 12 |
|
| 13 |
from guidata.qthelpers import qt_app_context |
| 14 |
from qtpy import QtCore as QC |
| 15 |
from qtpy import QtGui as QG |
| 16 |
|
| 17 |
from datalab.config import DATAPATH |
| 18 |
|
| 19 |
# Add project root to path |
| 20 |
project_root = Path(__file__).parent.parent |
| 21 |
sys.path.insert(0, str(project_root)) |
| 22 |
|
| 23 |
# Track which file is currently being loaded |
| 24 |
current_file: Path | None = None |
| 25 |
problematic_files: set[Path] = set() |
| 26 |
|
| 27 |
|
| 28 |
def message_handler( |
| 29 |
msg_type: QC.QtMsgType, context: QC.QMessageLogContext, message: str |
| 30 |
) -> None: |
| 31 |
"""Custom Qt message handler to catch SVG warnings.""" |
| 32 |
if "svg" in message.lower() or "path" in message.lower(): |
| 33 |
if current_file is not None and current_file not in problematic_files: |
| 34 |
problematic_files.add(current_file) |
| 35 |
print(f" ⚠️ {current_file.name}: {message}") |
| 36 |
|
| 37 |
|
| 38 |
def scan_svg_directory(svg_dir: Path) -> None: |
| 39 |
"""Scan all SVG files in a directory and test loading them.""" |
| 40 |
if not svg_dir.exists(): |
| 41 |
print(f"Directory not found: {svg_dir}") |
| 42 |
return |
| 43 |
|
| 44 |
svg_files = sorted(svg_dir.rglob("*.svg")) |
| 45 |
print(f"\nScanning {len(svg_files)} SVG files in {svg_dir}...") |
| 46 |
print("-" * 60) |
| 47 |
|
| 48 |
global current_file |
| 49 |
for svg_file in svg_files: |
| 50 |
current_file = svg_file |
| 51 |
relative_path = svg_file.relative_to(svg_dir) |
| 52 |
# Load the icon - this triggers Qt SVG parsing |
| 53 |
icon = QG.QIcon(str(svg_file)) |
| 54 |
# Force the icon to actually render by getting a pixmap |
| 55 |
pixmap = icon.pixmap(32, 32) |
| 56 |
if pixmap.isNull(): |
| 57 |
print(f" ❌ Failed to load: {relative_path}") |
| 58 |
|
| 59 |
current_file = None |
| 60 |
|
| 61 |
|
| 62 |
def main() -> None: |
| 63 |
"""Main function to scan SVG icons.""" |
| 64 |
with qt_app_context(): |
| 65 |
# Install custom message handler |
| 66 |
QC.qInstallMessageHandler(message_handler) |
| 67 |
|
| 68 |
print("=" * 60) |
| 69 |
print("SVG Icon Scanner - Detecting problematic SVG files") |
| 70 |
print("=" * 60) |
| 71 |
|
| 72 |
# Scan DataLab icons |
| 73 |
scan_svg_directory(Path(DATAPATH)) |
| 74 |
|
| 75 |
# Try to find and scan guidata icons |
| 76 |
try: |
| 77 |
import guidata |
| 78 |
|
| 79 |
guidata_path = Path(guidata.__file__).parent |
| 80 |
guidata_icons = guidata_path / "data" / "icons" |
| 81 |
scan_svg_directory(guidata_icons) |
| 82 |
except ImportError: |
| 83 |
print("\nguidata not found, skipping its icons") |
| 84 |
|
| 85 |
# Try to find and scan plotpy icons |
| 86 |
try: |
| 87 |
import plotpy |
| 88 |
|
| 89 |
plotpy_path = Path(plotpy.__file__).parent |
| 90 |
plotpy_icons = plotpy_path / "data" / "icons" |
| 91 |
scan_svg_directory(plotpy_icons) |
| 92 |
except ImportError: |
| 93 |
print("\nplotpy not found, skipping its icons") |
| 94 |
|
| 95 |
# Summary |
| 96 |
print("\n" + "=" * 60) |
| 97 |
print("SUMMARY") |
| 98 |
print("=" * 60) |
| 99 |
|
| 100 |
if problematic_files: |
| 101 |
print(f"\n❌ Found {len(problematic_files)} problematic SVG file(s):\n") |
| 102 |
for file_path in sorted(problematic_files): |
| 103 |
print(f" • {file_path}\n") |
| 104 |
else: |
| 105 |
print("\n✅ No problematic SVG files found!") |
| 106 |
|
| 107 |
|
| 108 |
if __name__ == "__main__": |
| 109 |
main() |