Skip to content

Commit 50990db

Browse files
committed
optionally use an svg for the mark icon instead of text
1 parent 32203af commit 50990db

File tree

4 files changed

+96
-25
lines changed

4 files changed

+96
-25
lines changed
Loading

res/skins/LateNight/waveform.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
</MarkRange>
6363
<Mark>
6464
<Control>loop_start_position</Control>
65-
<Text>&#8635;</Text>
65+
<Icon>skin:../LateNight/classic/style/mark_loop.svg</Icon>
6666
<Align>top|left</Align>
6767
<Color><Variable name="LoopColor"/></Color>
6868
<TextColor>#FFFFFF</TextColor>

src/waveform/renderers/waveformmark.cpp

+40-24
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,11 @@ WaveformMark::WaveformMark(const QString& group,
111111
if (!m_pixmapPath.isEmpty()) {
112112
m_pixmapPath = context.makeSkinPath(m_pixmapPath);
113113
}
114+
115+
m_iconPath = context.selectString(node, "Icon");
116+
if (!m_iconPath.isEmpty()) {
117+
m_iconPath = context.makeSkinPath(m_iconPath);
118+
}
114119
}
115120

116121
WaveformMark::~WaveformMark() = default;
@@ -143,11 +148,11 @@ bool WaveformMark::contains(QPoint point, Qt::Orientation orientation) const {
143148
struct MarkerGeometry {
144149
bool m_isSymbol; // it the label normal text or a single symbol (e.g. open circle arrow)
145150
QFont m_font;
146-
QRectF m_textRect;
151+
QRectF m_contentRect;
147152
QRectF m_labelRect;
148153
QSizeF m_imageSize;
149154

150-
MarkerGeometry(const QString& label, Qt::Alignment align, float breadth) {
155+
MarkerGeometry(const QString& label, bool useIcon, Qt::Alignment align, float breadth) {
151156
// If the label is 1 character long, and this character isn't a letter or a number,
152157
// we can assume it's a special symbol
153158
m_isSymbol = label.length() == 1 && !label[0].isLetterOrNumber();
@@ -169,6 +174,8 @@ struct MarkerGeometry {
169174
m_font.setWeight(75); // bold
170175
m_font.setItalic(false);
171176

177+
const qreal margin{3.f};
178+
172179
qreal capHeight;
173180
{
174181
QFontMetricsF metrics{m_font};
@@ -186,35 +193,36 @@ struct MarkerGeometry {
186193
// large font size
187194
m_font.setPointSize(50.0);
188195
metrics = QFontMetricsF(m_font);
189-
m_textRect = metrics.tightBoundingRect(label);
196+
m_contentRect = metrics.tightBoundingRect(label);
190197

191198
// Now we calculate how much bigger this is than our target
192199
// size.
193-
const auto ratioH = targetHeight / m_textRect.height();
194-
const auto ratioW = targetWidth / m_textRect.width();
200+
const auto ratioH = targetHeight / m_contentRect.height();
201+
const auto ratioW = targetWidth / m_contentRect.width();
195202
const auto ratio = std::min(ratioH, ratioW);
196203

197204
// And we scale the font size accordingly.
198205
m_font.setPointSizeF(50.0 * ratio);
199206
metrics = QFontMetricsF(m_font);
200-
m_textRect = metrics.tightBoundingRect(label);
207+
m_contentRect = metrics.tightBoundingRect(label);
208+
} else if (useIcon) {
209+
m_contentRect = QRectF(0.f, 0.f, std::ceil(capHeight), std::ceil(capHeight));
201210
} else {
202-
m_textRect = QRectF{0.f,
203-
-metrics.capHeight(),
211+
m_contentRect = QRectF{0.f,
212+
-capHeight,
204213
metrics.boundingRect(label).width(),
205-
metrics.capHeight()};
214+
capHeight};
206215
}
207216
}
208217

209-
const qreal margin{3.f};
210218
const Qt::Alignment alignH = align & Qt::AlignHorizontal_Mask;
211219
const Qt::Alignment alignV = align & Qt::AlignVertical_Mask;
212220
const bool alignHCenter{alignH == Qt::AlignHCenter};
213221
const qreal widthRounding{alignHCenter ? 2.f : 1.f};
214222

215223
m_labelRect = QRectF{0.f,
216224
0.f,
217-
std::ceil((m_textRect.width() + 2.f * margin) / widthRounding) *
225+
std::ceil((m_contentRect.width() + 2.f * margin) / widthRounding) *
218226
widthRounding,
219227
std::ceil(capHeight + 2.f * margin)};
220228

@@ -285,8 +293,10 @@ QImage WaveformMark::generateImage(float breadth, float devicePixelRatio) {
285293
}
286294
}
287295

296+
const bool useIcon = m_iconPath != "";
297+
288298
// Determine drawing geometries
289-
const MarkerGeometry markerGeometry(label, m_align, breadth);
299+
const MarkerGeometry markerGeometry(label, useIcon, m_align, breadth);
290300

291301
m_label.setAreaRect(markerGeometry.m_labelRect);
292302

@@ -324,7 +334,7 @@ QImage WaveformMark::generateImage(float breadth, float devicePixelRatio) {
324334
hcenter + 1.f,
325335
markerGeometry.m_imageSize.height()));
326336

327-
if (label.length() != 0) {
337+
if (useIcon || label.length() != 0) {
328338
painter.setPen(borderColor());
329339

330340
// Draw the label rounded rect with border
@@ -333,24 +343,30 @@ QImage WaveformMark::generateImage(float breadth, float devicePixelRatio) {
333343
painter.fillPath(path, fillColor());
334344
painter.drawPath(path);
335345

336-
// Draw the text
337-
painter.setBrush(Qt::transparent);
338-
painter.setPen(labelColor());
339-
painter.setFont(markerGeometry.m_font);
340-
341-
// Center m_textRect.width() and m_textRect.height() inside m_labelRect
346+
// Center m_contentRect.width() and m_contentRect.height() inside m_labelRect
342347
// and apply the offset x,y so the text ends up in the centered width,height.
343348
QPointF pos(markerGeometry.m_labelRect.x() +
344349
(markerGeometry.m_labelRect.width() -
345-
markerGeometry.m_textRect.width()) /
350+
markerGeometry.m_contentRect.width()) /
346351
2.f -
347-
markerGeometry.m_textRect.x(),
352+
markerGeometry.m_contentRect.x(),
348353
markerGeometry.m_labelRect.y() +
349354
(markerGeometry.m_labelRect.height() -
350-
markerGeometry.m_textRect.height()) /
355+
markerGeometry.m_contentRect.height()) /
351356
2.f -
352-
markerGeometry.m_textRect.y());
353-
painter.drawText(pos, label);
357+
markerGeometry.m_contentRect.y());
358+
359+
if (useIcon) {
360+
QSvgRenderer svgRenderer(m_iconPath);
361+
svgRenderer.render(&painter, QRectF(pos, markerGeometry.m_contentRect.size()));
362+
} else {
363+
// Draw the text
364+
painter.setBrush(Qt::transparent);
365+
painter.setPen(labelColor());
366+
painter.setFont(markerGeometry.m_font);
367+
368+
painter.drawText(pos, label);
369+
}
354370
}
355371

356372
painter.end();

src/waveform/renderers/waveformmark.h

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class WaveformMark {
102102
QString m_text;
103103
Qt::Alignment m_align;
104104
QString m_pixmapPath;
105+
QString m_iconPath;
105106

106107
float m_linePosition{};
107108
int m_breadth{};

0 commit comments

Comments
 (0)