• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kde-runtime API Reference
  • KDE Home
  • Contact Us
 

PlasmaCore

  • sources
  • kde-4.14
  • kde-runtime
  • plasma
  • declarativeimports
  • core
svgitem.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Marco Martin <mart@kde.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "svgitem.h"
21 
22 #include <QPainter>
23 
24 #include "kdebug.h"
25 #include "plasma/svg.h"
26 
27 namespace Plasma
28 {
29 
30 SvgItem::SvgItem(QDeclarativeItem *parent)
31  : QDeclarativeItem(parent),
32  m_smooth(false)
33 {
34  setFlag(QGraphicsItem::ItemHasNoContents, false);
35 }
36 
37 
38 SvgItem::~SvgItem()
39 {
40 }
41 
42 void SvgItem::setElementId(const QString &elementID)
43 {
44  if (elementID == m_elementID) {
45  return;
46  }
47 
48  if (implicitWidth() <= 0) {
49  setImplicitWidth(naturalSize().width());
50  }
51  if (implicitHeight() <= 0) {
52  setImplicitHeight(naturalSize().height());
53  }
54 
55  m_elementID = elementID;
56  emit elementIdChanged();
57  emit naturalSizeChanged();
58  update();
59 }
60 
61 QString SvgItem::elementId() const
62 {
63  return m_elementID;
64 }
65 
66 QSizeF SvgItem::naturalSize() const
67 {
68  if (!m_svg) {
69  return QSizeF();
70  } else if (!m_elementID.isEmpty()) {
71  return m_svg.data()->elementSize(m_elementID);
72  }
73 
74  return m_svg.data()->size();
75 }
76 
77 
78 void SvgItem::setSvg(Plasma::Svg *svg)
79 {
80  if (m_svg) {
81  disconnect(m_svg.data(), 0, this, 0);
82  }
83  m_svg = svg;
84  if (svg) {
85  connect(svg, SIGNAL(repaintNeeded()), this, SLOT(updateNeeded()));
86  connect(svg, SIGNAL(repaintNeeded()), this, SIGNAL(naturalSizeChanged()));
87  connect(svg, SIGNAL(sizeChanged()), this, SIGNAL(naturalSizeChanged()));
88  }
89 
90  if (implicitWidth() <= 0) {
91  setImplicitWidth(naturalSize().width());
92  }
93  if (implicitHeight() <= 0) {
94  setImplicitHeight(naturalSize().height());
95  }
96 
97  emit svgChanged();
98  emit naturalSizeChanged();
99 }
100 
101 Plasma::Svg *SvgItem::svg() const
102 {
103  return m_svg.data();
104 }
105 
106 void SvgItem::setSmooth(const bool smooth)
107 {
108  if (smooth == m_smooth) {
109  return;
110  }
111  m_smooth = smooth;
112  emit smoothChanged();
113  update();
114 }
115 
116 bool SvgItem::smooth() const
117 {
118  return m_smooth;
119 }
120 
121 void SvgItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
122 {
123  Q_UNUSED(option);
124  Q_UNUSED(widget);
125 
126  if (!m_svg) {
127  return;
128  }
129  //do without painter save, faster and the support can be compiled out
130  const bool wasAntiAlias = painter->testRenderHint(QPainter::Antialiasing);
131  const bool wasSmoothTransform = painter->testRenderHint(QPainter::SmoothPixmapTransform);
132  painter->setRenderHint(QPainter::Antialiasing, m_smooth);
133  painter->setRenderHint(QPainter::SmoothPixmapTransform, m_smooth);
134 
135  //setContainsMultipleImages has to be done there since m_frameSvg can be shared with somebody else
136  m_svg.data()->setContainsMultipleImages(!m_elementID.isEmpty());
137  m_svg.data()->paint(painter, boundingRect(), m_elementID);
138  painter->setRenderHint(QPainter::Antialiasing, wasAntiAlias);
139  painter->setRenderHint(QPainter::SmoothPixmapTransform, wasSmoothTransform);
140 }
141 
142 void SvgItem::updateNeeded()
143 {
144  if (implicitWidth() <= 0) {
145  setImplicitWidth(naturalSize().width());
146  }
147  if (implicitHeight() <= 0) {
148  setImplicitHeight(naturalSize().height());
149  }
150  update();
151 }
152 
153 void SvgItem::setImplicitWidth(qreal width)
154 {
155  if (implicitWidth() == width) {
156  return;
157  }
158 
159  QDeclarativeItem::setImplicitWidth(width);
160 
161  emit implicitWidthChanged();
162 }
163 
164 qreal SvgItem::implicitWidth() const
165 {
166  return QDeclarativeItem::implicitWidth();
167 }
168 
169 void SvgItem::setImplicitHeight(qreal height)
170 {
171  if (implicitHeight() == height) {
172  return;
173  }
174 
175  QDeclarativeItem::setImplicitHeight(height);
176 
177  emit implicitHeightChanged();
178 }
179 
180 qreal SvgItem::implicitHeight() const
181 {
182  return QDeclarativeItem::implicitHeight();
183 }
184 
185 } // Plasma namespace
186 
187 #include "svgitem.moc"
QDeclarativeItem::setImplicitWidth
void setImplicitWidth(qreal w)
QWidget
Plasma::SvgItem::setSvg
void setSvg(Plasma::Svg *svg)
Definition: svgitem.cpp:78
QPainter::setRenderHint
void setRenderHint(RenderHint hint, bool on)
QGraphicsItem::setFlag
void setFlag(GraphicsItemFlag flag, bool enabled)
Plasma::SvgItem::implicitHeightChanged
void implicitHeightChanged()
svgitem.h
Plasma::SvgItem::~SvgItem
~SvgItem()
Definition: svgitem.cpp:38
Plasma::SvgItem::svgChanged
void svgChanged()
QWeakPointer::data
T * data() const
Plasma::SvgItem::svg
Plasma::Svg * svg() const
QDeclarativeItem::implicitHeight
qreal implicitHeight() const
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
Plasma::SvgItem::naturalSize
QSizeF naturalSize() const
QGraphicsItem::update
void update(const QRectF &rect)
Plasma::SvgItem::smooth
bool smooth() const
Plasma::SvgItem::implicitHeight
qreal implicitHeight() const
Plasma::SvgItem::setImplicitWidth
void setImplicitWidth(qreal width)
Definition: svgitem.cpp:153
Plasma::SvgItem::paint
void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
Definition: svgitem.cpp:121
QPainter
Plasma::SvgItem::setSmooth
void setSmooth(const bool smooth)
Definition: svgitem.cpp:106
QString::isEmpty
bool isEmpty() const
Plasma::SvgItem::setImplicitHeight
void setImplicitHeight(qreal height)
Definition: svgitem.cpp:169
QString
QDeclarativeItem::implicitWidth
qreal implicitWidth() const
Plasma::SvgItem::elementIdChanged
void elementIdChanged()
Plasma::SvgItem::implicitWidthChanged
void implicitWidthChanged()
Plasma::SvgItem::SvgItem
SvgItem(QDeclarativeItem *parent=0)
Definition: svgitem.cpp:30
Plasma::SvgItem::setElementId
void setElementId(const QString &elementID)
Definition: svgitem.cpp:42
Plasma::SvgItem::implicitWidth
qreal implicitWidth() const
QSizeF
QGraphicsItem::boundingRect
virtual QRectF boundingRect() const =0
Plasma::SvgItem::updateNeeded
void updateNeeded()
Definition: svgitem.cpp:142
QDeclarativeItem::setImplicitHeight
void setImplicitHeight(qreal h)
QPainter::testRenderHint
bool testRenderHint(RenderHint hint) const
QStyleOptionGraphicsItem
QDeclarativeItem
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Plasma::SvgItem::elementId
QString elementId() const
Plasma::SvgItem::naturalSizeChanged
void naturalSizeChanged()
Plasma::SvgItem::smoothChanged
void smoothChanged()
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:08:28 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

PlasmaCore

Skip menu "PlasmaCore"
  • Main Page
  • Namespace List
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kde-runtime API Reference

Skip menu "kde-runtime API Reference"
  • KCMShell
  • KNotify
  • Plasma Runtime
  •     PlasmaCore
  •     DragAndDrop
  •     PlasmaComponents
  •     PlasmaExtraComponents
  •     QtExtraComponents

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal