//=========================================== // 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 #include #include 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 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; isetIcon( 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 0){ out << D.absolutePath(); } for(int i=0; i 0){ out << img; } } return out; } QStringList LXDG::getIconThemeDepChain(QString theme, QStringList paths){ QStringList results; for(int i=0; iappend(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; isetIcon(idat.icon); } idat.pendingButtons.clear(); for(int i=0; isetPixmap(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); } }