Add support to load a single element from a svg as icon

svn path=/trunk/KDE/kdelibs/; revision=1003809
This commit is contained in:
Arthur Renato Mello 2009-07-28 21:07:19 +00:00
parent 8c2bded373
commit f8580b442d
2 changed files with 35 additions and 2 deletions

View File

@ -66,11 +66,27 @@ public:
KMimeType::Ptr mime = KMimeType::findByPath(absImagePath); KMimeType::Ptr mime = KMimeType::findByPath(absImagePath);
QPixmap pm(q->size().toSize()); 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(); svg = new Svg();
QPainter p(&pm); QPainter p(&pm);
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()); svg->paint(&p, pm.rect());
}
} else { } else {
pm = QPixmap(absImagePath); pm = QPixmap(absImagePath);
} }
@ -93,6 +109,7 @@ public:
QString imagePath; QString imagePath;
QString absImagePath; QString absImagePath;
Svg *svg; Svg *svg;
QString svgElement;
}; };
void PushButtonPrivate::syncActiveRect() void PushButtonPrivate::syncActiveRect()
@ -203,6 +220,12 @@ void PushButton::setImage(const QString &path)
d->setPixmap(this); d->setPixmap(this);
} }
void PushButton::setImage(const QString &path, const QString &elementid)
{
d->svgElement = elementid;
setImage(path);
}
QString PushButton::image() const QString PushButton::image() const
{ {
return d->imagePath; return d->imagePath;

View File

@ -70,6 +70,16 @@ public:
*/ */
void setImage(const QString &path); 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. * @return the image path being displayed currently, or an empty string if none.
*/ */