pollymc/launcher/pages/modplatform/legacy_ftb/ListModel.cpp

261 lines
6.1 KiB
C++
Raw Normal View History

#include "ListModel.h"
2021-11-20 20:52:22 +05:30
#include "Application.h"
#include <MMCStrings.h>
2018-03-12 19:39:07 +05:30
#include <Version.h>
#include <QtMath>
#include <QLabel>
#include <RWStorage.h>
#include <Env.h>
#include <BuildConfig.h>
2019-01-04 06:18:36 +05:30
namespace LegacyFTB {
FilterModel::FilterModel(QObject *parent) : QSortFilterProxyModel(parent)
{
2018-07-15 18:21:05 +05:30
currentSorting = Sorting::ByGameVersion;
sortings.insert(tr("Sort by name"), Sorting::ByName);
sortings.insert(tr("Sort by game version"), Sorting::ByGameVersion);
}
bool FilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
Modpack leftPack = sourceModel()->data(left, Qt::UserRole).value<Modpack>();
Modpack rightPack = sourceModel()->data(right, Qt::UserRole).value<Modpack>();
2018-07-15 18:21:05 +05:30
if(currentSorting == Sorting::ByGameVersion) {
Version lv(leftPack.mcVersion);
Version rv(rightPack.mcVersion);
return lv < rv;
2018-07-15 18:21:05 +05:30
} else if(currentSorting == Sorting::ByName) {
return Strings::naturalCompare(leftPack.name, rightPack.name, Qt::CaseSensitive) >= 0;
}
2018-07-15 18:21:05 +05:30
//UHM, some inavlid value set?!
qWarning() << "Invalid sorting set!";
return true;
}
bool FilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
2018-07-15 18:21:05 +05:30
return true;
}
const QMap<QString, FilterModel::Sorting> FilterModel::getAvailableSortings()
{
2018-07-15 18:21:05 +05:30
return sortings;
}
QString FilterModel::translateCurrentSorting()
{
2018-07-15 18:21:05 +05:30
return sortings.key(currentSorting);
}
void FilterModel::setSorting(Sorting s)
{
2018-07-15 18:21:05 +05:30
currentSorting = s;
invalidate();
}
FilterModel::Sorting FilterModel::getCurrentSorting()
{
2018-07-15 18:21:05 +05:30
return currentSorting;
}
ListModel::ListModel(QObject *parent) : QAbstractListModel(parent)
{
}
ListModel::~ListModel()
{
}
QString ListModel::translatePackType(PackType type) const
{
switch(type)
{
case PackType::Public:
return tr("Public Modpack");
case PackType::ThirdParty:
return tr("Third Party Modpack");
case PackType::Private:
return tr("Private Modpack");
2018-07-15 18:21:05 +05:30
}
qWarning() << "Unknown FTB modpack type:" << int(type);
return QString();
}
int ListModel::rowCount(const QModelIndex &parent) const
{
2018-07-15 18:21:05 +05:30
return modpacks.size();
}
int ListModel::columnCount(const QModelIndex &parent) const
{
2018-07-15 18:21:05 +05:30
return 1;
}
QVariant ListModel::data(const QModelIndex &index, int role) const
{
2018-07-15 18:21:05 +05:30
int pos = index.row();
if(pos >= modpacks.size() || pos < 0 || !index.isValid())
{
2018-07-15 18:21:05 +05:30
return QString("INVALID INDEX %1").arg(pos);
}
Modpack pack = modpacks.at(pos);
if(role == Qt::DisplayRole)
{
2018-07-15 18:21:05 +05:30
return pack.name + "\n" + translatePackType(pack.type);
}
else if (role == Qt::ToolTipRole)
{
if(pack.description.length() > 100)
{
2018-07-15 18:21:05 +05:30
//some magic to prevent to long tooltips and replace html linebreaks
QString edit = pack.description.left(97);
edit = edit.left(edit.lastIndexOf("<br>")).left(edit.lastIndexOf(" ")).append("...");
return edit;
}
return pack.description;
}
else if(role == Qt::DecorationRole)
{
if(m_logoMap.contains(pack.logo))
{
2018-07-15 18:21:05 +05:30
return (m_logoMap.value(pack.logo));
}
2021-11-20 20:52:22 +05:30
QIcon icon = APPLICATION->getThemedIcon("screenshot-placeholder");
((ListModel *)this)->requestLogo(pack.logo);
2018-07-15 18:21:05 +05:30
return icon;
}
else if(role == Qt::TextColorRole)
{
if(pack.broken)
{
2018-07-15 18:21:05 +05:30
//FIXME: Hardcoded color
return QColor(255, 0, 50);
}
else if(pack.bugged)
{
2018-07-15 18:21:05 +05:30
//FIXME: Hardcoded color
//bugged pack, currently only indicates bugged xml
return QColor(244, 229, 66);
}
}
else if(role == Qt::UserRole)
{
2018-07-15 18:21:05 +05:30
QVariant v;
v.setValue(pack);
return v;
}
return QVariant();
}
void ListModel::fill(ModpackList modpacks)
{
2018-07-15 18:21:05 +05:30
beginResetModel();
this->modpacks = modpacks;
endResetModel();
}
void ListModel::addPack(Modpack modpack)
{
beginResetModel();
this->modpacks.append(modpack);
endResetModel();
}
void ListModel::clear()
{
beginResetModel();
modpacks.clear();
endResetModel();
}
Modpack ListModel::at(int row)
{
2018-07-15 18:21:05 +05:30
return modpacks.at(row);
}
void ListModel::remove(int row)
{
if(row < 0 || row >= modpacks.size())
{
qWarning() << "Attempt to remove FTB modpacks with invalid row" << row;
return;
}
beginRemoveRows(QModelIndex(), row, row);
modpacks.removeAt(row);
endRemoveRows();
}
void ListModel::logoLoaded(QString logo, QIcon out)
{
2018-07-15 18:21:05 +05:30
m_loadingLogos.removeAll(logo);
m_logoMap.insert(logo, out);
emit dataChanged(createIndex(0, 0), createIndex(1, 0));
}
void ListModel::logoFailed(QString logo)
{
2018-07-15 18:21:05 +05:30
m_failedLogos.append(logo);
m_loadingLogos.removeAll(logo);
}
void ListModel::requestLogo(QString file)
{
if(m_loadingLogos.contains(file) || m_failedLogos.contains(file))
{
2018-07-15 18:21:05 +05:30
return;
}
2021-11-20 20:52:22 +05:30
MetaEntryPtr entry = ENV->metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(file.section(".", 0, 0)));
2018-07-15 18:21:05 +05:30
NetJob *job = new NetJob(QString("FTB Icon Download for %1").arg(file));
job->addNetAction(Net::Download::makeCached(QUrl(QString(BuildConfig.LEGACY_FTB_CDN_BASE_URL + "static/%1").arg(file)), entry));
2018-07-15 18:21:05 +05:30
auto fullPath = entry->getFullPath();
QObject::connect(job, &NetJob::finished, this, [this, file, fullPath]
{
2018-07-15 18:21:05 +05:30
emit logoLoaded(file, QIcon(fullPath));
if(waitingCallbacks.contains(file))
{
2018-07-15 18:21:05 +05:30
waitingCallbacks.value(file)(fullPath);
}
});
QObject::connect(job, &NetJob::failed, this, [this, file]
{
2018-07-15 18:21:05 +05:30
emit logoFailed(file);
});
2018-07-15 18:21:05 +05:30
job->start();
2018-07-15 18:21:05 +05:30
m_loadingLogos.append(file);
}
void ListModel::getLogo(const QString &logo, LogoCallback callback)
{
if(m_logoMap.contains(logo))
{
2021-11-20 20:52:22 +05:30
callback(ENV->metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
}
else
{
2018-07-15 18:21:05 +05:30
requestLogo(logo);
}
}
Qt::ItemFlags ListModel::flags(const QModelIndex &index) const
{
return QAbstractListModel::flags(index);
}
}