Clementine-audio-player-Mac.../src/ui/iconloader.cpp

105 lines
3.3 KiB
C++
Raw Normal View History

/* This file is part of Clementine.
Copyright 2010, David Sansome <me@davidsansome.com>
Clementine is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Clementine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "iconloader.h"
2011-04-22 18:50:29 +02:00
#include "core/logging.h"
#include "core/utilities.h"
#include <QtDebug>
#include <QDir>
QList<int> IconLoader::sizes_;
QString IconLoader::custom_icon_path_;
QList<QString> IconLoader::icon_sub_path_;
void IconLoader::Init() {
sizes_.clear();
sizes_ << 22 << 32 << 48;
custom_icon_path_ = Utilities::GetConfigPath(Utilities::Path_Icons);
icon_sub_path_ = {"/icons", "/providers", "/last.fm"};
}
QIcon IconLoader::Load(const QString& name, const IconType& icontype) {
2010-05-25 23:41:53 +02:00
QIcon ret;
// If the icon name is empty
if (name.isEmpty()) {
qLog(Warning) << "Icon name is null";
return ret;
}
// Set the icon load location based on IConType
switch (icontype) {
case Base: case Provider:
break;
2010-05-25 23:41:53 +02:00
case Lastfm: {
// lastfm icons location
const QString custom_lastfm_icon_location = custom_icon_path_ + "/last.fm";
if (QDir(custom_lastfm_icon_location).exists()) {
// Try to load icons from the custom icon location initially
const QString locate_file(custom_lastfm_icon_location + "/" + name + ".png");
if (QFile::exists(locate_file)) ret.addFile(locate_file);
if (!ret.isNull()) return ret;
}
// Otherwise use our fallback theme
const QString lastfm_path_file(":/last.fm/" + name + ".png");
if (QFile::exists(lastfm_path_file)) ret.addFile(lastfm_path_file);
if (ret.isNull()) qLog(Warning) << "Couldn't load icon" << name;
return ret;
}
default:
// Should never be reached
qLog(Warning) << "Couldn't recognize IconType" << name;
return ret;
}
const QString custom_icon_location = custom_icon_path_ + icon_sub_path_.at(icontype);
if (QDir(custom_icon_location).exists()) {
// Try to load icons from the custom icon location initially
const QString locate(custom_icon_location + "/%1x%2/%3.png");
for (int size : sizes_) {
QString filename_custom(locate.arg(size).arg(size).arg(name));
if (QFile::exists(filename_custom)) ret.addFile(filename_custom, QSize(size, size));
}
if (!ret.isNull()) return ret;
}
2010-07-17 23:48:09 +02:00
2010-05-25 23:41:53 +02:00
#if QT_VERSION >= 0x040600
// Then try to load it from the system theme
2010-05-25 23:41:53 +02:00
ret = QIcon::fromTheme(name);
if (!ret.isNull()) return ret;
2010-05-25 23:41:53 +02:00
#endif
// Otherwise use our fallback theme
const QString path(":" + icon_sub_path_.at(icontype) + "/%1x%2/%3.png");
for (int size : sizes_) {
QString filename(path.arg(size).arg(size).arg(name));
if (QFile::exists(filename)) ret.addFile(filename, QSize(size, size));
}
if (ret.isNull()) qLog(Warning) << "Couldn't load icon" << name;
return ret;
}