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 | class AYUserImage(StyleMixin, QtWidgets.QLabel):
Variants = AYUserImageVariants
def __init__(
self,
*args,
src: Path | str = "",
name: str = "",
full_name: str = "",
size: int = 30,
highlight: bool = False,
outline: bool = True,
file_cacher: Callable | None = None,
variant=Variants.Default,
**kwargs,
):
# file path to icon
self._src = src
# short user name
self._name = name
# full user name
self._full_name = full_name
# pixmap size
self._size = size
# green outline if true, light grey otherwise
self._highlight = highlight
# enable / disable outline
self._outline = outline
# a file loader function for the image cache: src is the cache key.
self._file_cacher = file_cacher
# Variant string for looking up style data
self._variant_str = variant.value
super().__init__(*args, **kwargs)
stl = get_ayon_style()
self.setStyle(stl)
self._style = stl.model.get_style(
"AYUserImage", variant=self._variant_str
)
self._style.set_context(self)
self.set_image()
def _get_colors(
self,
) -> tuple[QtGui.QColor, QtGui.QColor, QtGui.QColor, QtGui.QColor]:
"""Read image colors from the AYUserImage style data.
Returns:
A tuple of (initials_bg, outline_color, highlight_color).
"""
style = self._style
fg = QtGui.QColor(style.get("initials-color", "#ffffff"))
bg = QtGui.QColor(style.get("initials-background-color", "#484875"))
outline = QtGui.QColor(style.get("outline-color", "#e1e1e1"))
highlight = QtGui.QColor(style.get("highlight-color", "#6be1ac"))
return fg, bg, outline, highlight
def set_image(self) -> None:
"""Render the user avatar pixmap and assign it to this label."""
fg_color, bg_color, outline_color, highlight_color = self._get_colors()
active_outline = highlight_color if self._highlight else outline_color
dpr = self.devicePixelRatioF()
dpr_size = int(self._size * dpr)
has_outline = self._outline or self._highlight
line_width = 1 if has_outline else 0
half_line = line_width / 2
self.pxm = QtGui.QPixmap(dpr_size, dpr_size)
self.pxm.setDevicePixelRatio(dpr)
self.pxm.fill(QtCore.Qt.GlobalColor.transparent)
painter = QtGui.QPainter(self.pxm)
painter.setRenderHint(QtGui.QPainter.RenderHint.Antialiasing, True)
painter.setFont(self.font())
if self._src:
if not Path(str(self._src)).exists() and self._file_cacher:
ic = ImageCache.get_instance()
self._src = ic.get(
str(self._src), partial(self._file_cacher, self._src)
)
# Load and draw src icon file in a circle
source_pixmap = QtGui.QPixmap(self._src)
source_pixmap.setDevicePixelRatio(dpr)
if not source_pixmap.isNull():
# Scale the source image to fit within the circle (with some
# margin for outline)
# Leave space for outline
inner_size = int(dpr_size - line_width)
scaled_pixmap = source_pixmap.scaled(
inner_size,
inner_size,
QtCore.Qt.AspectRatioMode.KeepAspectRatioByExpanding,
QtCore.Qt.TransformationMode.SmoothTransformation,
)
# Create circular clipping path
clip_path = QtGui.QPainterPath()
clip_path.addEllipse(0, 0, self._size, self._size)
painter.setClipPath(clip_path)
# Draw the scaled image centered
x = (dpr_size - scaled_pixmap.width()) // 2
y = (dpr_size - scaled_pixmap.height()) // 2
painter.drawPixmap(x, y, scaled_pixmap)
# Reset clipping
painter.setClipping(False)
else:
log.warning("Could not load src %s", self._src)
else:
initials = "?"
if self._full_name:
initials = "".join([p[0] for p in self._full_name.split()])
elif self._name:
initials = self._name[0]
# Draw a circle with white initials over a color background
# Fill circle with background color from style
painter.setBrush(QtGui.QBrush(bg_color))
painter.setPen(QtCore.Qt.PenStyle.NoPen)
painter.drawEllipse(0, 0, self._size, self._size)
# Draw white initials
painter.setPen(QtGui.QPen(fg_color))
font = painter.font()
pt_size = max(8, self._size // 2 - 3)
font.setPointSizeF(pt_size)
painter.setFont(font)
painter.drawText(
QtCore.QRect(0, 0, self._size, self._size),
QtCore.Qt.AlignmentFlag.AlignCenter,
initials.upper(),
)
# Draw outline
if has_outline:
painter.setBrush(QtCore.Qt.BrushStyle.NoBrush)
painter.setPen(QtGui.QPen(active_outline, line_width))
painter.drawEllipse(
QtCore.QRectF(
half_line,
half_line,
self._size - line_width,
self._size - line_width,
)
)
painter.end()
# Set the pixmap to the label
self.setPixmap(self.pxm)
def update_params(self, src, full_name):
self._src = src
self._full_name = full_name
self.set_image()
|