diff --git a/widgets/pushbutton.cpp b/widgets/pushbutton.cpp index 8415f4aa7..105cf9674 100644 --- a/widgets/pushbutton.cpp +++ b/widgets/pushbutton.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 PushButtonPrivate::syncActiveRect() @@ -203,6 +220,12 @@ void PushButton::setImage(const QString &path) d->setPixmap(this); } +void PushButton::setImage(const QString &path, const QString &elementid) +{ + d->svgElement = elementid; + setImage(path); +} + QString PushButton::image() const { return d->imagePath; diff --git a/widgets/pushbutton.h b/widgets/pushbutton.h index 1cd44c14e..7f2a4c9e7 100644 --- a/widgets/pushbutton.h +++ b/widgets/pushbutton.h @@ -70,6 +70,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. */