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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
#include "RecordStructureModel.h"
#include "TESData/SingleRecordParser.h"
#include "TESFile/Reader.h"
#include <log.h>
#include <boost/container/flat_map.hpp>
namespace BSPluginInfo
{
RecordStructureModel::RecordStructureModel(TESData::PluginList* pluginList,
TESData::Record* record,
const TESData::RecordPath& path,
MOBase::IOrganizer* organizer)
: m_Organizer{organizer}, m_PluginList{pluginList}, m_Record{record}, m_Path{path},
m_Root{std::make_shared<Item>()}
{
refresh();
}
void RecordStructureModel::refresh()
{
boost::container::flat_map<int, const TESData::FileEntry*> entries;
for (const auto handle : m_Record->alternatives()) {
const auto entry = m_PluginList->findEntryByHandle(handle);
const auto info =
m_PluginList->getPluginByName(QString::fromStdString(entry->name()));
if (info && info->enabled()) {
entries.emplace(info->priority(), entry);
}
}
m_Files.resize(entries.size());
for (int index = static_cast<int>(entries.size()) - 1; index >= 0; --index) {
const auto entry = entries.nth(index)->second;
const auto name = QString::fromStdString(entry->name());
const auto vfsEntry =
m_Organizer->virtualFileTree()->find(name, MOBase::FileTreeEntry::FILE);
const auto filePath = m_Organizer->resolvePath(name);
m_Files[index] = QFileInfo(filePath).fileName();
readFile(m_Path, filePath, index);
}
}
void RecordStructureModel::readFile(const TESData::RecordPath& path,
const QString& filePath, int index)
try {
const auto fileName = QFileInfo(filePath).fileName().toStdString();
const auto gameName = m_Organizer->managedGame()->gameName();
TESData::SingleRecordParser handler(gameName, path, fileName, m_Root.get(), index);
TESFile::Reader<TESData::SingleRecordParser> reader{};
reader.parse(std::filesystem::path(filePath.toStdWString()), handler);
} catch (const std::exception& e) {
MOBase::log::error("Error parsing \"{}\": {}", filePath, e.what());
}
QModelIndex RecordStructureModel::index(int row, int column,
const QModelIndex& parent) const
{
if (row < 0 || column < 0) {
return QModelIndex();
}
const auto parentItem = parent.isValid()
? static_cast<const Item*>(parent.internalPointer())
: m_Root.get();
if (parentItem && row < parentItem->numChildren()) {
const auto childItem = parentItem->childAt(row);
return createIndex(row, column, childItem);
}
return QModelIndex();
}
QModelIndex RecordStructureModel::parent(const QModelIndex& index) const
{
const auto item =
index.isValid() ? static_cast<const Item*>(index.internalPointer()) : nullptr;
const auto parentItem = item ? item->parent() : nullptr;
if (!parentItem) {
return QModelIndex();
}
const int row = parentItem->index();
return createIndex(row, 0, parentItem);
}
int RecordStructureModel::rowCount(const QModelIndex& parent) const
{
const auto item = parent.isValid()
? static_cast<const Item*>(parent.internalPointer())
: m_Root.get();
return item ? item->numChildren() : 0;
}
int RecordStructureModel::columnCount([[maybe_unused]] const QModelIndex& parent) const
{
return 1 + static_cast<int>(m_Files.size());
}
QVariant RecordStructureModel::data(const QModelIndex& index, int role) const
{
const auto item =
index.isValid() ? static_cast<const Item*>(index.internalPointer()) : nullptr;
if (!item) {
return QVariant();
}
switch (role) {
case Qt::DisplayRole:
case Qt::EditRole:
if (index.column() == 0) {
return item->rowHeader();
} else {
return item->displayData(index.column() - 1);
}
case Qt::BackgroundRole: {
if (index.column() == 0) {
return QVariant();
}
const int count = columnCount() - 1;
const int fileIndex = index.column() - 1;
if (item->isLosingConflict(fileIndex, count)) {
return QColor(255, 0, 0, 64);
} else if (item->isOverriding(fileIndex)) {
return QColor(0, 255, 0, 64);
}
return QVariant();
}
case Qt::UserRole:
return QVariant::fromValue(item);
}
return QVariant();
}
QVariant RecordStructureModel::headerData(int section, Qt::Orientation orientation,
int role) const
{
if (orientation != Qt::Horizontal)
return QVariant();
switch (role) {
case Qt::DisplayRole: {
if (section == 0) {
// Name
return QVariant();
}
if (section - 1 < m_Files.length()) {
return m_Files[section - 1];
}
}
}
return QVariant();
}
} // namespace BSPluginInfo
|