summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/multimedia/opieplayer2/skin.cpp39
1 files changed, 35 insertions, 4 deletions
diff --git a/noncore/multimedia/opieplayer2/skin.cpp b/noncore/multimedia/opieplayer2/skin.cpp
index 5013bb4..0f49862 100644
--- a/noncore/multimedia/opieplayer2/skin.cpp
+++ b/noncore/multimedia/opieplayer2/skin.cpp
@@ -32,52 +32,55 @@
32#include <assert.h> 32#include <assert.h>
33 33
34struct SkinData 34struct SkinData
35{ 35{
36 typedef QMap<QString, QImage> ButtonMaskImageMap; 36 typedef QMap<QString, QImage> ButtonMaskImageMap;
37 37
38 QImage backgroundImage; 38 QImage backgroundImage;
39 QImage buttonUpImage; 39 QImage buttonUpImage;
40 QImage buttonDownImage; 40 QImage buttonDownImage;
41 QImage buttonMask; 41 QImage buttonMask;
42 ButtonMaskImageMap buttonMasks; 42 ButtonMaskImageMap buttonMasks;
43}; 43};
44 44
45class SkinCache : public Singleton<SkinCache> 45class SkinCache : public Singleton<SkinCache>
46{ 46{
47public: 47public:
48 SkinCache(); 48 SkinCache();
49 49
50 SkinData *lookupAndTake( const QString &skinPath, const QString &fileNameInfix ); 50 SkinData *lookupAndTake( const QString &skinPath, const QString &fileNameInfix );
51 51
52 void store( const QString &skinPath, const QString &fileNameInfix, SkinData *data ); 52 void store( const QString &skinPath, const QString &fileNameInfix, SkinData *data );
53 53
54private: 54private:
55 typedef QCache<SkinData> DataCache; 55 typedef QCache<SkinData> DataCache;
56 typedef QCache<QPixmap> BackgroundPixmapCache; 56 typedef QCache<QImage> BackgroundImageCache;
57
58 template <class CacheType>
59 void store( const QCache<CacheType> &cache, const QString &key, CacheType *data );
57 60
58 DataCache m_cache; 61 DataCache m_cache;
59 BackgroundPixmapCache m_backgroundPixmapCache; 62 BackgroundImageCache m_backgroundImageCache;
60}; 63};
61 64
62Skin::Skin( const QString &name, const QString &fileNameInfix ) 65Skin::Skin( const QString &name, const QString &fileNameInfix )
63 : m_fileNameInfix( fileNameInfix ) 66 : m_fileNameInfix( fileNameInfix )
64{ 67{
65 init( name ); 68 init( name );
66} 69}
67 70
68Skin::Skin( const QString &fileNameInfix ) 71Skin::Skin( const QString &fileNameInfix )
69 : m_fileNameInfix( fileNameInfix ) 72 : m_fileNameInfix( fileNameInfix )
70{ 73{
71 init( defaultSkinName() ); 74 init( defaultSkinName() );
72} 75}
73 76
74Skin::~Skin() 77Skin::~Skin()
75{ 78{
76 SkinCache::self().store( m_skinPath, m_fileNameInfix, d ); 79 SkinCache::self().store( m_skinPath, m_fileNameInfix, d );
77} 80}
78 81
79void Skin::init( const QString &name ) 82void Skin::init( const QString &name )
80{ 83{
81 m_skinPath = "opieplayer2/skins/" + name; 84 m_skinPath = "opieplayer2/skins/" + name;
82 d = SkinCache::self().lookupAndTake( m_skinPath, m_fileNameInfix ); 85 d = SkinCache::self().lookupAndTake( m_skinPath, m_fileNameInfix );
83} 86}
@@ -145,58 +148,86 @@ QImage Skin::buttonMaskImage( const QString &fileName ) const
145{ 148{
146 SkinData::ButtonMaskImageMap::Iterator it = d->buttonMasks.find( fileName ); 149 SkinData::ButtonMaskImageMap::Iterator it = d->buttonMasks.find( fileName );
147 if ( it == d->buttonMasks.end() ) { 150 if ( it == d->buttonMasks.end() ) {
148 QString prefix = m_skinPath + QString::fromLatin1( "/skin%1_mask_" ).arg( m_fileNameInfix ); 151 QString prefix = m_skinPath + QString::fromLatin1( "/skin%1_mask_" ).arg( m_fileNameInfix );
149 QString path = prefix + fileName + ".png"; 152 QString path = prefix + fileName + ".png";
150 it = d->buttonMasks.insert( fileName, loadImage( path ) ); 153 it = d->buttonMasks.insert( fileName, loadImage( path ) );
151 } 154 }
152 return *it; 155 return *it;
153} 156}
154 157
155QString Skin::defaultSkinName() 158QString Skin::defaultSkinName()
156{ 159{
157 Config cfg( "OpiePlayer" ); 160 Config cfg( "OpiePlayer" );
158 cfg.setGroup( "Options" ); 161 cfg.setGroup( "Options" );
159 return cfg.readEntry( "Skin", "default" ); 162 return cfg.readEntry( "Skin", "default" );
160} 163}
161 164
162QImage Skin::loadImage( const QString &fileName ) 165QImage Skin::loadImage( const QString &fileName )
163{ 166{
164 return QImage( Resource::findPixmap( fileName ) ); 167 return QImage( Resource::findPixmap( fileName ) );
165} 168}
166 169
167SkinCache::SkinCache() 170SkinCache::SkinCache()
168{ 171{
172 // let's say we cache two skins (audio+video) at maximum
173 m_cache.setMaxCost( 2 );
174 // ... and one background pixmap
175 m_backgroundImageCache.setMaxCost( 1 );
169} 176}
170 177
171SkinData *SkinCache::lookupAndTake( const QString &skinPath, const QString &fileNameInfix ) 178SkinData *SkinCache::lookupAndTake( const QString &skinPath, const QString &fileNameInfix )
172{ 179{
173 return new SkinData; 180 QString key = skinPath + fileNameInfix;
181
182 SkinData *data = m_cache.take( key );
183 if ( !data )
184 data = new SkinData;
185
186 QImage *bgImage = m_backgroundImageCache.find( skinPath );
187 if ( bgImage )
188 data->backgroundImage = *bgImage;
189 else
190 data->backgroundImage = QImage();
191
192 return data;
174} 193}
175 194
176void SkinCache::store( const QString &skinPath, const QString &fileNameInfix, SkinData *data ) 195void SkinCache::store( const QString &skinPath, const QString &fileNameInfix, SkinData *data )
177{ 196{
178 delete data; 197 QImage *backgroundImage = new QImage( data->backgroundImage );
198
199 data->backgroundImage = QImage();
200
201 QString key = skinPath + fileNameInfix;
202
203 if ( m_cache.find( key, false /*ref*/ ) != 0 ||
204 !m_cache.insert( key, data ) )
205 delete data;
206
207 if ( m_backgroundImageCache.find( skinPath, false /*ref*/ ) != 0 ||
208 !m_backgroundImageCache.insert( skinPath, backgroundImage ) )
209 delete backgroundImage;
179} 210}
180 211
181SkinLoader::SkinLoader() 212SkinLoader::SkinLoader()
182{ 213{
183} 214}
184 215
185void SkinLoader::schedule( const QString &skinName, const QString &fileNameInfix, 216void SkinLoader::schedule( const QString &skinName, const QString &fileNameInfix,
186 const MediaWidget::SkinButtonInfo *skinButtonInfo, const uint buttonCount ) 217 const MediaWidget::SkinButtonInfo *skinButtonInfo, const uint buttonCount )
187{ 218{
188 assert( isRunning() == false ); 219 assert( isRunning() == false );
189 220
190 pendingSkins << Info( skinName, fileNameInfix, skinButtonInfo, buttonCount ); 221 pendingSkins << Info( skinName, fileNameInfix, skinButtonInfo, buttonCount );
191} 222}
192 223
193void SkinLoader::run() 224void SkinLoader::run()
194{ 225{
195 qDebug( "SkinLoader::run()" ); 226 qDebug( "SkinLoader::run()" );
196 for ( InfoList::ConstIterator it = pendingSkins.begin(); it != pendingSkins.end(); ++it ) 227 for ( InfoList::ConstIterator it = pendingSkins.begin(); it != pendingSkins.end(); ++it )
197 load( *it ); 228 load( *it );
198 qDebug( "SkinLoader is done." ); 229 qDebug( "SkinLoader is done." );
199} 230}
200 231
201void SkinLoader::load( const Info &nfo ) 232void SkinLoader::load( const Info &nfo )
202{ 233{