@@ -111,6 +111,11 @@ WaveformMark::WaveformMark(const QString& group,
111
111
if (!m_pixmapPath.isEmpty ()) {
112
112
m_pixmapPath = context.makeSkinPath (m_pixmapPath);
113
113
}
114
+
115
+ m_iconPath = context.selectString (node, " Icon" );
116
+ if (!m_iconPath.isEmpty ()) {
117
+ m_iconPath = context.makeSkinPath (m_iconPath);
118
+ }
114
119
}
115
120
116
121
WaveformMark::~WaveformMark () = default ;
@@ -143,11 +148,11 @@ bool WaveformMark::contains(QPoint point, Qt::Orientation orientation) const {
143
148
struct MarkerGeometry {
144
149
bool m_isSymbol; // it the label normal text or a single symbol (e.g. open circle arrow)
145
150
QFont m_font;
146
- QRectF m_textRect ;
151
+ QRectF m_contentRect ;
147
152
QRectF m_labelRect;
148
153
QSizeF m_imageSize;
149
154
150
- MarkerGeometry (const QString& label, Qt::Alignment align, float breadth) {
155
+ MarkerGeometry (const QString& label, bool useIcon, Qt::Alignment align, float breadth) {
151
156
// If the label is 1 character long, and this character isn't a letter or a number,
152
157
// we can assume it's a special symbol
153
158
m_isSymbol = label.length () == 1 && !label[0 ].isLetterOrNumber ();
@@ -169,6 +174,8 @@ struct MarkerGeometry {
169
174
m_font.setWeight (75 ); // bold
170
175
m_font.setItalic (false );
171
176
177
+ const qreal margin{3 .f };
178
+
172
179
qreal capHeight;
173
180
{
174
181
QFontMetricsF metrics{m_font};
@@ -186,35 +193,36 @@ struct MarkerGeometry {
186
193
// large font size
187
194
m_font.setPointSize (50.0 );
188
195
metrics = QFontMetricsF (m_font);
189
- m_textRect = metrics.tightBoundingRect (label);
196
+ m_contentRect = metrics.tightBoundingRect (label);
190
197
191
198
// Now we calculate how much bigger this is than our target
192
199
// 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 ();
195
202
const auto ratio = std::min (ratioH, ratioW);
196
203
197
204
// And we scale the font size accordingly.
198
205
m_font.setPointSizeF (50.0 * ratio);
199
206
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));
201
210
} else {
202
- m_textRect = QRectF{0 .f ,
203
- -metrics. capHeight () ,
211
+ m_contentRect = QRectF{0 .f ,
212
+ -capHeight,
204
213
metrics.boundingRect (label).width (),
205
- metrics. capHeight () };
214
+ capHeight};
206
215
}
207
216
}
208
217
209
- const qreal margin{3 .f };
210
218
const Qt::Alignment alignH = align & Qt::AlignHorizontal_Mask;
211
219
const Qt::Alignment alignV = align & Qt::AlignVertical_Mask;
212
220
const bool alignHCenter{alignH == Qt::AlignHCenter};
213
221
const qreal widthRounding{alignHCenter ? 2 .f : 1 .f };
214
222
215
223
m_labelRect = QRectF{0 .f ,
216
224
0 .f ,
217
- std::ceil ((m_textRect .width () + 2 .f * margin) / widthRounding) *
225
+ std::ceil ((m_contentRect .width () + 2 .f * margin) / widthRounding) *
218
226
widthRounding,
219
227
std::ceil (capHeight + 2 .f * margin)};
220
228
@@ -285,8 +293,10 @@ QImage WaveformMark::generateImage(float breadth, float devicePixelRatio) {
285
293
}
286
294
}
287
295
296
+ const bool useIcon = m_iconPath != " " ;
297
+
288
298
// Determine drawing geometries
289
- const MarkerGeometry markerGeometry (label, m_align, breadth);
299
+ const MarkerGeometry markerGeometry (label, useIcon, m_align, breadth);
290
300
291
301
m_label.setAreaRect (markerGeometry.m_labelRect );
292
302
@@ -324,7 +334,7 @@ QImage WaveformMark::generateImage(float breadth, float devicePixelRatio) {
324
334
hcenter + 1 .f ,
325
335
markerGeometry.m_imageSize .height ()));
326
336
327
- if (label.length () != 0 ) {
337
+ if (useIcon || label.length () != 0 ) {
328
338
painter.setPen (borderColor ());
329
339
330
340
// Draw the label rounded rect with border
@@ -333,24 +343,30 @@ QImage WaveformMark::generateImage(float breadth, float devicePixelRatio) {
333
343
painter.fillPath (path, fillColor ());
334
344
painter.drawPath (path);
335
345
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
342
347
// and apply the offset x,y so the text ends up in the centered width,height.
343
348
QPointF pos (markerGeometry.m_labelRect .x () +
344
349
(markerGeometry.m_labelRect .width () -
345
- markerGeometry.m_textRect .width ()) /
350
+ markerGeometry.m_contentRect .width ()) /
346
351
2 .f -
347
- markerGeometry.m_textRect .x (),
352
+ markerGeometry.m_contentRect .x (),
348
353
markerGeometry.m_labelRect .y () +
349
354
(markerGeometry.m_labelRect .height () -
350
- markerGeometry.m_textRect .height ()) /
355
+ markerGeometry.m_contentRect .height ()) /
351
356
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
+ }
354
370
}
355
371
356
372
painter.end ();
0 commit comments