From 0a19f0433992be5f9791c8869380d09cd06b66b8 Mon Sep 17 00:00:00 2001 From: Tannin Date: Fri, 22 Mar 2013 18:46:40 +0100 Subject: - support for grouping filters for mod list (incomplete) - offering multiple options for mod names during installation - support for renaming profiles - updated installer plugins - minor bugfixes --- src/modlistgroupcategoriesproxy.cpp | 123 ++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 src/modlistgroupcategoriesproxy.cpp (limited to 'src/modlistgroupcategoriesproxy.cpp') diff --git a/src/modlistgroupcategoriesproxy.cpp b/src/modlistgroupcategoriesproxy.cpp new file mode 100644 index 00000000..4320be97 --- /dev/null +++ b/src/modlistgroupcategoriesproxy.cpp @@ -0,0 +1,123 @@ +#include "modlistgroupcategoriesproxy.h" +#include "categories.h" +#include "modinfo.h" + + +ModListGroupCategoriesProxy::ModListGroupCategoriesProxy(QObject *parent) + : QAbstractProxyModel(parent) +{ +} + +int ModListGroupCategoriesProxy::rowCount(const QModelIndex &parent) const +{ + if (!parent.isValid()) { + return CategoryFactory::instance().numCategories(); + } else { + unsigned int count = 0; + for (int i = 0; i < sourceModel()->rowCount(); ++i) { + if (ModInfo::getByIndex(i)->getPrimaryCategory() == + CategoryFactory::instance().getCategoryID(parent.row())) { + ++count; + } + } + return count; + } +} + +int ModListGroupCategoriesProxy::columnCount(const QModelIndex &parent) const +{ + return sourceModel()->columnCount(mapToSource(parent)); +} + +QModelIndex ModListGroupCategoriesProxy::mapToSource(const QModelIndex &proxyIndex) const +{ + auto iter = m_IndexMap.find(proxyIndex); + if (iter != m_IndexMap.end()) { + return iter->second; + } else { + return QModelIndex(); + } +} + +QModelIndex ModListGroupCategoriesProxy::mapFromSource(const QModelIndex &sourceIndex) const +{ + if (!sourceIndex.isValid()) { + return QModelIndex(); + } else { + ModInfo::Ptr mod = ModInfo::getByIndex(sourceIndex.data(Qt::UserRole + 1).toInt()); + return index(sourceIndex.row(), sourceIndex.column(), index(mod->getPrimaryCategory(), 0, QModelIndex())); + } +} + +QModelIndex ModListGroupCategoriesProxy::index(int row, int column, const QModelIndex &parent) const +{ + if (parent.isValid()) { + // mod + int categoryId = CategoryFactory::instance().getCategoryID(parent.row()); + + int srcRow = 0; + int offset = row; + for (; srcRow < sourceModel()->rowCount(); ++srcRow) { + int modId = sourceModel()->index(srcRow, column).data(Qt::UserRole + 1).toInt(); + if (ModInfo::getByIndex(modId)->getPrimaryCategory() == categoryId) { + + if (--offset < 0) { + break; + } + } + } + QPersistentModelIndex idx = createIndex(row, column, categoryId); + m_IndexMap[idx] = QPersistentModelIndex(sourceModel()->index(srcRow, column)); + return idx; + } else { + // category + return createIndex(row, column, -1); + } +} + +QModelIndex ModListGroupCategoriesProxy::parent(const QModelIndex &child) const +{ + if (!child.isValid()) { + return QModelIndex(); + } else if (child.internalId() == -1) { + return QModelIndex(); // top level + } else { + return index(CategoryFactory::instance().getCategoryIndex(child.internalId()), 0, QModelIndex()); + } +} + +bool ModListGroupCategoriesProxy::hasChildren(const QModelIndex &parent) const +{ + // root item always has children (the categories), mods don't + if (!parent.isValid()) return true; + else if (parent.internalId() != -1) return false; + + for (int i = 0; i < sourceModel()->rowCount(); ++i) { + ModInfo::Ptr mod = ModInfo::getByIndex(i); + if (mod->getPrimaryCategory() == CategoryFactory::instance().getCategoryID(parent.row())) { + return true; + } + } + return false; +} + +QVariant ModListGroupCategoriesProxy::data(const QModelIndex &proxyIndex, int role) const +{ + auto iter = m_IndexMap.find(proxyIndex); + if (iter != m_IndexMap.end()) { + // mod + return sourceModel()->data(iter->second, role); + } else { + // category + if ((role == Qt::DisplayRole) && (proxyIndex.column() == 0)) { + return CategoryFactory::instance().getCategoryName(proxyIndex.row()); + } else { + return QVariant(); + } + } +} + +QVariant ModListGroupCategoriesProxy::headerData(int section, Qt::Orientation orientation, int role) const +{ + return sourceModel()->headerData(section, orientation, role); +} -- cgit v1.3.1