* use the to-disk-cache-delay in Theme rather than do it per-FrameSvg object

* be a bit more careful to use value() instead of [] in places as we sometimes check the hash for existence of an entry, and [] creates an entry while value() is more kind in that way

svn path=/trunk/KDE/kdelibs/; revision=975773
This commit is contained in:
Aaron J. Seigo 2009-05-31 05:38:42 +00:00
parent 177be5f6af
commit 9e50b9b61e
3 changed files with 54 additions and 41 deletions

View File

@ -42,10 +42,6 @@ FrameSvg::FrameSvg(QObject *parent)
{ {
connect(this, SIGNAL(repaintNeeded()), this, SLOT(updateNeeded())); connect(this, SIGNAL(repaintNeeded()), this, SLOT(updateNeeded()));
d->frames.insert(QString(), new FrameData()); d->frames.insert(QString(), new FrameData());
d->saveTimer = new QTimer(this);
d->saveTimer->setSingleShot(true);
connect(d->saveTimer, SIGNAL(timeout()), this, SLOT(scheduledCacheUpdate()));
} }
FrameSvg::~FrameSvg() FrameSvg::~FrameSvg()
@ -106,6 +102,7 @@ void FrameSvg::setElementPrefix(Plasma::Location location)
setElementPrefix(QString()); setElementPrefix(QString());
break; break;
} }
d->location = location; d->location = location;
} }
@ -120,21 +117,25 @@ void FrameSvg::setElementPrefix(const QString & prefix)
if (!d->prefix.isEmpty()) { if (!d->prefix.isEmpty()) {
d->prefix += '-'; d->prefix += '-';
} }
} }
if (oldPrefix == d->prefix && d->frames[oldPrefix]) { FrameData *oldFrameData = d->frames.value(oldPrefix);
if (oldPrefix == d->prefix && oldFrameData) {
return; return;
} }
if (!d->frames.contains(d->prefix)) { if (!d->frames.contains(d->prefix)) {
d->frames.insert(d->prefix, new FrameData(*(d->frames[oldPrefix]))); if (oldFrameData) {
d->frames.insert(d->prefix, new FrameData(*oldFrameData));
} else {
d->frames.insert(d->prefix, new FrameData());
}
d->updateSizes(); d->updateSizes();
} }
if (!d->cacheAll) { if (!d->cacheAll) {
delete d->frames[oldPrefix]; delete d->frames[oldPrefix];
d->framesToSave.removeAll(oldPrefix);
d->frames.remove(oldPrefix); d->frames.remove(oldPrefix);
} }
@ -329,15 +330,13 @@ void FrameSvg::clearCache()
{ {
FrameData *frame = d->frames[d->prefix]; FrameData *frame = d->frames[d->prefix];
d->saveTimer->stop();
d->framesToSave.clear();
// delete all the frames that aren't this one // delete all the frames that aren't this one
QMutableHashIterator<QString, FrameData*> it(d->frames); QMutableHashIterator<QString, FrameData*> it(d->frames);
while (it.hasNext()) { while (it.hasNext()) {
FrameData *p = it.next().value(); FrameData *p = it.next().value();
if (frame != p) { if (frame != p) {
//TODO: should we clear from the pixmap cache as well?
delete p; delete p;
it.remove(); it.remove();
} }
@ -598,24 +597,23 @@ void FrameSvgPrivate::generateBackground(FrameData *frame)
p.drawPixmap(overlayPos, overlay, QRect(overlayPos, overlaySize)); p.drawPixmap(overlayPos, overlay, QRect(overlayPos, overlaySize));
} }
if (!framesToSave.contains(prefix)) { cacheFrame(prefix);
framesToSave.append(prefix);
}
saveTimer->start(300);
} }
void FrameSvgPrivate::scheduledCacheUpdate() void FrameSvgPrivate::cacheFrame(const QString &prefixToSave)
{ {
if (!q->isUsingRenderingCache()) { if (!q->isUsingRenderingCache()) {
framesToSave.clear();
return; return;
} }
foreach (QString prefixToSave, framesToSave) {
//insert background //insert background
FrameData *frame = frames[prefixToSave]; FrameData *frame = frames.value(prefixToSave);
if (!frame) {
return;
}
QString id = QString::fromLatin1("%7_%6_%5_%4_%3_%2_%1_"). QString id = QString::fromLatin1("%7_%6_%5_%4_%3_%2_%1_").
arg(overlayPos.y()).arg(overlayPos.x()).arg(frame->enabledBorders).arg(frame->frameSize.width()).arg(frame->frameSize.height()).arg(prefixToSave).arg(q->imagePath()); arg(overlayPos.y()).arg(overlayPos.x()).arg(frame->enabledBorders).arg(frame->frameSize.width()).arg(frame->frameSize.height()).arg(prefixToSave).arg(q->imagePath());
@ -630,9 +628,6 @@ void FrameSvgPrivate::scheduledCacheUpdate()
Theme::defaultTheme()->insertIntoCache(id, frame->cachedBackground); Theme::defaultTheme()->insertIntoCache(id, frame->cachedBackground);
} }
framesToSave.clear();
}
void FrameSvgPrivate::updateSizes() void FrameSvgPrivate::updateSizes()
{ {
//kDebug() << "!!!!!!!!!!!!!!!!!!!!!! updating sizes" << prefix; //kDebug() << "!!!!!!!!!!!!!!!!!!!!!! updating sizes" << prefix;

View File

@ -262,7 +262,6 @@ class PLASMA_EXPORT FrameSvg : public Svg
Q_PRIVATE_SLOT(d, void updateSizes()) Q_PRIVATE_SLOT(d, void updateSizes())
Q_PRIVATE_SLOT(d, void updateNeeded()) Q_PRIVATE_SLOT(d, void updateNeeded())
Q_PRIVATE_SLOT(d, void scheduledCacheUpdate())
}; };
} // Plasma namespace } // Plasma namespace

