summaryrefslogtreecommitdiff
path: root/src/modlistgroupcategoriesproxy.cpp
blob: 4320be974212188f42d74cd745920d52aafdd6c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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);
}