diff --git a/src/plasma/framesvg.cpp b/src/plasma/framesvg.cpp index a6d0d740d..a1d6e7d6a 100644 --- a/src/plasma/framesvg.cpp +++ b/src/plasma/framesvg.cpp @@ -1,1035 +1,1025 @@ /* * Copyright 2008-2010 by Aaron Seigo * Copyright 2008-2010 Marco Martin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "framesvg.h" #include "private/framesvg_p.h" #include #include #include #include #include #include #include #include #include #include "theme.h" #include "private/svg_p.h" #include "private/framesvg_helpers.h" #include "debug_p.h" namespace Plasma { QHash > FrameSvgPrivate::s_sharedFrames; // Any attempt to generate a frame whose width or height is larger than this // will be rejected static const int MAX_FRAME_SIZE = 100000; FrameData::~FrameData() { for (auto it = references.constBegin(), end = references.constEnd(); it != end; ++it) { if (it.key()->d->frame == this) { it.key()->d->frame = nullptr; } } } FrameSvg::FrameSvg(QObject *parent) : Svg(parent), d(new FrameSvgPrivate(this)) { connect(this, SIGNAL(repaintNeeded()), this, SLOT(updateNeeded())); - d->frame = new FrameData(this, QString()); + d->frame = nullptr; } FrameSvg::~FrameSvg() { delete d; } void FrameSvg::setImagePath(const QString &path) { if (path == imagePath()) { return; } - bool updateNeeded = true; clearCache(); - FrameData *fd = d->frame; - if (fd->refcount() == 1) { - // we're the only user of it, let's remove it from the shared keys - // we don't want to deref it, however, as we'll still be using it - const QString oldKey = d->cacheId(fd, d->prefix); - FrameSvgPrivate::s_sharedFrames[fd->theme].remove(oldKey); - } else { - // others are using this frame, so deref it for ourselves - fd->deref(this); - fd = 0; - } - - Svg::d->setImagePath(path); - - if (!fd) { - // we need to replace our frame, start by looking in the frame cache - FrameData *oldFd = d->frame; - const QString key = d->cacheId(oldFd, d->prefix); - fd = FrameSvgPrivate::s_sharedFrames[theme()->d].value(key); - - if (fd) { - // we found one, so ref it and use it; we also don't need to (or want to!) - // trigger a full update of the frame since it is already the one we want - // and likely already rendered just fine - fd->ref(this); - updateNeeded = false; - } else { - // nothing exists for us in the cache, so create a new FrameData based - // on the old one - fd = new FrameData(*oldFd, this); - } - - d->frame = fd; - } - setContainsMultipleImages(true); - if (updateNeeded) { - // ensure our frame is in the cache - const QString key = d->cacheId(fd, d->prefix); - FrameSvgPrivate::s_sharedFrames[theme()->d].insert(key, fd); - fd->theme = theme()->d; - - // this will emit repaintNeeded() as well when it is done - d->updateAndSignalSizes(); - } else { - emit repaintNeeded(); + Svg::d->setImagePath(path); + if (!d->repaintBlocked) { + d->updateFrameData(); } } void FrameSvg::setEnabledBorders(const EnabledBorders borders) { if (borders == d->enabledBorders) { return; } d->enabledBorders = borders; if (!d->repaintBlocked) { d->updateFrameData(); } } FrameSvg::EnabledBorders FrameSvg::enabledBorders() const { return d->enabledBorders; } void FrameSvg::setElementPrefix(Plasma::Types::Location location) { switch (location) { case Types::TopEdge: setElementPrefix(QStringLiteral("north")); break; case Types::BottomEdge: setElementPrefix(QStringLiteral("south")); break; case Types::LeftEdge: setElementPrefix(QStringLiteral("west")); break; case Types::RightEdge: setElementPrefix(QStringLiteral("east")); break; default: setElementPrefix(QString()); break; } d->location = location; } void FrameSvg::setElementPrefix(const QString &prefix) { if (!hasElement(prefix % QLatin1String("-center"))) { d->prefix.clear(); } else { d->prefix = prefix; if (!d->prefix.isEmpty()) { d->prefix += '-'; } } d->requestedPrefix = prefix; d->location = Types::Floating; if (!d->repaintBlocked) { d->updateFrameData(); } } bool FrameSvg::hasElementPrefix(const QString &prefix) const { //for now it simply checks if a center element exists, //because it could make sense for certain themes to not have all the elements if (prefix.isEmpty()) { return hasElement(QStringLiteral("center")); } else { return hasElement(prefix % QLatin1String("-center")); } } bool FrameSvg::hasElementPrefix(Plasma::Types::Location location) const { switch (location) { case Types::TopEdge: return hasElementPrefix(QStringLiteral("north")); break; case Types::BottomEdge: return hasElementPrefix(QStringLiteral("south")); break; case Types::LeftEdge: return hasElementPrefix(QStringLiteral("west")); break; case Types::RightEdge: return hasElementPrefix(QStringLiteral("east")); break; default: return hasElementPrefix(QString()); break; } } QString FrameSvg::prefix() { return d->requestedPrefix; } void FrameSvg::resizeFrame(const QSizeF &size) { if (imagePath().isEmpty()) { return; } if (size.isEmpty()) { #ifndef NDEBUG // qCDebug(LOG_PLASMA) << "Invalid size" << size; #endif return; } - if (size.toSize() == d->frame->frameSize) { + if (d->frame && size.toSize() == d->frame->frameSize) { return; } d->pendingFrameSize = size.toSize(); if (!d->repaintBlocked) { d->updateFrameData(FrameSvgPrivate::UpdateFrame); } } QSizeF FrameSvg::frameSize() const { if (!d->frame) { return QSize(-1, -1); } else { return d->frameSize(d->frame); } } qreal FrameSvg::marginSize(const Plasma::Types::MarginEdge edge) const { + if (!d->frame) { + return .0; + } + if (d->frame->noBorderPadding) { return .0; } switch (edge) { case Plasma::Types::TopMargin: return d->frame->topMargin; break; case Plasma::Types::LeftMargin: return d->frame->leftMargin; break; case Plasma::Types::RightMargin: return d->frame->rightMargin; break; //Plasma::BottomMargin default: return d->frame->bottomMargin; break; } } qreal FrameSvg::fixedMarginSize(const Plasma::Types::MarginEdge edge) const { + if (!d->frame) { + return .0; + } + if (d->frame->noBorderPadding) { return .0; } switch (edge) { case Plasma::Types::TopMargin: return d->frame->fixedTopMargin; break; case Plasma::Types::LeftMargin: return d->frame->fixedLeftMargin; break; case Plasma::Types::RightMargin: return d->frame->fixedRightMargin; break; //Plasma::BottomMargin default: return d->frame->fixedBottomMargin; break; } } void FrameSvg::getMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const { if (d->frame->noBorderPadding) { left = top = right = bottom = 0; return; } top = d->frame->topMargin; left = d->frame->leftMargin; right = d->frame->rightMargin; bottom = d->frame->bottomMargin; } void FrameSvg::getFixedMargins(qreal &left, qreal &top, qreal &right, qreal &bottom) const { if (d->frame->noBorderPadding) { left = top = right = bottom = 0; return; } top = d->frame->fixedTopMargin; left = d->frame->fixedLeftMargin; right = d->frame->fixedRightMargin; bottom = d->frame->fixedBottomMargin; } QRectF FrameSvg::contentsRect() const { if (d->frame) { QRectF rect(QPoint(0,0), d->frame->frameSize); return rect.adjusted(d->frame->leftMargin, d->frame->topMargin, -d->frame->rightMargin, -d->frame->bottomMargin); } else { return QRectF(); } } QPixmap FrameSvg::alphaMask() const { //FIXME: the distinction between overlay and return d->alphaMask(); } QRegion FrameSvg::mask() const { QString id = d->cacheId(d->frame, QString()); QRegion* obj = d->frame->cachedMasks.object(id); QRegion result; if (!obj) { obj = new QRegion(QBitmap(d->alphaMask().alphaChannel().createMaskFromColor(Qt::black))); result = *obj; d->frame->cachedMasks.insert(id, obj); } else { result = *obj; } return result; } void FrameSvg::setCacheAllRenderedFrames(bool cache) { if (d->cacheAll && !cache) { clearCache(); } d->cacheAll = cache; } bool FrameSvg::cacheAllRenderedFrames() const { return d->cacheAll; } void FrameSvg::clearCache() { if (d->frame) { d->frame->cachedBackground = QPixmap(); } if (d->maskFrame) { d->maskFrame->cachedBackground = QPixmap(); } } QPixmap FrameSvg::framePixmap() { if (d->frame->cachedBackground.isNull()) { d->generateBackground(d->frame); } return d->frame->cachedBackground; } void FrameSvg::paintFrame(QPainter *painter, const QRectF &target, const QRectF &source) { if (d->frame->cachedBackground.isNull()) { d->generateBackground(d->frame); if (d->frame->cachedBackground.isNull()) { return; } } painter->drawPixmap(target, d->frame->cachedBackground, source.isValid() ? source : target); } void FrameSvg::paintFrame(QPainter *painter, const QPointF &pos) { if (d->frame->cachedBackground.isNull()) { d->generateBackground(d->frame); if (d->frame->cachedBackground.isNull()) { return; } } painter->drawPixmap(pos, d->frame->cachedBackground); } //#define DEBUG_FRAMESVG_CACHE FrameSvgPrivate::~FrameSvgPrivate() { #ifdef DEBUG_FRAMESVG_CACHE #ifndef NDEBUG // qCDebug(LOG_PLASMA) << "*************" << q << q->imagePath() << "****************"; #endif #endif // we remove all references from this widget to the frame, and delete it if we're the // last user if (frame && frame->removeRefs(q)) { const QString key = cacheId(frame, frame->prefix); #ifdef DEBUG_FRAMESVG_CACHE #ifndef NDEBUG // qCDebug(LOG_PLASMA) << "2. Removing it" << key << frame << frame->refcount() << s_sharedFrames[theme()->d].contains(key); #endif #endif s_sharedFrames[frame->theme].remove(key); delete frame; } //same thing for maskFrame if (maskFrame && maskFrame->removeRefs(q)) { const QString key = cacheId(maskFrame, maskFrame->prefix); s_sharedFrames[maskFrame->theme].remove(key); delete maskFrame; } #ifdef DEBUG_FRAMESVG_CACHE QHashIterator it2(s_sharedFrames[theme()->d]); int shares = 0; while (it2.hasNext()) { it2.next(); const int rc = it2.value()->refcount(); if (rc == 0) { #ifndef NDEBUG // qCDebug(LOG_PLASMA) << " LOST!" << it2.key() << rc << it2.value();// << it2.value()->references; #endif } else { #ifndef NDEBUG // qCDebug(LOG_PLASMA) << " " << it2.key() << rc << it2.value(); #endif foreach (FrameSvg *data, it2.value()->references.keys()) { #ifndef NDEBUG qCDebug(LOG_PLASMA) << " " << (void *)data << it2.value()->references[data]; #endif } shares += rc - 1; } } #ifndef NDEBUG // qCDebug(LOG_PLASMA) << "#####################################" << s_sharedFrames[theme()->d].count() << ", pixmaps saved:" << shares; #endif #endif frame = nullptr; maskFrame = nullptr; } QPixmap FrameSvgPrivate::alphaMask() { QString maskPrefix; if (q->hasElement(QLatin1String("mask-") % prefix % QLatin1String("center"))) { maskPrefix = QStringLiteral("mask-"); } if (maskPrefix.isNull()) { if (frame->cachedBackground.isNull()) { generateBackground(frame); if (frame->cachedBackground.isNull()) { return QPixmap(); } } return frame->cachedBackground; } else { // We are setting the prefix only temporary to generate // the needed mask image const QString maskRequestedPrefix = maskPrefix % requestedPrefix; maskPrefix = maskPrefix % prefix; if (!maskFrame) { const QString key = cacheId(frame, maskPrefix); // see if we can find a suitable candidate in the shared frames // if successful, ref and insert, otherwise create a new one // and insert that into both the shared frames and our frames. maskFrame = s_sharedFrames[q->theme()->d].value(key); if (maskFrame) { maskFrame->ref(q); } else { maskFrame = new FrameData(*frame, q); maskFrame->prefix = maskPrefix; maskFrame->requestedPrefix = maskRequestedPrefix; maskFrame->theme = q->theme()->d; + maskFrame->imagePath = q->imagePath(); s_sharedFrames[q->theme()->d].insert(key, maskFrame); } maskFrame->enabledBorders = frame->enabledBorders; updateSizes(maskFrame); } // maskFrame = frame; maskFrame->enabledBorders = frame->enabledBorders; if (maskFrame->cachedBackground.isNull() || maskFrame->frameSize != frameSize(frame)) { const QString oldKey = cacheId(maskFrame, maskPrefix); maskFrame->frameSize = frameSize(frame).toSize(); const QString newKey = cacheId(maskFrame, maskPrefix); if (s_sharedFrames[q->theme()->d].contains(oldKey)) { s_sharedFrames[q->theme()->d].remove(oldKey); s_sharedFrames[q->theme()->d].insert(newKey, maskFrame); } maskFrame->cachedBackground = QPixmap(); generateBackground(maskFrame); if (maskFrame->cachedBackground.isNull()) { return QPixmap(); } } return maskFrame->cachedBackground; } } void FrameSvgPrivate::generateBackground(FrameData *frame) { if (!frame->cachedBackground.isNull() || !q->hasElementPrefix(frame->requestedPrefix)) { return; } const QString id = cacheId(frame, frame->prefix); bool frameCached = !frame->cachedBackground.isNull(); bool overlayCached = false; const bool overlayAvailable = !frame->prefix.startsWith(QLatin1String("mask-")) && q->hasElement(frame->prefix % QLatin1String("overlay")); QPixmap overlay; if (q->isUsingRenderingCache()) { frameCached = q->theme()->findInCache(id, frame->cachedBackground) && !frame->cachedBackground.isNull(); if (overlayAvailable) { overlayCached = q->theme()->findInCache(QLatin1String("overlay_") % id, overlay) && !overlay.isNull(); } } if (!frameCached) { generateFrameBackground(frame); } //Overlays QSize overlaySize; QPoint actualOverlayPos = QPoint(0, 0); if (overlayAvailable && !overlayCached) { overlaySize = q->elementSize(frame->prefix % QLatin1String("overlay")); if (q->hasElement(frame->prefix % QLatin1String("hint-overlay-pos-right"))) { actualOverlayPos.setX(frame->frameSize.width() - overlaySize.width()); } else if (q->hasElement(frame->prefix % QLatin1String("hint-overlay-pos-bottom"))) { actualOverlayPos.setY(frame->frameSize.height() - overlaySize.height()); //Stretched or Tiled? } else if (q->hasElement(frame->prefix % QLatin1String("hint-overlay-stretch"))) { overlaySize = frameSize(frame).toSize(); } else { if (q->hasElement(frame->prefix % QLatin1String("hint-overlay-tile-horizontal"))) { overlaySize.setWidth(frameSize(frame).width()); } if (q->hasElement(frame->prefix % QLatin1String("hint-overlay-tile-vertical"))) { overlaySize.setHeight(frameSize(frame).height()); } } overlay = alphaMask(); QPainter overlayPainter(&overlay); overlayPainter.setCompositionMode(QPainter::CompositionMode_SourceIn); //Tiling? if (q->hasElement(frame->prefix % QLatin1String("hint-overlay-tile-horizontal")) || q->hasElement(frame->prefix % QLatin1String("hint-overlay-tile-vertical"))) { QSize s = q->size(); q->resize(q->elementSize(frame->prefix % QLatin1String("overlay"))); overlayPainter.drawTiledPixmap(QRect(QPoint(0, 0), overlaySize), q->pixmap(frame->prefix % QLatin1String("overlay"))); q->resize(s); } else { q->paint(&overlayPainter, QRect(actualOverlayPos, overlaySize), frame->prefix % QLatin1String("overlay")); } overlayPainter.end(); } if (!frameCached) { cacheFrame(frame->prefix, frame->cachedBackground, overlayCached ? overlay : QPixmap()); } if (!overlay.isNull()) { QPainter p(&frame->cachedBackground); p.setCompositionMode(QPainter::CompositionMode_SourceOver); p.drawPixmap(actualOverlayPos, overlay, QRect(actualOverlayPos, overlaySize)); } } void FrameSvgPrivate::generateFrameBackground(FrameData *frame) { //qCDebug(LOG_PLASMA) << "generating background"; const QSize size = frameSize(frame).toSize() * q->devicePixelRatio(); if (!size.isValid()) { #ifndef NDEBUG // qCDebug(LOG_PLASMA) << "Invalid frame size" << size; #endif return; } if (size.width() >= MAX_FRAME_SIZE || size.height() >= MAX_FRAME_SIZE) { qCWarning(LOG_PLASMA) << "Not generating frame background for a size whose width or height is more than" << MAX_FRAME_SIZE << size; return; } frame->cachedBackground = QPixmap(size); frame->cachedBackground.fill(Qt::transparent); QPainter p(&frame->cachedBackground); p.setCompositionMode(QPainter::CompositionMode_Source); p.setRenderHint(QPainter::SmoothPixmapTransform); QRect contentRect = contentGeometry(frame, size); paintCenter(p, frame, contentRect, size); paintCorner(p, frame, FrameSvg::LeftBorder|FrameSvg::TopBorder, contentRect); paintCorner(p, frame, FrameSvg::RightBorder|FrameSvg::TopBorder, contentRect); paintCorner(p, frame, FrameSvg::LeftBorder|FrameSvg::BottomBorder, contentRect); paintCorner(p, frame, FrameSvg::RightBorder|FrameSvg::BottomBorder, contentRect); // Sides const int leftHeight = q->elementSize(frame->prefix % QLatin1String("left")).height(); paintBorder(p, frame, FrameSvg::LeftBorder, QSize(frame->leftWidth, leftHeight) * q->devicePixelRatio(), contentRect); paintBorder(p, frame, FrameSvg::RightBorder, QSize(frame->rightWidth, leftHeight) * q->devicePixelRatio(), contentRect); const int topWidth = q->elementSize(frame->prefix % QLatin1String("top")).width(); paintBorder(p, frame, FrameSvg::TopBorder, QSize(topWidth, frame->topHeight) * q->devicePixelRatio(), contentRect); paintBorder(p, frame, FrameSvg::BottomBorder, QSize(topWidth, frame->bottomHeight) * q->devicePixelRatio(), contentRect); p.end(); frame->cachedBackground.setDevicePixelRatio(q->devicePixelRatio()); } QRect FrameSvgPrivate::contentGeometry(FrameData* frame, const QSize& size) const { const QSize contentSize(size.width() - frame->leftWidth * q->devicePixelRatio() - frame->rightWidth * q->devicePixelRatio(), size.height() - frame->topHeight * q->devicePixelRatio() - frame->bottomHeight * q->devicePixelRatio()); QRect contentRect(QPoint(0,0), contentSize); if (frame->enabledBorders & FrameSvg::LeftBorder && q->hasElement(frame->prefix % QLatin1String("left"))) { contentRect.translate(frame->leftWidth * q->devicePixelRatio(), 0); } // Corners if (frame->enabledBorders & FrameSvg::TopBorder && q->hasElement(frame->prefix % QLatin1String("top"))) { contentRect.translate(0, frame->topHeight * q->devicePixelRatio()); } return contentRect; } void FrameSvgPrivate::updateFrameData(UpdateType updateType) { FrameData *fd = frame; + QString newKey; - const QString oldKey = cacheId(fd, fd->prefix); - - const FrameSvg::EnabledBorders oldBorders = fd->enabledBorders; - const QSize currentSize = fd->frameSize; + if (fd) { + const QString oldKey = cacheId(fd, fd->prefix); - fd->enabledBorders = enabledBorders; - fd->frameSize = pendingFrameSize; + const QString oldPath = fd->imagePath; + const FrameSvg::EnabledBorders oldBorders = fd->enabledBorders; + const QSize currentSize = fd->frameSize; - const QString newKey = cacheId(fd, prefix); + fd->enabledBorders = enabledBorders; + fd->frameSize = pendingFrameSize; + fd->imagePath = q->imagePath(); - //reset frame to old values - fd->enabledBorders = oldBorders; - fd->frameSize = currentSize; + newKey = cacheId(fd, prefix); - //FIXME: something more efficient than string comparison? - if (oldKey == newKey) { - return; - } + //reset frame to old values + fd->enabledBorders = oldBorders; + fd->frameSize = currentSize; + fd->imagePath = oldPath; - //qCDebug(LOG_PLASMA) << "looking for" << newKey; - FrameData *newFd = FrameSvgPrivate::s_sharedFrames[q->theme()->d].value(newKey); - if (newFd) { - //qCDebug(LOG_PLASMA) << "FOUND IT!" << newFd->refcount; - // we've found a math, so insert that new one and ref it .. - newFd->ref(q); - frame = newFd; + //FIXME: something more efficient than string comparison? + if (oldKey == newKey) { + return; + } - //.. then deref the old one and if it's no longer used, get rid of it - if (fd->deref(q)) { - //const QString oldKey = cacheId(fd, prefix); - //qCDebug(LOG_PLASMA) << "1. Removing it" << oldKey << fd->refcount; - FrameSvgPrivate::s_sharedFrames[fd->theme].remove(oldKey); + //qCDebug(LOG_PLASMA) << "looking for" << newKey; + FrameData *newFd = FrameSvgPrivate::s_sharedFrames[q->theme()->d].value(newKey); + if (newFd) { + //qCDebug(LOG_PLASMA) << "FOUND IT!" << newFd->refcount; + // we've found a math, so insert that new one and ref it .. + newFd->ref(q); + frame = newFd; + + //.. then deref the old one and if it's no longer used, get rid of it + if (fd->deref(q)) { + //const QString oldKey = cacheId(fd, prefix); + //qCDebug(LOG_PLASMA) << "1. Removing it" << oldKey << fd->refcount; + FrameSvgPrivate::s_sharedFrames[fd->theme].remove(oldKey); + delete fd; + } - delete fd; + return; } - return; - } - - if (fd->refcount() == 1) { - // we're the only user of it, let's remove it from the shared keys - // we don't want to deref it, however, as we'll still be using it - FrameSvgPrivate::s_sharedFrames[fd->theme].remove(oldKey); + if (fd->refcount() == 1) { + // we're the only user of it, let's remove it from the shared keys + // we don't want to deref it, however, as we'll still be using it + FrameSvgPrivate::s_sharedFrames[fd->theme].remove(oldKey); + } else { + // others are using it, but we wish to change its size. so deref it, + // then create a copy of it (we're automatically ref'd via the ctor), + // then insert it into our frames. + fd->deref(q); + fd = new FrameData(*fd, q); + } } else { - // others are using it, but we wish to change its size. so deref it, - // then create a copy of it (we're automatically ref'd via the ctor), - // then insert it into our frames. - fd->deref(q); - fd = new FrameData(*fd, q); + fd = new FrameData(q, QString()); } frame = fd; fd->prefix = prefix; fd->requestedPrefix = requestedPrefix; //updateSizes(); fd->enabledBorders = enabledBorders; fd->frameSize = pendingFrameSize; + fd->imagePath = q->imagePath(); + //was fd just created empty now? + if (newKey.isEmpty()) { + newKey = cacheId(fd, prefix); + } // we know it isn't in s_sharedFrames due to the check above, so insert it now FrameSvgPrivate::s_sharedFrames[q->theme()->d].insert(newKey, fd); fd->theme = q->theme()->d; if (updateType == UpdateFrameAndMargins) { updateAndSignalSizes(); } else { updateSizes(frame); } } void FrameSvgPrivate::paintCenter(QPainter& p, FrameData* frame, const QRect& contentRect, const QSize& fullSize) { if (!contentRect.isEmpty()) { const QString centerElementId = frame->prefix % QLatin1String("center"); if (frame->tileCenter) { QSize centerTileSize = q->elementSize(centerElementId); QPixmap center(centerTileSize); center.fill(Qt::transparent); QPainter centerPainter(¢er); centerPainter.setCompositionMode(QPainter::CompositionMode_Source); q->paint(¢erPainter, QRect(QPoint(0, 0), centerTileSize),centerElementId); if (frame->composeOverBorder) { p.drawTiledPixmap(QRect(QPoint(0, 0), fullSize), center); } else { p.drawTiledPixmap(FrameSvgHelpers::sectionRect(FrameSvg::NoBorder, contentRect, fullSize * q->devicePixelRatio()), center); } } else { if (frame->composeOverBorder) { q->paint(&p, QRect(QPoint(0, 0), fullSize), centerElementId); } else { q->paint(&p, FrameSvgHelpers::sectionRect(FrameSvg::NoBorder, contentRect, fullSize * q->devicePixelRatio()), centerElementId); } } } if (frame->composeOverBorder) { p.setCompositionMode(QPainter::CompositionMode_DestinationIn); p.drawPixmap(QRect(QPoint(0, 0), fullSize), alphaMask()); p.setCompositionMode(QPainter::CompositionMode_SourceOver); } } void FrameSvgPrivate::paintBorder(QPainter& p, FrameData* frame, const FrameSvg::EnabledBorders borders, const QSize& size, const QRect& contentRect) const { QString side = frame->prefix % FrameSvgHelpers::borderToElementId(borders); if (frame->enabledBorders & borders && q->hasElement(side) && !size.isEmpty()) { if (frame->stretchBorders) { q->paint(&p, FrameSvgHelpers::sectionRect(borders, contentRect, frame->frameSize * q->devicePixelRatio()), side); } else { QPixmap px(size); px.fill(Qt::transparent); QPainter sidePainter(&px); sidePainter.setCompositionMode(QPainter::CompositionMode_Source); q->paint(&sidePainter, QRect(QPoint(0, 0), size), side); p.drawTiledPixmap(FrameSvgHelpers::sectionRect(borders, contentRect, frame->frameSize * q->devicePixelRatio()), px); } } } void FrameSvgPrivate::paintCorner(QPainter& p, FrameData* frame, Plasma::FrameSvg::EnabledBorders border, const QRect& contentRect) const { QString corner = frame->prefix % FrameSvgHelpers::borderToElementId(border); if (frame->enabledBorders & border && q->hasElement(corner)) { q->paint(&p, FrameSvgHelpers::sectionRect(border, contentRect, frame->frameSize * q->devicePixelRatio()), corner); } } QString FrameSvgPrivate::cacheId(FrameData *frame, const QString &prefixToSave) const { const QSize size = frameSize(frame).toSize(); const QLatin1Char s('_'); - return QString::number(frame->enabledBorders) % s % QString::number(size.width()) % s % QString::number(size.height()) % s % QString::number(q->scaleFactor()) % s % QString::number(q->devicePixelRatio()) % s % prefixToSave % s % q->imagePath(); + return QString::number(frame->enabledBorders) % s % QString::number(size.width()) % s % QString::number(size.height()) % s % QString::number(q->scaleFactor()) % s % QString::number(q->devicePixelRatio()) % s % prefixToSave % s % frame->imagePath; } void FrameSvgPrivate::cacheFrame(const QString &prefixToSave, const QPixmap &background, const QPixmap &overlay) { if (!q->isUsingRenderingCache()) { return; } //insert background if (!frame) { return; } const QString id = cacheId(frame, prefixToSave); //qCDebug(LOG_PLASMA)<<"Saving to cache frame"<theme()->insertIntoCache(id, background, QString::number((qint64)q, 16) % prefixToSave); if (!overlay.isNull()) { //insert overlay q->theme()->insertIntoCache(QLatin1String("overlay_") % id, overlay, QString::number((qint64)q, 16) % prefixToSave % QLatin1String("overlay")); } } void FrameSvgPrivate::updateSizes(FrameData *frame) const { //qCDebug(LOG_PLASMA) << "!!!!!!!!!!!!!!!!!!!!!! updating sizes" << prefix; Q_ASSERT(frame); QSize s = q->size(); q->resize(); frame->cachedBackground = QPixmap(); //This has the same size regardless the border is enabled or not frame->fixedTopHeight = q->elementSize(frame->prefix % QLatin1String("top")).height(); if (q->hasElement(frame->prefix % QLatin1String("hint-top-margin"))) { frame->fixedTopMargin = q->elementSize(frame->prefix % QLatin1String("hint-top-margin")).height(); } else { frame->fixedTopMargin = frame->fixedTopHeight; } //The same, but its size depends from the margin being enabled if (frame->enabledBorders & FrameSvg::TopBorder) { frame->topHeight = q->elementSize(frame->prefix % QLatin1String("top")).height(); if (q->hasElement(frame->prefix % QLatin1String("hint-top-margin"))) { frame->topMargin = q->elementSize(frame->prefix % QLatin1String("hint-top-margin")).height(); } else { frame->topMargin = frame->topHeight; } } else { frame->topMargin = frame->topHeight = 0; } frame->fixedLeftWidth = q->elementSize(frame->prefix % QLatin1String("left")).width(); if (q->hasElement(frame->prefix % QLatin1String("hint-left-margin"))) { frame->fixedLeftMargin = q->elementSize(frame->prefix % QLatin1String("hint-left-margin")).width(); } else { frame->fixedLeftMargin = frame->fixedLeftWidth; } if (frame->enabledBorders & FrameSvg::LeftBorder) { frame->leftWidth = q->elementSize(frame->prefix % QLatin1String("left")).width(); if (q->hasElement(frame->prefix % QLatin1String("hint-left-margin"))) { frame->leftMargin = q->elementSize(frame->prefix % QLatin1String("hint-left-margin")).width(); } else { frame->leftMargin = frame->leftWidth; } } else { frame->leftMargin = frame->leftWidth = 0; } frame->fixedRightWidth = q->elementSize(frame->prefix % QLatin1String("right")).width(); if (q->hasElement(frame->prefix % QLatin1String("hint-right-margin"))) { frame->fixedRightMargin = q->elementSize(frame->prefix % QLatin1String("hint-right-margin")).width(); } else { frame->fixedRightMargin = frame->fixedRightWidth; } if (frame->enabledBorders & FrameSvg::RightBorder) { frame->rightWidth = q->elementSize(frame->prefix % QLatin1String("right")).width(); if (q->hasElement(frame->prefix % QLatin1String("hint-right-margin"))) { frame->rightMargin = q->elementSize(frame->prefix % QLatin1String("hint-right-margin")).width(); } else { frame->rightMargin = frame->rightWidth; } } else { frame->rightMargin = frame->rightWidth = 0; } frame->fixedBottomHeight = q->elementSize(frame->prefix % QLatin1String("bottom")).height(); if (q->hasElement(frame->prefix % QLatin1String("hint-bottom-margin"))) { frame->fixedBottomMargin = q->elementSize(frame->prefix % QLatin1String("hint-bottom-margin")).height(); } else { frame->fixedBottomMargin = frame->fixedBottomHeight; } if (frame->enabledBorders & FrameSvg::BottomBorder) { frame->bottomHeight = q->elementSize(frame->prefix % QLatin1String("bottom")).height(); if (q->hasElement(frame->prefix % QLatin1String("hint-bottom-margin"))) { frame->bottomMargin = q->elementSize(frame->prefix % QLatin1String("hint-bottom-margin")).height(); } else { frame->bottomMargin = frame->bottomHeight; } } else { frame->bottomMargin = frame->bottomHeight = 0; } frame->composeOverBorder = (q->hasElement(frame->prefix % QLatin1String("hint-compose-over-border")) && q->hasElement(QLatin1String("mask-") % frame->prefix % QLatin1String("center"))); //since it's rectangular, topWidth and bottomWidth must be the same //the ones that don't have a frame->prefix is for retrocompatibility frame->tileCenter = (q->hasElement(QStringLiteral("hint-tile-center")) || q->hasElement(frame->prefix % QLatin1String("hint-tile-center"))); frame->noBorderPadding = (q->hasElement(QStringLiteral("hint-no-border-padding")) || q->hasElement(frame->prefix % QLatin1String("hint-no-border-padding"))); frame->stretchBorders = (q->hasElement(QStringLiteral("hint-stretch-borders")) || q->hasElement(frame->prefix % QLatin1String("hint-stretch-borders"))); q->resize(s); } void FrameSvgPrivate::updateNeeded() { q->setElementPrefix(requestedPrefix); + //frame not created yet? + if (!frame) { + return; + } q->clearCache(); updateSizes(frame); } void FrameSvgPrivate::updateAndSignalSizes() { + //frame not created yet? + if (!frame) { + return; + } updateSizes(frame); emit q->repaintNeeded(); } QSizeF FrameSvgPrivate::frameSize(FrameData *frame) const { + if (!frame) { + return QSizeF(); + } + if (!frame->frameSize.isValid()) { updateSizes(frame); frame->frameSize = q->size(); } return frame->frameSize; } void FrameData::ref(FrameSvg *svg) { references[svg] = references[svg] + 1; //qCDebug(LOG_PLASMA) << this << svg << references[svg]; } bool FrameData::deref(FrameSvg *svg) { references[svg] = references[svg] - 1; //qCDebug(LOG_PLASMA) << this << svg << references[svg]; if (references[svg] < 1) { references.remove(svg); } return references.isEmpty(); } bool FrameData::removeRefs(FrameSvg *svg) { references.remove(svg); return references.isEmpty(); } bool FrameData::isUsed() const { return !references.isEmpty(); } int FrameData::refcount() const { return references.count(); } QString FrameSvg::actualPrefix() const { return d->prefix; } bool FrameSvg::isRepaintBlocked() const { return d->repaintBlocked; } void FrameSvg::setRepaintBlocked(bool blocked) { d->repaintBlocked = blocked; if (!blocked) { d->updateFrameData(); } } } // Plasma namespace #include "moc_framesvg.cpp" diff --git a/src/plasma/private/framesvg_p.h b/src/plasma/private/framesvg_p.h index c277276d1..ef50ca34d 100644 --- a/src/plasma/private/framesvg_p.h +++ b/src/plasma/private/framesvg_p.h @@ -1,199 +1,202 @@ /* * Copyright 2008 by Aaron Seigo * Copyright 2009 Marco Martin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #ifndef PLASMA_FRAMESVG_P_H #define PLASMA_FRAMESVG_P_H #include #include #include #include #include namespace Plasma { class FrameData { public: FrameData(FrameSvg *svg, const QString &p) - : prefix(p), + : imagePath(svg->imagePath()), + prefix(p), enabledBorders(FrameSvg::AllBorders), frameSize(-1, -1), topHeight(0), leftWidth(0), rightWidth(0), bottomHeight(0), topMargin(0), leftMargin(0), rightMargin(0), bottomMargin(0), noBorderPadding(false), stretchBorders(false), tileCenter(false), composeOverBorder(false), theme(0) { ref(svg); } FrameData(const FrameData &other, FrameSvg *svg) - : prefix(other.prefix), + : imagePath(other.imagePath), + prefix(other.prefix), enabledBorders(other.enabledBorders), cachedMasks(MAX_CACHED_MASKS), frameSize(other.frameSize), topHeight(0), leftWidth(0), rightWidth(0), bottomHeight(0), topMargin(0), leftMargin(0), rightMargin(0), bottomMargin(0), devicePixelRatio(svg->devicePixelRatio()), noBorderPadding(false), stretchBorders(false), tileCenter(false), composeOverBorder(false), theme(0) { ref(svg); } ~FrameData(); void ref(FrameSvg *svg); bool deref(FrameSvg *svg); bool removeRefs(FrameSvg *svg); bool isUsed() const; int refcount() const; + QString imagePath; QString prefix; QString requestedPrefix; FrameSvg::EnabledBorders enabledBorders; QPixmap cachedBackground; QCache cachedMasks; static const int MAX_CACHED_MASKS = 10; QSize frameSize; //measures int topHeight; int leftWidth; int rightWidth; int bottomHeight; //margins, are equal to the measures by default int topMargin; int leftMargin; int rightMargin; int bottomMargin; //measures int fixedTopHeight; int fixedLeftWidth; int fixedRightWidth; int fixedBottomHeight; //margins, are equal to the measures by default int fixedTopMargin; int fixedLeftMargin; int fixedRightMargin; int fixedBottomMargin; qreal devicePixelRatio; //size of the svg where the size of the "center" //element is contentWidth x contentHeight bool noBorderPadding : 1; bool stretchBorders : 1; bool tileCenter : 1; bool composeOverBorder : 1; QHash references; Plasma::ThemePrivate *theme; }; class FrameSvgPrivate { public: FrameSvgPrivate(FrameSvg *psvg) : q(psvg), overlayPos(0, 0), frame(nullptr), maskFrame(nullptr), enabledBorders(FrameSvg::AllBorders), cacheAll(false), repaintBlocked(false) { } ~FrameSvgPrivate(); QPixmap alphaMask(); enum UpdateType { UpdateFrame, UpdateFrameAndMargins }; void generateBackground(FrameData *frame); void generateFrameBackground(FrameData *frame); QString cacheId(FrameData *frame, const QString &prefixToUse) const; void cacheFrame(const QString &prefixToSave, const QPixmap &background, const QPixmap &overlay); void updateSizes(FrameData *frame) const; void updateNeeded(); void updateAndSignalSizes(); QSizeF frameSize(FrameData *frame) const; void paintBorder(QPainter& p, FrameData* frame, Plasma::FrameSvg::EnabledBorders border, const QSize& originalSize, const QRect& output) const; void paintCorner(QPainter& p, FrameData* frame, Plasma::FrameSvg::EnabledBorders border, const QRect& output) const; void paintCenter(QPainter& p, FrameData* frame, const QRect& contentRect, const QSize& fullSize); QRect contentGeometry(FrameData* frame, const QSize& size) const; void updateFrameData(UpdateType updateType = UpdateFrameAndMargins); Types::Location location; QString prefix; //sometimes the prefix we requested is not available, so prefix will be emoty //keep track of the requested one anyways, we'll try again when the theme changes QString requestedPrefix; FrameSvg *q; QPoint overlayPos; FrameData *frame; FrameData *maskFrame; //those can differ from frame->enabledBorders if we are in a transition FrameSvg::EnabledBorders enabledBorders; //this can differ from frame->frameSize if we are in a transition QSize pendingFrameSize; static QHash > s_sharedFrames; bool cacheAll : 1; bool repaintBlocked : 1; }; } #endif diff --git a/src/plasma/svg.cpp b/src/plasma/svg.cpp index 9aa2bb72d..6ededa093 100644 --- a/src/plasma/svg.cpp +++ b/src/plasma/svg.cpp @@ -1,1003 +1,1002 @@ /* * Copyright 2006-2007 Aaron Seigo * Copyright 2008-2010 Marco Martin * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details * * You should have received a copy of the GNU Library General Public * License along with this program; if not, write to the * Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include "svg.h" #include "private/svg_p.h" #include "private/theme_p.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "applet.h" #include "package.h" #include "theme.h" #include "debug_p.h" namespace Plasma { SharedSvgRenderer::SharedSvgRenderer(QObject *parent) : QSvgRenderer(parent) { } SharedSvgRenderer::SharedSvgRenderer( const QString &filename, const QString &styleSheet, QHash &interestingElements, QObject *parent) : QSvgRenderer(parent) { KCompressionDevice file(filename, KCompressionDevice::GZip); if (!file.open(QIODevice::ReadOnly)) { return; } load(file.readAll(), styleSheet, interestingElements); } SharedSvgRenderer::SharedSvgRenderer( const QByteArray &contents, const QString &styleSheet, QHash &interestingElements, QObject *parent) : QSvgRenderer(parent) { load(contents, styleSheet, interestingElements); } bool SharedSvgRenderer::load( const QByteArray &contents, const QString &styleSheet, QHash &interestingElements) { // Apply the style sheet. if (!styleSheet.isEmpty() && contents.contains("current-color-scheme")) { QByteArray processedContents; QXmlStreamReader reader(contents); QBuffer buffer(&processedContents); buffer.open(QIODevice::WriteOnly); QXmlStreamWriter writer(&buffer); while (!reader.atEnd()) { if (reader.readNext() == QXmlStreamReader::StartElement && reader.qualifiedName() == QLatin1String("style") && reader.attributes().value(QLatin1String("id")) == QLatin1String("current-color-scheme")) { writer.writeStartElement(QLatin1String("style")); writer.writeAttributes(reader.attributes()); writer.writeCharacters(styleSheet); writer.writeEndElement(); while (reader.tokenType() != QXmlStreamReader::EndElement) { reader.readNext(); } } else if (reader.tokenType() != QXmlStreamReader::Invalid) { writer.writeCurrentToken(reader); } } buffer.close(); if (!QSvgRenderer::load(processedContents)) { return false; } } else if (!QSvgRenderer::load(contents)) { return false; } // Search the SVG to find and store all ids that contain size hints. const QString contentsAsString(QString::fromLatin1(contents)); QRegExp idExpr(QLatin1String("id\\s*=\\s*(['\"])(\\d+-\\d+-.*)\\1")); idExpr.setMinimal(true); int pos = 0; while ((pos = idExpr.indexIn(contentsAsString, pos)) != -1) { QString elementId = idExpr.cap(2); QRectF elementRect = boundsOnElement(elementId); if (elementRect.isValid()) { interestingElements.insert(elementId, elementRect); } pos += idExpr.matchedLength(); } return true; } #define QLSEP QLatin1Char('_') #define CACHE_ID_WITH_SIZE(size, id, status, devicePixelRatio) QString::number(int(size.width())) % QLSEP % QString::number(int(size.height())) % QLSEP % id % QLSEP % QString::number(status) % QLSEP % QString::number(int(devicePixelRatio)) #define CACHE_ID_NATURAL_SIZE(id, status, devicePixelRatio) QLatin1String("Natural") % QLSEP % id % QLSEP % QString::number(status) % QLSEP % QString::number(int(devicePixelRatio)) SvgPrivate::SvgPrivate(Svg *svg) : q(svg), renderer(0), styleCrc(0), colorGroup(Plasma::Theme::NormalColorGroup), lastModified(0), devicePixelRatio(1.0), scaleFactor(1.0), status(Svg::Status::Normal), multipleImages(false), themed(false), useSystemColors(false), fromCurrentTheme(false), applyColors(false), usesColors(false), cacheRendering(true), themeFailed(false) { } SvgPrivate::~SvgPrivate() { eraseRenderer(); } //This function is meant for the rects cache QString SvgPrivate::cacheId(const QString &elementId) const { if (size.isValid() && size != naturalSize) { return CACHE_ID_WITH_SIZE(size, elementId, status, devicePixelRatio); } else { return CACHE_ID_NATURAL_SIZE(elementId, status, devicePixelRatio); } } //This function is meant for the pixmap cache QString SvgPrivate::cachePath(const QString &path, const QSize &size) const { return CACHE_ID_WITH_SIZE(size, path, status, devicePixelRatio) % QLSEP % QString::number(colorGroup); } bool SvgPrivate::setImagePath(const QString &imagePath) { QString actualPath = imagePath; if (imagePath.startsWith(QLatin1String("file://"))) { //length of file:// actualPath = actualPath.mid(7); } bool isThemed = !QDir::isAbsolutePath(actualPath); bool inIconTheme = false; //an absolute path.. let's try if this actually an *icon* theme if (!isThemed) { const auto *iconTheme = KIconLoader::global()->theme(); isThemed = inIconTheme = iconTheme && actualPath.startsWith(iconTheme->dir()); } // lets check to see if we're already set to this file if (isThemed == themed && ((themed && themePath == actualPath) || (!themed && path == actualPath))) { return false; } eraseRenderer(); // if we don't have any path right now and are going to set one, // then lets not schedule a repaint because we are just initializing! bool updateNeeded = true; //!path.isEmpty() || !themePath.isEmpty(); QObject::disconnect(actualTheme(), SIGNAL(themeChanged()), q, SLOT(themeChanged())); if (isThemed && !themed && s_systemColorsCache) { // catch the case where we weren't themed, but now we are, and the colors cache was set up // ensure we are not connected to that theme previously QObject::disconnect(s_systemColorsCache.data(), 0, q, 0); } themed = isThemed; path.clear(); themePath.clear(); localRectCache.clear(); elementsWithSizeHints.clear(); bool oldFromCurrentTheme = fromCurrentTheme; fromCurrentTheme = actualTheme()->currentThemeHasImage(imagePath); if (fromCurrentTheme != oldFromCurrentTheme) { emit q->fromCurrentThemeChanged(fromCurrentTheme); } if (inIconTheme) { themePath = actualPath; path = actualPath; QObject::connect(actualTheme(), SIGNAL(themeChanged()), q, SLOT(themeChanged())); } else if (themed) { themePath = actualPath; path = actualTheme()->imagePath(themePath); themeFailed = path.isEmpty(); QObject::connect(actualTheme(), SIGNAL(themeChanged()), q, SLOT(themeChanged())); } else if (QFile::exists(actualPath)) { QObject::connect(cacheAndColorsTheme(), SIGNAL(themeChanged()), q, SLOT(themeChanged()), Qt::UniqueConnection); path = actualPath; } else { #ifndef NDEBUG // qCDebug(LOG_PLASMA) << "file '" << path << "' does not exist!"; #endif } // check if svg wants colorscheme applied checkColorHints(); // also images with absolute path needs to have a natural size initialized, // even if looks a bit weird using Theme to store non-themed stuff if ((themed && QFile::exists(path)) || QFile::exists(actualPath)) { QRectF rect; if (cacheAndColorsTheme()->findInRectsCache(path, QStringLiteral("_Natural_%1").arg(scaleFactor), rect)) { naturalSize = rect.size(); } else { createRenderer(); naturalSize = renderer->defaultSize() * scaleFactor; //qCDebug(LOG_PLASMA) << "natural size for" << path << "from renderer is" << naturalSize; cacheAndColorsTheme()->insertIntoRectsCache(path, QStringLiteral("_Natural_%1").arg(scaleFactor), QRectF(QPointF(0, 0), naturalSize)); //qCDebug(LOG_PLASMA) << "natural size for" << path << "from cache is" << naturalSize; } } if (!themed) { QFile f(actualPath); QFileInfo info(f); lastModified = info.lastModified().toTime_t(); } q->resize(); emit q->imagePathChanged(); return updateNeeded; } Theme *SvgPrivate::actualTheme() { if (!theme) { theme = new Plasma::Theme(q); } return theme.data(); } Theme *SvgPrivate::cacheAndColorsTheme() { if (themed || !useSystemColors) { return actualTheme(); } else { // use a separate cache source for unthemed svg's if (!s_systemColorsCache) { //FIXME: reference count this, so that it is deleted when no longer in use s_systemColorsCache = new Plasma::Theme(QStringLiteral("internal-system-colors")); } return s_systemColorsCache.data(); } } QPixmap SvgPrivate::findInCache(const QString &elementId, qreal ratio, const QSizeF &s) { QSize size; QString actualElementId; if (elementsWithSizeHints.isEmpty()) { // Fetch all size hinted element ids from the theme's rect cache // and store them locally. QRegExp sizeHintedKeyExpr(CACHE_ID_NATURAL_SIZE("(\\d+)-(\\d+)-(.+)", status, ratio)); foreach (const QString &key, cacheAndColorsTheme()->listCachedRectKeys(path)) { if (sizeHintedKeyExpr.exactMatch(key)) { QString baseElementId = sizeHintedKeyExpr.cap(3); QSize sizeHint(sizeHintedKeyExpr.cap(1).toInt(), sizeHintedKeyExpr.cap(2).toInt()); if (sizeHint.isValid()) { elementsWithSizeHints.insertMulti(baseElementId, sizeHint); } } } if (elementsWithSizeHints.isEmpty()) { // Make sure we won't query the theme unnecessarily. elementsWithSizeHints.insert(QString(), QSize()); } } // Look at the size hinted elements and try to find the smallest one with an // identical aspect ratio. if (s.isValid() && !elementId.isEmpty()) { QList elementSizeHints = elementsWithSizeHints.values(elementId); if (!elementSizeHints.isEmpty()) { QSize bestFit(-1, -1); Q_FOREACH (QSize hint, elementSizeHints) { if (hint.width() >= s.width() * ratio && hint.height() >= s.height() * ratio && (!bestFit.isValid() || (bestFit.width() * bestFit.height()) > (hint.width() * hint.height()))) { bestFit = hint; } } if (bestFit.isValid()) { actualElementId = QString::number(bestFit.width()) % '-' % QString::number(bestFit.height()) % '-' % elementId; } } } if (elementId.isEmpty() || !q->hasElement(actualElementId)) { actualElementId = elementId; } if (elementId.isEmpty() || (multipleImages && s.isValid())) { size = s.toSize() * ratio; } else { size = elementRect(actualElementId).size().toSize() * ratio; } if (size.isEmpty()) { return QPixmap(); } QString id = cachePath(path, size); if (!actualElementId.isEmpty()) { id.append(actualElementId); } //qCDebug(LOG_PLASMA) << "id is " << id; QPixmap p; if (cacheRendering && cacheAndColorsTheme()->findInCache(id, p, lastModified)) { p.setDevicePixelRatio(ratio); //qCDebug(LOG_PLASMA) << "found cached version of " << id << p.size(); return p; } //qCDebug(LOG_PLASMA) << "didn't find cached version of " << id << ", so re-rendering"; //qCDebug(LOG_PLASMA) << "size for " << actualElementId << " is " << s; // we have to re-render this puppy createRenderer(); QRectF finalRect = makeUniform(renderer->boundsOnElement(actualElementId), QRect(QPoint(0, 0), size)); //don't alter the pixmap size or it won't match up properly to, e.g., FrameSvg elements //makeUniform should never change the size so much that it gains or loses a whole pixel p = QPixmap(size); p.fill(Qt::transparent); QPainter renderPainter(&p); if (actualElementId.isEmpty()) { renderer->render(&renderPainter, finalRect); } else { renderer->render(&renderPainter, actualElementId, finalRect); } renderPainter.end(); p.setDevicePixelRatio(ratio); // Apply current color scheme if the svg asks for it if (applyColors) { QImage itmp = p.toImage(); KIconEffect::colorize(itmp, cacheAndColorsTheme()->color(Theme::BackgroundColor), 1.0); p = p.fromImage(itmp); } if (cacheRendering) { cacheAndColorsTheme()->insertIntoCache(id, p, QString::number((qint64)q, 16) % QLSEP % actualElementId); } return p; } void SvgPrivate::createRenderer() { if (renderer) { return; } //qCDebug(LOG_PLASMA) << kBacktrace(); if (themed && path.isEmpty() && !themeFailed) { Applet *applet = qobject_cast(q->parent()); //FIXME: this maybe could be more efficient if we knew if the package was empty, e.g. for //C++; however, I'm not sure this has any real world runtime impact. something to measure //for. if (applet && applet->kPackage().isValid()) { const KPackage::Package package = applet->kPackage(); path = package.filePath("images", themePath + QLatin1String(".svg")); if (path.isEmpty()) { path = package.filePath("images", themePath + QLatin1String(".svgz")); } } if (path.isEmpty()) { path = actualTheme()->imagePath(themePath); themeFailed = path.isEmpty(); if (themeFailed) { qCWarning(LOG_PLASMA) << "No image path found for" << themePath; } } } //qCDebug(LOG_PLASMA) << "********************************"; //qCDebug(LOG_PLASMA) << "FAIL! **************************"; //qCDebug(LOG_PLASMA) << path << "**"; QString styleSheet = cacheAndColorsTheme()->d->svgStyleSheet(colorGroup, status); styleCrc = qChecksum(styleSheet.toUtf8(), styleSheet.size()); QHash::const_iterator it = s_renderers.constFind(styleCrc + path); if (it != s_renderers.constEnd()) { //qCDebug(LOG_PLASMA) << "gots us an existing one!"; renderer = it.value(); } else { if (path.isEmpty()) { renderer = new SharedSvgRenderer(); } else { QHash interestingElements; renderer = new SharedSvgRenderer(path, styleSheet, interestingElements); // Add interesting elements to the theme's rect cache. QHashIterator i(interestingElements); while (i.hasNext()) { i.next(); const QString &elementId = i.key(); const QRectF &elementRect = i.value(); const QString cacheId = CACHE_ID_NATURAL_SIZE(elementId, status, devicePixelRatio); localRectCache.insert(cacheId, elementRect); cacheAndColorsTheme()->insertIntoRectsCache(path, cacheId, elementRect); } } s_renderers[styleCrc + path] = renderer; } if (size == QSizeF()) { size = renderer->defaultSize(); } } void SvgPrivate::eraseRenderer() { if (renderer && renderer->ref.load() == 2) { // this and the cache reference it s_renderers.erase(s_renderers.find(styleCrc + path)); if (theme) { theme.data()->releaseRectsCache(path); } } renderer = 0; styleCrc = 0; localRectCache.clear(); elementsWithSizeHints.clear(); } QRectF SvgPrivate::elementRect(const QString &elementId) { if (themed && path.isEmpty()) { if (themeFailed) { return QRectF(); } path = actualTheme()->imagePath(themePath); themeFailed = path.isEmpty(); if (themeFailed) { return QRectF(); } } if (path.isEmpty()) { return QRectF(); } QString id = cacheId(elementId); if (localRectCache.contains(id)) { return localRectCache.value(id); } QRectF rect; bool found = cacheAndColorsTheme()->findInRectsCache(path, id, rect); - //This is a corner case where we are *sure* the element is not valid if (found && rect == QRectF()) { return rect; } else if (found) { localRectCache.insert(id, rect); } else { rect = findAndCacheElementRect(elementId); } return rect; } QRectF SvgPrivate::findAndCacheElementRect(const QString &elementId) { + //we need to check the id before createRenderer(), otherwise it may generate a different id compared to the previous cacheId)( call + const QString id = cacheId(elementId); createRenderer(); - // createRenderer() can insert some interesting rects in the cache, so check it - const QString id = cacheId(elementId); if (localRectCache.contains(id)) { return localRectCache.value(id); } QRectF elementRect = renderer->elementExists(elementId) ? renderer->matrixForElement(elementId).map(renderer->boundsOnElement(elementId)).boundingRect() : QRectF(); naturalSize = renderer->defaultSize() * scaleFactor; qreal dx = size.width() / renderer->defaultSize().width(); qreal dy = size.height() / renderer->defaultSize().height(); elementRect = QRectF(elementRect.x() * dx, elementRect.y() * dy, elementRect.width() * dx, elementRect.height() * dy); cacheAndColorsTheme()->insertIntoRectsCache(path, id, elementRect); return elementRect; } QMatrix SvgPrivate::matrixForElement(const QString &elementId) { createRenderer(); return renderer->matrixForElement(elementId); } void SvgPrivate::checkColorHints() { if (elementRect(QStringLiteral("hint-apply-color-scheme")).isValid()) { applyColors = true; usesColors = true; } else if (elementRect(QStringLiteral("current-color-scheme")).isValid()) { applyColors = false; usesColors = true; } else { applyColors = false; usesColors = false; } // check to see if we are using colors, but the theme isn't being used or isn't providing // a colorscheme if (qGuiApp) { if (usesColors && (!themed || !actualTheme()->colorScheme())) { QObject::connect(actualTheme()->d, SIGNAL(applicationPaletteChange()), q, SLOT(colorsChanged())); } else { QObject::disconnect(actualTheme()->d, SIGNAL(applicationPaletteChange()), q, SLOT(colorsChanged())); } } } bool Svg::eventFilter(QObject *watched, QEvent *event) { return QObject::eventFilter(watched, event); } //Following two are utility functions to snap rendered elements to the pixel grid //to and from are always 0 <= val <= 1 qreal SvgPrivate::closestDistance(qreal to, qreal from) { qreal a = to - from; if (qFuzzyCompare(to, from)) { return 0; } else if (to > from) { qreal b = to - from - 1; return (qAbs(a) > qAbs(b)) ? b : a; } else { qreal b = 1 + to - from; return (qAbs(a) > qAbs(b)) ? b : a; } } QRectF SvgPrivate::makeUniform(const QRectF &orig, const QRectF &dst) { if (qFuzzyIsNull(orig.x()) || qFuzzyIsNull(orig.y())) { return dst; } QRectF res(dst); qreal div_w = dst.width() / orig.width(); qreal div_h = dst.height() / orig.height(); qreal div_x = dst.x() / orig.x(); qreal div_y = dst.y() / orig.y(); //horizontal snap if (!qFuzzyIsNull(div_x) && !qFuzzyCompare(div_w, div_x)) { qreal rem_orig = orig.x() - (floor(orig.x())); qreal rem_dst = dst.x() - (floor(dst.x())); qreal offset = closestDistance(rem_dst, rem_orig); res.translate(offset + offset * div_w, 0); res.setWidth(res.width() + offset); } //vertical snap if (!qFuzzyIsNull(div_y) && !qFuzzyCompare(div_h, div_y)) { qreal rem_orig = orig.y() - (floor(orig.y())); qreal rem_dst = dst.y() - (floor(dst.y())); qreal offset = closestDistance(rem_dst, rem_orig); res.translate(0, offset + offset * div_h); res.setHeight(res.height() + offset); } //qCDebug(LOG_PLASMA)<<"Aligning Rects, origin:"<imagePath().isEmpty()) { return; } if (themed) { // check if new theme svg wants colorscheme applied checkColorHints(); } QString currentPath = themed ? themePath : path; themePath.clear(); eraseRenderer(); setImagePath(currentPath); q->resize(); //qCDebug(LOG_PLASMA) << themePath << ">>>>>>>>>>>>>>>>>> theme changed"; emit q->repaintNeeded(); } void SvgPrivate::colorsChanged() { if (!usesColors) { return; } eraseRenderer(); qCDebug(LOG_PLASMA) << "repaint needed from colorsChanged"; emit q->repaintNeeded(); } QHash SvgPrivate::s_renderers; QWeakPointer SvgPrivate::s_systemColorsCache; Svg::Svg(QObject *parent) : QObject(parent), d(new SvgPrivate(this)) { } Svg::~Svg() { delete d; } void Svg::setDevicePixelRatio(qreal ratio) { //be completely integer for now //devicepixelratio is always set integer in the svg, so needs at least 192dpi to double up. //(it needs to be integer to have lines contained inside a svg piece to keep being pixel aligned) if (floor(d->devicePixelRatio) == floor(ratio)) { return; } if (FrameSvg *f = qobject_cast(this)) { f->clearCache(); } d->devicePixelRatio = floor(ratio); emit repaintNeeded(); } qreal Svg::devicePixelRatio() { return d->devicePixelRatio; } void Svg::setScaleFactor(qreal ratio) { //be completely integer for now //devicepixelratio is always set integer in the svg, so needs at least 192dpi to double up. //(it needs to be integer to have lines contained inside a svg piece to keep being pixel aligned) if (floor(d->scaleFactor) == floor(ratio)) { return; } d->scaleFactor = floor(ratio); //not resize() because we want to do it unconditionally QRectF rect; if (d->cacheAndColorsTheme()->findInRectsCache(d->path, QStringLiteral("_Natural_%1").arg(d->scaleFactor), rect)) { d->naturalSize = rect.size(); } else { d->createRenderer(); d->naturalSize = d->renderer->defaultSize() * d->scaleFactor; } d->size = d->naturalSize; emit repaintNeeded(); emit sizeChanged(); } qreal Svg::scaleFactor() const { return d->scaleFactor; } void Svg::setColorGroup(Plasma::Theme::ColorGroup group) { if (d->colorGroup == group) { return; } d->colorGroup = group; d->renderer = 0; emit colorGroupChanged(); emit repaintNeeded(); } Plasma::Theme::ColorGroup Svg::colorGroup() const { return d->colorGroup; } QPixmap Svg::pixmap(const QString &elementID) { if (elementID.isNull() || d->multipleImages) { return d->findInCache(elementID, d->devicePixelRatio, size()); } else { return d->findInCache(elementID, d->devicePixelRatio); } } QImage Svg::image(const QSize &size, const QString &elementID) { QPixmap pix(d->findInCache(elementID, d->devicePixelRatio, size)); return pix.toImage(); } void Svg::paint(QPainter *painter, const QPointF &point, const QString &elementID) { Q_ASSERT(painter->device()); const int ratio = painter->device()->devicePixelRatio(); QPixmap pix((elementID.isNull() || d->multipleImages) ? d->findInCache(elementID, ratio, size()) : d->findInCache(elementID, ratio)); if (pix.isNull()) { return; } painter->drawPixmap(QRectF(point, size()), pix, QRectF(QPointF(0, 0), pix.size())); } void Svg::paint(QPainter *painter, int x, int y, const QString &elementID) { paint(painter, QPointF(x, y), elementID); } void Svg::paint(QPainter *painter, const QRectF &rect, const QString &elementID) { Q_ASSERT(painter->device()); const int ratio = painter->device()->devicePixelRatio(); QPixmap pix(d->findInCache(elementID, ratio, rect.size())); painter->drawPixmap(QRectF(rect.topLeft(), rect.size()), pix, QRectF(QPointF(0, 0), pix.size())); } void Svg::paint(QPainter *painter, int x, int y, int width, int height, const QString &elementID) { Q_ASSERT(painter->device()); const int ratio = painter->device()->devicePixelRatio(); QPixmap pix(d->findInCache(elementID, ratio, QSizeF(width, height))); painter->drawPixmap(x, y, pix, 0, 0, pix.size().width(), pix.size().height()); } QSize Svg::size() const { if (d->size.isEmpty()) { d->size = d->naturalSize; } return d->size.toSize(); } void Svg::resize(qreal width, qreal height) { resize(QSize(width, height)); } void Svg::resize(const QSizeF &size) { if (qFuzzyCompare(size.width(), d->size.width()) && qFuzzyCompare(size.height(), d->size.height())) { return; } d->size = size; d->localRectCache.clear(); emit sizeChanged(); } void Svg::resize() { if (qFuzzyCompare(d->naturalSize.width(), d->size.width()) && qFuzzyCompare(d->naturalSize.height(), d->size.height())) { return; } d->size = d->naturalSize; d->localRectCache.clear(); emit sizeChanged(); } QSize Svg::elementSize(const QString &elementId) const { return d->elementRect(elementId).size().toSize(); } QRectF Svg::elementRect(const QString &elementId) const { return d->elementRect(elementId); } bool Svg::hasElement(const QString &elementId) const { if (d->path.isNull() && d->themePath.isNull()) { return false; } return d->elementRect(elementId).isValid(); } bool Svg::isValid() const { if (d->path.isNull() && d->themePath.isNull()) { return false; } //try very hard to avoid creation of a parser QRectF rect; if (d->cacheAndColorsTheme()->findInRectsCache(d->path, QStringLiteral("_Natural_%1").arg(d->scaleFactor), rect)) { return true; } if (!QFile::exists(d->path)) { return false; } d->createRenderer(); return d->renderer->isValid(); } void Svg::setContainsMultipleImages(bool multiple) { d->multipleImages = multiple; } bool Svg::containsMultipleImages() const { return d->multipleImages; } void Svg::setImagePath(const QString &svgFilePath) { if (d->setImagePath(svgFilePath)) { //qCDebug(LOG_PLASMA) << "repaintNeeded"; emit repaintNeeded(); } } QString Svg::imagePath() const { return d->themed ? d->themePath : d->path; } void Svg::setUsingRenderingCache(bool useCache) { d->cacheRendering = useCache; } bool Svg::isUsingRenderingCache() const { return d->cacheRendering; } bool Svg::fromCurrentTheme() const { return d->fromCurrentTheme; } void Svg::setUseSystemColors(bool system) { if (d->useSystemColors == system) { return; } d->useSystemColors = system; emit repaintNeeded(); } bool Svg::useSystemColors() const { return d->useSystemColors; } void Svg::setTheme(Plasma::Theme *theme) { if (!theme || theme == d->theme.data()) { return; } if (d->theme) { disconnect(d->theme.data(), 0, this, 0); } d->theme = theme; connect(theme, SIGNAL(themeChanged()), this, SLOT(themeChanged())); d->themeChanged(); } Theme *Svg::theme() const { return d->actualTheme(); } void Svg::setStatus(Plasma::Svg::Status status) { if (status == d->status) { return; } d->status = status; d->eraseRenderer(); emit statusChanged(status); emit repaintNeeded(); } Svg::Status Svg::status() const { return d->status; } } // Plasma namespace #include "private/moc_svg_p.cpp" #include "moc_svg.cpp"