2018-03-03 20:22:00 +00:00
|
|
|
#include "FtbListModel.h"
|
|
|
|
#include "MultiMC.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
|
|
FtbFilterModel::FtbFilterModel(QObject *parent) : QSortFilterProxyModel(parent)
|
|
|
|
{
|
|
|
|
currentSorting = Sorting::ByGameVersion;
|
2018-04-01 19:24:28 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool FtbFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
|
|
|
{
|
|
|
|
FtbModpack leftPack = sourceModel()->data(left, Qt::UserRole).value<FtbModpack>();
|
|
|
|
FtbModpack rightPack = sourceModel()->data(right, Qt::UserRole).value<FtbModpack>();
|
|
|
|
|
|
|
|
if(currentSorting == Sorting::ByGameVersion) {
|
2018-03-12 14:09:07 +00:00
|
|
|
Version lv(leftPack.mcVersion);
|
|
|
|
Version rv(rightPack.mcVersion);
|
|
|
|
return lv < rv;
|
2018-03-03 20:22:00 +00:00
|
|
|
|
|
|
|
} else if(currentSorting == Sorting::ByName) {
|
|
|
|
return Strings::naturalCompare(leftPack.name, rightPack.name, Qt::CaseSensitive) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//UHM, some inavlid value set?!
|
|
|
|
qWarning() << "Invalid sorting set!";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FtbFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QMap<QString, FtbFilterModel::Sorting> FtbFilterModel::getAvailableSortings()
|
|
|
|
{
|
|
|
|
return sortings;
|
|
|
|
}
|
|
|
|
|
2018-04-01 19:24:28 +01:00
|
|
|
QString FtbFilterModel::translateCurrentSorting()
|
|
|
|
{
|
|
|
|
return sortings.key(currentSorting);
|
|
|
|
}
|
|
|
|
|
2018-03-03 20:22:00 +00:00
|
|
|
void FtbFilterModel::setSorting(Sorting s)
|
|
|
|
{
|
|
|
|
currentSorting = s;
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
FtbFilterModel::Sorting FtbFilterModel::getCurrentSorting()
|
|
|
|
{
|
|
|
|
return currentSorting;
|
|
|
|
}
|
|
|
|
|
|
|
|
FtbListModel::FtbListModel(QObject *parent) : QAbstractListModel(parent)
|
|
|
|
{
|
2018-04-01 19:24:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FtbListModel::~FtbListModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FtbListModel::translatePackType(FtbPackType type) const
|
|
|
|
{
|
|
|
|
if(type == FtbPackType::Public) {
|
|
|
|
return tr("Public Modpack");
|
|
|
|
} else if(type == FtbPackType::ThirdParty) {
|
|
|
|
return tr("Third Party Modpack");
|
|
|
|
} else if(type == FtbPackType::Private) {
|
|
|
|
return tr("Private Modpack");
|
|
|
|
} else {
|
|
|
|
return tr("Unknown Type");
|
|
|
|
}
|
2018-03-03 20:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int FtbListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return modpacks.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
int FtbListModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FtbListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
int pos = index.row();
|
2018-04-06 14:08:59 +01:00
|
|
|
if(pos >= modpacks.size() || pos < 0 || !index.isValid()) {
|
2018-03-03 20:22:00 +00:00
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
FtbModpack pack = modpacks.at(pos);
|
|
|
|
if(role == Qt::DisplayRole) {
|
2018-04-01 19:24:28 +01:00
|
|
|
return pack.name + "\n" + translatePackType(pack.type);
|
2018-03-03 20:22:00 +00:00
|
|
|
} else if (role == Qt::ToolTipRole) {
|
|
|
|
if(pack.description.length() > 100) {
|
|
|
|
//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) {
|
2018-04-01 19:24:28 +01:00
|
|
|
if(m_logoMap.contains(pack.logo)) {
|
|
|
|
return (m_logoMap.value(pack.logo));
|
|
|
|
}
|
2018-04-06 14:08:59 +01:00
|
|
|
QIcon icon = MMC->getThemedIcon("screenshot-placeholder");
|
2018-04-01 19:24:28 +01:00
|
|
|
((FtbListModel *)this)->requestLogo(pack.logo);
|
2018-04-06 14:08:59 +01:00
|
|
|
return icon;
|
2018-03-03 20:22:00 +00:00
|
|
|
} else if(role == Qt::TextColorRole) {
|
|
|
|
if(pack.broken) {
|
2018-03-12 14:09:07 +00:00
|
|
|
//FIXME: Hardcoded color
|
2018-03-03 20:22:00 +00:00
|
|
|
return QColor(255, 0, 50);
|
|
|
|
} else if(pack.bugged) {
|
2018-03-12 14:09:07 +00:00
|
|
|
//FIXME: Hardcoded color
|
2018-03-03 20:22:00 +00:00
|
|
|
//bugged pack, currently only indicates bugged xml
|
|
|
|
return QColor(244, 229, 66);
|
|
|
|
}
|
|
|
|
} else if(role == Qt::UserRole) {
|
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
|
|
|
}
|
2018-04-01 19:24:28 +01:00
|
|
|
|
2018-03-03 20:22:00 +00:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FtbListModel::fill(FtbModpackList modpacks)
|
|
|
|
{
|
2018-03-19 01:36:12 +00:00
|
|
|
beginResetModel();
|
2018-03-03 20:22:00 +00:00
|
|
|
this->modpacks = modpacks;
|
2018-03-19 01:36:12 +00:00
|
|
|
endResetModel();
|
2018-03-03 20:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FtbModpack FtbListModel::at(int row)
|
|
|
|
{
|
|
|
|
return modpacks.at(row);
|
|
|
|
}
|
2018-04-01 19:24:28 +01:00
|
|
|
|
2018-04-06 14:08:59 +01:00
|
|
|
void FtbListModel::logoLoaded(QString logo, QIcon out)
|
2018-04-01 19:24:28 +01:00
|
|
|
{
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
m_logoMap.insert(logo, out);
|
2018-04-06 14:08:59 +01:00
|
|
|
emit dataChanged(createIndex(0, 0), createIndex(1, 0));
|
2018-04-01 19:24:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FtbListModel::logoFailed(QString logo)
|
|
|
|
{
|
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FtbListModel::requestLogo(QString file)
|
|
|
|
{
|
|
|
|
if(m_loadingLogos.contains(file) || m_failedLogos.contains(file)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:33:31 +01:00
|
|
|
MetaEntryPtr entry = ENV.metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(file.section(".", 0, 0)));
|
2018-04-01 19:24:28 +01:00
|
|
|
NetJob *job = new NetJob(QString("FTB Icon Download for %1").arg(file));
|
|
|
|
job->addNetAction(Net::Download::makeCached(QUrl(QString("https://ftb.cursecdn.com/FTB2/static/%1").arg(file)), entry));
|
|
|
|
|
2018-04-05 18:33:31 +01:00
|
|
|
auto fullPath = entry->getFullPath();
|
|
|
|
QObject::connect(job, &NetJob::finished, this, [this, file, fullPath]{
|
2018-04-06 14:08:59 +01:00
|
|
|
emit logoLoaded(file, QIcon(fullPath));
|
2018-04-07 15:15:58 +01:00
|
|
|
if(waitingCallbacks.contains(file)) {
|
|
|
|
waitingCallbacks.value(file)(fullPath);
|
|
|
|
}
|
2018-04-01 19:24:28 +01:00
|
|
|
});
|
|
|
|
|
2018-04-05 18:33:31 +01:00
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, file]{
|
|
|
|
emit logoFailed(file);
|
2018-04-01 19:24:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
job->start();
|
|
|
|
|
|
|
|
m_loadingLogos.append(file);
|
|
|
|
}
|
2018-04-07 15:15:58 +01:00
|
|
|
|
|
|
|
void FtbListModel::getLogo(const QString &logo, LogoCallback callback)
|
|
|
|
{
|
|
|
|
if(m_logoMap.contains(logo)) {
|
|
|
|
callback(ENV.metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
|
|
|
|
} else {
|
|
|
|
requestLogo(logo);
|
|
|
|
}
|
|
|
|
}
|