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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* Copyright 2008 Arthur Renato Mello <arthur@mandriva.com>
* Copyright 2008 Gustavo Pichorim Boiko <boiko@mandriva.com>
*
* 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 3 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <QFileInfo>
#include <QPainter>
#include <QPixmap>
#include <QSvgRenderer>
#include <kiconloader.h>
#include <KStandardDirs>
#include <KConfig>
#include <KConfigGroup>
#include "imageloader.h"
ImageLoader::ImageLoader()
{
}
ImageLoader::~ImageLoader()
{
}
void ImageLoader::setCachePrefix(const QString &cachePrefix)
{
m_cachePath = cachePrefix
+ "/"
+ QString("%1X%2/").arg(m_targetSize.width())
.arg(m_targetSize.height())
+ "/";
}
QSize ImageLoader::getSVGSize(const QString &p_file)
{
QSvgRenderer svg(p_file);
return svg.defaultSize();
}
void ImageLoader::setBaseSize(const QSize &s)
{
m_baseSize = s;
}
void ImageLoader::setTargetSize(const QSize &s)
{
m_targetSize = s;
}
loadedPixmap ImageLoader::loadPixmap(const QString &p_filename, const QSize &p_size, const QPoint &p_pos)
{
loadedPixmap ret;
QString file;
QStringList list = p_filename.split(":", QString::SkipEmptyParts);
if(list.isEmpty())
return ret;
file = list.at(0);
QString item;
if(list.size() > 1)
item = list.at(1);
QFileInfo fileInfo(file);
QString fileCache = p_filename.right((p_filename.size()
- p_filename.lastIndexOf("/")) - 1);
fileCache += ".png";
QString confFile = KGlobal::dirs()->findResource("data", m_cachePath
+ "Cache.rc");
if(!confFile.isEmpty())
{
KConfig cfg(confFile, KConfig::SimpleConfig);
if(cfg.hasGroup(fileCache))
{
KConfigGroup group = cfg.group(fileCache);
QString pixmap = group.readEntry("Path", QString());
QPoint pos = group.readEntry("Position", QPoint());
QDateTime modified = group.readEntry("Modified", QDateTime());
if( !pixmap.isEmpty() &&
!pos.isNull() &&
modified.isValid() &&
fileInfo.lastModified() <= modified)
{
ret.pixmap.load(pixmap);
ret.pos = pos;
return ret;
}
}
}
if(p_size.isValid())
ret.pixmap = QPixmap(p_size);
ret.pos = p_pos;
if(file.endsWith(".svg")) //.svgz not working correctly
{
QSvgRenderer svg(file);
QSize svgSize = svg.defaultSize();
if(!item.isEmpty())
{
QRect rect = svg.boundsOnElement(item).toRect();
if(!m_targetSize.isEmpty())
{
QMatrix matrix;
matrix.scale(((double)m_targetSize.width() / svgSize.width()),
((double)m_targetSize.height() / svgSize.height()));
rect = matrix.mapRect(rect);
}
if(!p_size.isValid())
ret.pixmap = QPixmap(rect.size());
if(p_pos.isNull())
ret.pos = rect.topLeft();
}
else if(!p_size.isValid())
ret.pixmap = QPixmap(svgSize);
ret.pixmap.fill(Qt::transparent);
QPainter p(&ret.pixmap);
if(!item.isEmpty())
svg.render(&p, item);
else
svg.render(&p);
}
else
{
ret.pixmap.load(file);
if(p_size.isValid() && p_size != ret.pixmap.size())
{
if(p_size.isValid())
ret.pixmap = ret.pixmap.scaled(p_size, Qt::KeepAspectRatio,
Qt::SmoothTransformation);
}
}
QString cache = KGlobal::dirs()->locateLocal("data", m_cachePath);
KConfig cfg(cache + "Cache.rc", KConfig::SimpleConfig);
KConfigGroup group = cfg.group(fileCache);
group.writeEntry("Path", cache + fileCache);
group.writeEntry("Position", ret.pos);
group.writeEntry("Modified", fileInfo.lastModified());
ret.pixmap.save(cache + fileCache, "PNG");
return ret;
}
QPixmap ImageLoader::loadBackground(const QString &p_background)
{
return loadPixmap(p_background, m_targetSize).pixmap;
}
loadedPixmap ImageLoader::loadItem(const QString &p_item, QPoint p_pos, const bool &p_useIconSet)
{
loadedPixmap ret;
if(!p_pos.isNull())
{
if(!m_baseSize.isEmpty() && !m_targetSize.isEmpty())
{
QMatrix matrix;
matrix.scale(((double)m_targetSize.width() / m_baseSize.width()),
((double)m_targetSize.height() / m_baseSize.height()));
p_pos = matrix.map(p_pos);
}
}
if(p_useIconSet)
{
ret.pixmap = DesktopIcon(p_item);
ret.pos = p_pos;
return ret;
}
ret = loadPixmap(p_item, QSize(), p_pos);
return ret;
}
QRect ImageLoader::loadStatusRect(const QString &p_status)
{
loadedPixmap pix = loadPixmap(p_status);
return(QRect(pix.pos, pix.pixmap.size()));
}
|