2017-07-13 10:26:03 +02:00
|
|
|
/*
|
2024-11-12 22:00:21 +02:00
|
|
|
* modstateview_moc.cpp, part of VCMI engine
|
2017-07-13 10:26:03 +02:00
|
|
|
*
|
|
|
|
* 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"
|
2024-11-12 22:00:21 +02:00
|
|
|
#include "modstateitemmodel_moc.h"
|
|
|
|
|
|
|
|
#include "modstatemodel.h"
|
2013-08-22 17:22:49 +03:00
|
|
|
|
|
|
|
#include <QIcon>
|
|
|
|
|
2024-11-13 19:25:59 +02:00
|
|
|
ModStateItemModel::ModStateItemModel(std::shared_ptr<ModStateModel> model, QObject * parent)
|
2018-04-13 07:34:58 +02:00
|
|
|
: QAbstractItemModel(parent)
|
2024-11-13 19:25:59 +02:00
|
|
|
, model(model)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QString ModStateItemModel::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
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QString ModStateItemModel::modTypeName(QString modTypeID) const
|
2023-03-27 00:10:44 +02:00
|
|
|
{
|
2024-02-12 13:22:54 +02:00
|
|
|
static const QMap<QString, QString> modTypes = {
|
2023-03-27 00:10:44 +02:00
|
|
|
{"Translation", tr("Translation")},
|
|
|
|
{"Town", tr("Town") },
|
|
|
|
{"Test", tr("Test") },
|
|
|
|
{"Templates", tr("Templates") },
|
|
|
|
{"Spells", tr("Spells") },
|
|
|
|
{"Music", tr("Music") },
|
2023-10-21 22:55:20 +02:00
|
|
|
{"Maps", tr("Maps") },
|
2023-03-27 00:10:44 +02:00
|
|
|
{"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") },
|
2023-10-21 22:55:20 +02:00
|
|
|
{"Compatibility", tr("Compatibility") },
|
2023-03-27 00:10:44 +02:00
|
|
|
{"Artifacts", tr("Artifacts") },
|
|
|
|
{"AI", tr("AI") },
|
|
|
|
};
|
|
|
|
|
|
|
|
if (modTypes.contains(modTypeID))
|
|
|
|
return modTypes[modTypeID];
|
|
|
|
return tr("Other");
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QVariant ModStateItemModel::getValue(const ModState & 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:
|
2024-11-12 22:00:21 +02:00
|
|
|
return model->isModEnabled(mod.getID());
|
2023-03-27 00:10:44 +02:00
|
|
|
|
|
|
|
case ModFields::STATUS_UPDATE:
|
2024-11-12 22:00:21 +02:00
|
|
|
return model->isModUpdateAvailable(mod.getID());
|
2023-03-27 00:10:44 +02:00
|
|
|
|
|
|
|
case ModFields::NAME:
|
2024-11-12 22:00:21 +02:00
|
|
|
return mod.getName();
|
2023-03-27 00:10:44 +02:00
|
|
|
|
|
|
|
case ModFields::TYPE:
|
2024-11-12 22:00:21 +02:00
|
|
|
return modTypeName(mod.getType());
|
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
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QVariant ModStateItemModel::getText(const ModState & mod, int field) const
|
2014-01-30 15:35:17 +03:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QVariant ModStateItemModel::getIcon(const ModState & mod, int field) const
|
2014-01-30 15:35:17 +03:00
|
|
|
{
|
2024-11-18 16:40:15 +02:00
|
|
|
static const QString iconDisabled = ":/icons/mod-disabled.png";
|
|
|
|
static const QString iconDisabledSubmod = ":/icons/submod-disabled.png";
|
|
|
|
static const QString iconDownload = ":/icons/mod-download.png";
|
|
|
|
static const QString iconEnabled = ":/icons/mod-enabled.png";
|
|
|
|
static const QString iconEnabledSubmod = ":/icons/submod-enabled.png";
|
|
|
|
static const QString iconUpdate = ":/icons/mod-update.png";
|
|
|
|
|
2024-10-16 21:39:35 +02:00
|
|
|
if (field == ModFields::STATUS_ENABLED)
|
|
|
|
{
|
2024-11-12 22:00:21 +02:00
|
|
|
if (!model->isModInstalled(mod.getID()))
|
|
|
|
return QVariant();
|
|
|
|
|
2024-11-20 17:24:52 +02:00
|
|
|
if(mod.isSubmod() && !model->isModEnabled(mod.getTopParentID()))
|
2024-10-16 21:39:35 +02:00
|
|
|
{
|
2024-11-20 17:24:52 +02:00
|
|
|
if (model->isModEnabled(mod.getID()))
|
|
|
|
return QIcon(iconEnabledSubmod);
|
|
|
|
else
|
|
|
|
return QIcon(iconDisabledSubmod);
|
2024-10-16 21:39:35 +02:00
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
if (model->isModEnabled(mod.getID()))
|
2024-11-18 16:40:15 +02:00
|
|
|
return QIcon(iconEnabled);
|
2024-11-12 22:00:21 +02:00
|
|
|
else
|
2024-11-18 16:40:15 +02:00
|
|
|
return QIcon(iconDisabled);
|
2024-10-16 21:39:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(field == ModFields::STATUS_UPDATE)
|
|
|
|
{
|
2024-11-12 22:00:21 +02:00
|
|
|
if (model->isModUpdateAvailable(mod.getID()))
|
2024-11-18 16:40:15 +02:00
|
|
|
return QIcon(iconUpdate);
|
2024-11-12 22:00:21 +02:00
|
|
|
if (!model->isModInstalled(mod.getID()))
|
2024-11-18 16:40:15 +02:00
|
|
|
return QIcon(iconDownload);
|
2024-10-16 21:39:35 +02:00
|
|
|
}
|
2014-01-30 15:35:17 +03:00
|
|
|
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QVariant ModStateItemModel::getTextAlign(int field) const
|
2014-01-30 15:35:17 +03:00
|
|
|
{
|
2018-04-13 07:34:58 +02:00
|
|
|
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
|
2014-01-30 15:35:17 +03:00
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QVariant ModStateItemModel::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
|
|
|
{
|
2024-11-12 22:00:21 +02:00
|
|
|
auto mod = model->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:
|
2024-11-13 19:25:59 +02:00
|
|
|
return mod.getID();
|
2013-08-22 17:22:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
int ModStateItemModel::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
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
int ModStateItemModel::columnCount(const QModelIndex &) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
return ModFields::COUNT;
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
Qt::ItemFlags ModStateItemModel::flags(const QModelIndex &) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
|
|
|
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QVariant ModStateItemModel::headerData(int section, Qt::Orientation orientation, int role) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2024-11-18 16:40:15 +02:00
|
|
|
static const std::array header =
|
2022-12-25 13:19:16 +02:00
|
|
|
{
|
2024-05-01 11:43:20 +02:00
|
|
|
QT_TRANSLATE_NOOP("ModFields", "Name"),
|
|
|
|
QT_TRANSLATE_NOOP("ModFields", ""), // status icon
|
|
|
|
QT_TRANSLATE_NOOP("ModFields", ""), // status icon
|
|
|
|
QT_TRANSLATE_NOOP("ModFields", "Type"),
|
2022-12-25 13:19:16 +02:00
|
|
|
};
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
|
2024-11-18 16:40:15 +02:00
|
|
|
return QCoreApplication::translate("ModFields", header[section]);
|
2013-08-22 17:22:49 +03:00
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
void ModStateItemModel::reloadRepositories()
|
2022-09-10 18:30:41 +02:00
|
|
|
{
|
|
|
|
beginResetModel();
|
|
|
|
endResetModel();
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
void ModStateItemModel::modChanged(QString modID)
|
2013-08-24 23:11:51 +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
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
void ModStateItemModel::endResetModel()
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2024-11-12 22:00:21 +02:00
|
|
|
modNameToID = model->getAllMods();
|
2014-03-20 20:06:25 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QModelIndex ModStateItemModel::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();
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
QModelIndex ModStateItemModel::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
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
void CModFilterModel::setTypeFilter(ModFilterMask newFilterMask)
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2024-11-12 22:00:21 +02:00
|
|
|
filterMask = newFilterMask;
|
2013-08-22 17:22:49 +03:00
|
|
|
invalidateFilter();
|
|
|
|
}
|
|
|
|
|
2024-11-14 18:42:30 +02:00
|
|
|
bool CModFilterModel::filterMatchesCategory(const QModelIndex & source) const
|
|
|
|
{
|
|
|
|
QString modID =source.data(ModRoles::ModNameRole).toString();
|
|
|
|
ModState mod = base->model->getMod(modID);
|
|
|
|
|
|
|
|
switch (filterMask)
|
|
|
|
{
|
|
|
|
case ModFilterMask::ALL:
|
|
|
|
return true;
|
|
|
|
case ModFilterMask::AVAILABLE:
|
|
|
|
return !mod.isInstalled();
|
|
|
|
case ModFilterMask::INSTALLED:
|
|
|
|
return mod.isInstalled();
|
|
|
|
case ModFilterMask::UPDATEABLE:
|
|
|
|
return mod.isUpdateAvailable();
|
|
|
|
case ModFilterMask::ENABLED:
|
2024-11-14 20:43:41 +02:00
|
|
|
return mod.isInstalled() && base->model->isModEnabled(modID);
|
2024-11-14 18:42:30 +02:00
|
|
|
case ModFilterMask::DISABLED:
|
2024-11-14 20:43:41 +02:00
|
|
|
return mod.isInstalled() && !base->model->isModEnabled(modID);
|
2024-11-14 18:42:30 +02:00
|
|
|
}
|
|
|
|
assert(0);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-04-13 07:34:58 +02:00
|
|
|
bool CModFilterModel::filterMatchesThis(const QModelIndex & source) const
|
2013-08-22 17:22:49 +03:00
|
|
|
{
|
2024-11-14 18:42:30 +02:00
|
|
|
return filterMatchesCategory(source) && 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);
|
2024-11-12 22:00:21 +02:00
|
|
|
QString modID = index.data(ModRoles::ModNameRole).toString();
|
2024-11-13 19:25:59 +02:00
|
|
|
if (base->model->getMod(modID).isHidden())
|
2023-10-21 22:55:20 +02:00
|
|
|
return false;
|
|
|
|
|
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
|
|
|
{
|
2024-11-18 16:40:15 +02:00
|
|
|
if(filterMatchesThis(base->index(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;
|
|
|
|
}
|
|
|
|
|
2024-11-12 22:00:21 +02:00
|
|
|
CModFilterModel::CModFilterModel(ModStateItemModel * model, QObject * parent)
|
|
|
|
: QSortFilterProxyModel(parent), base(model), filterMask(ModFilterMask::ALL)
|
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
|
|
|
}
|