-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathllamaspinner.cpp
More file actions
194 lines (160 loc) · 4.71 KB
/
llamaspinner.cpp
File metadata and controls
194 lines (160 loc) · 4.71 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
#include <QApplication>
#include <QPainter>
#include <QScreen>
#include "llamaspinner.h"
#include "llamatheme.h"
namespace LlamaCpp {
QImageIOPlugin::Capabilities SpinnerPlugin::capabilities(QIODevice *device,
const QByteArray &format) const
{
Q_UNUSED(device);
if (format == "spinner")
return CanRead;
return {};
}
QImageIOHandler *SpinnerPlugin::create(QIODevice *device, const QByteArray &format) const
{
Q_UNUSED(device);
Q_UNUSED(format);
return new SpinnerHandler();
}
SpinnerHandler::SpinnerHandler()
{
foregroundColor = QColor(replaceThemeColorNamesWithRGBNames("Token_Text_Default"));
}
bool SpinnerHandler::canRead() const
{
return true;
}
bool SpinnerHandler::read(QImage *image)
{
if (!image)
return false;
const qreal dpr = QApplication::primaryScreen()->devicePixelRatio();
const QSize logicalSize = m_size;
const QSize physicalSize = logicalSize * dpr;
QImage img(physicalSize, m_imageFormat);
img.setDevicePixelRatio(dpr); // tell Qt this is a DPR‑scaled image
img.fill(backgroundColor);
QPainter painter(&img);
painter.setRenderHint(QPainter::Antialiasing);
const qreal w = logicalSize.width();
const qreal h = logicalSize.height();
const qreal side = qMin(w, h); // keep it square
const qreal radius = (side / 2.0) - 1.0; // leave 1 px for the pen width
const QPointF centre(w / 2.0, h / 2.0);
// Background circle (the “track” – 25 % opacity)
QColor bgColor(foregroundColor);
bgColor.setAlphaF(0.25);
QPen bgPen(bgColor);
bgPen.setWidthF(2.0 * dpr);
bgPen.setCapStyle(Qt::RoundCap);
bgPen.setColor(bgColor);
bgPen.setCosmetic(true); // keep width constant on HiDPI
bgPen.setBrush(bgColor);
painter.setPen(bgPen);
painter.setBrush(Qt::NoBrush);
painter.drawEllipse(centre, radius, radius);
// Foreground “spinner” arc
const qreal arcSpanDeg = 90.0; // 90°
const qreal startDeg = -arcSpanDeg / 2.0; // centre the arc at 0°
const int startAngle = static_cast<int>((startDeg) * 16); // QPainter uses 1/16°
const int spanAngle = static_cast<int>(arcSpanDeg * 16);
// Compute the rotation that corresponds to the current frame.
// imageCount() returns 10, so each frame advances 36°.
const qreal rotationDeg = (frameIndex % imageCount())
* (360.0 / static_cast<qreal>(imageCount()));
painter.save();
painter.translate(centre);
painter.rotate(rotationDeg);
painter.translate(-centre);
// Pen for the moving arc – full opacity, same colour as foreground.
QPen fgPen(foregroundColor);
fgPen.setWidthF(2.0 * dpr);
fgPen.setCapStyle(Qt::RoundCap);
fgPen.setCosmetic(true);
painter.setPen(fgPen);
painter.setBrush(Qt::NoBrush);
// The rectangle that defines the full circle the arc lives on.
QRectF arcRect(centre.x() - radius, centre.y() - radius, radius * 2.0, radius * 2.0);
painter.drawArc(arcRect, startAngle, spanAngle);
painter.restore();
*image = img;
// advance to the next frame for the next call
++frameIndex;
return true;
}
bool SpinnerHandler::jumpToImage(int imageNumber)
{
if (imageNumber < 0 || imageNumber >= imageCount())
return false;
frameIndex = imageNumber;
return true;
}
int SpinnerHandler::currentImageNumber() const
{
return frameIndex;
}
int SpinnerHandler::imageCount() const
{
return 25;
}
int SpinnerHandler::nextImageDelay() const
{
return 33;
}
int SpinnerHandler::loopCount() const
{
return -1; // infinite looping
}
QByteArray SpinnerHandler::name() const
{
return "spn";
}
bool SpinnerHandler::supportsOption(ImageOption option) const
{
switch (option) {
case Size:
case ImageFormat:
case Animation:
case ScaledSize:
case BackgroundColor:
return true;
default:
return false;
}
}
QVariant SpinnerHandler::option(ImageOption option) const
{
switch (option) {
case Size:
case ScaledSize:
return m_size;
case BackgroundColor:
return backgroundColor;
case ImageFormat:
return m_imageFormat;
case Animation:
return true;
default:
return QVariant();
}
}
void SpinnerHandler::setOption(ImageOption option, const QVariant &value)
{
switch (option) {
case Size:
case ScaledSize:
m_size = value.toSize();
break;
case BackgroundColor:
backgroundColor = value.value<QColor>();
break;
case ImageFormat:
m_imageFormat = value.value<QImage::Format>();
break;
default:
break;
}
}
} // namespace LlamaCpp