KVIrc 5.2.6
Developer APIs
KviAnimatedPixmapCache.h
Go to the documentation of this file.
1#ifndef KVI_ANIMATEDPIXMAPCACHE_H_
2#define KVI_ANIMATEDPIXMAPCACHE_H_
3//=============================================================================
4//
5// File : KviAnimatedPixmapCache.h
6// Creation date : Thu Jul 31 2008 01:45:21 CEST by Alexey Uzhva
7//
8// This file is part of the KVIrc IRC client distribution
9// Copyright (C) 2008 Alexey Uzhva (wizard at opendoor dot ru)
10//
11// This program is FREE software. You can redistribute it and/or
12// modify it under the terms of the GNU General Public License
13// as published by the Free Software Foundation; either version 2
14// of the License, or (at your option) any later version.
15//
16// This program is distributed in the HOPE that it will be USEFUL,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19// See the GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with this program. If not, write to the Free Software Foundation,
23// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24//
25//=============================================================================
26
27#include "kvi_settings.h"
29
30#include <QMultiHash>
31#include <QMultiMap>
32#include <QMutex>
33#include <QObject>
34#include <QPixmap>
35#include <QTimer>
36
37class KVILIB_API KviAnimatedPixmapCache : public QObject
38{
39 Q_OBJECT
40public:
41 /*
42 * This subclass represents simple structure
43 * to store animated frame data: cached pixmap, and next frame delay.
44 *
45 * It provides copyconstructor, which makes possible to simple
46 * assign two containers with this classes.
47 *
48 * All data will be duplicated in such case.
49 */
51 {
52 public:
53 QPixmap * pixmap; //frame pixmap
54 uint delay; //next frame delay
55
56 FrameInfo(QPixmap * _pixmap, uint _delay)
57 {
58 pixmap = _pixmap;
59 delay = _delay;
60 }
61
62 FrameInfo(const FrameInfo & source)
63 {
64 pixmap = source.pixmap;
65 delay = source.delay;
66 }
67
68 void detach()
69 {
70 pixmap = new QPixmap(*pixmap);
71 }
72 };
73
74 /*
75 * This helper class represents data structure
76 * for storing frames.
77 *
78 * It adds references counter and
79 * mutex, to provide thread-safety.
80 */
81 class Data : public QList<FrameInfo>
82 {
83 public:
84 uint refs; //references count
85 QSize size; //size of the pixmaps
86 QString file; //just to speedup the cache
87 bool resized;
88
89 Data(QString szFile) : QList<FrameInfo>(), refs(0), file(szFile), resized(false)
90 {
91 }
92
93 Data(Data & other) : QList<FrameInfo>(other), refs(0), file(other.file), resized(false)
94 {
95 for(int i = 0; i < count(); i++)
96 {
97 this->operator[](i).detach();
98 }
99 }
100 };
101
102protected:
103 //
104 // This class is a singleton.
105 // It can't be created directly
106 //
108 virtual ~KviAnimatedPixmapCache();
109
110protected:
111 mutable QMutex m_cacheMutex;
112 mutable QMutex m_timerMutex;
113
114 QMultiHash<QString, Data *> m_hCache;
115 QMultiMap<long long, KviAnimatedPixmapInterface *> m_timerData;
117
119
120protected:
121 Data * internalLoad(const QString & szFile, int iWidth = 0, int iHeight = 0);
122 Data * internalResize(Data * data, const QSize & size);
123 void internalFree(Data * data);
124
125 void internalScheduleFrameChange(uint delay, KviAnimatedPixmapInterface * receiver);
127
128protected slots:
129 virtual void timeoutEvent();
130
131public:
132 static void init();
133 static void done();
134
135 static void scheduleFrameChange(uint delay, KviAnimatedPixmapInterface * receiver)
136 {
137 m_pInstance->internalScheduleFrameChange(delay, receiver);
138 }
139
140 static Data * load(const QString & szFileName, int iWidth = 0, int iHeight = 0)
141 {
142 return m_pInstance->internalLoad(szFileName, iWidth, iHeight);
143 }
144
145 static Data * resize(Data * data, const QSize & size)
146 {
147 return m_pInstance->internalResize(data, size);
148 }
149
150 static void free(Data * data)
151 {
152 m_pInstance->internalFree(data);
153 }
154
155 static QPixmap * dummyPixmap();
156
158 {
159 m_pInstance->internalNotifyDelete(receiver);
160 }
161};
162
163#endif /* KVI_ANIMATEDPIXMAPCACHE_H_ */
Definition KviAnimatedPixmapCache.h:82
uint refs
Definition KviAnimatedPixmapCache.h:84
QString file
Definition KviAnimatedPixmapCache.h:86
Data(QString szFile)
Definition KviAnimatedPixmapCache.h:89
bool resized
Definition KviAnimatedPixmapCache.h:87
Data(Data &other)
Definition KviAnimatedPixmapCache.h:93
QSize size
Definition KviAnimatedPixmapCache.h:85
Definition KviAnimatedPixmapCache.h:51
FrameInfo(QPixmap *_pixmap, uint _delay)
Definition KviAnimatedPixmapCache.h:56
uint delay
Definition KviAnimatedPixmapCache.h:54
void detach()
Definition KviAnimatedPixmapCache.h:68
FrameInfo(const FrameInfo &source)
Definition KviAnimatedPixmapCache.h:62
QPixmap * pixmap
Definition KviAnimatedPixmapCache.h:53
Definition KviAnimatedPixmapCache.h:38
void internalNotifyDelete(KviAnimatedPixmapInterface *receiver)
Definition KviAnimatedPixmapCache.cpp:251
QMutex m_timerMutex
Definition KviAnimatedPixmapCache.h:112
static void free(Data *data)
Definition KviAnimatedPixmapCache.h:150
QMultiMap< long long, KviAnimatedPixmapInterface * > m_timerData
Definition KviAnimatedPixmapCache.h:115
Data * internalResize(Data *data, const QSize &size)
Definition KviAnimatedPixmapCache.cpp:111
void internalFree(Data *data)
Definition KviAnimatedPixmapCache.cpp:156
Data * internalLoad(const QString &szFile, int iWidth=0, int iHeight=0)
Definition KviAnimatedPixmapCache.cpp:68
static void scheduleFrameChange(uint delay, KviAnimatedPixmapInterface *receiver)
Definition KviAnimatedPixmapCache.h:135
KviAnimatedPixmapCache()
Definition KviAnimatedPixmapCache.cpp:36
void internalScheduleFrameChange(uint delay, KviAnimatedPixmapInterface *receiver)
Definition KviAnimatedPixmapCache.cpp:174
static void notifyDelete(KviAnimatedPixmapInterface *receiver)
Definition KviAnimatedPixmapCache.h:157
static Data * resize(Data *data, const QSize &size)
Definition KviAnimatedPixmapCache.h:145
static KviAnimatedPixmapCache * m_pInstance
Definition KviAnimatedPixmapCache.h:118
QMultiHash< QString, Data * > m_hCache
Definition KviAnimatedPixmapCache.h:114
virtual void timeoutEvent()
Definition KviAnimatedPixmapCache.cpp:188
static Data * load(const QString &szFileName, int iWidth=0, int iHeight=0)
Definition KviAnimatedPixmapCache.h:140
static void done()
Definition KviAnimatedPixmapCache.cpp:60
QTimer m_animationTimer
Definition KviAnimatedPixmapCache.h:116
QMutex m_cacheMutex
Definition KviAnimatedPixmapCache.h:111
Definition KviAnimatedPixmapInterface.h:30
#define i
Definition detector.cpp:74
This file contains compile time settings.
#define KVILIB_API
Definition kvi_settings.h:124
int init()
Definition winamp.cpp:118