2018-03-04 01:52:00 +05:30
|
|
|
#include "FtbListModel.h"
|
|
|
|
#include "MultiMC.h"
|
|
|
|
|
|
|
|
#include <MMCStrings.h>
|
2018-03-12 19:39:07 +05:30
|
|
|
#include <Version.h>
|
2018-03-04 01:52:00 +05:30
|
|
|
|
|
|
|
#include <QtMath>
|
2018-04-01 23:54:28 +05:30
|
|
|
#include <QLabel>
|
|
|
|
|
|
|
|
#include <RWStorage.h>
|
|
|
|
#include <Env.h>
|
2018-03-04 01:52:00 +05:30
|
|
|
|
2019-01-04 06:18:36 +05:30
|
|
|
#include "net/URLConstants.h"
|
|
|
|
|
2018-03-04 01:52:00 +05:30
|
|
|
FtbFilterModel::FtbFilterModel(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);
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool FtbFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
FtbModpack leftPack = sourceModel()->data(left, Qt::UserRole).value<FtbModpack>();
|
|
|
|
FtbModpack rightPack = sourceModel()->data(right, Qt::UserRole).value<FtbModpack>();
|
2018-03-04 01:52:00 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
if(currentSorting == Sorting::ByGameVersion) {
|
|
|
|
Version lv(leftPack.mcVersion);
|
|
|
|
Version rv(rightPack.mcVersion);
|
|
|
|
return lv < rv;
|
2018-03-04 01:52:00 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
} else if(currentSorting == Sorting::ByName) {
|
|
|
|
return Strings::naturalCompare(leftPack.name, rightPack.name, Qt::CaseSensitive) >= 0;
|
|
|
|
}
|
2018-03-04 01:52:00 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
//UHM, some inavlid value set?!
|
|
|
|
qWarning() << "Invalid sorting set!";
|
|
|
|
return true;
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
bool FtbFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return true;
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
const QMap<QString, FtbFilterModel::Sorting> FtbFilterModel::getAvailableSortings()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return sortings;
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
2018-04-01 23:54:28 +05:30
|
|
|
QString FtbFilterModel::translateCurrentSorting()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return sortings.key(currentSorting);
|
2018-04-01 23:54:28 +05:30
|
|
|
}
|
|
|
|
|
2018-03-04 01:52:00 +05:30
|
|
|
void FtbFilterModel::setSorting(Sorting s)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
currentSorting = s;
|
|
|
|
invalidate();
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
FtbFilterModel::Sorting FtbFilterModel::getCurrentSorting()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return currentSorting;
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
FtbListModel::FtbListModel(QObject *parent) : QAbstractListModel(parent)
|
|
|
|
{
|
2018-04-01 23:54:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
FtbListModel::~FtbListModel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
QString FtbListModel::translatePackType(FtbPackType type) const
|
|
|
|
{
|
2018-08-02 04:22:31 +05:30
|
|
|
switch(type)
|
|
|
|
{
|
|
|
|
case FtbPackType::Public:
|
|
|
|
return tr("Public Modpack");
|
|
|
|
case FtbPackType::ThirdParty:
|
|
|
|
return tr("Third Party Modpack");
|
|
|
|
case FtbPackType::Private:
|
|
|
|
return tr("Private Modpack");
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2018-08-02 04:22:31 +05:30
|
|
|
qWarning() << "Unknown FTB modpack type:" << int(type);
|
|
|
|
return QString();
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int FtbListModel::rowCount(const QModelIndex &parent) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return modpacks.size();
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
int FtbListModel::columnCount(const QModelIndex &parent) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return 1;
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
QVariant FtbListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
int pos = index.row();
|
2018-08-02 04:22:31 +05:30
|
|
|
if(pos >= modpacks.size() || pos < 0 || !index.isValid())
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return QString("INVALID INDEX %1").arg(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
FtbModpack pack = modpacks.at(pos);
|
2018-08-02 04:22:31 +05:30
|
|
|
if(role == Qt::DisplayRole)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return pack.name + "\n" + translatePackType(pack.type);
|
2018-08-02 04:22:31 +05:30
|
|
|
}
|
|
|
|
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;
|
2018-08-02 04:22:31 +05:30
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
QIcon icon = MMC->getThemedIcon("screenshot-placeholder");
|
|
|
|
((FtbListModel *)this)->requestLogo(pack.logo);
|
|
|
|
return icon;
|
2018-08-02 04:22:31 +05:30
|
|
|
}
|
|
|
|
else if(role == Qt::TextColorRole)
|
|
|
|
{
|
|
|
|
if(pack.broken)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
//FIXME: Hardcoded color
|
|
|
|
return QColor(255, 0, 50);
|
2018-08-02 04:22:31 +05:30
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2018-08-02 04:22:31 +05:30
|
|
|
}
|
|
|
|
else if(role == Qt::UserRole)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QVariant v;
|
|
|
|
v.setValue(pack);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
return QVariant();
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void FtbListModel::fill(FtbModpackList modpacks)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
beginResetModel();
|
|
|
|
this->modpacks = modpacks;
|
|
|
|
endResetModel();
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
|
|
|
|
2018-08-02 04:22:31 +05:30
|
|
|
void FtbListModel::addPack(FtbModpack modpack)
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
this->modpacks.append(modpack);
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FtbListModel::clear()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
modpacks.clear();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2018-03-04 01:52:00 +05:30
|
|
|
FtbModpack FtbListModel::at(int row)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return modpacks.at(row);
|
2018-03-04 01:52:00 +05:30
|
|
|
}
|
2018-04-01 23:54:28 +05:30
|
|
|
|
2018-08-02 04:22:31 +05:30
|
|
|
void FtbListModel::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();
|
|
|
|
}
|
|
|
|
|
2018-04-06 18:38:59 +05:30
|
|
|
void FtbListModel::logoLoaded(QString logo, QIcon out)
|
2018-04-01 23:54:28 +05:30
|
|
|
{
|
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));
|
2018-04-01 23:54:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void FtbListModel::logoFailed(QString logo)
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_failedLogos.append(logo);
|
|
|
|
m_loadingLogos.removeAll(logo);
|
2018-04-01 23:54:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void FtbListModel::requestLogo(QString file)
|
|
|
|
{
|
2018-08-02 04:22:31 +05:30
|
|
|
if(m_loadingLogos.contains(file) || m_failedLogos.contains(file))
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
|
|
|
}
|
2018-04-01 23:54:28 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
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));
|
2019-01-04 06:18:36 +05:30
|
|
|
job->addNetAction(Net::Download::makeCached(QUrl(QString(URLConstants::FTB_CDN_BASE_URL + "static/%1").arg(file)), entry));
|
2018-04-01 23:54:28 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
auto fullPath = entry->getFullPath();
|
2018-08-02 04:22:31 +05:30
|
|
|
QObject::connect(job, &NetJob::finished, this, [this, file, fullPath]
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
emit logoLoaded(file, QIcon(fullPath));
|
2018-08-02 04:22:31 +05:30
|
|
|
if(waitingCallbacks.contains(file))
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
waitingCallbacks.value(file)(fullPath);
|
|
|
|
}
|
|
|
|
});
|
2018-04-01 23:54:28 +05:30
|
|
|
|
2018-08-02 04:22:31 +05:30
|
|
|
QObject::connect(job, &NetJob::failed, this, [this, file]
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
emit logoFailed(file);
|
|
|
|
});
|
2018-04-01 23:54:28 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
job->start();
|
2018-04-01 23:54:28 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
m_loadingLogos.append(file);
|
2018-04-01 23:54:28 +05:30
|
|
|
}
|
2018-04-07 19:45:58 +05:30
|
|
|
|
|
|
|
void FtbListModel::getLogo(const QString &logo, LogoCallback callback)
|
|
|
|
{
|
2018-08-02 04:22:31 +05:30
|
|
|
if(m_logoMap.contains(logo))
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
callback(ENV.metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath());
|
2018-08-02 04:22:31 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
requestLogo(logo);
|
|
|
|
}
|
2018-04-07 19:45:58 +05:30
|
|
|
}
|
2018-08-02 04:22:31 +05:30
|
|
|
|
|
|
|
Qt::ItemFlags FtbListModel::flags(const QModelIndex &index) const
|
|
|
|
{
|
|
|
|
return QAbstractListModel::flags(index);
|
|
|
|
}
|