Plasma
svg.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2006-2007 Aaron Seigo <aseigo@kde.org> 00003 * Copyright 2008-2010 Marco Martin <notmart@gmail.com> 00004 * 00005 * This program is free software; you can redistribute it and/or modify 00006 * it under the terms of the GNU Library General Public License as 00007 * published by the Free Software Foundation; either version 2, or 00008 * (at your option) any later version. 00009 * 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 * GNU General Public License for more details 00014 * 00015 * You should have received a copy of the GNU Library General Public 00016 * License along with this program; if not, write to the 00017 * Free Software Foundation, Inc., 00018 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include "svg.h" 00022 #include "private/svg_p.h" 00023 00024 #include <cmath> 00025 00026 #include <QDir> 00027 #include <QDomDocument> 00028 #include <QMatrix> 00029 #include <QPainter> 00030 #include <QStringBuilder> 00031 00032 #include <kcolorscheme.h> 00033 #include <kconfiggroup.h> 00034 #include <kdebug.h> 00035 #include <kfilterdev.h> 00036 #include <kiconeffect.h> 00037 #include <kglobalsettings.h> 00038 #include <ksharedptr.h> 00039 00040 #include "applet.h" 00041 #include "package.h" 00042 #include "theme.h" 00043 00044 namespace Plasma 00045 { 00046 00047 SharedSvgRenderer::SharedSvgRenderer(QObject *parent) 00048 : QSvgRenderer(parent) 00049 { 00050 } 00051 00052 SharedSvgRenderer::SharedSvgRenderer( 00053 const QString &filename, 00054 const QString &styleSheet, 00055 QHash<QString, QRectF> &interestingElements, 00056 QObject *parent) 00057 : QSvgRenderer(parent) 00058 { 00059 QIODevice *file = KFilterDev::deviceForFile(filename, "application/x-gzip"); 00060 if (!file->open(QIODevice::ReadOnly)) { 00061 delete file; 00062 return; 00063 } 00064 load(file->readAll(), styleSheet, interestingElements); 00065 delete file; 00066 } 00067 00068 SharedSvgRenderer::SharedSvgRenderer( 00069 const QByteArray &contents, 00070 const QString &styleSheet, 00071 QHash<QString, QRectF> &interestingElements, 00072 QObject *parent) 00073 : QSvgRenderer(parent) 00074 { 00075 load(contents, styleSheet, interestingElements); 00076 } 00077 00078 bool SharedSvgRenderer::load( 00079 const QByteArray &contents, 00080 const QString &styleSheet, 00081 QHash<QString, QRectF> &interestingElements) 00082 { 00083 // Apply the style sheet. 00084 if (!styleSheet.isEmpty() && contents.contains("current-color-scheme")) { 00085 QDomDocument svg; 00086 if (!svg.setContent(contents)) { 00087 return false; 00088 } 00089 00090 QDomNode defs = svg.elementsByTagName("defs").item(0); 00091 00092 for (QDomElement style = defs.firstChildElement("style"); !style.isNull(); 00093 style = style.nextSiblingElement("style")) { 00094 if (style.attribute("id") == "current-color-scheme") { 00095 QDomElement colorScheme = svg.createElement("style"); 00096 colorScheme.setAttribute("type", "text/css"); 00097 colorScheme.setAttribute("id", "current-color-scheme"); 00098 defs.replaceChild(colorScheme, style); 00099 colorScheme.appendChild(svg.createCDATASection(styleSheet)); 00100 00101 interestingElements.insert("current-color-scheme", QRect(0,0,1,1)); 00102 00103 break; 00104 } 00105 } 00106 if (!QSvgRenderer::load(svg.toByteArray(-1))) { 00107 return false; 00108 } 00109 } else if (!QSvgRenderer::load(contents)) { 00110 return false; 00111 } 00112 00113 // Search the SVG to find and store all ids that contain size hints. 00114 const QString contentsAsString(QString::fromLatin1(contents)); 00115 QRegExp idExpr("id\\s*=\\s*(['\"])(\\d+-\\d+-.*)\\1"); 00116 idExpr.setMinimal(true); 00117 00118 int pos = 0; 00119 while ((pos = idExpr.indexIn(contentsAsString, pos)) != -1) { 00120 QString elementId = idExpr.cap(2); 00121 00122 QRectF elementRect = boundsOnElement(elementId); 00123 if (elementRect.isValid()) { 00124 interestingElements.insert(elementId, elementRect); 00125 } 00126 00127 pos += idExpr.matchedLength(); 00128 } 00129 00130 return true; 00131 } 00132 00133 #define QLSEP QLatin1Char('_') 00134 #define CACHE_ID_WITH_SIZE(size, id) QString::number(int(size.width())) % QLSEP % QString::number(int(size.height())) % QLSEP % id 00135 #define CACHE_ID_NATURAL_SIZE(id) QLatin1Literal("Natural") % QLSEP % id 00136 00137 SvgPrivate::SvgPrivate(Svg *svg) 00138 : q(svg), 00139 renderer(0), 00140 styleCrc(0), 00141 lastModified(0), 00142 multipleImages(false), 00143 themed(false), 00144 applyColors(false), 00145 usesColors(false), 00146 cacheRendering(true), 00147 themeFailed(false) 00148 { 00149 } 00150 00151 SvgPrivate::~SvgPrivate() 00152 { 00153 eraseRenderer(); 00154 } 00155 00156 //This function is meant for the rects cache 00157 QString SvgPrivate::cacheId(const QString &elementId) 00158 { 00159 if (size.isValid() && size != naturalSize) { 00160 return CACHE_ID_WITH_SIZE(size, elementId); 00161 } else { 00162 return CACHE_ID_NATURAL_SIZE(elementId); 00163 } 00164 } 00165 00166 //This function is meant for the pixmap cache 00167 QString SvgPrivate::cachePath(const QString &path, const QSize &size) 00168 { 00169 return CACHE_ID_WITH_SIZE(size, path); 00170 } 00171 00172 bool SvgPrivate::setImagePath(const QString &imagePath) 00173 { 00174 const bool isThemed = !QDir::isAbsolutePath(imagePath); 00175 00176 // lets check to see if we're already set to this file 00177 if (isThemed == themed && 00178 ((themed && themePath == imagePath) || 00179 (!themed && path == imagePath))) { 00180 return false; 00181 } 00182 00183 eraseRenderer(); 00184 00185 // if we don't have any path right now and are going to set one, 00186 // then lets not schedule a repaint because we are just initializing! 00187 bool updateNeeded = true; 00188 00189 QObject::disconnect(actualTheme(), SIGNAL(themeChanged()), q, SLOT(themeChanged())); 00190 if (isThemed && !themed && s_systemColorsCache) { 00191 // catch the case where we weren't themed, but now we are, and the colors cache was set up 00192 // ensure we are not connected to that theme previously 00193 QObject::disconnect(s_systemColorsCache.data(), 0, q, 0); 00194 } 00195 00196 themed = isThemed; 00197 path.clear(); 00198 themePath.clear(); 00199 localRectCache.clear(); 00200 elementsWithSizeHints.clear(); 00201 00202 if (themed) { 00203 themePath = imagePath; 00204 themeFailed = false; 00205 QObject::connect(actualTheme(), SIGNAL(themeChanged()), q, SLOT(themeChanged())); 00206 } else if (QFile::exists(imagePath)) { 00207 QObject::connect(cacheAndColorsTheme(), SIGNAL(themeChanged()), q, SLOT(themeChanged()), Qt::UniqueConnection); 00208 path = imagePath; 00209 } else { 00210 kDebug() << "file '" << path << "' does not exist!"; 00211 } 00212 00213 // check if svg wants colorscheme applied 00214 checkColorHints(); 00215 00216 // also images with absolute path needs to have a natural size initialized, 00217 // even if looks a bit weird using Theme to store non-themed stuff 00218 if (themed || QFile::exists(imagePath)) { 00219 QRectF rect; 00220 if (cacheAndColorsTheme()->findInRectsCache(path, "_Natural", rect)) { 00221 naturalSize = rect.size(); 00222 } else { 00223 createRenderer(); 00224 naturalSize = renderer->defaultSize(); 00225 //kDebug() << "natural size for" << path << "from renderer is" << naturalSize; 00226 cacheAndColorsTheme()->insertIntoRectsCache(path, "_Natural", QRectF(QPointF(0,0), naturalSize)); 00227 //kDebug() << "natural size for" << path << "from cache is" << naturalSize; 00228 } 00229 } 00230 00231 if (!themed) { 00232 QFile f(imagePath); 00233 QFileInfo info(f); 00234 lastModified = info.lastModified().toTime_t(); 00235 } 00236 00237 return updateNeeded; 00238 } 00239 00240 Theme *SvgPrivate::actualTheme() 00241 { 00242 if (!theme) { 00243 theme = Plasma::Theme::defaultTheme(); 00244 } 00245 00246 return theme.data(); 00247 } 00248 00249 Theme *SvgPrivate::cacheAndColorsTheme() 00250 { 00251 if (themed) { 00252 return actualTheme(); 00253 } else { 00254 // use a separate cache source for unthemed svg's 00255 if (!s_systemColorsCache) { 00256 //FIXME: reference count this, so that it is deleted when no longer in use 00257 s_systemColorsCache = new Plasma::Theme("internal-system-colors"); 00258 } 00259 00260 return s_systemColorsCache.data(); 00261 } 00262 } 00263 00264 QPixmap SvgPrivate::findInCache(const QString &elementId, const QSizeF &s) 00265 { 00266 QSize size; 00267 QString actualElementId; 00268 00269 if (elementsWithSizeHints.isEmpty()) { 00270 // Fetch all size hinted element ids from the theme's rect cache 00271 // and store them locally. 00272 QRegExp sizeHintedKeyExpr(CACHE_ID_NATURAL_SIZE("(\\d+)-(\\d+)-(.+)")); 00273 00274 foreach (const QString &key, cacheAndColorsTheme()->listCachedRectKeys(path)) { 00275 if (sizeHintedKeyExpr.exactMatch(key)) { 00276 QString baseElementId = sizeHintedKeyExpr.cap(3); 00277 QSize sizeHint(sizeHintedKeyExpr.cap(1).toInt(), 00278 sizeHintedKeyExpr.cap(2).toInt()); 00279 00280 if (sizeHint.isValid()) { 00281 elementsWithSizeHints.insertMulti(baseElementId, sizeHint); 00282 } 00283 } 00284 } 00285 00286 if (elementsWithSizeHints.isEmpty()) { 00287 // Make sure we won't query the theme unnecessarily. 00288 elementsWithSizeHints.insert(QString(), QSize()); 00289 } 00290 } 00291 00292 // Look at the size hinted elements and try to find the smallest one with an 00293 // identical aspect ratio. 00294 if (s.isValid() && !elementId.isEmpty()) { 00295 QList<QSize> elementSizeHints = elementsWithSizeHints.values(elementId); 00296 00297 if (!elementSizeHints.isEmpty()) { 00298 QSize bestFit(-1, -1); 00299 00300 Q_FOREACH(const QSize &hint, elementSizeHints) { 00301 00302 if (hint.width() >= s.width() && hint.height() >= s.height() && 00303 (!bestFit.isValid() || 00304 (bestFit.width() * bestFit.height()) > (hint.width() * hint.height()))) { 00305 bestFit = hint; 00306 } 00307 } 00308 00309 if (bestFit.isValid()) { 00310 actualElementId = QString::number(bestFit.width()) % "-" % 00311 QString::number(bestFit.height()) % "-" % elementId; 00312 } 00313 } 00314 } 00315 00316 if (elementId.isEmpty() || !q->hasElement(actualElementId)) { 00317 actualElementId = elementId; 00318 } 00319 00320 if (elementId.isEmpty() || (multipleImages && s.isValid())) { 00321 size = s.toSize(); 00322 } else { 00323 size = elementRect(actualElementId).size().toSize(); 00324 } 00325 00326 if (size.isEmpty()) { 00327 return QPixmap(); 00328 } 00329 00330 QString id = cachePath(path, size); 00331 00332 if (!actualElementId.isEmpty()) { 00333 id.append(actualElementId); 00334 } 00335 00336 //kDebug() << "id is " << id; 00337 00338 QPixmap p; 00339 if (cacheRendering && cacheAndColorsTheme()->findInCache(id, p, lastModified)) { 00340 //kDebug() << "found cached version of " << id << p.size(); 00341 return p; 00342 } 00343 00344 //kDebug() << "didn't find cached version of " << id << ", so re-rendering"; 00345 00346 //kDebug() << "size for " << actualElementId << " is " << s; 00347 // we have to re-render this puppy 00348 00349 createRenderer(); 00350 00351 QRectF finalRect = makeUniform(renderer->boundsOnElement(actualElementId), QRect(QPoint(0,0), size)); 00352 00353 //don't alter the pixmap size or it won't match up properly to, e.g., FrameSvg elements 00354 //makeUniform should never change the size so much that it gains or loses a whole pixel 00355 p = QPixmap(size); 00356 00357 p.fill(Qt::transparent); 00358 QPainter renderPainter(&p); 00359 00360 if (actualElementId.isEmpty()) { 00361 renderer->render(&renderPainter, finalRect); 00362 } else { 00363 renderer->render(&renderPainter, actualElementId, finalRect); 00364 } 00365 00366 renderPainter.end(); 00367 00368 // Apply current color scheme if the svg asks for it 00369 if (applyColors) { 00370 QImage itmp = p.toImage(); 00371 KIconEffect::colorize(itmp, cacheAndColorsTheme()->color(Theme::BackgroundColor), 1.0); 00372 p = p.fromImage(itmp); 00373 } 00374 00375 if (cacheRendering) { 00376 cacheAndColorsTheme()->insertIntoCache(id, p, QString::number((qint64)q, 16) % QLSEP % actualElementId); 00377 } 00378 00379 return p; 00380 } 00381 00382 void SvgPrivate::createRenderer() 00383 { 00384 if (renderer) { 00385 return; 00386 } 00387 00388 //kDebug() << kBacktrace(); 00389 if (themed && path.isEmpty() && !themeFailed) { 00390 Applet *applet = qobject_cast<Applet*>(q->parent()); 00391 if (applet && applet->package()) { 00392 path = applet->package()->filePath("images", themePath + ".svg"); 00393 00394 if (path.isEmpty()) { 00395 path = applet->package()->filePath("images", themePath + ".svgz"); 00396 } 00397 } 00398 00399 if (path.isEmpty()) { 00400 path = actualTheme()->imagePath(themePath); 00401 themeFailed = path.isEmpty(); 00402 if (themeFailed) { 00403 kWarning() << "No image path found for" << themePath; 00404 } 00405 } 00406 } 00407 00408 //kDebug() << "********************************"; 00409 //kDebug() << "FAIL! **************************"; 00410 //kDebug() << path << "**"; 00411 00412 QString styleSheet = cacheAndColorsTheme()->styleSheet("SVG"); 00413 styleCrc = qChecksum(styleSheet.toUtf8(), styleSheet.size()); 00414 00415 QHash<QString, SharedSvgRenderer::Ptr>::const_iterator it = s_renderers.constFind(styleCrc + path); 00416 00417 if (it != s_renderers.constEnd()) { 00418 //kDebug() << "gots us an existing one!"; 00419 renderer = it.value(); 00420 } else { 00421 if (path.isEmpty()) { 00422 renderer = new SharedSvgRenderer(); 00423 } else { 00424 QHash<QString, QRectF> interestingElements; 00425 renderer = new SharedSvgRenderer(path, styleSheet, interestingElements); 00426 00427 // Add interesting elements to the theme's rect cache. 00428 QHashIterator<QString, QRectF> i(interestingElements); 00429 00430 while (i.hasNext()) { 00431 i.next(); 00432 const QString &elementId = i.key(); 00433 const QRectF &elementRect = i.value(); 00434 00435 const QString cacheId = CACHE_ID_NATURAL_SIZE(elementId); 00436 localRectCache.insert(cacheId, elementRect); 00437 cacheAndColorsTheme()->insertIntoRectsCache(path, cacheId, elementRect); 00438 } 00439 } 00440 00441 s_renderers[styleCrc + path] = renderer; 00442 } 00443 00444 if (size == QSizeF()) { 00445 size = renderer->defaultSize(); 00446 } 00447 } 00448 00449 void SvgPrivate::eraseRenderer() 00450 { 00451 if (renderer && renderer.count() == 2) { 00452 // this and the cache reference it 00453 s_renderers.erase(s_renderers.find(styleCrc + path)); 00454 00455 if (theme) { 00456 theme.data()->releaseRectsCache(path); 00457 } 00458 } 00459 00460 renderer = 0; 00461 styleCrc = 0; 00462 localRectCache.clear(); 00463 elementsWithSizeHints.clear(); 00464 } 00465 00466 QRectF SvgPrivate::elementRect(const QString &elementId) 00467 { 00468 if (themed && path.isEmpty()) { 00469 if (themeFailed) { 00470 return QRectF(); 00471 } 00472 00473 path = actualTheme()->imagePath(themePath); 00474 themeFailed = path.isEmpty(); 00475 00476 if (themeFailed) { 00477 return QRectF(); 00478 } 00479 } 00480 00481 QString id = cacheId(elementId); 00482 00483 if (localRectCache.contains(id)) { 00484 return localRectCache.value(id); 00485 } 00486 00487 QRectF rect; 00488 if (cacheAndColorsTheme()->findInRectsCache(path, id, rect)) { 00489 localRectCache.insert(id, rect); 00490 } else { 00491 rect = findAndCacheElementRect(elementId); 00492 } 00493 00494 return rect; 00495 } 00496 00497 QRectF SvgPrivate::findAndCacheElementRect(const QString &elementId) 00498 { 00499 createRenderer(); 00500 00501 // createRenderer() can insert some interesting rects in the cache, so check it 00502 const QString id = cacheId(elementId); 00503 if (localRectCache.contains(id)) { 00504 return localRectCache.value(id); 00505 } 00506 00507 QRectF elementRect = renderer->elementExists(elementId) ? 00508 renderer->matrixForElement(elementId).map(renderer->boundsOnElement(elementId)).boundingRect() : 00509 QRectF(); 00510 naturalSize = renderer->defaultSize(); 00511 qreal dx = size.width() / naturalSize.width(); 00512 qreal dy = size.height() / naturalSize.height(); 00513 00514 elementRect = QRectF(elementRect.x() * dx, elementRect.y() * dy, 00515 elementRect.width() * dx, elementRect.height() * dy); 00516 00517 cacheAndColorsTheme()->insertIntoRectsCache(path, id, elementRect); 00518 return elementRect; 00519 } 00520 00521 QMatrix SvgPrivate::matrixForElement(const QString &elementId) 00522 { 00523 createRenderer(); 00524 return renderer->matrixForElement(elementId); 00525 } 00526 00527 void SvgPrivate::checkColorHints() 00528 { 00529 if (elementRect("hint-apply-color-scheme").isValid()) { 00530 applyColors = true; 00531 usesColors = true; 00532 } else if (elementRect("current-color-scheme").isValid()) { 00533 applyColors = false; 00534 usesColors = true; 00535 } else { 00536 applyColors = false; 00537 usesColors = false; 00538 } 00539 00540 // check to see if we are using colors, but the theme isn't being used or isn't providing 00541 // a colorscheme 00542 if (usesColors && (!themed || !actualTheme()->colorScheme())) { 00543 QObject::connect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), 00544 q, SLOT(colorsChanged()), Qt::UniqueConnection); 00545 } else { 00546 QObject::disconnect(KGlobalSettings::self(), SIGNAL(kdisplayPaletteChanged()), 00547 q, SLOT(colorsChanged())); 00548 } 00549 } 00550 00551 //Folowing two are utility functions to snap rendered elements to the pixel grid 00552 //to and from are always 0 <= val <= 1 00553 qreal SvgPrivate::closestDistance(qreal to, qreal from) 00554 { 00555 qreal a = to - from; 00556 if (qFuzzyCompare(to, from)) 00557 return 0; 00558 else if ( to > from ) { 00559 qreal b = to - from - 1; 00560 return (qAbs(a) > qAbs(b)) ? b : a; 00561 } else { 00562 qreal b = 1 + to - from; 00563 return (qAbs(a) > qAbs(b)) ? b : a; 00564 } 00565 } 00566 00567 QRectF SvgPrivate::makeUniform(const QRectF &orig, const QRectF &dst) 00568 { 00569 if (qFuzzyIsNull(orig.x()) || qFuzzyIsNull(orig.y())) { 00570 return dst; 00571 } 00572 00573 QRectF res(dst); 00574 qreal div_w = dst.width() / orig.width(); 00575 qreal div_h = dst.height() / orig.height(); 00576 00577 qreal div_x = dst.x() / orig.x(); 00578 qreal div_y = dst.y() / orig.y(); 00579 00580 //horizontal snap 00581 if (!qFuzzyIsNull(div_x) && !qFuzzyCompare(div_w, div_x)) { 00582 qreal rem_orig = orig.x() - (floor(orig.x())); 00583 qreal rem_dst = dst.x() - (floor(dst.x())); 00584 qreal offset = closestDistance(rem_dst, rem_orig); 00585 res.translate(offset + offset*div_w, 0); 00586 res.setWidth(res.width() + offset); 00587 } 00588 //vertical snap 00589 if (!qFuzzyIsNull(div_y) && !qFuzzyCompare(div_h, div_y)) { 00590 qreal rem_orig = orig.y() - (floor(orig.y())); 00591 qreal rem_dst = dst.y() - (floor(dst.y())); 00592 qreal offset = closestDistance(rem_dst, rem_orig); 00593 res.translate(0, offset + offset*div_h); 00594 res.setHeight(res.height() + offset); 00595 } 00596 00597 //kDebug()<<"Aligning Rects, origin:"<<orig<<"destination:"<<dst<<"result:"<<res; 00598 return res; 00599 } 00600 00601 void SvgPrivate::themeChanged() 00602 { 00603 if (q->imagePath().isEmpty()) { 00604 return; 00605 } 00606 00607 if (themed) { 00608 // check if new theme svg wants colorscheme applied 00609 checkColorHints(); 00610 } 00611 00612 QString currentPath = themed ? themePath : path; 00613 themePath.clear(); 00614 eraseRenderer(); 00615 setImagePath(currentPath); 00616 00617 //kDebug() << themePath << ">>>>>>>>>>>>>>>>>> theme changed"; 00618 emit q->repaintNeeded(); 00619 } 00620 00621 void SvgPrivate::colorsChanged() 00622 { 00623 if (!usesColors) { 00624 return; 00625 } 00626 00627 eraseRenderer(); 00628 //kDebug() << "repaint needed from colorsChanged"; 00629 emit q->repaintNeeded(); 00630 } 00631 00632 QHash<QString, SharedSvgRenderer::Ptr> SvgPrivate::s_renderers; 00633 QWeakPointer<Theme> SvgPrivate::s_systemColorsCache; 00634 00635 Svg::Svg(QObject *parent) 00636 : QObject(parent), 00637 d(new SvgPrivate(this)) 00638 { 00639 } 00640 00641 Svg::~Svg() 00642 { 00643 delete d; 00644 } 00645 00646 QPixmap Svg::pixmap(const QString &elementID) 00647 { 00648 if (elementID.isNull() || d->multipleImages) { 00649 return d->findInCache(elementID, size()); 00650 } else { 00651 return d->findInCache(elementID); 00652 } 00653 } 00654 00655 void Svg::paint(QPainter *painter, const QPointF &point, const QString &elementID) 00656 { 00657 QPixmap pix((elementID.isNull() || d->multipleImages) ? d->findInCache(elementID, size()) : 00658 d->findInCache(elementID)); 00659 00660 if (pix.isNull()) { 00661 return; 00662 } 00663 00664 painter->drawPixmap(QRectF(point, pix.size()), pix, QRectF(QPointF(0, 0), pix.size())); 00665 } 00666 00667 void Svg::paint(QPainter *painter, int x, int y, const QString &elementID) 00668 { 00669 paint(painter, QPointF(x, y), elementID); 00670 } 00671 00672 void Svg::paint(QPainter *painter, const QRectF &rect, const QString &elementID) 00673 { 00674 QPixmap pix(d->findInCache(elementID, rect.size())); 00675 painter->drawPixmap(QRectF(rect.topLeft(), pix.size()), pix, QRectF(QPointF(0, 0), pix.size())); 00676 } 00677 00678 void Svg::paint(QPainter *painter, int x, int y, int width, int height, const QString &elementID) 00679 { 00680 QPixmap pix(d->findInCache(elementID, QSizeF(width, height))); 00681 painter->drawPixmap(x, y, pix, 0, 0, pix.size().width(), pix.size().height()); 00682 } 00683 00684 QSize Svg::size() const 00685 { 00686 if (d->size.isEmpty()) { 00687 d->size = d->naturalSize; 00688 } 00689 00690 return d->size.toSize(); 00691 } 00692 00693 void Svg::resize(qreal width, qreal height) 00694 { 00695 resize(QSize(width, height)); 00696 } 00697 00698 void Svg::resize(const QSizeF &size) 00699 { 00700 if (qFuzzyCompare(size.width(), d->size.width()) && 00701 qFuzzyCompare(size.height(), d->size.height())) { 00702 return; 00703 } 00704 00705 d->size = size; 00706 d->localRectCache.clear(); 00707 emit sizeChanged(); 00708 } 00709 00710 void Svg::resize() 00711 { 00712 if (qFuzzyCompare(d->naturalSize.width(), d->size.width()) && 00713 qFuzzyCompare(d->naturalSize.height(), d->size.height())) { 00714 return; 00715 } 00716 00717 d->size = d->naturalSize; 00718 d->localRectCache.clear(); 00719 emit sizeChanged(); 00720 } 00721 00722 QSize Svg::elementSize(const QString &elementId) const 00723 { 00724 return d->elementRect(elementId).size().toSize(); 00725 } 00726 00727 QRectF Svg::elementRect(const QString &elementId) const 00728 { 00729 return d->elementRect(elementId); 00730 } 00731 00732 bool Svg::hasElement(const QString &elementId) const 00733 { 00734 if (d->path.isNull() && d->themePath.isNull()) { 00735 return false; 00736 } 00737 00738 return d->elementRect(elementId).isValid(); 00739 } 00740 00741 QString Svg::elementAtPoint(const QPoint &point) const 00742 { 00743 Q_UNUSED(point) 00744 00745 return QString(); 00746 /* 00747 FIXME: implement when Qt can support us! 00748 d->createRenderer(); 00749 QSizeF naturalSize = d->renderer->defaultSize(); 00750 qreal dx = d->size.width() / naturalSize.width(); 00751 qreal dy = d->size.height() / naturalSize.height(); 00752 //kDebug() << point << "is really" 00753 // << QPoint(point.x() *dx, naturalSize.height() - point.y() * dy); 00754 00755 return QString(); // d->renderer->elementAtPoint(QPoint(point.x() *dx, naturalSize.height() - point.y() * dy)); 00756 */ 00757 } 00758 00759 bool Svg::isValid() const 00760 { 00761 if (d->path.isNull() && d->themePath.isNull()) { 00762 return false; 00763 } 00764 00765 d->createRenderer(); 00766 return d->renderer->isValid(); 00767 } 00768 00769 void Svg::setContainsMultipleImages(bool multiple) 00770 { 00771 d->multipleImages = multiple; 00772 } 00773 00774 bool Svg::containsMultipleImages() const 00775 { 00776 return d->multipleImages; 00777 } 00778 00779 void Svg::setImagePath(const QString &svgFilePath) 00780 { 00781 //BIC FIXME: setImagePath should be virtual, or call an internal virtual protected method 00782 if (FrameSvg *frame = qobject_cast<FrameSvg *>(this)) { 00783 frame->setImagePath(svgFilePath); 00784 return; 00785 } 00786 00787 d->setImagePath(svgFilePath); 00788 //kDebug() << "repaintNeeded"; 00789 emit repaintNeeded(); 00790 } 00791 00792 QString Svg::imagePath() const 00793 { 00794 return d->themed ? d->themePath : d->path; 00795 } 00796 00797 void Svg::setUsingRenderingCache(bool useCache) 00798 { 00799 d->cacheRendering = useCache; 00800 } 00801 00802 bool Svg::isUsingRenderingCache() const 00803 { 00804 return d->cacheRendering; 00805 } 00806 00807 void Svg::setTheme(Plasma::Theme *theme) 00808 { 00809 if (!theme || theme == d->theme.data()) { 00810 return; 00811 } 00812 00813 if (d->theme) { 00814 disconnect(d->theme.data(), 0, this, 0); 00815 } 00816 00817 d->theme = theme; 00818 connect(theme, SIGNAL(themeChanged()), this, SLOT(themeChanged())); 00819 d->themeChanged(); 00820 } 00821 00822 Theme *Svg::theme() const 00823 { 00824 return d->theme ? d->theme.data() : Theme::defaultTheme(); 00825 } 00826 00827 } // Plasma namespace 00828 00829 #include "svg.moc" 00830
KDE 4.7 API Reference