From 454e645b9294a779ca00f17c594b112b7ba7059f Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Tue, 4 Dec 2012 19:59:59 +0100 Subject: [PATCH] * Added ImageRegistry. Finally a global image cache (and soon to be SVG renderer). --- src/libtomahawk/CMakeLists.txt | 1 + src/libtomahawk/utils/ImageRegistry.cpp | 110 ++++++++++++++++++++++++ src/libtomahawk/utils/ImageRegistry.h | 41 +++++++++ 3 files changed, 152 insertions(+) create mode 100644 src/libtomahawk/utils/ImageRegistry.cpp create mode 100644 src/libtomahawk/utils/ImageRegistry.h diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index e2c6898f7..46091741f 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -99,6 +99,7 @@ set( libGuiSources resolvers/ScriptResolver.cpp resolvers/QtScriptResolver.cpp + utils/ImageRegistry.cpp utils/WidgetDragFilter.cpp utils/XspfGenerator.cpp utils/JspfLoader.cpp diff --git a/src/libtomahawk/utils/ImageRegistry.cpp b/src/libtomahawk/utils/ImageRegistry.cpp new file mode 100644 index 000000000..b8a4c21cb --- /dev/null +++ b/src/libtomahawk/utils/ImageRegistry.cpp @@ -0,0 +1,110 @@ +/* + * Copyright 2012, Christian Muehlhaeuser + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, 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 General Public License + along with this program. If not, see . +*/ + +#include "ImageRegistry.h" + +#include "utils/Logger.h" + +static QHash< QString, QHash< int, QHash< int, QPixmap > > > s_cache; +ImageRegistry* ImageRegistry::s_instance = 0; + + +ImageRegistry* +ImageRegistry::instance() +{ + return s_instance; +} + + +ImageRegistry::ImageRegistry() +{ + s_instance = this; +} + + +QPixmap +ImageRegistry::getFromCache( const QString& image, const QSize& size, TomahawkUtils::ImageMode mode ) +{ + QHash< int, QPixmap > subsubcache; + QHash< int, QHash< int, QPixmap > > subcache; + + if ( s_cache.contains( image ) ) + { + subcache = s_cache.value( image ); + + if ( subcache.contains( mode ) ) + { + subsubcache = subcache.value( mode ); + + if ( subsubcache.contains( size.width() ) ) + { + return subsubcache.value( size.width() ); + } + } + } + + // Image not found in cache. Let's load it. + QPixmap pixmap( image ); + if ( !pixmap.isNull() ) + { + switch ( mode ) + { + case TomahawkUtils::RoundedCorners: + pixmap = TomahawkUtils::createRoundedImage( pixmap, size ); + break; + + default: + break; + } + + if ( !size.isNull() && pixmap.size() != size ) + pixmap = pixmap.scaled( size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation ); + + putInCache( image, size, mode, pixmap ); + } + + return pixmap; +} + + +void +ImageRegistry::putInCache( const QString& image, const QSize& size, TomahawkUtils::ImageMode mode, const QPixmap& pixmap ) +{ + tDebug() << Q_FUNC_INFO << "Adding to image cache:" << image << size << mode; + + QHash< int, QPixmap > subsubcache; + QHash< int, QHash< int, QPixmap > > subcache; + + if ( s_cache.contains( image ) ) + { + subcache = s_cache.value( image ); + + if ( subcache.contains( mode ) ) + { + subsubcache = subcache.value( mode ); + +/* if ( subsubcache.contains( size.width() ) ) + { + Q_ASSERT( false ); + }*/ + } + } + + subsubcache.insert( size.width(), pixmap ); + subcache.insert( mode, subsubcache ); + s_cache.insert( image, subcache ); +} diff --git a/src/libtomahawk/utils/ImageRegistry.h b/src/libtomahawk/utils/ImageRegistry.h new file mode 100644 index 000000000..fca971f3d --- /dev/null +++ b/src/libtomahawk/utils/ImageRegistry.h @@ -0,0 +1,41 @@ +/* + * Copyright 2012, Christian Muehlhaeuser + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, 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 General Public License + along with this program. If not, see . +*/ + +#ifndef IMAGE_REGISTRY_H +#define IMAGE_REGISTRY_H + +#include + +#include "utils/TomahawkUtilsGui.h" +#include "DllMacro.h" + +class DLLEXPORT ImageRegistry +{ +public: + static ImageRegistry* instance(); + + explicit ImageRegistry(); + + QPixmap getFromCache( const QString& image, const QSize& size, TomahawkUtils::ImageMode mode = TomahawkUtils::Original ); + +private: + void putInCache( const QString& image, const QSize& size, TomahawkUtils::ImageMode mode, const QPixmap& pixmap ); + + static ImageRegistry* s_instance; +}; + +#endif // IMAGE_REGISTRY_H