cache the values for size and resize method

the main advantage to this is that only when the object signals a change
will the values be re-checked, which means that the wallpaper pointer
can be deleted behind our backs and we won't care
This commit is contained in:
Aaron Seigo 2011-02-16 11:41:18 -08:00
parent 56c25bd8f2
commit 3924d218a4
2 changed files with 17 additions and 8 deletions

View File

@ -213,7 +213,9 @@ ThemePackage::ThemePackage(QObject *parent)
WallpaperPackage::WallpaperPackage(Wallpaper *paper, QObject *parent) WallpaperPackage::WallpaperPackage(Wallpaper *paper, QObject *parent)
: PackageStructure(parent, "Background"), : PackageStructure(parent, "Background"),
m_paper(paper), m_paper(paper),
m_fullPackage(true) m_fullPackage(true),
m_targetSize(100000, 100000),
m_resizeMethod(Wallpaper::ScaledResize)
{ {
QStringList mimetypes; QStringList mimetypes;
mimetypes << "image/svg" << "image/png" << "image/jpeg" << "image/jpg"; mimetypes << "image/svg" << "image/png" << "image/jpeg" << "image/jpg";
@ -225,13 +227,21 @@ WallpaperPackage::WallpaperPackage(Wallpaper *paper, QObject *parent)
setAllowExternalPaths(true); setAllowExternalPaths(true);
if (m_paper) { if (m_paper) {
connect(paper, SIGNAL(renderHintsChanged()), this, SLOT(renderHintsChanged())); m_targetSize = m_paper->d->targetSize.toSize();
m_resizeMethod = m_paper->d->lastResizeMethod;
connect(m_paper, SIGNAL(renderHintsChanged()), this, SLOT(renderHintsChanged()));
connect(m_paper, SIGNAL(destroyed(QObject*)), this, SLOT(paperDestroyed())); connect(m_paper, SIGNAL(destroyed(QObject*)), this, SLOT(paperDestroyed()));
} }
} }
void WallpaperPackage::renderHintsChanged() void WallpaperPackage::renderHintsChanged()
{ {
if (m_paper) {
m_targetSize = m_paper->d->targetSize.toSize();
m_resizeMethod = m_paper->d->lastResizeMethod;
}
if (m_fullPackage) { if (m_fullPackage) {
findBestPaper(); findBestPaper();
} }
@ -248,14 +258,14 @@ void WallpaperPackage::pathChanged()
guard = true; guard = true;
QFileInfo info(path()); QFileInfo info(path());
m_fullPackage = info.isDir(); m_fullPackage = info.isDir();
removeDefinition("preferred");
if (m_fullPackage) { if (m_fullPackage) {
setContentsPrefixPaths(QStringList() << "contents/"); setContentsPrefixPaths(QStringList() << "contents/");
findBestPaper(); findBestPaper();
} else { } else {
// dirty trick to support having a file passed in instead of a directory // dirty trick to support having a file passed in instead of a directory
removeDefinition("preferred");
addFileDefinition("preferred", info.fileName(), i18n("Recommended wallpaper file")); addFileDefinition("preferred", info.fileName(), i18n("Recommended wallpaper file"));
setContentsPrefixPaths(QStringList()); setContentsPrefixPaths(QStringList());
//kDebug() << "changing" << path() << "to" << info.path(); //kDebug() << "changing" << path() << "to" << info.path();
@ -287,9 +297,6 @@ void WallpaperPackage::findBestPaper()
// choose the nearest resolution // choose the nearest resolution
float best = FLT_MAX; float best = FLT_MAX;
const QSize size = m_paper ? m_paper->d->targetSize.toSize() : QSize(100000, 100000);
const Wallpaper::ResizeMethod method = m_paper ? m_paper->d->lastResizeMethod
: Wallpaper::ScaledResize;
QString bestImage; QString bestImage;
foreach (const QString &entry, images) { foreach (const QString &entry, images) {
@ -298,7 +305,7 @@ void WallpaperPackage::findBestPaper()
continue; continue;
} }
double dist = distance(candidate, size, method); double dist = distance(candidate, m_targetSize, m_resizeMethod);
//kDebug() << "candidate" << candidate << "distance" << dist; //kDebug() << "candidate" << candidate << "distance" << dist;
if (bestImage.isEmpty() || dist < best) { if (bestImage.isEmpty() || dist < best) {
bestImage = entry; bestImage = entry;

View File

@ -94,6 +94,8 @@ private Q_SLOTS:
private: private:
Wallpaper *m_paper; Wallpaper *m_paper;
bool m_fullPackage; bool m_fullPackage;
QSize m_targetSize;
Wallpaper::ResizeMethod m_resizeMethod;
}; };
class ContainmentActionsPackage : public PackageStructure class ContainmentActionsPackage : public PackageStructure