2017-07-13 10:26:03 +02:00
|
|
|
/*
|
|
|
|
* cmodlistmodel_moc.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2013-08-22 17:22:49 +03:00
|
|
|
#include "StdInc.h"
|
2013-08-24 23:11:51 +03:00
|
|
|
#include "cmodlistmodel_moc.h"
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
#include <QIcon>
|
|
|
|
|
|
|
|
namespace ModStatus
|
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
static const QString iconDelete = "icons:mod-delete.png";
|
|
|
|
static const QString iconDisabled = "icons:mod-disabled.png";
|
|
|
|
static const QString iconDownload = "icons:mod-download.png";
|
|
|
|
static const QString iconEnabled = "icons:mod-enabled.png";
|
|
|
|
static const QString iconUpdate = "icons:mod-update.png";
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
CModListModel::CModListModel(QObject * parent)
|
|
|
|
: QAbstractItemModel(parent)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:06:25 +03:00
|
|
|
QString CModListModel::modIndexToName(const QModelIndex & index) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(index.isValid())
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
|
|
|
return modNameToID.at(index.internalId());
|
|
|
|
}
|
|
|
|
return "";
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2023-03-27 00:10:44 +02:00
|
|
|
|
|
|
|
QString CModListModel::modTypeName(QString modTypeID) const
|
|
|
|
{
|
|
|
|
static QMap<QString, QString> modTypes = {
|
|
|
|
{"Translation", tr("Translation")},
|
|
|
|
{"Town", tr("Town") },
|
|
|
|
{"Test", tr("Test") },
|
|
|
|
{"Templates", tr("Templates") },
|
|
|
|
{"Spells", tr("Spells") },
|
|
|
|
{"Music", tr("Music") },
|
|
|
|
{"Sounds", tr("Sounds") },
|
|
|
|
{"Skills", tr("Skills") },
|
|
|
|
{"Other", tr("Other") },
|
|
|
|
{"Objects", tr("Objects") },
|
|
|
|
{"Mechanical", tr("Mechanics") },
|
|
|
|
{"Mechanics", tr("Mechanics") },
|
|
|
|
{"Themes", tr("Interface") },
|
|
|
|
{"Interface", tr("Interface") },
|
|
|
|
{"Heroes", tr("Heroes") },
|
|
|
|
{"Graphic", tr("Graphical") },
|
|
|
|
{"Graphical", tr("Graphical") },
|
|
|
|
{"Expansion", tr("Expansion") },
|
|
|
|
{"Creatures", tr("Creatures") },
|
|
|
|
{"Artifacts", tr("Artifacts") },
|
|
|
|
{"AI", tr("AI") },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (modTypes.contains(modTypeID))
|
|
|
|
return modTypes[modTypeID];
|
|
|
|
return tr("Other");
|
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
QVariant CModListModel::getValue(const CModEntry & mod, int field) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2014-01-30 15:35:17 +03:00
|
|
|
switch(field)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2023-03-27 00:10:44 +02:00
|
|
|
case ModFields::STATUS_ENABLED:
|
|
|
|
return mod.getModStatus() & (ModStatus::ENABLED | ModStatus::INSTALLED);
|
|
|
|
|
|
|
|
case ModFields::STATUS_UPDATE:
|
|
|
|
return mod.getModStatus() & (ModStatus::UPDATEABLE | ModStatus::INSTALLED);
|
|
|
|
|
|
|
|
case ModFields::NAME:
|
|
|
|
return mod.getValue("name");
|
|
|
|
|
|
|
|
case ModFields::VERSION:
|
|
|
|
return mod.getValue("version");
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2023-03-27 00:10:44 +02:00
|
|
|
case ModFields::TYPE:
|
|
|
|
return modTypeName(mod.getValue("modType").toString());
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2023-03-27 00:10:44 +02:00
|
|
|
default:
|
|
|
|
return QVariant();
|
2014-01-30 15:35:17 +03:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2014-01-30 15:35:17 +03:00
|
|
|
QVariant CModListModel::getText(const CModEntry & mod, int field) const
|
|
|
|
{
|
|
|
|
switch(field)
|
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
case ModFields::STATUS_ENABLED:
|
|
|
|
case ModFields::STATUS_UPDATE:
|
|
|
|
return "";
|
|
|
|
default:
|
|
|
|
return getValue(mod, field);
|
2014-01-30 15:35:17 +03:00
|
|
|
}
|
|
|
|
}
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2014-01-30 15:35:17 +03:00
|
|
|
QVariant CModListModel::getIcon(const CModEntry & mod, int field) const
|
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(field == ModFields::STATUS_ENABLED && mod.isEnabled())
|
2014-01-30 15:35:17 +03:00
|
|
|
return QIcon(ModStatus::iconEnabled);
|
2018-04-13 07:34:58 +02:00
|
|
|
if(field == ModFields::STATUS_ENABLED && mod.isDisabled())
|
2014-01-30 15:35:17 +03:00
|
|
|
return QIcon(ModStatus::iconDisabled);
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(field == ModFields::STATUS_UPDATE && mod.isUpdateable())
|
2014-01-30 15:35:17 +03:00
|
|
|
return QIcon(ModStatus::iconUpdate);
|
2018-04-13 07:34:58 +02:00
|
|
|
if(field == ModFields::STATUS_UPDATE && !mod.isInstalled())
|
2014-01-30 15:35:17 +03:00
|
|
|
return QIcon(ModStatus::iconDownload);
|
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant CModListModel::getTextAlign(int field) const
|
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
|
2014-01-30 15:35:17 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
QVariant CModListModel::data(const QModelIndex & index, int role) const
|
2014-01-30 15:35:17 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(index.isValid())
|
2014-01-30 15:35:17 +03:00
|
|
|
{
|
2014-03-20 20:06:25 +03:00
|
|
|
auto mod = getMod(modIndexToName(index));
|
2013-08-22 17:22:49 +03:00
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
switch(role)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
case Qt::DecorationRole:
|
|
|
|
return getIcon(mod, index.column());
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
return getText(mod, index.column());
|
|
|
|
case Qt::TextAlignmentRole:
|
|
|
|
return getTextAlign(index.column());
|
|
|
|
case ModRoles::ValueRole:
|
|
|
|
return getValue(mod, index.column());
|
|
|
|
case ModRoles::ModNameRole:
|
|
|
|
return mod.getName();
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:06:25 +03:00
|
|
|
int CModListModel::rowCount(const QModelIndex & index) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(index.isValid())
|
2014-03-20 20:06:25 +03:00
|
|
|
return modIndex[modIndexToName(index)].size();
|
|
|
|
return modIndex[""].size();
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int CModListModel::columnCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return ModFields::COUNT;
|
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags CModListModel::flags(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant CModListModel::headerData(int section, Qt::Orientation orientation, int role) const
|
|
|
|
{
|
2022-12-25 13:19:16 +02:00
|
|
|
static const QString header[ModFields::COUNT] =
|
|
|
|
{
|
|
|
|
QT_TR_NOOP("Name"),
|
|
|
|
QT_TR_NOOP(""), // status icon
|
|
|
|
QT_TR_NOOP(""), // status icon
|
|
|
|
QT_TR_NOOP("Type"),
|
|
|
|
QT_TR_NOOP("Version"),
|
|
|
|
};
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
|
2022-12-25 13:19:16 +02:00
|
|
|
return QCoreApplication::translate("ModFields", header[section].toStdString().c_str());
|
2013-08-22 17:22:49 +03:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2022-09-10 18:30:41 +02:00
|
|
|
void CModListModel::reloadRepositories()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2013-08-24 23:11:51 +03:00
|
|
|
void CModListModel::resetRepositories()
|
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
CModList::resetRepositories();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2013-09-21 21:29:26 +03:00
|
|
|
void CModListModel::addRepository(QVariantMap data)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
CModList::addRepository(data);
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2014-03-20 20:06:25 +03:00
|
|
|
void CModListModel::modChanged(QString modID)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2014-03-20 20:06:25 +03:00
|
|
|
int index = modNameToID.indexOf(modID);
|
2018-04-13 07:34:58 +02:00
|
|
|
QModelIndex parent = this->parent(createIndex(0, 0, index));
|
2014-03-20 20:06:25 +03:00
|
|
|
int row = modIndex[modIndexToName(parent)].indexOf(modID);
|
|
|
|
emit dataChanged(createIndex(row, 0, index), createIndex(row, 4, index));
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2014-03-20 20:06:25 +03:00
|
|
|
void CModListModel::endResetModel()
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2014-03-20 20:06:25 +03:00
|
|
|
modNameToID = getModList();
|
|
|
|
modIndex.clear();
|
2018-04-13 07:34:58 +02:00
|
|
|
for(const QString & str : modNameToID)
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(str.contains('.'))
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
|
|
|
modIndex[str.section('.', 0, -2)].append(str);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
modIndex[""].append(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QAbstractItemModel::endResetModel();
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
QModelIndex CModListModel::index(int row, int column, const QModelIndex & parent) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(parent.isValid())
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(modIndex[modIndexToName(parent)].size() > row)
|
2014-03-20 20:06:25 +03:00
|
|
|
return createIndex(row, column, modNameToID.indexOf(modIndex[modIndexToName(parent)][row]));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(modIndex[""].size() > row)
|
2014-03-20 20:06:25 +03:00
|
|
|
return createIndex(row, column, modNameToID.indexOf(modIndex[""][row]));
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
QModelIndex CModListModel::parent(const QModelIndex & child) const
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
|
|
|
QString modID = modNameToID[child.internalId()];
|
2018-04-13 07:34:58 +02:00
|
|
|
for(auto entry = modIndex.begin(); entry != modIndex.end(); entry++) // because using range-for entry type is QMap::value_type oO
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(entry.key() != "" && entry.value().indexOf(modID) != -1)
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
|
|
|
return createIndex(entry.value().indexOf(modID), child.column(), modNameToID.indexOf(entry.key()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CModFilterModel::setTypeFilter(int filteredType, int filterMask)
|
|
|
|
{
|
|
|
|
this->filterMask = filterMask;
|
|
|
|
this->filteredType = filteredType;
|
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
bool CModFilterModel::filterMatchesThis(const QModelIndex & source) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2014-03-20 20:06:25 +03:00
|
|
|
CModEntry mod = base->getMod(source.data(ModRoles::ModNameRole).toString());
|
|
|
|
return (mod.getModStatus() & filterMask) == filteredType &&
|
2022-09-17 15:43:59 +02:00
|
|
|
mod.isValid() &&
|
2018-04-13 07:34:58 +02:00
|
|
|
QSortFilterProxyModel::filterAcceptsRow(source.row(), source.parent());
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
bool CModFilterModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2014-03-20 20:06:25 +03:00
|
|
|
QModelIndex index = base->index(source_row, 0, source_parent);
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(filterMatchesThis(index))
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
for(size_t i = 0; i < base->rowCount(index); i++)
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
2022-05-28 15:32:20 +02:00
|
|
|
if(filterMatchesThis(base->index((int)i, 0, index)))
|
2014-03-20 20:06:25 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex parent = source_parent;
|
2018-04-13 07:34:58 +02:00
|
|
|
while(parent.isValid())
|
2014-03-20 20:06:25 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
if(filterMatchesThis(parent))
|
2014-03-20 20:06:25 +03:00
|
|
|
return true;
|
|
|
|
parent = parent.parent();
|
|
|
|
}
|
2013-08-22 17:22:49 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
CModFilterModel::CModFilterModel(CModListModel * model, QObject * parent)
|
|
|
|
: QSortFilterProxyModel(parent), base(model), filteredType(ModStatus::MASK_NONE), filterMask(ModStatus::MASK_NONE)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
setSourceModel(model);
|
2014-03-20 20:06:25 +03:00
|
|
|
setSortRole(ModRoles::ValueRole);
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|