author | simon <simon> | 2002-12-11 20:26:47 (UTC) |
---|---|---|
committer | simon <simon> | 2002-12-11 20:26:47 (UTC) |
commit | c5737bbbe357ea7481a9f6a406ef81bbeba0d541 (patch) (unidiff) | |
tree | 62a970f2a18c0cb84c056545da729f71c8005bad | |
parent | b433feb7b048d5155e085c12f88ac5531d045140 (diff) | |
download | opie-c5737bbbe357ea7481a9f6a406ef81bbeba0d541.zip opie-c5737bbbe357ea7481a9f6a406ef81bbeba0d541.tar.gz opie-c5737bbbe357ea7481a9f6a406ef81bbeba0d541.tar.bz2 |
- make the cache work as expected
-rw-r--r-- | noncore/multimedia/opieplayer2/skin.cpp | 39 |
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 | |||
@@ -40,36 +40,39 @@ struct SkinData | |||
40 | QImage buttonDownImage; | 40 | QImage buttonDownImage; |
41 | QImage buttonMask; | 41 | QImage buttonMask; |
42 | ButtonMaskImageMap buttonMasks; | 42 | ButtonMaskImageMap buttonMasks; |
43 | }; | 43 | }; |
44 | 44 | ||
45 | class SkinCache : public Singleton<SkinCache> | 45 | class SkinCache : public Singleton<SkinCache> |
46 | { | 46 | { |
47 | public: | 47 | public: |
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 | ||
54 | private: | 54 | private: |
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 | ||
62 | Skin::Skin( const QString &name, const QString &fileNameInfix ) | 65 | Skin::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 | ||
68 | Skin::Skin( const QString &fileNameInfix ) | 71 | Skin::Skin( const QString &fileNameInfix ) |
69 | : m_fileNameInfix( fileNameInfix ) | 72 | : m_fileNameInfix( fileNameInfix ) |
70 | { | 73 | { |
71 | init( defaultSkinName() ); | 74 | init( defaultSkinName() ); |
72 | } | 75 | } |
73 | 76 | ||
74 | Skin::~Skin() | 77 | Skin::~Skin() |
75 | { | 78 | { |
@@ -153,42 +156,70 @@ QImage Skin::buttonMaskImage( const QString &fileName ) const | |||
153 | } | 156 | } |
154 | 157 | ||
155 | QString Skin::defaultSkinName() | 158 | QString 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 | ||
162 | QImage Skin::loadImage( const QString &fileName ) | 165 | QImage Skin::loadImage( const QString &fileName ) |
163 | { | 166 | { |
164 | return QImage( Resource::findPixmap( fileName ) ); | 167 | return QImage( Resource::findPixmap( fileName ) ); |
165 | } | 168 | } |
166 | 169 | ||
167 | SkinCache::SkinCache() | 170 | SkinCache::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 | ||
171 | SkinData *SkinCache::lookupAndTake( const QString &skinPath, const QString &fileNameInfix ) | 178 | SkinData *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 | ||
176 | void SkinCache::store( const QString &skinPath, const QString &fileNameInfix, SkinData *data ) | 195 | void 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 | ||
181 | SkinLoader::SkinLoader() | 212 | SkinLoader::SkinLoader() |
182 | { | 213 | { |
183 | } | 214 | } |
184 | 215 | ||
185 | void SkinLoader::schedule( const QString &skinName, const QString &fileNameInfix, | 216 | void 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 | ||
193 | void SkinLoader::run() | 224 | void SkinLoader::run() |
194 | { | 225 | { |