-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqcimagepool.cpp
157 lines (108 loc) · 2.95 KB
/
qcimagepool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* QuickCross Project
* License: APACHE-2.0
* Author: Ben Lau
* Project Site: https://github.com/benlau/quickcross
*
*/
#include <QCoreApplication>
#include <QMutexLocker>
#include <QFileInfo>
#include "qcimagepool.h"
static QString removeSuffix(const QString& fileName) {
QFileInfo info(fileName);
return info.baseName();
}
/*! \class QCImagePool
\inmodule QuickCross
QCImagePool is a singleton component for holding image data.
You may insert images to this object manually, or uses QCImageLoader to load a batch of images from a folder.
Then the load images can be used by component like QCImageProvider.
All the functions within QCImagePool are thread safe.
*/
QCImagePool::QCImagePool(QObject* parent) : QObject(parent)
{
}
/*! \fn int QCImagePool::count() const
Return number of images in QCImagePool
*/
int QCImagePool::count() const
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
return m_images.count();
}
int QCImagePool::byteCount() const
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
int res = 0;
QMap<QString, QImage>::const_iterator i = m_images.constBegin();
while (i != m_images.constEnd()) {
res += i.value().byteCount();
++i;
}
return res;
}
/*! \fn void QCImagePool::clear()
Remove all the images
*/
void QCImagePool::clear()
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
m_images.clear();
}
/*! \fn void QCImagePool::contains(const QString &key) const
Return TRUE if the image pool contains the image with key.
*/
bool QCImagePool::contains(const QString &key) const
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
return m_images.contains(normalizeKey(key));
}
/*! \fn QImage QCImagePool::image(const QString &key) const
Get the image with key. If it is not existed, it will return an empty image.
*/
QImage QCImagePool::image(const QString &key) const
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
QImage res;
if (m_images.contains(key)) {
res = m_images[key];
}
return res;
}
/*! \fn void QCImagePool::insert(const QString &key, const QImage &image)
Insert the image with key into the image pool.
*/
void QCImagePool::insert(const QString &key, const QImage &image)
{
QMutexLocker locker(&mutex);
Q_UNUSED(locker);
m_images[key] = image;
}
/*! \fn QCImagePool *QCImagePool::instance()
Returns the global QCImagePool instance.
*/
QCImagePool *QCImagePool::instance()
{
static QCImagePool* m_instance = 0;
if (!m_instance) {
QCoreApplication* app = QCoreApplication::instance();
m_instance = new QCImagePool(app);
}
return m_instance;
}
/*! \fn QString QCImagePool::normalizeKey(const QString &key)
Remove suffix and convert to lowercase.
*/
QString QCImagePool::normalizeKey(const QString &key)
{
return removeSuffix(key).toLower();
}
static void preload() {
QCImagePool::instance();
}
Q_COREAPP_STARTUP_FUNCTION(preload)