1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
|
/*
* 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
*
*/
#include "StdInc.h"
#include "cmodlistmodel_moc.h"
#include <QIcon>
namespace ModFields
{
static const QString names[ModFields::COUNT] =
{
"name",
"",
"",
"modType",
"version",
"size",
"author"
};
static const QString header[ModFields::COUNT] =
{
"Name",
"", // status icon
"", // status icon
"Type",
"Version",
"Size",
"Author"
};
}
namespace ModStatus
{
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";
}
CModListModel::CModListModel(QObject * parent)
: QAbstractItemModel(parent)
{
}
QString CModListModel::modIndexToName(const QModelIndex & index) const
{
if(index.isValid())
{
return modNameToID.at(index.internalId());
}
return "";
}
QVariant CModListModel::getValue(const CModEntry & mod, int field) const
{
switch(field)
{
case ModFields::STATUS_ENABLED:
return mod.getModStatus() & (ModStatus::ENABLED | ModStatus::INSTALLED);
case ModFields::STATUS_UPDATE:
return mod.getModStatus() & (ModStatus::UPDATEABLE | ModStatus::INSTALLED);
default:
return mod.getValue(ModFields::names[field]);
}
}
QVariant CModListModel::getText(const CModEntry & mod, int field) const
{
switch(field)
{
case ModFields::STATUS_ENABLED:
case ModFields::STATUS_UPDATE:
return "";
case ModFields::SIZE:
return CModEntry::sizeToString(getValue(mod, field).toDouble());
default:
return getValue(mod, field);
}
}
QVariant CModListModel::getIcon(const CModEntry & mod, int field) const
{
if(field == ModFields::STATUS_ENABLED && mod.isEnabled())
return QIcon(ModStatus::iconEnabled);
if(field == ModFields::STATUS_ENABLED && mod.isDisabled())
return QIcon(ModStatus::iconDisabled);
if(field == ModFields::STATUS_UPDATE && mod.isUpdateable())
return QIcon(ModStatus::iconUpdate);
if(field == ModFields::STATUS_UPDATE && !mod.isInstalled())
return QIcon(ModStatus::iconDownload);
return QVariant();
}
QVariant CModListModel::getTextAlign(int field) const
{
if(field == ModFields::SIZE)
return QVariant(Qt::AlignRight | Qt::AlignVCenter);
//if (field == ModFields::NAME)
// return QVariant(Qt::AlignHCenter | Qt::AlignVCenter);
return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
}
QVariant CModListModel::data(const QModelIndex & index, int role) const
{
if(index.isValid())
{
auto mod = getMod(modIndexToName(index));
switch(role)
{
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();
}
}
return QVariant();
}
int CModListModel::rowCount(const QModelIndex & index) const
{
if(index.isValid())
return modIndex[modIndexToName(index)].size();
return modIndex[""].size();
}
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
{
if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
return ModFields::header[section];
return QVariant();
}
void CModListModel::resetRepositories()
{
beginResetModel();
CModList::resetRepositories();
endResetModel();
}
void CModListModel::addRepository(QVariantMap data)
{
beginResetModel();
CModList::addRepository(data);
endResetModel();
}
void CModListModel::modChanged(QString modID)
{
int index = modNameToID.indexOf(modID);
QModelIndex parent = this->parent(createIndex(0, 0, index));
int row = modIndex[modIndexToName(parent)].indexOf(modID);
emit dataChanged(createIndex(row, 0, index), createIndex(row, 4, index));
}
void CModListModel::endResetModel()
{
modNameToID = getModList();
modIndex.clear();
for(const QString & str : modNameToID)
{
if(str.contains('.'))
{
modIndex[str.section('.', 0, -2)].append(str);
}
else
{
modIndex[""].append(str);
}
}
QAbstractItemModel::endResetModel();
}
QModelIndex CModListModel::index(int row, int column, const QModelIndex & parent) const
{
if(parent.isValid())
{
if(modIndex[modIndexToName(parent)].size() > row)
return createIndex(row, column, modNameToID.indexOf(modIndex[modIndexToName(parent)][row]));
}
else
{
if(modIndex[""].size() > row)
return createIndex(row, column, modNameToID.indexOf(modIndex[""][row]));
}
return QModelIndex();
}
QModelIndex CModListModel::parent(const QModelIndex & child) const
{
QString modID = modNameToID[child.internalId()];
for(auto entry = modIndex.begin(); entry != modIndex.end(); entry++) // because using range-for entry type is QMap::value_type oO
{
if(entry.key() != "" && entry.value().indexOf(modID) != -1)
{
return createIndex(entry.value().indexOf(modID), child.column(), modNameToID.indexOf(entry.key()));
}
}
return QModelIndex();
}
void CModFilterModel::setTypeFilter(int filteredType, int filterMask)
{
this->filterMask = filterMask;
this->filteredType = filteredType;
invalidateFilter();
}
bool CModFilterModel::filterMatchesThis(const QModelIndex & source) const
{
CModEntry mod = base->getMod(source.data(ModRoles::ModNameRole).toString());
return (mod.getModStatus() & filterMask) == filteredType &&
QSortFilterProxyModel::filterAcceptsRow(source.row(), source.parent());
}
bool CModFilterModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
{
QModelIndex index = base->index(source_row, 0, source_parent);
if(filterMatchesThis(index))
{
return true;
}
for(size_t i = 0; i < base->rowCount(index); i++)
{
if(filterMatchesThis(index.child(i, 0)))
return true;
}
QModelIndex parent = source_parent;
while(parent.isValid())
{
if(filterMatchesThis(parent))
return true;
parent = parent.parent();
}
return false;
}
CModFilterModel::CModFilterModel(CModListModel * model, QObject * parent)
: QSortFilterProxyModel(parent), base(model), filteredType(ModStatus::MASK_NONE), filterMask(ModStatus::MASK_NONE)
{
setSourceModel(model);
setSortRole(ModRoles::ValueRole);
}
|