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

#include <qsvgiohandler.h>

Inheritance diagram for QSvgIOHandler:
QImageIOHandler

Public Functions

virtual bool canRead () const
 Returns true if an image can be read from the device (i. More...
 
virtual QByteArray name () const
 Use format() instead. More...
 
virtual QVariant option (ImageOption option) const
 Returns the value assigned to option as a QVariant. More...
 
 QSvgIOHandler ()
 
virtual bool read (QImage *image)
 Read an image from the device, and stores it in image. More...
 
virtual void setOption (ImageOption option, const QVariant &value)
 Sets the option option with the value value. More...
 
virtual bool supportsOption (ImageOption option) const
 Returns true if the QImageIOHandler supports the option option; otherwise returns false. More...
 
 ~QSvgIOHandler ()
 
- Public Functions inherited from QImageIOHandler
virtual int currentImageNumber () const
 For image formats that support animation, this function returns the sequence number of the current image in the animation. More...
 
virtual QRect currentImageRect () const
 Returns the rect of the current image. More...
 
QIODevicedevice () const
 Returns the device currently assigned to the QImageIOHandler. More...
 
QByteArray format () const
 Returns the format that is currently assigned to QImageIOHandler. More...
 
virtual int imageCount () const
 For image formats that support animation, this function returns the number of images in the animation. More...
 
virtual bool jumpToImage (int imageNumber)
 For image formats that support animation, this function jumps to the image whose sequence number is imageNumber. More...
 
virtual bool jumpToNextImage ()
 For image formats that support animation, this function jumps to the next image. More...
 
virtual int loopCount () const
 For image formats that support animation, this function returns the number of times the animation should loop. More...
 
virtual int nextImageDelay () const
 For image formats that support animation, this function returns the number of milliseconds to wait until reading the next image. More...
 
 QImageIOHandler ()
 Constructs a QImageIOHandler object. More...
 
void setDevice (QIODevice *device)
 Sets the device of the QImageIOHandler to device. More...
 
void setFormat (const QByteArray &format)
 Sets the format of the QImageIOHandler to format. More...
 
void setFormat (const QByteArray &format) const
 Sets the format of the QImageIOHandler to format. More...
 
virtual bool write (const QImage &image)
 Writes the image image to the assigned device. More...
 
virtual ~QImageIOHandler ()
 Destructs the QImageIOHandler object. More...
 

Static Public Functions

static bool canRead (QIODevice *device)
 

Properties

QSvgIOHandlerPrivated
 

Additional Inherited Members

- Public Types inherited from QImageIOHandler
enum  ImageOption {
  Size, ClipRect, Description, ScaledClipRect,
  ScaledSize, CompressionRatio, Gamma, Quality,
  Name, SubType, IncrementalReading, Endianness,
  Animation, BackgroundColor, ImageFormat
}
 This enum describes the different options supported by QImageIOHandler. More...
 
- Protected Functions inherited from QImageIOHandler
 QImageIOHandler (QImageIOHandlerPrivate &dd)
 Constructs a QImageIOHandler object, using the private member dd. More...
 
- Protected Variables inherited from QImageIOHandler
QScopedPointer< QImageIOHandlerPrivated_ptr
 

Detailed Description

Definition at line 57 of file qsvgiohandler.h.

Constructors and Destructors

◆ QSvgIOHandler()

QSvgIOHandler::QSvgIOHandler ( )

Definition at line 109 of file qsvgiohandler.cpp.

110  : d(new QSvgIOHandlerPrivate(this))
111 {
112 
113 }
QSvgIOHandlerPrivate * d
Definition: qsvgiohandler.h:71

◆ ~QSvgIOHandler()

QSvgIOHandler::~QSvgIOHandler ( )

Definition at line 116 of file qsvgiohandler.cpp.

117 {
118  delete d;
119 }
QSvgIOHandlerPrivate * d
Definition: qsvgiohandler.h:71

Functions

◆ canRead() [1/2]

bool QSvgIOHandler::canRead ( ) const
virtual

Returns true if an image can be read from the device (i.

e., the image format is supported, the device can be read from and the initial header information suggests that the image can be read); otherwise returns false.

When reimplementing canRead(), make sure that the I/O device (device()) is left in its original state (e.g., by using peek() rather than read()).

See also
read(), QIODevice::peek()

Implements QImageIOHandler.

Definition at line 122 of file qsvgiohandler.cpp.

Referenced by QSvgPlugin::capabilities(), and QSvgIOHandlerPrivate::load().

123 {
124  if (!device())
125  return false;
126  if (d->loaded && !d->readDone)
127  return true; // Will happen if we have been asked for the size
128 
129  QByteArray buf = device()->peek(8);
130  if (buf.startsWith("\x1f\x8b")) {
131  setFormat("svgz");
132  return true;
133  } else if (buf.contains("<?xml") || buf.contains("<svg")) {
134  setFormat("svg");
135  return true;
136  }
137  return false;
138 }
void setFormat(const QByteArray &format)
Sets the format of the QImageIOHandler to format.
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool startsWith(const QByteArray &a) const
Returns true if this byte array starts with byte array ba; otherwise returns false.
qint64 peek(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, without side effects (i.
Definition: qiodevice.cpp:1563
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
QSvgIOHandlerPrivate * d
Definition: qsvgiohandler.h:71
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525

◆ canRead() [2/2]

bool QSvgIOHandler::canRead ( QIODevice device)
static

Definition at line 259 of file qsvgiohandler.cpp.

260 {
261  QByteArray buf = device->peek(8);
262  return buf.startsWith("\x1f\x8b") || buf.contains("<?xml") || buf.contains("<svg");
263 }
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
bool startsWith(const QByteArray &a) const
Returns true if this byte array starts with byte array ba; otherwise returns false.
qint64 peek(char *data, qint64 maxlen)
Reads at most maxSize bytes from the device into data, without side effects (i.
Definition: qiodevice.cpp:1563
QBool contains(char c) const
Returns true if the byte array contains the character ch; otherwise returns false.
Definition: qbytearray.h:525

◆ name()

QByteArray QSvgIOHandler::name ( ) const
virtual

Use format() instead.

Reimplemented from QImageIOHandler.

Definition at line 141 of file qsvgiohandler.cpp.

142 {
143  return "svg";
144 }

◆ option()

QVariant QSvgIOHandler::option ( ImageOption  option) const
virtual

Returns the value assigned to option as a QVariant.

The type of the value depends on the option. For example, option(Size) returns a QSize variant.

See also
setOption(), supportsOption()

Reimplemented from QImageIOHandler.

Definition at line 191 of file qsvgiohandler.cpp.

192 {
193  switch(option) {
194  case ImageFormat:
196  break;
197  case Size:
198  d->load(device());
199  return d->defaultSize;
200  break;
201  case ClipRect:
202  return d->clipRect;
203  break;
204  case ScaledSize:
205  return d->scaledSize;
206  break;
207  case ScaledClipRect:
208  return d->scaledClipRect;
209  break;
210  case BackgroundColor:
211  return d->backColor;
212  break;
213  default:
214  break;
215  }
216  return QVariant();
217 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
virtual QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
bool load(QIODevice *device)
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
QSvgIOHandlerPrivate * d
Definition: qsvgiohandler.h:71

◆ read()

bool QSvgIOHandler::read ( QImage image)
virtual

Read an image from the device, and stores it in image.

Returns true if the image is successfully read; otherwise returns false.

For image formats that support incremental loading, and for animation formats, the image handler can assume that image points to the previous frame.

See also
canRead()

Implements QImageIOHandler.

Definition at line 147 of file qsvgiohandler.cpp.

148 {
149  if (!d->readDone && d->load(device())) {
150  bool xform = (d->clipRect.isValid() || d->scaledSize.isValid() || d->scaledClipRect.isValid());
151  QSize finalSize = d->defaultSize;
152  QRectF bounds;
153  if (xform && !d->defaultSize.isEmpty()) {
154  bounds = QRectF(QPointF(0,0), QSizeF(d->defaultSize));
155  QPoint tr1, tr2;
156  QSizeF sc(1, 1);
157  if (d->clipRect.isValid()) {
158  tr1 = -d->clipRect.topLeft();
159  finalSize = d->clipRect.size();
160  }
161  if (d->scaledSize.isValid()) {
162  sc = QSizeF(qreal(d->scaledSize.width()) / finalSize.width(),
163  qreal(d->scaledSize.height()) / finalSize.height());
164  finalSize = d->scaledSize;
165  }
166  if (d->scaledClipRect.isValid()) {
167  tr2 = -d->scaledClipRect.topLeft();
168  finalSize = d->scaledClipRect.size();
169  }
170  QTransform t;
171  t.translate(tr2.x(), tr2.y());
172  t.scale(sc.width(), sc.height());
173  t.translate(tr1.x(), tr1.y());
174  bounds = t.mapRect(bounds);
175  }
176  *image = QImage(finalSize, QImage::Format_ARGB32_Premultiplied);
177  if (!finalSize.isEmpty()) {
178  image->fill(d->backColor.rgba());
179  QPainter p(image);
180  d->r.render(&p, bounds);
181  p.end();
182  }
183  d->readDone = true;
184  return true;
185  }
186 
187  return false;
188 }
The QPainter class performs low-level painting on widgets and other paint devices.
Definition: qpainter.h:86
double qreal
Definition: qglobal.h:1193
void fill(uint pixel)
Fills the entire image with the given pixelValue.
Definition: qimage.cpp:2032
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
QTransform & translate(qreal dx, qreal dy)
Moves the coordinate system dx along the x axis and dy along the y axis, and returns a reference to t...
Definition: qtransform.cpp:417
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
int width() const
Returns the width.
Definition: qsize.h:126
QRect mapRect(const QRect &) const
Creates and returns a QRect object that is a copy of the given rectangle, mapped into the coordinate ...
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QSize size() const
Returns the size of the rectangle.
Definition: qrect.h:309
The QImage class provides a hardware-independent image representation that allows direct access to th...
Definition: qimage.h:87
bool load(QIODevice *device)
The QPoint class defines a point in the plane using integer precision.
Definition: qpoint.h:53
void render(QPainter *p)
Renders the current document, or the current frame of an animated document, using the given painter...
int height() const
Returns the height.
Definition: qsize.h:129
bool isValid() const
Returns true if both the width and height is equal to or greater than 0; otherwise returns false...
Definition: qsize.h:123
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.
int y() const
Returns the y coordinate of this point.
Definition: qpoint.h:131
QSvgIOHandlerPrivate * d
Definition: qsvgiohandler.h:71
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QTransform & scale(qreal sx, qreal sy)
Scales the coordinate system by sx horizontally and sy vertically, and returns a reference to the mat...
Definition: qtransform.cpp:485
int x() const
Returns the x coordinate of this point.
Definition: qpoint.h:128
QRgb rgba() const
Returns the RGB value of the color, including its alpha.
Definition: qcolor.cpp:1019
bool isEmpty() const
Returns true if either of the width and height is less than or equal to 0; otherwise returns false...
Definition: qsize.h:120
bool isValid() const
Returns true if the rectangle is valid, otherwise returns false.
Definition: qrect.h:237
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
QPoint topLeft() const
Returns the position of the rectangle&#39;s top-left corner.
Definition: qrect.h:288

◆ setOption()

void QSvgIOHandler::setOption ( ImageOption  option,
const QVariant value 
)
virtual

Sets the option option with the value value.

See also
option(), ImageOption

Reimplemented from QImageIOHandler.

Definition at line 220 of file qsvgiohandler.cpp.

221 {
222  switch(option) {
223  case ClipRect:
224  d->clipRect = value.toRect();
225  break;
226  case ScaledSize:
227  d->scaledSize = value.toSize();
228  break;
229  case ScaledClipRect:
230  d->scaledClipRect = value.toRect();
231  break;
232  case BackgroundColor:
233  d->backColor = value.value<QColor>();
234  break;
235  default:
236  break;
237  }
238 }
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
virtual QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
QSize toSize() const
Returns the variant as a QSize if the variant has type() Size ; otherwise returns an invalid QSize...
Definition: qvariant.cpp:2432
QRect toRect() const
Returns the variant as a QRect if the variant has type() Rect ; otherwise returns an invalid QRect...
Definition: qvariant.cpp:2416
QSvgIOHandlerPrivate * d
Definition: qsvgiohandler.h:71
T value() const
Returns the stored value converted to the template type T.
Definition: qvariant.h:332

◆ supportsOption()

bool QSvgIOHandler::supportsOption ( ImageOption  option) const
virtual

Returns true if the QImageIOHandler supports the option option; otherwise returns false.

For example, if the QImageIOHandler supports the Size option, supportsOption(Size) must return true.

See also
setOption(), option()

Reimplemented from QImageIOHandler.

Definition at line 241 of file qsvgiohandler.cpp.

242 {
243  switch(option)
244  {
245  case ImageFormat:
246  case Size:
247  case ClipRect:
248  case ScaledSize:
249  case ScaledClipRect:
250  case BackgroundColor:
251  return true;
252  default:
253  break;
254  }
255  return false;
256 }
virtual QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.

Properties

◆ d

QSvgIOHandlerPrivate* QSvgIOHandler::d
private

Definition at line 71 of file qsvgiohandler.h.

Referenced by canRead(), option(), read(), setOption(), and ~QSvgIOHandler().


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