Qt 4.8
Public Functions | Properties | List of all members
QSvgIconEngine Class Reference

#include <qsvgiconengine.h>

Inheritance diagram for QSvgIconEngine:
QIconEngineV2 QIconEngine

Public Functions

QSize actualSize (const QSize &size, QIcon::Mode mode, QIcon::State state)
 Returns the actual size of the icon the engine provides for the requested size, mode and state. More...
 
void addFile (const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state)
 Called by QIcon::addFile(). More...
 
void addPixmap (const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
 Called by QIcon::addPixmap(). More...
 
QIconEngineV2clone () const
 Returns a clone of this icon engine. More...
 
QString key () const
 Returns a key that identifies this icon engine. More...
 
void paint (QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
 Uses the given painter to paint the icon with the required mode and state into the rectangle rect. More...
 
QPixmap pixmap (const QSize &size, QIcon::Mode mode, QIcon::State state)
 Returns the icon as a pixmap with the required size, mode, and state. More...
 
 QSvgIconEngine ()
 
 QSvgIconEngine (const QSvgIconEngine &other)
 
bool read (QDataStream &in)
 Reads icon engine contents from the QDataStream in. More...
 
bool write (QDataStream &out) const
 Writes the contents of this engine to the QDataStream out. More...
 
 ~QSvgIconEngine ()
 
- Public Functions inherited from QIconEngineV2
QList< QSizeavailableSizes (QIcon::Mode mode=QIcon::Normal, QIcon::State state=QIcon::Off)
 Returns sizes of all images that are contained in the engine for the specific mode and state. More...
 
QString iconName ()
 Returns the name used to create the engine, if available. More...
 
virtual void virtual_hook (int id, void *data)
 Additional method to allow extending QIconEngineV2 without adding new virtual methods (and without breaking binary compatibility). More...
 
- Public Functions inherited from QIconEngine
virtual ~QIconEngine ()
 Destroys the icon engine. More...
 

Properties

QSharedDataPointer< QSvgIconEnginePrivated
 

Additional Inherited Members

- Public Types inherited from QIconEngineV2
enum  IconEngineHook { AvailableSizesHook = 1, IconNameHook }
 These enum values are used for virtual_hook() to allow additional queries to icon engine without breaking binary compatibility. More...
 

Detailed Description

Definition at line 54 of file qsvgiconengine.h.

Constructors and Destructors

◆ QSvgIconEngine() [1/2]

QSvgIconEngine::QSvgIconEngine ( )

Definition at line 95 of file qsvgiconengine.cpp.

Referenced by clone().

97 {
98 }
QSharedDataPointer< QSvgIconEnginePrivate > d

◆ QSvgIconEngine() [2/2]

QSvgIconEngine::QSvgIconEngine ( const QSvgIconEngine other)

Definition at line 100 of file qsvgiconengine.cpp.

101  : QIconEngineV2(other), d(new QSvgIconEnginePrivate)
102 {
103  d->svgFiles = other.d->svgFiles;
104  if (other.d->svgBuffers)
106  if (other.d->addedPixmaps)
108 }
The QIconEngineV2 class provides an abstract base class for QIcon renderers.
Definition: qiconengine.h:73
QHash< int, QPixmap > * addedPixmaps
QHash< int, QString > svgFiles
QSharedDataPointer< QSvgIconEnginePrivate > d
QHash< int, QByteArray > * svgBuffers

◆ ~QSvgIconEngine()

QSvgIconEngine::~QSvgIconEngine ( )

Definition at line 111 of file qsvgiconengine.cpp.

112 {
113 }

Functions

◆ actualSize()

QSize QSvgIconEngine::actualSize ( const QSize size,
QIcon::Mode  mode,
QIcon::State  state 
)
virtual

Returns the actual size of the icon the engine provides for the requested size, mode and state.

The default implementation returns the given size.

Reimplemented from QIconEngine.

Definition at line 116 of file qsvgiconengine.cpp.

Referenced by pixmap().

118 {
119  if (d->addedPixmaps) {
120  QPixmap pm = d->addedPixmaps->value(d->hashKey(mode, state));
121  if (!pm.isNull() && pm.size() == size)
122  return size;
123  }
124 
125  QPixmap pm = pixmap(size, mode, state);
126  if (pm.isNull())
127  return QSize();
128  return pm.size();
129 }
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
static int hashKey(QIcon::Mode mode, QIcon::State state)
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
Returns the icon as a pixmap with the required size, mode, and state.
QHash< int, QPixmap > * addedPixmaps
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QSharedDataPointer< QSvgIconEnginePrivate > d
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615

◆ addFile()

void QSvgIconEngine::addFile ( const QString fileName,
const QSize size,
QIcon::Mode  mode,
QIcon::State  state 
)
virtual

Called by QIcon::addFile().

Adds a specialized pixmap from the file with the given fileName, size, mode and state. The default pixmap-based engine stores any supplied file names, and it loads the pixmaps on demand instead of using scaled pixmaps if the size of a pixmap matches the size of icon requested. Custom icon engines that implement scalable vector formats are free to ignores any extra files.

Reimplemented from QIconEngine.

Definition at line 206 of file qsvgiconengine.cpp.

Referenced by QSvgIconPlugin::create().

208 {
209  if (!fileName.isEmpty()) {
210  QString abs = fileName;
211  if (fileName.at(0) != QLatin1Char(':'))
212  abs = QFileInfo(fileName).absoluteFilePath();
213  if (abs.endsWith(QLatin1String(".svg"), Qt::CaseInsensitive)
214 #ifndef QT_NO_COMPRESS
215  || abs.endsWith(QLatin1String(".svgz"), Qt::CaseInsensitive)
216  || abs.endsWith(QLatin1String(".svg.gz"), Qt::CaseInsensitive))
217 #endif
218  {
219  QSvgRenderer renderer(abs);
220  if (renderer.isValid()) {
221  d->stepSerialNum();
222  d->svgFiles.insert(d->hashKey(mode, state), abs);
223  }
224  } else {
225  QPixmap pm(abs);
226  if (!pm.isNull())
227  addPixmap(pm, mode, state);
228  }
229  }
230 }
const QChar at(int i) const
Returns the character at the given index position in the string.
Definition: qstring.h:698
QString absoluteFilePath() const
Returns an absolute path including the file name.
Definition: qfileinfo.cpp:534
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
void addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
Called by QIcon::addPixmap().
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
static int hashKey(QIcon::Mode mode, QIcon::State state)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
The QSvgRenderer class is used to draw the contents of SVG files onto paint devices.
Definition: qsvgrenderer.h:64
QHash< int, QString > svgFiles
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QSharedDataPointer< QSvgIconEnginePrivate > d
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition: qstring.cpp:3796
The QFileInfo class provides system-independent file information.
Definition: qfileinfo.h:60
static QString fileName(const QString &fileUrl)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ addPixmap()

void QSvgIconEngine::addPixmap ( const QPixmap pixmap,
QIcon::Mode  mode,
QIcon::State  state 
)
virtual

Called by QIcon::addPixmap().

Adds a specialized pixmap for the given mode and state. The default pixmap-based engine stores any supplied pixmaps, and it uses them instead of scaled pixmaps if the size of a pixmap matches the size of icon requested. Custom icon engines that implement scalable vector formats are free to ignores any extra pixmaps.

Reimplemented from QIconEngine.

Definition at line 196 of file qsvgiconengine.cpp.

Referenced by addFile().

198 {
199  if (!d->addedPixmaps)
201  d->stepSerialNum();
202  d->addedPixmaps->insert(d->hashKey(mode, state), pixmap);
203 }
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
static int hashKey(QIcon::Mode mode, QIcon::State state)
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
Returns the icon as a pixmap with the required size, mode, and state.
QHash< int, QPixmap > * addedPixmaps
QSharedDataPointer< QSvgIconEnginePrivate > d

◆ clone()

QIconEngineV2 * QSvgIconEngine::clone ( ) const
virtual

Returns a clone of this icon engine.

Reimplemented from QIconEngineV2.

Definition at line 243 of file qsvgiconengine.cpp.

244 {
245  return new QSvgIconEngine(*this);
246 }

◆ key()

QString QSvgIconEngine::key ( ) const
virtual

Returns a key that identifies this icon engine.

Reimplemented from QIconEngineV2.

Definition at line 238 of file qsvgiconengine.cpp.

Referenced by read(), and write().

239 {
240  return QLatin1String("svg");
241 }
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString

◆ paint()

void QSvgIconEngine::paint ( QPainter painter,
const QRect rect,
QIcon::Mode  mode,
QIcon::State  state 
)
virtual

Uses the given painter to paint the icon with the required mode and state into the rectangle rect.

Implements QIconEngine.

Definition at line 232 of file qsvgiconengine.cpp.

234 {
235  painter->drawPixmap(rect, pixmap(rect.size(), mode, state));
236 }
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
Returns the icon as a pixmap with the required size, mode, and state.
void drawPixmap(const QRectF &targetRect, const QPixmap &pixmap, const QRectF &sourceRect)
Draws the rectangular portion source of the given pixmap into the given target in the paint device...
Definition: qpainter.cpp:5619

◆ pixmap()

QPixmap QSvgIconEngine::pixmap ( const QSize size,
QIcon::Mode  mode,
QIcon::State  state 
)
virtual

Returns the icon as a pixmap with the required size, mode, and state.

The default implementation creates a new pixmap and calls paint() to fill it.

Reimplemented from QIconEngine.

Definition at line 153 of file qsvgiconengine.cpp.

Referenced by actualSize(), addPixmap(), paint(), and read().

155 {
156  QPixmap pm;
157 
158  QString pmckey(d->pmcKey(size, mode, state));
159  if (QPixmapCache::find(pmckey, pm))
160  return pm;
161 
162  if (d->addedPixmaps) {
163  pm = d->addedPixmaps->value(d->hashKey(mode, state));
164  if (!pm.isNull() && pm.size() == size)
165  return pm;
166  }
167 
168  QSvgRenderer renderer;
169  d->loadDataForModeAndState(&renderer, mode, state);
170  if (!renderer.isValid())
171  return pm;
172 
173  QSize actualSize = renderer.defaultSize();
174  if (!actualSize.isNull())
175  actualSize.scale(size, Qt::KeepAspectRatio);
176 
178  img.fill(0x00000000);
179  QPainter p(&img);
180  renderer.render(&p);
181  p.end();
182  pm = QPixmap::fromImage(img);
183  QStyleOption opt(0);
184  opt.palette = QApplication::palette();
185  QPixmap generated = QApplication::style()->generatedIconPixmap(mode, pm, &opt);
186  if (!generated.isNull())
187  pm = generated;
188 
189  if (!pm.isNull())
190  QPixmapCache::insert(pmckey, pm);
191 
192  return pm;
193 }
virtual QPixmap generatedIconPixmap(QIcon::Mode iconMode, const QPixmap &pixmap, const QStyleOption *opt) const =0
Returns a copy of the given pixmap, styled to conform to the specified iconMode and taking into accou...
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
static QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags=Qt::AutoColor)
Converts the given image to a pixmap using the specified flags to control the conversion.
Definition: qpixmap.cpp:2197
QString pmcKey(const QSize &size, QIcon::Mode mode, QIcon::State state)
QSize size() const
Returns the size of the pixmap.
Definition: qpixmap.cpp:661
QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
Returns the actual size of the icon the engine provides for the requested size, mode and state...
static QPalette palette()
Returns the application palette.
static QStyle * style()
Returns the application&#39;s style object.
The QString class provides a Unicode character string.
Definition: qstring.h:83
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
static QPixmap * find(const QString &key)
static int hashKey(QIcon::Mode mode, QIcon::State state)
The QStyleOption class stores the parameters used by QStyle functions.
Definition: qstyleoption.h:67
bool isValid() const
Returns true if there is a valid current document; otherwise returns false.
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
QSize defaultSize() const
Returns the default size of the document contents.
The QSvgRenderer class is used to draw the contents of SVG files onto paint devices.
Definition: qsvgrenderer.h:64
QHash< int, QPixmap > * addedPixmaps
void loadDataForModeAndState(QSvgRenderer *renderer, QIcon::Mode mode, QIcon::State state)
static bool insert(const QString &key, const QPixmap &pixmap)
Inserts a copy of the pixmap pixmap associated with the key into the cache.
void render(QPainter *p)
Renders the current document, or the current frame of an animated document, using the given painter...
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
void scale(int w, int h, Qt::AspectRatioMode mode)
Scales the size to a rectangle with the given width and height, according to the specified mode: ...
Definition: qsize.h:138
bool isNull() const
Returns true if both the width and height is 0; otherwise returns false.
Definition: qsize.h:117
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QSharedDataPointer< QSvgIconEnginePrivate > d
bool isNull() const
Returns true if this is a null pixmap; otherwise returns false.
Definition: qpixmap.cpp:615

◆ read()

bool QSvgIconEngine::read ( QDataStream in)
virtual

Reads icon engine contents from the QDataStream in.

Returns true if the contents were read; otherwise returns false.

QIconEngineV2's default implementation always return false.

Reimplemented from QIconEngineV2.

Definition at line 249 of file qsvgiconengine.cpp.

250 {
251  d = new QSvgIconEnginePrivate;
253 
254  if (in.version() >= QDataStream::Qt_4_4) {
255  int isCompressed;
256  QHash<int, QString> fileNames; // For memoryoptimization later
257  in >> fileNames >> isCompressed >> *d->svgBuffers;
258 #ifndef QT_NO_COMPRESS
259  if (!isCompressed) {
260  foreach(int key, d->svgBuffers->keys())
261  d->svgBuffers->insert(key, qCompress(d->svgBuffers->value(key)));
262  }
263 #else
264  if (isCompressed) {
265  qWarning("QSvgIconEngine: Can not decompress SVG data");
266  d->svgBuffers->clear();
267  }
268 #endif
269  int hasAddedPixmaps;
270  in >> hasAddedPixmaps;
271  if (hasAddedPixmaps) {
273  in >> *d->addedPixmaps;
274  }
275  }
276  else {
277  QPixmap pixmap;
279  uint mode;
280  uint state;
281  int num_entries;
282 
283  in >> data;
284  if (!data.isEmpty()) {
285 #ifndef QT_NO_COMPRESS
286  data = qUncompress(data);
287 #endif
288  if (!data.isEmpty())
290  }
291  in >> num_entries;
292  for (int i=0; i<num_entries; ++i) {
293  if (in.atEnd())
294  return false;
295  in >> pixmap;
296  in >> mode;
297  in >> state;
298  // The pm list written by 4.3 is buggy and/or useless, so ignore.
299  //addPixmap(pixmap, QIcon::Mode(mode), QIcon::State(state));
300  }
301  }
302 
303  return true;
304 }
QString key() const
Returns a key that identifies this icon engine.
void clear()
Removes all items from the hash.
Definition: qhash.h:574
Q_CORE_EXPORT QByteArray qUncompress(const uchar *data, int nbytes)
bool atEnd() const
Returns true if the I/O device has reached the end position (end of the stream or file) or if there i...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
static int hashKey(QIcon::Mode mode, QIcon::State state)
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
Returns the icon as a pixmap with the required size, mode, and state.
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
Q_CORE_EXPORT QByteArray qCompress(const uchar *data, int nbytes, int compressionLevel=-1)
QHash< int, QPixmap > * addedPixmaps
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QSharedDataPointer< QSvgIconEnginePrivate > d
QHash< int, QByteArray > * svgBuffers
QList< Key > keys() const
Returns a list containing all the keys in the hash, in an arbitrary order.
Definition: qhash.h:648

◆ write()

bool QSvgIconEngine::write ( QDataStream out) const
virtual

Writes the contents of this engine to the QDataStream out.

Returns true if the contents were written; otherwise returns false.

QIconEngineV2's default implementation always return false.

Reimplemented from QIconEngineV2.

Definition at line 307 of file qsvgiconengine.cpp.

308 {
309  if (out.version() >= QDataStream::Qt_4_4) {
310  int isCompressed = 0;
311 #ifndef QT_NO_COMPRESS
312  isCompressed = 1;
313 #endif
314  QHash<int, QByteArray> svgBuffers;
315  if (d->svgBuffers)
316  svgBuffers = *d->svgBuffers;
317  foreach(int key, d->svgFiles.keys()) {
318  QByteArray buf;
319  QFile f(d->svgFiles.value(key));
320  if (f.open(QIODevice::ReadOnly))
321  buf = f.readAll();
322 #ifndef QT_NO_COMPRESS
323  buf = qCompress(buf);
324 #endif
325  svgBuffers.insert(key, buf);
326  }
327  out << d->svgFiles << isCompressed << svgBuffers;
328  if (d->addedPixmaps)
329  out << (int)1 << *d->addedPixmaps;
330  else
331  out << (int)0;
332  }
333  else {
334  QByteArray buf;
335  if (d->svgBuffers)
337  if (buf.isEmpty()) {
339  if (!svgFile.isEmpty()) {
340  QFile f(svgFile);
341  if (f.open(QIODevice::ReadOnly))
342  buf = f.readAll();
343  }
344  }
345 #ifndef QT_NO_COMPRESS
346  buf = qCompress(buf);
347 #endif
348  out << buf;
349  // 4.3 has buggy handling of added pixmaps, so don't write any
350  out << (int)0;
351  }
352  return true;
353 }
QString key() const
Returns a key that identifies this icon engine.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
The QString class provides a Unicode character string.
Definition: qstring.h:83
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
static int hashKey(QIcon::Mode mode, QIcon::State state)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
int version() const
Returns the version number of the data serialization format.
Definition: qdatastream.h:212
Q_CORE_EXPORT QByteArray qCompress(const uchar *data, int nbytes, int compressionLevel=-1)
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
QHash< int, QPixmap > * addedPixmaps
QHash< int, QString > svgFiles
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
QSharedDataPointer< QSvgIconEnginePrivate > d
QHash< int, QByteArray > * svgBuffers
QList< Key > keys() const
Returns a list containing all the keys in the hash, in an arbitrary order.
Definition: qhash.h:648

Properties

◆ d

QSharedDataPointer<QSvgIconEnginePrivate> QSvgIconEngine::d
private

Definition at line 78 of file qsvgiconengine.h.

Referenced by actualSize(), addFile(), addPixmap(), pixmap(), QSvgIconEngine(), read(), and write().


The documentation for this class was generated from the following files: