KVIrc  4.9.2
DeveloperAPIs
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 <QMutex>
31 #include <QPixmap>
32 #include <QMultiHash>
33 #include <QMultiMap>
34 #include <QTimer>
35 
36 class KVILIB_API KviAnimatedPixmapCache : public QObject
37 {
38  Q_OBJECT
39 public:
40  /*
41  * This subclass represents simple structure
42  * to store animated frame data: cached pixmap, and next frame delay.
43  *
44  * It provides copyconstructor, which makes possible to simple
45  * assign two containers with this classes.
46  *
47  * All data will be duplicated in such case.
48  */
49  class FrameInfo
50  {
51  public:
52  QPixmap * pixmap; //frame pixmap
53  uint delay; //next frame delay
54 
55  FrameInfo(QPixmap * _pixmap, uint _delay)
56  {
57  pixmap = _pixmap;
58  delay = _delay;
59  }
60 
61  FrameInfo(const FrameInfo & source)
62  {
63  pixmap = source.pixmap;
64  delay = source.delay;
65  }
66 
67  void detach()
68  {
69  pixmap = new QPixmap(*pixmap);
70  }
71  };
72 
73  /*
74  * This helper class represents data structure
75  * for storing frames.
76  *
77  * It adds references counter and
78  * mutex, to provide thread-safety.
79  */
80  class Data : public QList<FrameInfo>
81  {
82  public:
83  uint refs; //references count
84  QSize size; //size of the pixmaps
85  QString file; //just to speedup the cache
86  bool resized;
87 
88  Data(QString szFile) : QList<FrameInfo>(), refs(0), file(szFile), resized(false)
89  {
90  }
91 
92  Data(Data & other) : QList<FrameInfo>(other), refs(0), file(other.file), resized(false)
93  {
94  for(int i = 0; i < count(); i++)
95  {
96  this->operator[](i).detach();
97  }
98  }
99  };
100 
101 protected:
102  //
103  // This class is a singleton.
104  // It can't be created directly
105  //
107  virtual ~KviAnimatedPixmapCache();
108 
109 protected:
110  mutable QMutex m_cacheMutex;
111  mutable QMutex m_timerMutex;
112 
113  QMultiHash<QString, Data *> m_hCache;
114  QMultiMap<long long, KviAnimatedPixmapInterface *> m_timerData;
116 
118 
119 protected:
120  Data * internalLoad(const QString & szFile, int iWidth = 0, int iHeight = 0);
121  Data * internalResize(Data * data, const QSize & size);
122  void internalFree(Data * data);
123 
124  void internalScheduleFrameChange(uint delay, KviAnimatedPixmapInterface * receiver);
125  void internalNotifyDelete(KviAnimatedPixmapInterface * receiver);
126 
127 protected slots:
128  virtual void timeoutEvent();
129 
130 public:
131  static void init();
132  static void done();
133 
134  static void scheduleFrameChange(uint delay, KviAnimatedPixmapInterface * receiver)
135  {
136  m_pInstance->internalScheduleFrameChange(delay, receiver);
137  }
138 
139  static Data * load(const QString & szFileName, int iWidth = 0, int iHeight = 0)
140  {
141  return m_pInstance->internalLoad(szFileName, iWidth, iHeight);
142  }
143 
144  static Data * resize(Data * data, const QSize & size)
145  {
146  return m_pInstance->internalResize(data, size);
147  }
148 
149  static void free(Data * data)
150  {
151  m_pInstance->internalFree(data);
152  }
153 
154  static QPixmap * dummyPixmap();
155 
156  static void notifyDelete(KviAnimatedPixmapInterface * receiver)
157  {
158  m_pInstance->internalNotifyDelete(receiver);
159  }
160 };
161 
162 #endif /* KVI_ANIMATEDPIXMAPCACHE_H_ */
FrameInfo(QPixmap *_pixmap, uint _delay)
Definition: KviAnimatedPixmapCache.h:55
static Data * resize(Data *data, const QSize &size)
Definition: KviAnimatedPixmapCache.h:144
Definition: KviAnimatedPixmapCache.h:49
static void scheduleFrameChange(uint delay, KviAnimatedPixmapInterface *receiver)
Definition: KviAnimatedPixmapCache.h:134
Definition: KviAnimatedPixmapInterface.h:30
void detach()
Definition: KviAnimatedPixmapCache.h:67
QMutex m_cacheMutex
Definition: KviAnimatedPixmapCache.h:110
static KviAnimatedPixmapCache * m_pInstance
Definition: KviAnimatedPixmapCache.h:117
static void notifyDelete(KviAnimatedPixmapInterface *receiver)
Definition: KviAnimatedPixmapCache.h:156
#define i
Definition: detector.cpp:73
QMultiHash< QString, Data * > m_hCache
Definition: KviAnimatedPixmapCache.h:113
static Data * load(const QString &szFileName, int iWidth=0, int iHeight=0)
Definition: KviAnimatedPixmapCache.h:139
void done()
Definition: KviKvs.cpp:50
QString file
Definition: KviAnimatedPixmapCache.h:85
FrameInfo(const FrameInfo &source)
Definition: KviAnimatedPixmapCache.h:61
bool resized
Definition: KviAnimatedPixmapCache.h:86
static void free(Data *data)
Definition: KviAnimatedPixmapCache.h:149
Data(Data &other)
Definition: KviAnimatedPixmapCache.h:92
QSize size
Definition: KviAnimatedPixmapCache.h:84
uint refs
Definition: KviAnimatedPixmapCache.h:83
Definition: KviAnimatedPixmapCache.h:80
QMultiMap< long long, KviAnimatedPixmapInterface * > m_timerData
Definition: KviAnimatedPixmapCache.h:114
QMutex m_timerMutex
Definition: KviAnimatedPixmapCache.h:111
Data(QString szFile)
Definition: KviAnimatedPixmapCache.h:88
int init()
Definition: winamp.cpp:118
uint delay
Definition: KviAnimatedPixmapCache.h:53
This file contains compile time settings.
QTimer m_animationTimer
Definition: KviAnimatedPixmapCache.h:115
#define KVILIB_API
Definition: kvi_settings.h:125
Definition: KviAnimatedPixmapCache.h:36
QPixmap * pixmap
Definition: KviAnimatedPixmapCache.h:52