-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
197 lines (169 loc) · 9.02 KB
/
utils.py
File metadata and controls
197 lines (169 loc) · 9.02 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import sys
import io
from pathlib import Path
from functools import lru_cache
from PyQt6.QtWidgets import QApplication
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QIcon, QPixmap, QPainter
from translations import tr, trf
def get_base_path():
"""Return the base path for application resources (handles frozen exe and dev modes)"""
if getattr(sys, 'frozen', False):
if hasattr(sys, '_MEIPASS'):
# One-file mode
return Path(sys._MEIPASS)
# One-dir mode
return Path(sys.executable).parent
# Dev mode
return Path(__file__).parent
def get_icon(name: str, icons_dir: Path = None) -> QIcon:
"""
Load an icon by name from the specified or default icons directory
Args:
name: Icon name (without extension)
icons_dir: Path to the icons folder (defaults to ./resources/icons)
Returns:
QIcon or an empty icon if not found
"""
if icons_dir is None:
icons_dir = get_base_path() / "resources" / "icons"
# Try different formats
for ext in ['.png', '.svg', '.ico']:
path = icons_dir / f"{name}{ext}"
if path.exists():
return QIcon(str(path))
return QIcon()
@lru_cache(maxsize=512)
def load_icon(file_path: Path, target_size: int, force_square: bool = False) -> QIcon:
"""Load, scale and return a QIcon from a file path"""
if file_path.exists() and file_path.is_file():
pixmap = QPixmap(str(file_path))
if not pixmap.isNull():
size_px = int(target_size * 1.5)
# 1. Scale original image once (Foreground)
fg = pixmap.scaled(size_px, size_px, Qt.AspectRatioMode.KeepAspectRatio, Qt.TransformationMode.SmoothTransformation)
if force_square and (fg.width() < size_px or fg.height() < size_px):
# Create a square canvas
result = QPixmap(size_px, size_px)
result.fill(Qt.GlobalColor.black)
painter = QPainter(result)
try:
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
painter.setRenderHint(QPainter.RenderHint.SmoothPixmapTransform)
# Logic to fill gaps by stretching edges
blur_factor = 0.05 # Strong blur for the background
if fg.height() < size_px: # Landscape
y_offset = (size_px - fg.height()) // 2
# Top
if y_offset > 0:
top_strip = fg.copy(0, 0, fg.width(), 1)
top_bg = top_strip.scaled(size_px, y_offset, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
# Blur
small = top_bg.scaled(int(size_px * blur_factor), int(y_offset * blur_factor) or 1, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
blurred = small.scaled(size_px, y_offset, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
painter.drawPixmap(0, 0, blurred)
# Bottom
if size_px - (y_offset + fg.height()) > 0:
bot_h = size_px - (y_offset + fg.height())
bot_strip = fg.copy(0, fg.height()-1, fg.width(), 1)
bot_bg = bot_strip.scaled(size_px, bot_h, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
# Blur
small = bot_bg.scaled(int(size_px * blur_factor), int(bot_h * blur_factor) or 1, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
blurred = small.scaled(size_px, bot_h, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
painter.drawPixmap(0, y_offset + fg.height(), blurred)
elif fg.width() < size_px: # Portrait
x_offset = (size_px - fg.width()) // 2
# Left
if x_offset > 0:
left_strip = fg.copy(0, 0, 1, fg.height())
left_bg = left_strip.scaled(x_offset, size_px, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
# Blur
small = left_bg.scaled(int(x_offset * blur_factor) or 1, int(size_px * blur_factor), Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
blurred = small.scaled(x_offset, size_px, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
painter.drawPixmap(0, 0, blurred)
# Right
if size_px - (x_offset + fg.width()) > 0:
right_w = size_px - (x_offset + fg.width())
right_strip = fg.copy(fg.width()-1, 0, 1, fg.height())
right_bg = right_strip.scaled(right_w, size_px, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
# Blur
small = right_bg.scaled(int(right_w * blur_factor) or 1, int(size_px * blur_factor), Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
blurred = small.scaled(right_w, size_px, Qt.AspectRatioMode.IgnoreAspectRatio, Qt.TransformationMode.SmoothTransformation)
painter.drawPixmap(x_offset + fg.width(), 0, blurred)
# 2. Draw original image in center
x = (size_px - fg.width()) // 2
y = (size_px - fg.height()) // 2
painter.drawPixmap(x, y, fg)
finally:
painter.end()
pixmap = result
else:
pixmap = fg
icon = QIcon()
icon.addPixmap(pixmap)
return icon
return None
def resize_icon(icon: QIcon, size: int) -> QIcon:
"""Resize an existing QIcon to the specified size"""
return QIcon(icon.pixmap(QSize(size, size)))
def format_duration(seconds):
"""Format duration in seconds to a human readable string for tree display"""
if not seconds:
return ""
hours, remainder = divmod(int(seconds), 3600)
minutes, secs = divmod(remainder, 60)
if hours:
return trf("formats.duration_hours", hours=hours, minutes=minutes)
return trf("formats.duration_minutes", minutes=minutes) if minutes else trf("formats.duration_seconds", seconds=secs)
def format_time(seconds):
"""Format time in seconds to HH:MM:SS string"""
if seconds < 0:
seconds = 0
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
secs = int(seconds % 60)
return trf("formats.time_hms", hours=hours, minutes=minutes, seconds=secs)
def format_time_short(seconds):
"""Format time in seconds to MM:SS string"""
if seconds < 0:
seconds = 0
minutes = int(seconds // 60)
secs = int(seconds % 60)
return trf("formats.time_ms", minutes=minutes, seconds=secs)
def format_size(size_bytes: int) -> str:
"""Format bytes into human readable string"""
if size_bytes < 1024:
return f"{size_bytes} B"
elif size_bytes < 1024 * 1024:
return f"{size_bytes / 1024:.1f} KB"
elif size_bytes < 1024 * 1024 * 1024:
return f"{size_bytes / (1024 * 1024):.1f} MB"
else:
return f"{size_bytes / (1024 * 1024 * 1024):.1f} GB"
class OutputCapture(io.StringIO):
"""Intercepts print output and sends it via signals"""
def __init__(self, signal):
"""Initialize the capture with a Qt signal"""
super().__init__()
self.signal = signal
self._real_stdout = sys.__stdout__
def write(self, text):
"""Write text to the signal and original stdout"""
if text:
# Send to signal
self.signal.emit(text)
# Duplicate to real stdout for debugging
if self._real_stdout:
try:
self._real_stdout.write(text)
except UnicodeEncodeError:
# Handle cases where console doesn't support the encoding
try:
safe_text = text.encode(self._real_stdout.encoding or 'utf-8', errors='replace').decode(self._real_stdout.encoding or 'utf-8')
self._real_stdout.write(safe_text)
except Exception:
pass
def flush(self):
"""Flush the original stdout"""
if self._real_stdout:
self._real_stdout.flush()