⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 iconcache.cc

📁 c++的guiQt做的开发
💻 CC
字号:
/** @file IconCache - class responsible for loading and caching icons<br> @author Martin Petricek*/#include "iconcache.h"#include "settings.h"#include "util.h"#include <assert.h>#include <QFile>#include <QIcon>#include <QMap>#include <QPixmap>#include <QString>#include <QStringList>#include <QList>namespace gui {using namespace std;using namespace util;/** Number of concurrently existing icon cache instances. When last instance is deleted, cached data is deleted too*/int iconCacheInstances=0;//Internal types /** Icon Cache type: mapping from icon name to pixmap */ typedef QMap<QString, QPixmap*> IconCacheData; /** Icon Sets Cache type: mapping from icon name to icon set */ typedef QMap<QString, QIcon*> IconSetsData;//Internal icon cache data shared by all icon cache instances /** List with paths to application icons */ QStringList iconPath; /** Name of current icon style */ QString iconStyleName; /** Cache storing loaded icons */ IconCacheData iconCache; /** Cache storing created icons sets*/ IconSetsData setCache; /** Name of settings with icon paths */ QString pathSettingsName; /** Name of settings with icon style name */ QString styleSettingsName;/** Default constructor of IconCache Note: parameters are taken into account only when creating first cache instance. Subsequent cache instances uses same parameters as the first @param pathSettings Name of setting that contains paths to look for icons @param styleSettings Name of setting that contains name of current icon style*/IconCache::IconCache(const QString &pathSettings/*=QString("icon")*/, const QString &styleSettings/*=QString("icon/theme/current")*/) { if (iconCacheInstances>0) {  iconCacheInstances++;  return;//Initialization already done } iconCacheInstances++; pathSettingsName=pathSettings; styleSettingsName=styleSettings; readSettings();}/** Read settings from setting names specified in constructor*/void IconCache::readSettings() { //Read icon path setPath(globalSettings->readPath(pathSettingsName)); //Read icon style setTheme(globalSettings->read(styleSettingsName));}/** Sets new set of paths where to look for icons. Changes paths globally for all cache instances Icon cache is not flushed automatically after setting new paths - you have to flush it manually if desired. @param iconPaths new set of paths to use */void IconCache::setPath(const QStringList &iconPaths) { iconPath=iconPaths;}/** Sets new icon style. Changes style globally for all cache instances Icon cache is flushed if new style name is different from the old one Note: "default", "" and QString::null all refer to the default style @param iconTheme name of new icon theme */void IconCache::setTheme(const QString &iconTheme) { QString tn=iconTheme; if (tn=="default") tn=QString::null; if (tn=="") tn=QString::null; if (tn!=iconStyleName) {  iconStyleName=tn;  flush(); }}/** Given name of the icon, finds and returns full path to the icon, considering all relevant settings (icon path ..) @param name Name of icon @return Full filename of the icon*/QString IconCache::getIconFile(const QString &name) { if (name.startsWith("/")) { //absolute path -> no change  return name; } QString absName; if (!iconStyleName.isNull()) {  //Check icons from style first  for(QStringList::Iterator it=iconPath.begin();it!=iconPath.end();++it) {   //Check each path in icon path   absName=*it+"/"+iconStyleName+"/"+name;   if (QFile::exists(absName)) return absName;  } } //Icon from style not found (or no icon style used) - check default icons for(QStringList::Iterator it=iconPath.begin();it!=iconPath.end();++it) {  //Check each path in icon path  absName=*it+"/"+name;  if (QFile::exists(absName)) return absName; } warn("Icon file not found: "+name); return name;}/** Returns icon with given name, loading if necessary and caching for later use<br> Returns NULL if the icon cannot be loaded. Warning: Pixmap will be deleted when last instance of iconcache is deleted. If it is desired for application to hold the pixmap after iconcache is destroyed, it should make a copy @param name Name of icon to get @return Pixmap containing specified icon*/QPixmap* IconCache::getIcon(const QString &name) { //Look in cache first if (iconCache.contains(name)) return iconCache[name]; //Not found in cache - try to load QString absName=getIconFile(name); QFile f(absName); if (!f.open(QIODevice::ReadOnly)) {  //file not found or unreadable or whatever ...  warn("File not found:" + absName);  return NULL; } QByteArray qb=f.readAll(); f.close(); //File is OK -> create pixmap QPixmap *pix=new QPixmap(); pix->loadFromData (qb,0,0); iconCache[name]=pix; return pix;}/** Returns icon set with given name, creating from corresponding icon if necessary and caching for later use<br> Returns NULL if the icon in the icon set cannot be loaded. @param name Name of icon set to get @return IconSet containing specified icon set*/QIcon* IconCache::getIconSet(const QString &name) { //Look in cache first if (setCache.contains(name)) return setCache[name]; //Not found in cache - try to create QPixmap* pix=getIcon(name); if (!pix) return NULL; //File not found ... //Pixmap is OK QIcon *ico=new QIcon(*pix); setCache[name]=ico; return ico;}/** Flush all data in the icon cache */void IconCache::flush() { QList<QString> pixmaps=iconCache.keys(); //Delete all pixmaps from cache for (QList<QString>::Iterator it=pixmaps.begin();it!=pixmaps.end();++it) {  QPixmap *rm=iconCache[*it];  delete rm; } iconCache.clear(); QList<QString> iconSets=setCache.keys(); //Delete all icon sets from cache for (QList<QString>::Iterator it=iconSets.begin();it!=iconSets.end();++it) {  QIcon *rm=setCache[*it];  delete rm; } setCache.clear();}/** Reload settings, then flush all data in the icon cache */void IconCache::reload() { readSettings(); flush();}/** default destructor */IconCache::~IconCache() { assert(iconCacheInstances>=1); iconCacheInstances--; if (iconCacheInstances<=0) {  //This was last instance -> free the internal data  flush(); }}} // namespace gui

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -