cmodlistmodel_moc.cpp 7.5 KB

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