2010-10-12 21:51:57 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2010 Marco Martin <mart@kde.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Library General Public License as
|
|
|
|
* published by the Free Software Foundation; either version 2, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2011-11-01 19:27:55 +01:00
|
|
|
#include "svgitem.h"
|
2010-10-12 21:51:57 +02:00
|
|
|
|
2012-04-10 00:22:13 +02:00
|
|
|
#include <QPainter>
|
2010-10-12 21:51:57 +02:00
|
|
|
|
|
|
|
#include "kdebug.h"
|
|
|
|
#include "plasma/svg.h"
|
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
2013-02-01 17:24:07 +01:00
|
|
|
SvgItem::SvgItem(QQuickItem *parent)
|
|
|
|
: QQuickItem(parent),
|
2010-12-06 23:19:33 +01:00
|
|
|
m_smooth(false)
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2013-02-01 17:24:07 +01:00
|
|
|
setFlag(QQuickItem::ItemHasContents, true);
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SvgItem::~SvgItem()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-05 21:22:47 +01:00
|
|
|
void SvgItem::setElementId(const QString &elementID)
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2012-06-20 14:06:23 +02:00
|
|
|
if (elementID == m_elementID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:33:50 +01:00
|
|
|
if (implicitWidth() <= 0) {
|
|
|
|
setImplicitWidth(naturalSize().width());
|
|
|
|
}
|
|
|
|
if (implicitHeight() <= 0) {
|
|
|
|
setImplicitHeight(naturalSize().height());
|
|
|
|
}
|
|
|
|
|
2010-11-05 21:22:47 +01:00
|
|
|
m_elementID = elementID;
|
2012-05-27 20:36:14 +02:00
|
|
|
emit elementIdChanged();
|
2010-12-06 22:54:58 +01:00
|
|
|
emit naturalSizeChanged();
|
2010-10-12 21:51:57 +02:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2010-11-05 21:22:47 +01:00
|
|
|
QString SvgItem::elementId() const
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2010-11-05 21:22:47 +01:00
|
|
|
return m_elementID;
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2010-12-06 22:54:58 +01:00
|
|
|
QSizeF SvgItem::naturalSize() const
|
|
|
|
{
|
|
|
|
if (!m_svg) {
|
|
|
|
return QSizeF();
|
|
|
|
} else if (!m_elementID.isEmpty()) {
|
|
|
|
return m_svg.data()->elementSize(m_elementID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return m_svg.data()->size();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-11-05 21:22:47 +01:00
|
|
|
void SvgItem::setSvg(Plasma::Svg *svg)
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2010-11-05 21:22:47 +01:00
|
|
|
if (m_svg) {
|
|
|
|
disconnect(m_svg.data(), 0, this, 0);
|
|
|
|
}
|
|
|
|
m_svg = svg;
|
2011-06-24 17:56:42 +02:00
|
|
|
if (svg) {
|
|
|
|
connect(svg, SIGNAL(repaintNeeded()), this, SLOT(updateNeeded()));
|
|
|
|
connect(svg, SIGNAL(repaintNeeded()), this, SIGNAL(naturalSizeChanged()));
|
|
|
|
connect(svg, SIGNAL(sizeChanged()), this, SIGNAL(naturalSizeChanged()));
|
|
|
|
}
|
2012-11-27 13:33:50 +01:00
|
|
|
|
|
|
|
if (implicitWidth() <= 0) {
|
|
|
|
setImplicitWidth(naturalSize().width());
|
|
|
|
}
|
|
|
|
if (implicitHeight() <= 0) {
|
|
|
|
setImplicitHeight(naturalSize().height());
|
|
|
|
}
|
|
|
|
|
2012-05-27 20:36:14 +02:00
|
|
|
emit svgChanged();
|
2010-12-06 22:54:58 +01:00
|
|
|
emit naturalSizeChanged();
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2010-11-05 21:22:47 +01:00
|
|
|
Plasma::Svg *SvgItem::svg() const
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2010-11-05 21:22:47 +01:00
|
|
|
return m_svg.data();
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2010-12-06 23:19:33 +01:00
|
|
|
void SvgItem::setSmooth(const bool smooth)
|
|
|
|
{
|
|
|
|
if (smooth == m_smooth) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_smooth = smooth;
|
2012-05-27 20:36:14 +02:00
|
|
|
emit smoothChanged();
|
2010-12-06 23:19:33 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SvgItem::smooth() const
|
|
|
|
{
|
|
|
|
return m_smooth;
|
|
|
|
}
|
|
|
|
|
2013-02-01 17:24:07 +01:00
|
|
|
void SvgItem::paint(QPainter *painter)
|
2010-10-12 21:51:57 +02:00
|
|
|
{
|
2010-11-05 21:22:47 +01:00
|
|
|
if (!m_svg) {
|
|
|
|
return;
|
|
|
|
}
|
2010-12-06 23:19:33 +01:00
|
|
|
//do without painter save, faster and the support can be compiled out
|
|
|
|
const bool wasAntiAlias = painter->testRenderHint(QPainter::Antialiasing);
|
|
|
|
const bool wasSmoothTransform = painter->testRenderHint(QPainter::SmoothPixmapTransform);
|
|
|
|
painter->setRenderHint(QPainter::Antialiasing, m_smooth);
|
|
|
|
painter->setRenderHint(QPainter::SmoothPixmapTransform, m_smooth);
|
|
|
|
|
2010-11-05 21:22:47 +01:00
|
|
|
//setContainsMultipleImages has to be done there since m_frameSvg can be shared with somebody else
|
|
|
|
m_svg.data()->setContainsMultipleImages(!m_elementID.isEmpty());
|
|
|
|
m_svg.data()->paint(painter, boundingRect(), m_elementID);
|
2010-12-06 23:19:33 +01:00
|
|
|
painter->setRenderHint(QPainter::Antialiasing, wasAntiAlias);
|
|
|
|
painter->setRenderHint(QPainter::SmoothPixmapTransform, wasSmoothTransform);
|
2010-10-12 21:51:57 +02:00
|
|
|
}
|
|
|
|
|
2011-03-18 13:32:49 +01:00
|
|
|
void SvgItem::updateNeeded()
|
|
|
|
{
|
2012-11-27 13:33:50 +01:00
|
|
|
if (implicitWidth() <= 0) {
|
|
|
|
setImplicitWidth(naturalSize().width());
|
|
|
|
}
|
|
|
|
if (implicitHeight() <= 0) {
|
|
|
|
setImplicitHeight(naturalSize().height());
|
|
|
|
}
|
2011-03-18 13:32:49 +01:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2012-11-27 13:33:50 +01:00
|
|
|
void SvgItem::setImplicitWidth(qreal width)
|
|
|
|
{
|
|
|
|
if (implicitWidth() == width) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-01 17:24:07 +01:00
|
|
|
QQuickItem::setImplicitWidth(width);
|
2012-11-27 13:33:50 +01:00
|
|
|
|
|
|
|
emit implicitWidthChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal SvgItem::implicitWidth() const
|
|
|
|
{
|
2013-02-01 17:24:07 +01:00
|
|
|
return QQuickItem::implicitWidth();
|
2012-11-27 13:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SvgItem::setImplicitHeight(qreal height)
|
|
|
|
{
|
|
|
|
if (implicitHeight() == height) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-01 17:24:07 +01:00
|
|
|
QQuickItem::setImplicitHeight(height);
|
2012-11-27 13:33:50 +01:00
|
|
|
|
|
|
|
emit implicitHeightChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
qreal SvgItem::implicitHeight() const
|
|
|
|
{
|
2013-02-01 17:24:07 +01:00
|
|
|
return QQuickItem::implicitHeight();
|
2012-11-27 13:33:50 +01:00
|
|
|
}
|
|
|
|
|
2010-10-12 21:51:57 +02:00
|
|
|
} // Plasma namespace
|
|
|
|
|
2011-11-01 19:27:55 +01:00
|
|
|
#include "svgitem.moc"
|