cmodlistmodel_moc.cpp 6.5 KB

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