cmodlistmodel_moc.cpp 6.7 KB

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