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
124
125
126
127
128
129
130
|
#include "modlistgroupnexusidproxy.h"
#include "modinfo.h"
ModListGroupNexusIDProxy::ModListGroupNexusIDProxy(Profile *profile, QObject *parent)
: QAbstractProxyModel(parent)
{
refreshMap(profile);
}
void ModListGroupNexusIDProxy::refreshMap(Profile *profile)
{
int row = 0;
for (unsigned int i = 0; i < ModInfo::getNumMods(); ++i) {
int nexusID = ModInfo::getByIndex(i)->getNexusID();
if (m_GroupMap.find(nexusID) == m_GroupMap.end()) {
m_RowIdxMap[row] = i;
m_IdxRowMap[i] = row;
++row;
}
m_GroupMap[nexusID].push_back(i);
}
for (auto iter = m_GroupMap.begin(); iter != m_GroupMap.end(); ++iter) {
std::sort(iter->second.begin(), iter->second.end(),
[profile] (unsigned int LHS, unsigned int RHS) {
return profile->getModPriority(LHS) < profile->getModPriority(RHS);
});
}
}
int ModListGroupNexusIDProxy::rowCount(const QModelIndex &parent) const
{
int res = 0;
if (!parent.isValid()) {
res = m_GroupMap.size();
} else {
ModInfo::Ptr info = ModInfo::getByIndex(parent.internalId());
res = m_GroupMap.at(info->getNexusID()).size();
}
return res;
}
int ModListGroupNexusIDProxy::columnCount(const QModelIndex &parent) const
{
return sourceModel()->columnCount(mapToSource(parent));
}
QModelIndex ModListGroupNexusIDProxy::mapToSource(const QModelIndex &proxyIndex) const
{
auto iter = m_IndexMap.find(proxyIndex);
if (iter != m_IndexMap.end()) {
return iter->second;
} else {
return QModelIndex();
}
}
QModelIndex ModListGroupNexusIDProxy::mapFromSource(const QModelIndex &sourceIndex) const
{
unsigned int modID = sourceIndex.data(Qt::UserRole + 1).toInt();
ModInfo::Ptr mod = ModInfo::getByIndex(modID);
const std::vector<unsigned int> &subMods = m_GroupMap.at(mod->getNexusID());
if (subMods.size() == 0) return QModelIndex();
QModelIndex parent = QModelIndex();
if (modID != subMods[0]) {
parent = index(m_IdxRowMap.at(subMods[0]), 0, QModelIndex());
}
return index(sourceIndex.row(), sourceIndex.column(), parent);
}
QModelIndex ModListGroupNexusIDProxy::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid()) {
// sub-mod
ModInfo::Ptr parentMod = ModInfo::getByIndex(parent.internalId());
const std::vector<unsigned int> &subMods = m_GroupMap.at(parentMod->getNexusID());
if ((row < 0) || (row >= static_cast<int>(subMods.size()))) {
qCritical("invalid index: %dx%d", row, column);
return QModelIndex();
}
QPersistentModelIndex idx = createIndex(row, column, subMods[row]);
m_IndexMap[idx] = QPersistentModelIndex(sourceModel()->index(subMods[row], column));
return idx;
} else {
// top-level mod
return createIndex(row, column, m_RowIdxMap.at(row));
}
}
QModelIndex ModListGroupNexusIDProxy::parent(const QModelIndex &child) const
{
if (!child.isValid()) return QModelIndex();
ModInfo::Ptr mod = ModInfo::getByIndex(child.internalId());
if (m_GroupMap.at(mod->getNexusID()).size() < 1) {
return QModelIndex();
}
if (m_GroupMap.at(mod->getNexusID())[0] == child.internalId()) {
return QModelIndex();
} else {
return index(m_IdxRowMap.at(m_GroupMap.at(mod->getNexusID())[0]), 0, QModelIndex());
}
}
bool ModListGroupNexusIDProxy::hasChildren(const QModelIndex &parent) const
{
if (!parent.isValid()) return true;
ModInfo::Ptr mod = ModInfo::getByIndex(parent.internalId());
return m_GroupMap.at(mod->getNexusID()).size() > 1;
}
QVariant ModListGroupNexusIDProxy::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 {
return QVariant();
}
}
QVariant ModListGroupNexusIDProxy::headerData(int section, Qt::Orientation orientation, int role) const
{
return sourceModel()->headerData(section, orientation, role);
}
|