File indexing completed on 2024-05-26 16:15:05
0001 /* This file is part of the KDE project 0002 Copyright (C) 2009 Thorsten Zachmann <zachmann@kde.org> 0003 0004 This library is free software; you can redistribute it and/or 0005 modify it under the terms of the GNU Library General Public 0006 License as published by the Free Software Foundation; either 0007 version 2 of the License, or (at your option) any later version. 0008 0009 This library is distributed in the hope that it will be useful, 0010 but WITHOUT ANY WARRANTY; without even the implied warranty of 0011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0012 Library General Public License for more details. 0013 0014 You should have received a copy of the GNU Library General Public License 0015 along with this library; see the file COPYING.LIB. If not, write to 0016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 0017 * Boston, MA 02110-1301, USA. 0018 */ 0019 0020 #include "KoPAPixmapCache.h" 0021 0022 #include <QPixmapCache> 0023 #include <QGlobalStatic> 0024 0025 class KoPAPixmapCache::Singleton 0026 { 0027 public: 0028 KoPAPixmapCache q; 0029 }; 0030 0031 Q_GLOBAL_STATIC( KoPAPixmapCache::Singleton, singleton ) 0032 0033 KoPAPixmapCache * KoPAPixmapCache::instance() 0034 { 0035 return &( singleton->q ); 0036 } 0037 0038 KoPAPixmapCache::KoPAPixmapCache() 0039 { 0040 } 0041 0042 KoPAPixmapCache::~KoPAPixmapCache() 0043 { 0044 } 0045 0046 int KoPAPixmapCache::cacheLimit() 0047 { 0048 return QPixmapCache::cacheLimit(); 0049 } 0050 0051 void KoPAPixmapCache::clear( bool all ) 0052 { 0053 if ( all ) { 0054 QPixmapCache::clear(); 0055 } 0056 else { 0057 QMap<QString, QVector<QSize> >::ConstIterator it( m_keySize.constBegin() ); 0058 0059 for ( ; it != m_keySize.constEnd(); ++it ) { 0060 foreach( const QSize& size, it.value() ) { 0061 QString k = generateKey( it.key(), size ); 0062 QPixmapCache::remove( k ); 0063 } 0064 } 0065 m_keySize.clear(); 0066 } 0067 } 0068 0069 bool KoPAPixmapCache::find( const QString & key, const QSize & size, QPixmap & pm ) 0070 { 0071 QString k = generateKey( key, size ); 0072 return QPixmapCache::find( k, pm ); 0073 } 0074 0075 bool KoPAPixmapCache::insert( const QString & key, const QPixmap & pm, const QSize &size ) 0076 { 0077 QString k = generateKey( key, size.isValid() ? size: pm.size() ); 0078 m_keySize[key].append( size.isValid() ? size: pm.size() ); 0079 return QPixmapCache::insert( k, pm ); 0080 } 0081 0082 void KoPAPixmapCache::remove( const QString & key ) 0083 { 0084 QMap<QString, QVector<QSize> >::iterator it( m_keySize.find( key ) ); 0085 0086 if ( it != m_keySize.end() ) { 0087 foreach( const QSize& size, it.value() ) { 0088 QString k = generateKey( key, size ); 0089 QPixmapCache::remove( k ); 0090 } 0091 m_keySize.erase( it ); 0092 } 0093 } 0094 0095 void KoPAPixmapCache::setCacheLimit( int n ) 0096 { 0097 QPixmapCache::setCacheLimit( n ); 0098 } 0099 0100 QString KoPAPixmapCache::generateKey( const QString &key, const QSize & size ) 0101 { 0102 return QString( "%1-%2-%3" ).arg( key ).arg( size.width() ).arg( size.height() ); 0103 }