Play-/Source/ui_qt/CoverUtils.cpp

64 lines
1.6 KiB
C++
Raw Normal View History

2019-01-16 20:00:10 +00:00
#include <iostream>
#include "AppConfig.h"
#include "CoverUtils.h"
#include "PathUtils.h"
#include "QStringUtils.h"
std::map<std::string, QPixmap> CoverUtils::cache;
std::mutex CoverUtils::m_lock;
2019-01-16 20:00:10 +00:00
QPixmap CoverUtils::find(std::string key)
{
auto itr = CoverUtils::cache.find(key);
if(itr == CoverUtils::cache.end())
{
return QPixmap();
2019-01-16 20:00:10 +00:00
}
return itr->second;
}
void CoverUtils::PopulatePlaceholderCover()
{
auto itr = CoverUtils::cache.find("PH");
if(itr == CoverUtils::cache.end())
{
auto pixmap = QPixmap(QString(":/assets/boxart.png")).scaledToWidth(250 / 2, Qt::SmoothTransformation);
CoverUtils::cache.insert(std::make_pair("PH", pixmap));
}
}
2019-10-16 20:51:11 -04:00
void CoverUtils::PopulateCache(std::vector<BootablesDb::Bootable> bootables)
2019-01-16 20:00:10 +00:00
{
m_lock.lock();
PopulatePlaceholderCover();
2019-10-16 20:51:11 -04:00
auto coverpath(CAppConfig::GetBasePath() / fs::path("covers"));
2019-01-16 20:00:10 +00:00
Framework::PathUtils::EnsurePathExists(coverpath);
auto itr = CoverUtils::cache.find("PH");
auto placeholder_size = itr->second.size();
2019-01-16 20:00:10 +00:00
for(auto bootable : bootables)
{
if(bootable.discId.empty())
continue;
auto path = coverpath / (bootable.discId + ".jpg");
2019-10-16 20:51:11 -04:00
if(fs::exists(path))
2019-01-16 20:00:10 +00:00
{
auto itr = CoverUtils::cache.find(bootable.discId.c_str());
if(itr == CoverUtils::cache.end())
{
auto pixmap = QPixmap();
if(!pixmap.load(PathToQString(path)))
{
pixmap.load(PathToQString(path), "png");
}
pixmap = pixmap.scaled(placeholder_size, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
2019-01-16 20:00:10 +00:00
CoverUtils::cache.insert(std::make_pair(bootable.discId.c_str(), pixmap));
}
}
}
m_lock.unlock();
2019-01-16 20:00:10 +00:00
}