-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrw_icons.py
More file actions
391 lines (350 loc) · 13.9 KB
/
Copy pathrw_icons.py
File metadata and controls
391 lines (350 loc) · 13.9 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
"""
Record Wizard — icon loading and the hand-painted fallback icon set.
PNG icons are preferred; every icon also has a QPainter fallback so the app
still looks right if the icons/ folder is missing.
"""
import os
import math
import struct
from PyQt6.QtCore import Qt, QRectF, QPointF, QByteArray, QBuffer, QIODevice
from PyQt6.QtGui import (
QPainter, QPainterPath, QColor, QPen, QBrush, QPixmap, QImage,
QRadialGradient,
)
from rw_theme import REC_LIVE
from rw_util import asset
ICON_FILES = {
"screen": "icon_screen.png",
"audio": "icon_recorder.png",
"region": "icon_region.png",
"resolution": "icon_resolution.png",
"cursor": "icon_cursor.png",
"folder": "icon_folder.png",
"pencil": "icon_brush.png",
}
_source_cache: dict[str, QPixmap | None] = {}
_scaled_cache: dict[tuple[str, int, int], QPixmap] = {}
def pen(col, w=2.0) -> QPen:
p = QPen(QColor(col) if isinstance(col, str) else col)
p.setWidthF(w)
p.setCapStyle(Qt.PenCapStyle.RoundCap)
p.setJoinStyle(Qt.PenJoinStyle.RoundJoin)
return p
def _load_source(key: str) -> QPixmap | None:
if key in _source_cache:
return _source_cache[key]
result = None
fname = ICON_FILES.get(key)
if fname:
path = asset(fname)
if os.path.exists(path):
pm = QPixmap(path)
if not pm.isNull():
result = pm
_source_cache[key] = result
return result
def icon_pixmap(key: str, w: float, h: float, dpr: float = 1.0) -> QPixmap | None:
"""A smoothly scaled, DPI-correct pixmap for `key`, or None if absent.
The returned pixmap carries its devicePixelRatio, so draw it with
drawPixmap(QPointF, pm) and it lands at the right logical size.
"""
src = _load_source(key)
if src is None:
return None
tw = max(1, int(round(w * dpr)))
th = max(1, int(round(h * dpr)))
ck = (key, tw, th)
pm = _scaled_cache.get(ck)
if pm is None:
pm = src.scaled(tw, th, Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation)
pm.setDevicePixelRatio(dpr)
_scaled_cache[ck] = pm
return pm
def draw_icon(p: QPainter, r: QRectF, key: str, fallback_fn, col: str,
dpr: float = 1.0, **kw):
"""Draw the PNG icon centred in `r`, or fall back to the painter fn."""
pm = icon_pixmap(key, r.width(), r.height(), dpr) if key else None
if pm is not None:
lw = pm.width() / pm.devicePixelRatio()
lh = pm.height() / pm.devicePixelRatio()
p.drawPixmap(QPointF(r.center().x() - lw / 2,
r.center().y() - lh / 2), pm)
else:
fallback_fn(p, r, col, **kw)
# ═══════════════════════════════════════════════════════════════════
# Icon painters (QPainter, QRectF, colour-string, **kw)
# ═══════════════════════════════════════════════════════════════════
def icon_screen(p: QPainter, r: QRectF, col: str, *, on=True, **_):
c = QColor(col)
p.setPen(pen(c, 2.0))
fc = QColor(c); fc.setAlpha(25 if on else 0)
p.setBrush(fc if on else Qt.BrushStyle.NoBrush)
sr = r.adjusted(0, 0, 0, -r.height() * 0.22)
p.drawRoundedRect(sr, 3, 3)
cx = r.center().x()
bot = r.bottom()
p.drawLine(QPointF(cx, sr.bottom()), QPointF(cx, bot - 2))
p.drawLine(QPointF(cx - r.width() * 0.22, bot - 1),
QPointF(cx + r.width() * 0.22, bot - 1))
if on:
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor(col))
tri = QPainterPath()
icx = sr.center().x() + sr.width() * 0.04
icy = sr.center().y()
sz = sr.height() * 0.22
tri.moveTo(icx - sz * 0.6, icy - sz)
tri.lineTo(icx + sz * 0.8, icy)
tri.lineTo(icx - sz * 0.6, icy + sz)
tri.closeSubpath()
p.drawPath(tri)
def icon_audio(p: QPainter, r: QRectF, col: str, *, on=True, **_):
c = QColor(col)
p.setPen(pen(c, 2.0))
p.setBrush(Qt.BrushStyle.NoBrush)
cy = r.center().y()
bx = r.left() + r.width() * 0.02
bw, bh = r.width() * 0.22, r.height() * 0.28
p.drawRect(QRectF(bx, cy - bh / 2, bw, bh))
cone = QPainterPath()
x2 = bx + bw + r.width() * 0.22
cone.moveTo(bx + bw, cy - bh / 2)
cone.lineTo(x2, cy - r.height() * 0.42)
cone.lineTo(x2, cy + r.height() * 0.42)
cone.lineTo(bx + bw, cy + bh / 2)
cone.closeSubpath()
p.drawPath(cone)
if on:
wx = x2 + r.width() * 0.06
for rf in (0.24, 0.38):
rad = r.height() * rf
p.drawArc(QRectF(wx - rad, cy - rad, 2 * rad, 2 * rad),
-35 * 16, 70 * 16)
else:
x1 = r.right() - r.width() * 0.30
x2b = r.right() - r.width() * 0.06
y1 = cy - r.height() * 0.20
y2 = cy + r.height() * 0.20
p.setPen(pen(c, 2.4))
p.drawLine(QPointF(x1, y1), QPointF(x2b, y2))
p.drawLine(QPointF(x1, y2), QPointF(x2b, y1))
def icon_region(p: QPainter, r: QRectF, col: str, **_):
c = QColor(col)
inner = r.adjusted(r.width() * 0.12, r.height() * 0.12,
-r.width() * 0.12, -r.height() * 0.12)
pn = pen(c, 1.4)
pn.setStyle(Qt.PenStyle.DashLine)
p.setPen(pn)
p.setBrush(Qt.BrushStyle.NoBrush)
p.drawRect(inner)
p.setPen(pen(c, 2.6))
cl = r.width() * 0.22
for cx, cy, dx, dy in [
(inner.left(), inner.top(), 1, 1),
(inner.right(), inner.top(), -1, 1),
(inner.left(), inner.bottom(), 1, -1),
(inner.right(), inner.bottom(), -1, -1),
]:
p.drawLine(QPointF(cx, cy), QPointF(cx + dx * cl, cy))
p.drawLine(QPointF(cx, cy), QPointF(cx, cy + dy * cl))
def icon_resolution(p: QPainter, r: QRectF, col: str, **_):
c = QColor(col)
p.setPen(pen(c, 2.0))
p.setBrush(Qt.BrushStyle.NoBrush)
sr = r.adjusted(r.width() * 0.04, r.height() * 0.04,
-r.width() * 0.04, -r.height() * 0.22)
p.drawRoundedRect(sr, 3, 3)
cx = sr.center().x()
bot = r.bottom() - r.height() * 0.02
p.drawLine(QPointF(cx, sr.bottom()), QPointF(cx, bot))
p.drawLine(QPointF(cx - r.width() * 0.20, bot),
QPointF(cx + r.width() * 0.20, bot))
p.setPen(pen(c, 1.0))
my = sr.top() + sr.height() * 0.5
mx = sr.left() + sr.width() * 0.5
p.drawLine(QPointF(sr.left() + 3, my), QPointF(sr.right() - 3, my))
p.drawLine(QPointF(mx, sr.top() + 3), QPointF(mx, sr.bottom() - 3))
def icon_cursor(p: QPainter, r: QRectF, col: str, *, on=True, **_):
c = QColor(col)
ax = r.left() + r.width() * 0.22
ay = r.top() + r.height() * 0.06
arrow = QPainterPath()
arrow.moveTo(ax, ay)
arrow.lineTo(ax, ay + r.height() * 0.80)
arrow.lineTo(ax + r.width() * 0.22, ay + r.height() * 0.62)
arrow.lineTo(ax + r.width() * 0.40, ay + r.height() * 0.88)
arrow.lineTo(ax + r.width() * 0.52, ay + r.height() * 0.78)
arrow.lineTo(ax + r.width() * 0.34, ay + r.height() * 0.54)
arrow.lineTo(ax + r.width() * 0.56, ay + r.height() * 0.50)
arrow.closeSubpath()
p.setPen(pen(c, 1.6))
if on:
fc = QColor(c); fc.setAlpha(50)
p.setBrush(fc)
else:
p.setBrush(Qt.BrushStyle.NoBrush)
p.drawPath(arrow)
if not on:
p.setPen(pen(c, 2.4))
ccx = r.center().x() + r.width() * 0.08
ccy = r.center().y() + r.height() * 0.08
d = r.width() * 0.18
p.drawLine(QPointF(ccx - d, ccy - d), QPointF(ccx + d, ccy + d))
p.drawLine(QPointF(ccx - d, ccy + d), QPointF(ccx + d, ccy - d))
def icon_folder(p: QPainter, r: QRectF, col: str, **_):
c = QColor(col)
p.setPen(pen(c, 2.0))
fc = QColor(c); fc.setAlpha(30)
p.setBrush(fc)
fx = r.left() + r.width() * 0.04
fy = r.top() + r.height() * 0.30
fw, fh = r.width() * 0.92, r.height() * 0.58
p.drawRoundedRect(QRectF(fx, fy, fw, fh), 3, 3)
tab = QPainterPath()
tab.moveTo(fx + 3, fy)
tab.lineTo(fx + 3, fy - r.height() * 0.14)
tab.lineTo(fx + fw * 0.42, fy - r.height() * 0.14)
tab.lineTo(fx + fw * 0.50, fy)
p.setBrush(Qt.BrushStyle.NoBrush)
p.drawPath(tab)
def icon_pencil(p: QPainter, r: QRectF, col: str, **_):
c = QColor(col)
p.setPen(pen(c, 2.0))
p.setBrush(Qt.BrushStyle.NoBrush)
bx1, by1 = r.left() + r.width() * 0.72, r.top() + r.height() * 0.08
bx2, by2 = r.left() + r.width() * 0.10, r.top() + r.height() * 0.70
hw = r.width() * 0.10
dx, dy = bx2 - bx1, by2 - by1
length = math.hypot(dx, dy) or 1.0
nx, ny = -dy / length * hw, dx / length * hw
body = QPainterPath()
body.moveTo(bx1 + nx, by1 + ny)
body.lineTo(bx1 - nx, by1 - ny)
body.lineTo(bx2 - nx, by2 - ny)
body.lineTo(bx2 + nx, by2 + ny)
body.closeSubpath()
fc = QColor(c); fc.setAlpha(30)
p.setBrush(fc)
p.drawPath(body)
tip_x = r.left() + r.width() * 0.02
tip_y = r.top() + r.height() * 0.78
tip = QPainterPath()
tip.moveTo(bx2 + nx, by2 + ny)
tip.lineTo(bx2 - nx, by2 - ny)
tip.lineTo(tip_x, tip_y)
tip.closeSubpath()
p.setBrush(Qt.BrushStyle.NoBrush)
p.drawPath(tip)
p.setPen(pen(c, 1.6))
sx = r.left() + r.width() * 0.30
sy = r.top() + r.height() * 0.88
path = QPainterPath()
path.moveTo(sx, sy)
path.cubicTo(sx + r.width() * 0.15, sy - r.height() * 0.08,
sx + r.width() * 0.30, sy + r.height() * 0.08,
sx + r.width() * 0.55, sy)
p.drawPath(path)
def icon_quality(p: QPainter, r: QRectF, col: str, **_):
"""Gauge / dial — quality & encoder settings."""
c = QColor(col)
p.setBrush(Qt.BrushStyle.NoBrush)
p.setPen(pen(c, 2.0))
arc = r.adjusted(r.width() * 0.06, r.height() * 0.14,
-r.width() * 0.06, -r.height() * 0.06)
p.drawArc(arc, 20 * 16, 140 * 16)
cx = arc.center().x()
cy = arc.center().y() + arc.height() * 0.18
p.setPen(pen(c, 2.4))
ang = math.radians(125)
p.drawLine(QPointF(cx, cy),
QPointF(cx + math.cos(ang) * arc.width() * 0.36,
cy - math.sin(ang) * arc.height() * 0.36))
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(c)
p.drawEllipse(QPointF(cx, cy), r.width() * 0.06, r.width() * 0.06)
for frac in (0.12, 0.5, 0.88):
a = math.radians(20 + 140 * frac)
tx = cx + math.cos(a) * arc.width() * 0.47
ty = cy - math.sin(a) * arc.height() * 0.47
p.drawEllipse(QPointF(tx, ty), 1.2, 1.2)
def icon_record(p: QPainter, r: QRectF, col: str, *, recording=False,
paused=False):
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor(col))
cx, cy = r.center().x(), r.center().y()
if paused:
bw = r.width() * 0.12
bh = r.height() * 0.38
gap = r.width() * 0.09
p.drawRoundedRect(QRectF(cx - gap - bw, cy - bh / 2, bw, bh), 2, 2)
p.drawRoundedRect(QRectF(cx + gap, cy - bh / 2, bw, bh), 2, 2)
elif recording:
sz = r.width() * 0.36
p.drawRoundedRect(QRectF(cx - sz / 2, cy - sz / 2, sz, sz), 2, 2)
else:
rad = r.width() * 0.32
p.drawEllipse(QPointF(cx, cy), rad, rad)
# ═══════════════════════════════════════════════════════════════════
# Fallback app icon + .ico writer (build-time helper)
# ═══════════════════════════════════════════════════════════════════
def generate_fallback_icon(size=128) -> QImage:
img = QImage(size, size, QImage.Format.Format_ARGB32_Premultiplied)
img.fill(Qt.GlobalColor.transparent)
p = QPainter(img)
p.setRenderHint(QPainter.RenderHint.Antialiasing)
s = size
cx = s / 2
grad = QRadialGradient(cx, cx, s * 0.48)
grad.setColorAt(0, QColor("#352d6e"))
grad.setColorAt(1, QColor("#1a1430"))
p.setBrush(QBrush(grad))
p.setPen(QPen(QColor("#554da0"), s * 0.025))
p.drawEllipse(QRectF(s * 0.04, s * 0.04, s * 0.92, s * 0.92))
p.setPen(Qt.PenStyle.NoPen)
p.setBrush(QColor(REC_LIVE))
p.drawEllipse(QPointF(cx, cx), s * 0.22, s * 0.22)
p.end()
return img
def _square(img: QImage, sz: int) -> QImage:
"""Scale keeping aspect, then pad to an exact sz x sz transparent canvas."""
scaled = img.scaled(sz, sz, Qt.AspectRatioMode.KeepAspectRatio,
Qt.TransformationMode.SmoothTransformation)
if scaled.width() == sz and scaled.height() == sz:
return scaled
canvas = QImage(sz, sz, QImage.Format.Format_ARGB32_Premultiplied)
canvas.fill(Qt.GlobalColor.transparent)
p = QPainter(canvas)
p.drawImage((sz - scaled.width()) // 2, (sz - scaled.height()) // 2, scaled)
p.end()
return canvas
def save_ico(path: str, source_png: str | None = None):
"""Write a multi-size .ico with PNG-compressed entries.
Build-time helper — the running app does not need this.
"""
entries = []
for sz in (256, 128, 64, 48, 32, 16):
if source_png and os.path.exists(source_png):
img = _square(QImage(source_png), sz)
else:
img = generate_fallback_icon(sz)
ba = QByteArray()
buf = QBuffer(ba)
buf.open(QIODevice.OpenModeFlag.WriteOnly)
img.save(buf, "PNG")
buf.close()
entries.append((sz, bytes(ba)))
n = len(entries)
header = struct.pack("<HHH", 0, 1, n)
offset = 6 + 16 * n
directory = b""
for sz, data in entries:
wb = 0 if sz >= 256 else sz
directory += struct.pack("<BBBBHHII",
wb, wb, 0, 0, 1, 32, len(data), offset)
offset += len(data)
with open(path, "wb") as f:
f.write(header)
f.write(directory)
for _, data in entries:
f.write(data)