-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui_utils.py
More file actions
50 lines (42 loc) · 1.61 KB
/
Copy pathgui_utils.py
File metadata and controls
50 lines (42 loc) · 1.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""Shared GUI utility functions for RapNote."""
from __future__ import annotations
import logging
import os
from typing import Optional
from PyQt6.QtCore import QSize, Qt
from PyQt6.QtGui import QColor, QIcon, QPainter, QPixmap
LOGGER = logging.getLogger(__name__)
def resolve_resource(file_name: str) -> Optional[str]:
"""Resolve an image resource path from known candidate directories."""
candidates = [
os.path.join("rapnote", "img", file_name),
os.path.join("img", file_name),
]
for path in candidates:
if os.path.exists(path):
return path
LOGGER.warning("Resource not found: %s", file_name)
return None
def colored_icon(path: Optional[str], color: str, size: QSize) -> QIcon:
"""Load an SVG icon and tint it with the given color."""
if not path:
return QIcon()
pixmap = QPixmap(path)
if pixmap.isNull():
LOGGER.warning("Failed to load icon: %s", path)
return QIcon()
tinted = QPixmap(pixmap.size())
if tinted.isNull():
LOGGER.warning("Cannot tint icon with empty pixmap: %s", path)
return QIcon()
tinted.fill(Qt.GlobalColor.transparent)
painter = QPainter(tinted)
painter.setCompositionMode(QPainter.CompositionMode.CompositionMode_Source)
painter.drawPixmap(0, 0, pixmap)
painter.setCompositionMode(QPainter.CompositionMode.CompositionMode_SourceIn)
painter.fillRect(tinted.rect(), QColor(color))
painter.end()
scaled = tinted.scaled(size, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
icon = QIcon()
icon.addPixmap(scaled)
return icon