| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the plugins of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qsvgiohandler.h"
|
|---|
| 43 |
|
|---|
| 44 | #ifndef QT_NO_SVGRENDERER
|
|---|
| 45 |
|
|---|
| 46 | #include "qsvgrenderer.h"
|
|---|
| 47 | #include "qimage.h"
|
|---|
| 48 | #include "qpixmap.h"
|
|---|
| 49 | #include "qpainter.h"
|
|---|
| 50 | #include "qvariant.h"
|
|---|
| 51 | #include "qdebug.h"
|
|---|
| 52 |
|
|---|
| 53 | QT_BEGIN_NAMESPACE
|
|---|
| 54 |
|
|---|
| 55 | class QSvgIOHandlerPrivate
|
|---|
| 56 | {
|
|---|
| 57 | public:
|
|---|
| 58 | QSvgIOHandlerPrivate()
|
|---|
| 59 | : r(new QSvgRenderer()), loaded(false)
|
|---|
| 60 | {}
|
|---|
| 61 | ~QSvgIOHandlerPrivate()
|
|---|
| 62 | {
|
|---|
| 63 | delete r;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | bool load(QIODevice *device);
|
|---|
| 67 |
|
|---|
| 68 | QSvgRenderer *r;
|
|---|
| 69 | QSize defaultSize;
|
|---|
| 70 | QSize currentSize;
|
|---|
| 71 | bool loaded;
|
|---|
| 72 | };
|
|---|
| 73 |
|
|---|
| 74 | bool QSvgIOHandlerPrivate::load(QIODevice *device)
|
|---|
| 75 | {
|
|---|
| 76 | if (loaded)
|
|---|
| 77 | return true;
|
|---|
| 78 |
|
|---|
| 79 | if (r->load(device->readAll())) {
|
|---|
| 80 | defaultSize = QSize(r->viewBox().width(), r->viewBox().height());
|
|---|
| 81 | if (currentSize.isEmpty())
|
|---|
| 82 | currentSize = defaultSize;
|
|---|
| 83 | }
|
|---|
| 84 | loaded = r->isValid();
|
|---|
| 85 |
|
|---|
| 86 | return loaded;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | QSvgIOHandler::QSvgIOHandler()
|
|---|
| 90 | : d(new QSvgIOHandlerPrivate())
|
|---|
| 91 | {
|
|---|
| 92 |
|
|---|
| 93 | }
|
|---|
| 94 |
|
|---|
| 95 |
|
|---|
| 96 | QSvgIOHandler::~QSvgIOHandler()
|
|---|
| 97 | {
|
|---|
| 98 | delete d;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 | bool QSvgIOHandler::canRead() const
|
|---|
| 103 | {
|
|---|
| 104 | QByteArray contents = device()->peek(80);
|
|---|
| 105 |
|
|---|
| 106 | return contents.contains("<svg");
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | QByteArray QSvgIOHandler::name() const
|
|---|
| 111 | {
|
|---|
| 112 | return "svg";
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 | bool QSvgIOHandler::read(QImage *image)
|
|---|
| 117 | {
|
|---|
| 118 | if (d->load(device())) {
|
|---|
| 119 | *image = QImage(d->currentSize, QImage::Format_ARGB32_Premultiplied);
|
|---|
| 120 | if (!d->currentSize.isEmpty()) {
|
|---|
| 121 | image->fill(0x00000000);
|
|---|
| 122 | QPainter p(image);
|
|---|
| 123 | d->r->render(&p);
|
|---|
| 124 | p.end();
|
|---|
| 125 | }
|
|---|
| 126 | return true;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | return false;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 | QVariant QSvgIOHandler::option(ImageOption option) const
|
|---|
| 134 | {
|
|---|
| 135 | switch(option) {
|
|---|
| 136 | case ImageFormat:
|
|---|
| 137 | return QImage::Format_ARGB32_Premultiplied;
|
|---|
| 138 | break;
|
|---|
| 139 | case Size:
|
|---|
| 140 | d->load(device());
|
|---|
| 141 | return d->defaultSize;
|
|---|
| 142 | break;
|
|---|
| 143 | case ScaledSize:
|
|---|
| 144 | return d->currentSize;
|
|---|
| 145 | break;
|
|---|
| 146 | default:
|
|---|
| 147 | break;
|
|---|
| 148 | }
|
|---|
| 149 | return QVariant();
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 | void QSvgIOHandler::setOption(ImageOption option, const QVariant & value)
|
|---|
| 154 | {
|
|---|
| 155 | switch(option) {
|
|---|
| 156 | case Size:
|
|---|
| 157 | d->defaultSize = value.toSize();
|
|---|
| 158 | d->currentSize = value.toSize();
|
|---|
| 159 | break;
|
|---|
| 160 | case ScaledSize:
|
|---|
| 161 | d->currentSize = value.toSize();
|
|---|
| 162 | break;
|
|---|
| 163 | default:
|
|---|
| 164 | break;
|
|---|
| 165 | }
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 |
|
|---|
| 169 | bool QSvgIOHandler::supportsOption(ImageOption option) const
|
|---|
| 170 | {
|
|---|
| 171 | switch(option)
|
|---|
| 172 | {
|
|---|
| 173 | case ImageFormat:
|
|---|
| 174 | case Size:
|
|---|
| 175 | case ScaledSize:
|
|---|
| 176 | return true;
|
|---|
| 177 | default:
|
|---|
| 178 | break;
|
|---|
| 179 | }
|
|---|
| 180 | return false;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | bool QSvgIOHandler::canRead(QIODevice *device)
|
|---|
| 184 | {
|
|---|
| 185 | QByteArray contents = device->peek(80);
|
|---|
| 186 | return contents.contains("<svg");
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | QT_END_NAMESPACE
|
|---|
| 190 |
|
|---|
| 191 | #endif // QT_NO_SVGRENDERER
|
|---|