cmodlistmodel_moc.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. /*
  2. * cmodlistmodel_moc.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "cmodlistmodel_moc.h"
  12. #include <QIcon>
  13. namespace ModStatus
  14. {
  15. static const QString iconDelete = "icons:mod-delete.png";
  16. static const QString iconDisabled = "icons:mod-disabled.png";
  17. static const QString iconDownload = "icons:mod-download.png";
  18. static const QString iconEnabled = "icons:mod-enabled.png";
  19. static const QString iconUpdate = "icons:mod-update.png";
  20. }
  21. CModListModel::CModListModel(QObject * parent)
  22. : QAbstractItemModel(parent)
  23. {
  24. }
  25. QString CModListModel::modIndexToName(const QModelIndex & index) const
  26. {
  27. if(index.isValid())
  28. {
  29. return modNameToID.at(index.internalId());
  30. }
  31. return "";
  32. }
  33. QString CModListModel::modTypeName(QString modTypeID) const
  34. {
  35. static QMap<QString, QString> modTypes = {
  36. {"Translation", tr("Translation")},
  37. {"Town", tr("Town") },
  38. {"Test", tr("Test") },
  39. {"Templates", tr("Templates") },
  40. {"Spells", tr("Spells") },
  41. {"Music", tr("Music") },
  42. {"Maps", tr("Maps") },
  43. {"Sounds", tr("Sounds") },
  44. {"Skills", tr("Skills") },
  45. {"Other", tr("Other") },
  46. {"Objects", tr("Objects") },
  47. {"Mechanical", tr("Mechanics") },
  48. {"Mechanics", tr("Mechanics") },
  49. {"Themes", tr("Interface") },
  50. {"Interface", tr("Interface") },
  51. {"Heroes", tr("Heroes") },
  52. {"Graphic", tr("Graphical") },
  53. {"Graphical", tr("Graphical") },
  54. {"Expansion", tr("Expansion") },
  55. {"Creatures", tr("Creatures") },
  56. {"Compatibility", tr("Compatibility") },
  57. {"Artifacts", tr("Artifacts") },
  58. {"AI", tr("AI") },
  59. };
  60. if (modTypes.contains(modTypeID))
  61. return modTypes[modTypeID];
  62. return tr("Other");
  63. }
  64. QVariant CModListModel::getValue(const CModEntry & mod, int field) const
  65. {
  66. switch(field)
  67. {
  68. case ModFields::STATUS_ENABLED:
  69. return mod.getModStatus() & (ModStatus::ENABLED | ModStatus::INSTALLED);
  70. case ModFields::STATUS_UPDATE:
  71. return mod.getModStatus() & (ModStatus::UPDATEABLE | ModStatus::INSTALLED);
  72. case ModFields::NAME:
  73. return mod.getValue("name");
  74. case ModFields::VERSION:
  75. return mod.getValue("version");
  76. case ModFields::TYPE:
  77. return modTypeName(mod.getValue("modType").toString());
  78. default:
  79. return QVariant();
  80. }
  81. }
  82. QVariant CModListModel::getText(const CModEntry & mod, int field) const
  83. {
  84. switch(field)
  85. {
  86. case ModFields::STATUS_ENABLED:
  87. case ModFields::STATUS_UPDATE:
  88. return "";
  89. default:
  90. return getValue(mod, field);
  91. }
  92. }
  93. QVariant CModListModel::getIcon(const CModEntry & mod, int field) const
  94. {
  95. if(field == ModFields::STATUS_ENABLED && mod.isEnabled())
  96. return QIcon(ModStatus::iconEnabled);
  97. if(field == ModFields::STATUS_ENABLED && mod.isDisabled())
  98. return QIcon(ModStatus::iconDisabled);
  99. if(field == ModFields::STATUS_UPDATE && mod.isUpdateable())
  100. return QIcon(ModStatus::iconUpdate);
  101. if(field == ModFields::STATUS_UPDATE && !mod.isInstalled())
  102. return QIcon(ModStatus::iconDownload);
  103. return QVariant();
  104. }
  105. QVariant CModListModel::getTextAlign(int field) const
  106. {
  107. return QVariant(Qt::AlignLeft | Qt::AlignVCenter);
  108. }
  109. QVariant CModListModel::data(const QModelIndex & index, int role) const
  110. {
  111. if(index.isValid())
  112. {
  113. auto mod = getMod(modIndexToName(index));
  114. switch(role)
  115. {
  116. case Qt::DecorationRole:
  117. return getIcon(mod, index.column());
  118. case Qt::DisplayRole:
  119. return getText(mod, index.column());
  120. case Qt::TextAlignmentRole:
  121. return getTextAlign(index.column());
  122. case ModRoles::ValueRole:
  123. return getValue(mod, index.column());
  124. case ModRoles::ModNameRole:
  125. return mod.getName();
  126. }
  127. }
  128. return QVariant();
  129. }
  130. int CModListModel::rowCount(const QModelIndex & index) const
  131. {
  132. if(index.isValid())
  133. return modIndex[modIndexToName(index)].size();
  134. return modIndex[""].size();
  135. }
  136. int CModListModel::columnCount(const QModelIndex &) const
  137. {
  138. return ModFields::COUNT;
  139. }
  140. Qt::ItemFlags CModListModel::flags(const QModelIndex &) const
  141. {
  142. return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  143. }
  144. QVariant CModListModel::headerData(int section, Qt::Orientation orientation, int role) const
  145. {
  146. static const QString header[ModFields::COUNT] =
  147. {
  148. QT_TR_NOOP("Name"),
  149. QT_TR_NOOP(""), // status icon
  150. QT_TR_NOOP(""), // status icon
  151. QT_TR_NOOP("Type"),
  152. QT_TR_NOOP("Version"),
  153. };
  154. if(role == Qt::DisplayRole && orientation == Qt::Horizontal)
  155. return QCoreApplication::translate("ModFields", header[section].toStdString().c_str());
  156. return QVariant();
  157. }
  158. void CModListModel::reloadRepositories()
  159. {
  160. beginResetModel();
  161. endResetModel();
  162. }
  163. void CModListModel::resetRepositories()
  164. {
  165. beginResetModel();
  166. CModList::resetRepositories();
  167. endResetModel();
  168. }
  169. void CModListModel::modChanged(QString modID)
  170. {
  171. int index = modNameToID.indexOf(modID);
  172. QModelIndex parent = this->parent(createIndex(0, 0, index));
  173. int row = modIndex[modIndexToName(parent)].indexOf(modID);
  174. emit dataChanged(createIndex(row, 0, index), createIndex(row, 4, index));
  175. }
  176. void CModListModel::endResetModel()
  177. {
  178. modNameToID = getModList();
  179. modIndex.clear();
  180. for(const QString & str : modNameToID)
  181. {
  182. if(str.contains('.'))
  183. {
  184. modIndex[str.section('.', 0, -2)].append(str);
  185. }
  186. else
  187. {
  188. modIndex[""].append(str);
  189. }
  190. }
  191. QAbstractItemModel::endResetModel();
  192. }
  193. QModelIndex CModListModel::index(int row, int column, const QModelIndex & parent) const
  194. {
  195. if(parent.isValid())
  196. {
  197. if(modIndex[modIndexToName(parent)].size() > row)
  198. return createIndex(row, column, modNameToID.indexOf(modIndex[modIndexToName(parent)][row]));
  199. }
  200. else
  201. {
  202. if(modIndex[""].size() > row)
  203. return createIndex(row, column, modNameToID.indexOf(modIndex[""][row]));
  204. }
  205. return QModelIndex();
  206. }
  207. QModelIndex CModListModel::parent(const QModelIndex & child) const
  208. {
  209. QString modID = modNameToID[child.internalId()];
  210. for(auto entry = modIndex.begin(); entry != modIndex.end(); entry++) // because using range-for entry type is QMap::value_type oO
  211. {
  212. if(entry.key() != "" && entry.value().indexOf(modID) != -1)
  213. {
  214. return createIndex(entry.value().indexOf(modID), child.column(), modNameToID.indexOf(entry.key()));
  215. }
  216. }
  217. return QModelIndex();
  218. }
  219. void CModFilterModel::setTypeFilter(int filteredType, int filterMask)
  220. {
  221. this->filterMask = filterMask;
  222. this->filteredType = filteredType;
  223. invalidateFilter();
  224. }
  225. bool CModFilterModel::filterMatchesThis(const QModelIndex & source) const
  226. {
  227. CModEntry mod = base->getMod(source.data(ModRoles::ModNameRole).toString());
  228. return (mod.getModStatus() & filterMask) == filteredType &&
  229. QSortFilterProxyModel::filterAcceptsRow(source.row(), source.parent());
  230. }
  231. bool CModFilterModel::filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
  232. {
  233. QModelIndex index = base->index(source_row, 0, source_parent);
  234. CModEntry mod = base->getMod(index.data(ModRoles::ModNameRole).toString());
  235. if (!mod.isVisible())
  236. return false;
  237. if(filterMatchesThis(index))
  238. {
  239. return true;
  240. }
  241. for(size_t i = 0; i < base->rowCount(index); i++)
  242. {
  243. if(filterMatchesThis(base->index((int)i, 0, index)))
  244. return true;
  245. }
  246. QModelIndex parent = source_parent;
  247. while(parent.isValid())
  248. {
  249. if(filterMatchesThis(parent))
  250. return true;
  251. parent = parent.parent();
  252. }
  253. return false;
  254. }
  255. CModFilterModel::CModFilterModel(CModListModel * model, QObject * parent)
  256. : QSortFilterProxyModel(parent), base(model), filteredType(ModStatus::MASK_NONE), filterMask(ModStatus::MASK_NONE)
  257. {
  258. setSourceModel(model);
  259. setSortRole(ModRoles::ValueRole);
  260. }