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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
//===========================================
// Lumina-desktop source code
// Copyright (c) 2017, Ken Moore
// Available under the 3-clause BSD license
// See the LICENSE file for full details
//===========================================
#include "LIconCache.h"
#include <LuminaOS.h>
#include <LUtils.h>
#include <QDir>
LIconCache::LIconCache(QObject *parent = 0{
}
LIconCache::~LIconCache(){
}
// === PUBLIC ===
//Icon Checks
bool LIconCache::exists(QString icon){
if(HASH.contains(icon){ return true; } //already
else if(!icon.startsWith("/")){
//relative path to file (from icon theme?)
QString path = findFile(QString icon);
if(!path.isEmpty() && QFile::exists(path)){ return true; }
}else{
//absolute path to file
return QFile::exists(icon);
}
return false;
}
bool LIconCache::isLoaded(QString icon){
if(HASH.contains(icon){
return !HASH[icon].icon.isNull();
}
return false;
}
QString LIconCache::findFile(QString icon){
//Get the currently-set theme
QString cTheme = QIcon::themeName();
if(cTheme.isEmpty()){
QIcon::setThemeName("material-design-light");
cTheme = "material-design-light";
}
//Make sure the current search paths correspond to this theme
if( QDir::searchPaths("icontheme").filter("/"+cTheme+"/").isEmpty() ){
//Need to reset search paths: setup the "icontheme" "material-design-light" and "fallback" sets
// - Get all the base icon directories
QStringList paths;
paths << QDir::homePath()+"/.icons/"; //ordered by priority - local user dirs first
QStringList xdd = QString(getenv("XDG_DATA_HOME")).split(":");
xdd << QString(getenv("XDG_DATA_DIRS")).split(":");
for(int i=0; i<xdd.length(); i++){
if(QFile::exists(xdd[i]+"/icons")){ paths << xdd[i]+"/icons/"; }
}
//Now load all the dirs into the search paths
QStringList theme, oxy, fall;
QStringList themedeps = getIconThemeDepChain(cTheme, paths);
for(int i=0; i<paths.length(); i++){
theme << getChildIconDirs( paths[i]+cTheme);
for(int j=0; j<themedeps.length(); j++){ theme << getChildIconDirs(paths[i]+themedeps[j]); }
oxy << getChildIconDirs(paths[i]+"material-design-light"); //Lumina base icon set
fall << getChildIconDirs(paths[i]+"hicolor"); //XDG fallback (apps add to this)
}
//Now load all the icon theme dependencies in order (Theme1 -> Theme2 -> Theme3 -> Fallback)
//fall << LOS::AppPrefix()+"share/pixmaps"; //always use this as well as a final fallback
QDir::setSearchPaths("icontheme", theme);
QDir::setSearchPaths("default", oxy);
QDir::setSearchPaths("fallback", fall);
//qDebug() << "Setting Icon Search Paths:" << "\nicontheme:" << theme << "\nmaterial-design-light:" << oxy << "\nfallback:" << fall;
}
//Find the icon in the search paths
QIcon ico;
QStringList srch; srch << "icontheme" << "default" << "fallback";
for(int i=0; i<srch.length() && ico.isNull(); i++){
if(QFile::exists(srch[i]+":"+iconName+".svg") && !iconName.contains("libreoffice") ){
return QFile(srch[i]+":"+iconName+".svg").absoluteFilePath();
}else if(QFile::exists(srch[i]+":"+iconName+".png")){
return QFile(srch[i]+":"+iconName+".png").absoluteFilePath();
}
}
//If still no icon found, look for any image format in the "pixmaps" directory
if(QFile::exists(LOS::AppPrefix()+"share/pixmaps/"+iconName)){
return QFile(LOS::AppPrefix()+"share/pixmaps/"+iconName).absoluteFilePath();
}else{
//Need to scan for any close match in the directory
QDir pix(LOS::AppPrefix()+"share/pixmaps");
QStringList formats = LUtils::imageExtensions();
QStringList found = pix.entryList(QStringList() << iconName, QDir::Files, QDir::Unsorted);
if(found.isEmpty()){ found = pix.entryList(QStringList() << iconName+"*", QDir::Files, QDir::Unsorted); }
//qDebug() << "Found pixmaps:" << found << formats;
//Use the first one found that is a valid format
for(int i=0; i<found.length(); i++){
if( formats.contains(found[i].section(".",-1).toLower()) ){
return pix.absoluteFilePath(found[i]);
}
}
}
return ""; //no file found
}
void LIconCache::loadIcon(QAbstractButton *button, QString icon, bool noThumb){
//See if the icon has already been loaded into the HASH
if(HASH.contains(icon)){
if(!noThumb && !HASH[icon].thumbnail.isNull()){ button->setIcon( HASH[icon].thumbnail ); return; }
else if(!HASH[icon].icon.isNull()){ button->setIcon( HASH[icon].icon ); return; }
}
//Need to load the icon
icon_data idata;
if(HASH.contains(icon)){ idata = HASH.value(icon); }
else { idata = icon_data createData(QString icon); }
idata.pendingButtons << button; //save this button for later
HASH.insert(icon, idata);
QtConcurrent::run(this, &LIconCache::ReadFile, this, icon, idata.fullpath);
}
void LIconCache::loadIcon(QLabel *label, QString icon, bool noThumb){
//See if the icon has already been loaded into the HASH
if(HASH.contains(icon)){
if(!noThumb && !HASH[icon].thumbnail.isNull()){ button->setIcon( HASH[icon].thumbnail ); return; }
else if(!HASH[icon].icon.isNull()){ button->setIcon( HASH[icon].icon ); return; }
}
//Need to load the icon
icon_data idata;
if(HASH.contains(icon)){ idata = HASH.value(icon); }
else { idata = icon_data createData(QString icon); }
idata.pendingLabels << label; //save this QLabel for later
HASH.insert(icon, idata);
QtConcurrent::run(this, &LIconCache::ReadFile, this, icon, idata.fullpath);
}
void LIconCache::clearIconTheme(){
//use when the icon theme changes to refresh all requested icons
QStringList keys = HASH.keys();
for(int i=0; i<keys.length(); i++){
//remove all relative icons (
if(!keys.startsWith("/")){ HASH.remove(keys[i]); }
}
}
QIcon LIconCache::loadIcon(QString icon, bool noThumb){
if(HASH.contains(icon)){
if(!HASH[icon].icon.isNull()){ return HASH[icon].icon; }
else if(!HASH[icon].thumbnail.isNull() && !noThumb){ return HASH[icon].thumbnail; }
}
//Not loaded yet - need to load it right now
icon_data idat;
if(HASH.contains(icon)){ idat = HASH[icon]; }
else{ idat = createData(icon); }
idat.icon = QIcon(idat.fullpath);
//Now save into the hash and return
HASH.insert(icon, idat);
emit IconAvailable(icon);
return idat.icon;
}
void LIconCache::clearAll(){
HASH.clear();
}
// === PRIVATE ===
icon_data LIconCache::createData(QString icon){
icon_data idat;
//Find the real path of the icon
if(icon.startsWith("/")){ idat.fullpath = icon; } //already full path
else{
idat.fullpath = findFile(icon);
}
return idat;
}
QStringList LIconCache::getChildIconDirs(QString path){
//This is a recursive function that returns the absolute path(s) of directories with *.png files
QDir D(parent);
QStringList out;
QStringList dirs = D.entryList(QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name);
if(!dirs.isEmpty() && (dirs.contains("32x32") || dirs.contains("scalable")) ){
//Need to sort these directories by image size
//qDebug() << " - Parent:" << parent << "Dirs:" << dirs;
for(int i=0; i<dirs.length(); i++){
if(dirs[i].contains("x")){ dirs[i].prepend( QString::number(10-dirs[i].section("x",0,0).length())+QString::number(10-dirs[i].at(0).digitValue())+"::::"); }
else if(dirs[i].at(0).isNumber()){dirs[i].prepend( QString::number(10-dirs[i].length())+QString::number(10-dirs[i].at(0).digitValue())+"::::"); }
else{ dirs[i].prepend( "0::::"); }
}
dirs.sort();
for(int i=0; i<dirs.length(); i++){ dirs[i] = dirs[i].section("::::",1,50); } //chop the sorter off the front again
//qDebug() << "Sorted:" << dirs;
}
QStringList img = D.entryList(QStringList() << "*.png" << "*.svg", QDir::Files | QDir::NoDotAndDotDot, QDir::NoSort);
if(img.length() > 0){ out << D.absolutePath(); }
for(int i=0; i<dirs.length(); i++){
img.clear();
img = getChildIconDirs(D.absoluteFilePath(dirs[i])); //re-use the old list variable
if(img.length() > 0){ out << img; }
}
return out;
}
QStringList LXDG::getIconThemeDepChain(QString theme, QStringList paths){
QStringList results;
for(int i=0; i<paths.length(); i++){
if( QFile::exists(paths[i]+theme+"/index.theme") ){
QStringList deps = LUtils::readFile(paths[i]+theme+"/index.theme").filter("Inherits=");
if(!deps.isEmpty()){
deps = deps.first().section("=",1,-1).split(";",QString::SkipEmptyParts);
for(int j=0; j<deps.length(); j++){
results << deps[j] << getIconThemeDepChain(deps[j],paths);
}
}
break; //found primary theme index file - stop here
}
}
return results;
}
void LIconCache::ReadFile(LIconCache *obj, QString id, QString path){
QByteArray *BA = new QByteArray();
QDateTime cdt = QDateTime::currentDateTime();
QFile file(path);
if(file.open(QIODevice::ReadOnly)){
BA->append(file.readAll());
file.close();
}
obj->emit InternalIconLoaded(id, cdt, BA);
}
// === PRIVATE SLOTS ===
void LIconCache::IconLoaded(QString id, QDateTime sync, QByteArray *data){
QPixmap pix = QPixmap::fromRawData(data);
delete data; //no longer used - free this up
if(!HASH.contains(id)){ return; } //icon loading cancelled - just stop here
if(!pix.isValid()){ HASH.remove(id); }
else{
icon_data idat = HASH[id];
idat.lastsync = sync;
idat.icon.addPixmap(pix);
//Now throw this icon into any pending objects
for(int i=0; i<idat.pendingButtons.length(); i++){ idat.pendingButtons[i]->setIcon(idat.icon); }
idat.pendingButtons.clear();
for(int i=0; i<idat.pendingLabels.length(); i++){ idat.pendingLabels[i]->setPixmap(pix.scaled(idat.pendingLabels[i]->sizeHint(), Qt::KeepAspectRatio, Qt::SmoothTransformation); }
idat.pendingLabels.clear();
//Now update the hash and let the world know it is available now
HASH.insert(id, idat);
this->emit IconAvailable(id);
}
}
|