View File

@ -30,13 +30,35 @@ class FrameData
public: public:
FrameData() FrameData()
: enabledBorders(FrameSvg::AllBorders), : enabledBorders(FrameSvg::AllBorders),
frameSize(-1,-1) 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)
{ {
} }
FrameData(const FrameData &other) FrameData(const FrameData &other)
: enabledBorders(other.enabledBorders), : enabledBorders(other.enabledBorders),
frameSize(other.frameSize) frameSize(other.frameSize),
topHeight(0),
leftWidth(0),
rightWidth(0),
bottomHeight(0),
topMargin(0),
leftMargin(0),
rightMargin(0),
bottomMargin(0),
noBorderPadding(false),
stretchBorders(false),
tileCenter(false)
{ {
} }
@ -74,7 +96,6 @@ public:
FrameSvgPrivate(FrameSvg *psvg) FrameSvgPrivate(FrameSvg *psvg)
: q(psvg), : q(psvg),
cacheAll(false), cacheAll(false),
saveTimer(0),
overlayPos(0,0) overlayPos(0,0)
{ {
} }
@ -86,7 +107,7 @@ public:
} }
void generateBackground(FrameData *frame); void generateBackground(FrameData *frame);
void scheduledCacheUpdate(); void cacheFrame(const QString &prefix);
void updateSizes(); void updateSizes();
void updateNeeded(); void updateNeeded();
void updateAndSignalSizes(); void updateAndSignalSizes();
@ -97,8 +118,6 @@ public:
FrameSvg *q; FrameSvg *q;
bool cacheAll : 1; bool cacheAll : 1;
QStringList framesToSave;
QTimer *saveTimer;
QPoint overlayPos; QPoint overlayPos;
QHash<QString, FrameData*> frames; QHash<QString, FrameData*> frames;