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