1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
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 ***************************************************************************/
23 #include <QCoreApplication>
28 #include "iconloader.h"
32 IconLoader IconLoader::_iconLoader;
33 int IconLoader::_groupSize[] = { 48, 22, 22, 16, 32, 22 }; // default sizes taken from Oxygen
35 IconLoader *IconLoader::global() {
36 // Workaround: the static _iconLoader might be initialized before the resources it needs
37 // This way, first call to global() will init it by setting the theme
38 if(_iconLoader.theme().isEmpty())
39 _iconLoader.setTheme("oxygen");
43 IconLoader::IconLoader(QObject *parent) : QObject(parent) {
45 // setTheme("oxygen");
48 IconLoader::~IconLoader() {
52 void IconLoader::setTheme(const QString &theme) {
54 // check which dirs could contain themed icons
55 _themedIconDirNames.clear();
56 _plainIconDirNames.clear();
58 QStringList dataDirNames = Quassel::dataDirPaths();
60 // System theme in $data/icons/$theme
61 foreach(QString dir, dataDirNames) {
62 path = QString("%1/icons/%2").arg(dir, theme);
63 if(QFile::exists(path))
64 _themedIconDirNames.append(path);
66 // Resource for system theme :/icons/$theme
67 path = QString(":/icons/%1").arg(theme);
68 if(QFile::exists(path))
69 _themedIconDirNames.append(path);
71 // Own icons in $data/apps/quassel/icons/hicolor
72 // Also, plain icon dirs $data/apps/quassel/pics
73 foreach(QString dir, dataDirNames) {
74 path = QString("%1/icons/hicolor").arg(dir);
75 if(QFile::exists(path))
76 _themedIconDirNames.append(path);
77 path = QString("%1/apps/quassel/pics").arg(dir);
78 if(QFile::exists(path))
79 _plainIconDirNames.append(path);
82 // Same for :/icons/hicolor and :/pics
83 path = QString(":/icons/hicolor");
84 if(QFile::exists(path))
85 _themedIconDirNames.append(path);
87 path = QString(":/pics");
88 if(QFile::exists(path))
89 _plainIconDirNames.append(path);
92 // TODO: optionally implement cache (speed/memory tradeoff?)
93 QPixmap IconLoader::loadIcon(const QString &name, IconLoader::Group group, int size) {
94 if(group < 0 || group >= LastGroup) {
95 qWarning() << "Invalid icon group!";
99 size = _groupSize[group];
101 QString path = findIconPath(name, size);
102 if(path.isEmpty()) return QPixmap();
105 return QPixmap(path);
108 QString IconLoader::findIconPath(const QString &name, int size) {
109 QString fname = QString("%1.png").arg(name); // we only support PNG so far
110 // First, look for a themed icon... we don't do anything fancy here, only exact match for both name and size
111 foreach(QString basedir, _themedIconDirNames) {
112 QDir sizedir(QString("%1/%2x%2").arg(basedir).arg(QString::number(size)));
113 if(sizedir.exists()) {
114 // ignore context, i.e. scan all subdirs
115 QStringList contextdirs = sizedir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
116 foreach(QString dir, contextdirs) {
117 QString path = QString("%1/%2/%3").arg(sizedir.absolutePath(), dir, fname);
118 if(QFile::exists(path)) return path;
122 // Now check the plain dirs
123 foreach(QString dir, _plainIconDirNames) {
124 QString path = QString("%1/%2").arg(dir, name);
125 if(QFile::exists(path)) return path;
128 qWarning() << "Icon not found:" << name << size;
132 // Convenience constructors
134 QPixmap DesktopIcon(const QString& name, int force_size) {
135 IconLoader *loader = IconLoader::global();
136 return loader->loadIcon(name, IconLoader::Desktop, force_size);
139 QPixmap BarIcon(const QString& name, int force_size) {
140 IconLoader *loader = IconLoader::global();
141 return loader->loadIcon(name, IconLoader::Toolbar, force_size);
144 QPixmap MainBarIcon(const QString& name, int force_size) {
145 IconLoader *loader = IconLoader::global();
146 return loader->loadIcon(name, IconLoader::MainToolbar, force_size);
149 QPixmap SmallIcon(const QString& name, int force_size) {
150 IconLoader *loader = IconLoader::global();
151 return loader->loadIcon(name, IconLoader::Small, force_size);