-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathimagefactory.cpp
177 lines (147 loc) · 4.52 KB
/
imagefactory.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/****************************************************************************
* EZ Viewer
* Copyright (C) 2013 huangezhao. CHINA.
* Contact: huangezhao ([email protected])
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***************************************************************************/
#include "imagefactory.h"
#include "config.h"
#include "imagewrapper.h"
#include "toolkit.h"
#include <QCoreApplication>
#include <QRunnable>
#include <QThreadPool>
/* list:
* first one hold curCache for PicManager,
* second one is for pre-reading (if enabled),
* others are caches for file that had been viewed.
*/
QList<ImageWrapper *> ImageFactory::list;
int ImageFactory::CacheNumber = 0;
class Runnable : public QRunnable
{
public:
Runnable(ImageWrapper * iw, const QString &filePath, bool preReading = true)
: image(iw), path(filePath), isPreReading(preReading) {}
void run() {
if (image)
image->load(path, isPreReading);
}
private:
ImageWrapper *image;
QString path;
bool isPreReading;
};
ImageWrapper * ImageFactory::newOrReuseImage()
{
int total = 1 + (Config::enablePreReading() ? 1 : 0) + CacheNumber;
ImageWrapper *image;
if(list.size() < total){
image = new ImageWrapper();
}else{
image = list.at(total - 1);
waitForImageReady(image);
list.removeAt(total - 1);
image->setReady(false);
image->setHashCode(ImageWrapper::HASH_INVALID);
}
return image;
}
ImageWrapper * ImageFactory::findImageByHash(uint hash)
{
foreach(ImageWrapper *image, list){
if(image->getHashCode() == hash){
list.removeOne(image);
return image;
}
}
return NULL;
}
void ImageFactory::waitForImageReady(ImageWrapper *image)
{
while(!image->getReady()){
// qApp->processEvents(QEventLoop::AllEvents);
// wait for pre-reading in another thread
}
}
ImageWrapper * ImageFactory::getImageWrapper(const QString &filePath)
{
uint hash = filePath.isEmpty() ? ImageWrapper::HASH_INVALID :
ToolKit::getFileHash(filePath);
ImageWrapper *image;
if((image = findImageByHash(hash))){
list.prepend(image);
// TODO: update first frame of svg animation format here (if it has been shown before).
return image;
}
image = newOrReuseImage();
image->setHashCode(hash);
list.prepend(image);
if (hash == ImageWrapper::HASH_INVALID) {
image->setReady(true);
} else {
QThreadPool::globalInstance()->start(new Runnable(image, filePath, false));
}
return image;
}
void ImageFactory::preReading(const QString &filePath)
{
if(!Config::enablePreReading())
return;
uint hash = ToolKit::getFileHash(filePath);
ImageWrapper *image;
if((image = findImageByHash(hash))){
list.insert(1, image); /// assert(list.size());
return;
}
image = newOrReuseImage();
image->setHashCode(hash);
list.insert(1, image); /// assert(list.size());
if (hash == ImageWrapper::HASH_INVALID) {
image->setReady(true);
} else {
QThreadPool::globalInstance()->start(new Runnable(image, filePath));
}
}
void ImageFactory::freeAllCache()
{
QThreadPool::globalInstance()->waitForDone();
foreach(ImageWrapper *image, list)
freeImage(image);
}
void ImageFactory::freeImage(ImageWrapper *image)
{
waitForImageReady(image);
delete image;
}
void ImageFactory::cacheSizeAdjusted()
{
int total = 1 + (Config::enablePreReading() ? 1 : 0) + CacheNumber;
while(list.size() > total){
freeImage(list.last());
list.removeLast();
}
}
void ImageFactory::setCacheNumber(int val)
{
if(val < 0 || CacheNumber == val) return;
CacheNumber = val;
cacheSizeAdjusted();
}
void ImageFactory::setPreReadingEnabled(bool /*enabled*/)
{
cacheSizeAdjusted();
}