35 QQuickImageProvider(QQuickImageProvider::Image),
42 qDeleteAll(m_renderers);
47 QSvgRenderer *renderer = m_renderers.value(svgFile, NULL);
51 if(!QFileInfo::exists(fn)) {
53 fn = QUrl::fromLocalFile(m_basePath).resolved(svgFile).toLocalFile();
56 if(!QFileInfo::exists(fn)) {
58 qWarning() <<
"[SvgImageProvider::loadRenderer]SVG file not found:" << svgFile;
62 renderer =
new QSvgRenderer(fn);
64 if (!renderer->isValid()) {
65 qWarning() <<
"[SvgImageProvider::loadRenderer]Failed to load svg file:" << svgFile;
70 m_renderers.insert(svgFile, renderer);
96 int separatorPos =
id.indexOf(
'!');
97 if (separatorPos != -1) {
98 svgFile =
id.left(separatorPos);
99 element =
id.mid(separatorPos+1);
102 int parametersPos = element.indexOf(
'?');
103 if (parametersPos != -1) {
104 parameters = element.mid(parametersPos+1);
105 element = element.left(parametersPos);
108 int hSlicesCount = 0;
110 int vSlicesCount = 0;
113 if (!parameters.isEmpty()) {
114 QRegExp hSliceRx(
"hslice=(\\d+):(\\d+)");
115 if (hSliceRx.indexIn(parameters) != -1) {
116 hSlice = hSliceRx.cap(1).toInt();
117 hSlicesCount = hSliceRx.cap(2).toInt();
119 QRegExp vSliceRx(
"vslice=(\\d+):(\\d+)");
120 if (vSliceRx.indexIn(parameters) != -1) {
121 vSlice = vSliceRx.cap(1).toInt();
122 vSlicesCount = vSliceRx.cap(2).toInt();
124 QRegExp borderRx(
"border=(\\d+)");
125 if (borderRx.indexIn(parameters) != -1) {
126 border = borderRx.cap(1).toInt();
140 QSize docSize = renderer->defaultSize();
142 if (!requestedSize.isEmpty()) {
143 if (!element.isEmpty()) {
144 QRectF elementBounds = renderer->boundsOnElement(element);
145 xScale = qreal(requestedSize.width())/elementBounds.width();
146 yScale = qreal(requestedSize.height())/elementBounds.height();
147 }
else if (!docSize.isEmpty()) {
148 xScale = qreal(requestedSize.width())/docSize.width();
149 yScale = qreal(requestedSize.height())/docSize.height();
155 xScale = yScale = qMin(xScale, yScale);
157 if (!element.isEmpty()) {
158 if (!renderer->elementExists(element)) {
159 qWarning() <<
"invalid element:" << element <<
"of" << svgFile;
163 QRectF elementBounds = renderer->boundsOnElement(element);
164 int elementWidth = qRound(elementBounds.width() * xScale);
165 int elementHeigh = qRound(elementBounds.height() * yScale);
166 int w = elementWidth;
167 int h = elementHeigh;
171 if (hSlicesCount > 1) {
172 x = (w*hSlice)/hSlicesCount;
173 w = (w*(hSlice+1))/hSlicesCount -
x;
176 if (vSlicesCount > 1) {
177 y = (h*(vSlice))/vSlicesCount;
178 h = (h*(vSlice+1))/vSlicesCount -
y;
181 QImage img(w+border*2, h+border*2, QImage::Format_ARGB32_Premultiplied);
184 p.setRenderHints(QPainter::TextAntialiasing |
185 QPainter::Antialiasing |
186 QPainter::SmoothPixmapTransform);
188 p.translate(-x+border,-y+border);
189 renderer->render(&p, element, QRectF(0, 0, elementWidth, elementHeigh));
198 int w = qRound(docSize.width() * xScale);
199 int h = qRound(docSize.height() * yScale);
201 QImage img(w, h, QImage::Format_ARGB32_Premultiplied);
204 p.setRenderHints(QPainter::TextAntialiasing |
205 QPainter::Antialiasing |
206 QPainter::SmoothPixmapTransform);
208 p.scale(xScale, yScale);
209 renderer->render(&p, QRectF(QPointF(), QSizeF(docSize)));
219 return QPixmap::fromImage(
requestImage(
id, size, requestedSize));
235 if (!renderer->elementExists(elementName)) {
236 qWarning() <<
"invalid element:" << elementName <<
"of" << svgFile;
240 QRectF elementBounds = renderer->boundsOnElement(elementName);
241 QMatrix matrix = renderer->matrixForElement(elementName);
242 elementBounds = matrix.mapRect(elementBounds);
244 QSize docSize = renderer->defaultSize();
245 return QRectF(elementBounds.x()/docSize.width(),
246 elementBounds.y()/docSize.height(),
247 elementBounds.width()/docSize.width(),
248 elementBounds.height()/docSize.height());
SvgImageProvider(const QString &basePath)
QSvgRenderer * loadRenderer(const QString &svgFile)
Q_INVOKABLE QRectF scaledElementBounds(const QString &svgFile, const QString &elementName)
QPixmap requestPixmap(const QString &id, QSize *size, const QSize &requestedSize)
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize)