diff --git a/widgets/toolbutton.cpp b/widgets/toolbutton.cpp index cd8e46ecb..e41d0395d 100644 --- a/widgets/toolbutton.cpp +++ b/widgets/toolbutton.cpp @@ -66,11 +66,27 @@ public: KMimeType::Ptr mime = KMimeType::findByPath(absImagePath); QPixmap pm(q->size().toSize()); + pm.fill(Qt::transparent); - if (mime->is("image/svg+xml")) { + if (mime->is("image/svg+xml") || mime->is("image/svg+xml-compressed")) { svg = new Svg(); QPainter p(&pm); - svg->paint(&p, pm.rect()); + + svg->setImagePath(absImagePath); + + if (!svgElement.isNull() && svg->hasElement(svgElement)) { + QSizeF elementSize = svg->elementSize(svgElement); + float scale = pm.width() / qMax(elementSize.width(), elementSize.height()); + + svg->resize(svg->size() * scale); + + QRectF elementRect(QPointF(0,0), svg->elementSize(svgElement)); + elementRect.moveCenter(q->rect().center()); + svg->paint(&p, elementRect, svgElement); + } else { + svg->resize(pm.size()); + svg->paint(&p, pm.rect()); + } } else { pm = QPixmap(absImagePath); } @@ -93,6 +109,7 @@ public: QString imagePath; QString absImagePath; Svg *svg; + QString svgElement; }; void ToolButtonPrivate::syncActiveRect() @@ -221,6 +238,12 @@ void ToolButton::setImage(const QString &path) d->setPixmap(this); } +void ToolButton::setImage(const QString &path, const QString &elementid) +{ + d->svgElement = elementid; + setImage(path); +} + void ToolButton::setIcon(const QIcon &icon) { nativeWidget()->setIcon(icon); diff --git a/widgets/toolbutton.h b/widgets/toolbutton.h index 534f3102c..c5c666fd1 100644 --- a/widgets/toolbutton.h +++ b/widgets/toolbutton.h @@ -83,6 +83,16 @@ public: */ void setImage(const QString &path); + /** + * Sets the path to an svg image to display and the id of the used svg element, if necessary. + * + * @arg path the path to the image; if a relative path, then a themed image will be loaded. + * @arg elementid the id of a svg element. + * + * @since 4.4 + */ + void setImage(const QString &path, const QString &elementid); + /** * @return the image path being displayed currently, or an empty string if none. */