1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
25 #include "iconloader.h"
27 IconLoader IconLoader::_iconLoader;
28 int IconLoader::_groupSize[] = { 48, 22, 22, 16, 32, 22 }; // default sizes taken from Oxygen
30 IconLoader *IconLoader::global() {
31 // Workaround: the static _iconLoader might be initialized before the resources it needs
32 // This way, first call to global() will init it by setting the theme
33 if(_iconLoader.theme().isEmpty())
34 _iconLoader.setTheme("oxygen");
38 IconLoader::IconLoader(QObject *parent) : QObject(parent) {
40 // setTheme("oxygen");
43 IconLoader::~IconLoader() {
47 void IconLoader::setTheme(const QString &theme) {
49 // check which dirs could contain themed icons
50 _themedIconDirNames.clear();
51 _plainIconDirNames.clear();
53 QStringList dataDirNames = QString(qgetenv("XDG_DATA_DIRS")).split(':');
55 // System theme in $data/icons/$theme
56 foreach(QString dir, dataDirNames) {
57 path = QString("%1/icons/%2").arg(dir, theme);
58 if(QFile::exists(path))
59 _themedIconDirNames.append(path);
61 // Resource for system theme :/icons/$theme
62 path = QString(":/icons/%2");
63 if(QFile::exists(path))
64 _themedIconDirNames.append(path);
66 // Own icons in $data/apps/quassel/icons/hicolor
67 // Also, plain icon dirs $data/apps/quassel/pics
68 foreach(QString dir, dataDirNames) {
69 path = QString("%1/apps/quassel/icons/hicolor").arg(dir);
70 if(QFile::exists(path))
71 _themedIconDirNames.append(path);
72 path = QString("%1/apps/quassel/pics").arg(dir);
73 if(QFile::exists(path))
74 _plainIconDirNames.append(path);
77 // Same for :/icons/hicolor and :/pics
78 path = QString(":/icons/hicolor");
79 if(QFile::exists(path))
80 _themedIconDirNames.append(path);
82 path = QString(":/pics");
83 if(QFile::exists(path))
84 _plainIconDirNames.append(path);
87 // TODO: optionally implement cache (speed/memory tradeoff?)
88 QPixmap IconLoader::loadIcon(const QString &name, IconLoader::Group group, int size) {
89 if(group < 0 || group >= LastGroup) {
90 qWarning() << "Invalid icon group!";
94 size = _groupSize[group];
96 QString path = findIconPath(name, size);
97 if(path.isEmpty()) return QPixmap();
100 return QPixmap(path);
103 QString IconLoader::findIconPath(const QString &name, int size) {
104 QString fname = QString("%1.png").arg(name); // we only support PNG so far
105 // First, look for a themed icon... we don't do anything fancy here, only exact match for both name and size
106 foreach(QString basedir, _themedIconDirNames) {
107 QDir sizedir(QString("%1/%2x%2").arg(basedir).arg(QString::number(size)));
108 if(sizedir.exists()) {
109 // ignore context, i.e. scan all subdirs
110 QStringList contextdirs = sizedir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
111 foreach(QString dir, contextdirs) {
112 QString path = QString("%1/%2/%3").arg(sizedir.absolutePath(), dir, fname);
113 if(QFile::exists(path)) return path;
117 // Now check the plain dirs
118 foreach(QString dir, _plainIconDirNames) {
119 QString path = QString("%1/%2").arg(dir, name);
120 if(QFile::exists(path)) return path;
126 // Convenience constructors
128 QPixmap DesktopIcon(const QString& name, int force_size) {
129 IconLoader *loader = IconLoader::global();
130 return loader->loadIcon(name, IconLoader::Desktop, force_size);
133 QPixmap BarIcon(const QString& name, int force_size) {
134 IconLoader *loader = IconLoader::global();
135 return loader->loadIcon(name, IconLoader::Toolbar, force_size);
138 QPixmap MainBarIcon(const QString& name, int force_size) {
139 IconLoader *loader = IconLoader::global();
140 return loader->loadIcon(name, IconLoader::MainToolbar, force_size);
143 QPixmap SmallIcon(const QString& name, int force_size) {
144 IconLoader *loader = IconLoader::global();
145 return loader->loadIcon(name, IconLoader::Small, force_size);