Skip to content

Commit

Permalink
Make category struct attributes private
Browse files Browse the repository at this point in the history
  • Loading branch information
Silarn committed Sep 24, 2023
1 parent b7d4d20 commit efdff42
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 58 deletions.
61 changes: 28 additions & 33 deletions src/categories.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ void CategoryFactory::loadCategories()
nexCells[0].constData());
}
m_NexusMap.insert_or_assign(nexID, NexusCategory(nexName, nexID));
m_NexusMap.at(nexID).m_CategoryID = catID;
m_NexusMap.at(nexID).setCategoryID(catID);
} else {
log::error(tr("invalid nexus category line {}: {} ({} cells)").toStdString(),
lineNum, nexLine.constData(), nexCells.count());
Expand All @@ -157,27 +157,23 @@ void CategoryFactory::reset()
m_Categories.clear();
m_NexusMap.clear();
m_IDMap.clear();
// 28 =
// 43 = Savegames (makes no sense to install them through MO)
// 45 = Videos and trailers
// 87 = Miscelanous
addCategory(0, "None", std::vector<NexusCategory>(), 0);
}

void CategoryFactory::setParents()
{
for (std::vector<Category>::iterator iter = m_Categories.begin();
iter != m_Categories.end(); ++iter) {
iter->m_HasChildren = false;
iter->setHasChildren(false);
}

for (std::vector<Category>::const_iterator categoryIter = m_Categories.begin();
categoryIter != m_Categories.end(); ++categoryIter) {
if (categoryIter->m_ParentID != 0) {
if (categoryIter->parentID() != 0) {
std::map<int, unsigned int>::const_iterator iter =
m_IDMap.find(categoryIter->m_ParentID);
m_IDMap.find(categoryIter->parentID());
if (iter != m_IDMap.end()) {
m_Categories[iter->second].m_HasChildren = true;
m_Categories[iter->second].setHasChildren(true);
}
}
}
Expand All @@ -201,15 +197,15 @@ void CategoryFactory::saveCategories()
categoryFile.resize(0);
for (std::vector<Category>::const_iterator iter = m_Categories.begin();
iter != m_Categories.end(); ++iter) {
if (iter->m_ID == 0) {
if (iter->ID() == 0) {
continue;
}
QByteArray line;
line.append(QByteArray::number(iter->m_ID))
line.append(QByteArray::number(iter->ID()))
.append("|")
.append(iter->m_Name.toUtf8())
.append(iter->name().toUtf8())
.append("|")
.append(QByteArray::number(iter->m_ParentID))
.append(QByteArray::number(iter->parentID()))
.append("\n");
categoryFile.write(line);
}
Expand All @@ -225,9 +221,9 @@ void CategoryFactory::saveCategories()
nexusMapFile.resize(0);
for (auto iter = m_NexusMap.begin(); iter != m_NexusMap.end(); ++iter) {
QByteArray line;
line.append(QByteArray::number(iter->second.m_CategoryID)).append("|");
line.append(iter->second.m_Name.toUtf8()).append("|");
line.append(QByteArray::number(iter->second.m_ID)).append("\n");
line.append(QByteArray::number(iter->second.categoryID())).append("|");
line.append(iter->second.name().toUtf8()).append("|");
line.append(QByteArray::number(iter->second.ID())).append("\n");
nexusMapFile.write(line);
}
nexusMapFile.close();
Expand Down Expand Up @@ -274,8 +270,8 @@ void CategoryFactory::addCategory(int id, const QString& name,
int parentID)
{
for (auto nexusCat : nexusCats) {
m_NexusMap.insert_or_assign(nexusCat.m_ID, nexusCat);
m_NexusMap.at(nexusCat.m_ID).m_CategoryID = id;
m_NexusMap.insert_or_assign(nexusCat.ID(), nexusCat);
m_NexusMap.at(nexusCat.ID()).setCategoryID(id);
}
int index = static_cast<int>(m_Categories.size());
m_Categories.push_back(Category(index, id, name, parentID, nexusCats));
Expand All @@ -285,9 +281,8 @@ void CategoryFactory::addCategory(int id, const QString& name,
void CategoryFactory::setNexusCategories(
std::vector<CategoryFactory::NexusCategory>& nexusCats)
{
m_NexusMap.clear();
for (auto nexusCat : nexusCats) {
m_NexusMap.emplace(nexusCat.m_ID, nexusCat);
m_NexusMap.emplace(nexusCat.ID(), nexusCat);
}

saveCategories();
Expand Down Expand Up @@ -367,7 +362,7 @@ int CategoryFactory::getParentID(unsigned int index) const
throw MyException(tr("invalid category index: %1").arg(index));
}

return m_Categories[index].m_ParentID;
return m_Categories[index].parentID();
}

bool CategoryFactory::categoryExists(int id) const
Expand All @@ -394,12 +389,12 @@ bool CategoryFactory::isDescendantOfImpl(int id, int parentID,

if (iter != m_IDMap.end()) {
unsigned int index = iter->second;
if (m_Categories[index].m_ParentID == 0) {
if (m_Categories[index].parentID() == 0) {
return false;
} else if (m_Categories[index].m_ParentID == parentID) {
} else if (m_Categories[index].parentID() == parentID) {
return true;
} else {
return isDescendantOfImpl(m_Categories[index].m_ParentID, parentID, seen);
return isDescendantOfImpl(m_Categories[index].parentID(), parentID, seen);
}
} else {
log::warn(tr("{} is no valid category id").toStdString(), id);
Expand All @@ -413,7 +408,7 @@ bool CategoryFactory::hasChildren(unsigned int index) const
throw MyException(tr("invalid category index: %1").arg(index));
}

return m_Categories[index].m_HasChildren;
return m_Categories[index].hasChildren();
}

QString CategoryFactory::getCategoryName(unsigned int index) const
Expand All @@ -422,7 +417,7 @@ QString CategoryFactory::getCategoryName(unsigned int index) const
throw MyException(tr("invalid category index: %1").arg(index));
}

return m_Categories[index].m_Name;
return m_Categories[index].name();
}

QString CategoryFactory::getSpecialCategoryName(SpecialCategories type) const
Expand Down Expand Up @@ -480,7 +475,7 @@ QString CategoryFactory::getCategoryNameByID(int id) const
return {};
}

return m_Categories[index].m_Name;
return m_Categories[index].name();
}
}

Expand All @@ -490,7 +485,7 @@ int CategoryFactory::getCategoryID(unsigned int index) const
throw MyException(tr("invalid category index: %1").arg(index));
}

return m_Categories[index].m_ID;
return m_Categories[index].ID();
}

int CategoryFactory::getCategoryIndex(int ID) const
Expand All @@ -506,11 +501,11 @@ int CategoryFactory::getCategoryID(const QString& name) const
{
auto iter = std::find_if(m_Categories.begin(), m_Categories.end(),
[name](const Category& cat) -> bool {
return cat.m_Name == name;
return cat.name() == name;
});

if (iter != m_Categories.end()) {
return iter->m_ID;
return iter->ID();
} else {
return -1;
}
Expand All @@ -520,10 +515,10 @@ unsigned int CategoryFactory::resolveNexusID(int nexusID) const
{
auto result = m_NexusMap.find(nexusID);
if (result != m_NexusMap.end()) {
if (m_IDMap.count(result->second.m_CategoryID)) {
if (m_IDMap.count(result->second.categoryID())) {
log::debug(tr("nexus category id {} maps to internal {}").toStdString(), nexusID,
m_IDMap.at(result->second.m_CategoryID));
return m_IDMap.at(result->second.m_CategoryID);
m_IDMap.at(result->second.categoryID()));
return m_IDMap.at(result->second.categoryID());
}
}
log::debug(tr("nexus category id {} not mapped").toStdString(), nexusID);
Expand Down
44 changes: 30 additions & 14 deletions src/categories.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,46 +58,62 @@ class CategoryFactory : public QObject
public:
struct NexusCategory
{
NexusCategory(const QString& name, const int nexusID) : m_Name(name), m_ID(nexusID)
NexusCategory(const QString name, const int nexusID) : m_Name(name), m_ID(nexusID)
{}
QString m_Name;
int m_ID;
int m_CategoryID = -1;

friend bool operator==(const NexusCategory& LHS, const NexusCategory& RHS)
{
return LHS.m_ID == RHS.m_ID;
return LHS.ID() == RHS.ID();
}

friend bool operator==(const NexusCategory& LHS, const int RHS)
{
return LHS.m_ID == RHS;
return LHS.ID() == RHS;
}

friend bool operator<(const NexusCategory& LHS, const NexusCategory& RHS)
{
return LHS.m_ID < RHS.m_ID;
return LHS.ID() < RHS.ID();
}

QString name() const { return m_Name; }
int ID() const { return m_ID; }
int categoryID() const { return m_CategoryID; }
void setCategoryID(int categoryID) { m_CategoryID = categoryID; }

private:
QString m_Name;
int m_ID;
int m_CategoryID = -1;
};

struct Category
{
Category(int sortValue, int id, const QString& name, int parentID,
Category(int sortValue, int id, const QString name, int parentID,
std::vector<NexusCategory> nexusCats)
: m_SortValue(sortValue), m_ID(id), m_Name(name), m_HasChildren(false),
m_ParentID(parentID), m_NexusCats(nexusCats)
{}

friend bool operator<(const Category& LHS, const Category& RHS)
{
return LHS.sortValue() < RHS.sortValue();
}

int sortValue() const { return m_SortValue; }
int ID() const { return m_ID; }
int parentID() const { return m_ParentID; }
QString name() const { return m_Name; }
bool hasChildren() const { return m_HasChildren; }
void setHasChildren(bool b) { m_HasChildren = b; }

private:
int m_SortValue;
int m_ID;
int m_ParentID;
bool m_HasChildren;
QString m_Name;
std::vector<NexusCategory> m_NexusCats;

friend bool operator<(const Category& LHS, const Category& RHS)
{
return LHS.m_SortValue < RHS.m_SortValue;
}
bool m_HasChildren;
};

public:
Expand Down
16 changes: 8 additions & 8 deletions src/categoriesdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,19 @@ void CategoriesDialog::fillTable()
categories.m_Categories.begin();
iter != categories.m_Categories.end(); ++iter, ++row) {
const CategoryFactory::Category& category = *iter;
if (category.m_ID == 0) {
if (category.ID() == 0) {
--row;
continue;
}
table->insertRow(row);
// table->setVerticalHeaderItem(row, new QTableWidgetItem(" "));

QScopedPointer<QTableWidgetItem> idItem(new QTableWidgetItem());
idItem->setData(Qt::DisplayRole, category.m_ID);
idItem->setData(Qt::DisplayRole, category.ID());

QScopedPointer<QTableWidgetItem> nameItem(new QTableWidgetItem(category.m_Name));
QScopedPointer<QTableWidgetItem> nameItem(new QTableWidgetItem(category.name()));
QScopedPointer<QTableWidgetItem> parentIDItem(new QTableWidgetItem());
parentIDItem->setData(Qt::DisplayRole, category.m_ParentID);
parentIDItem->setData(Qt::DisplayRole, category.parentID());
QScopedPointer<QTableWidgetItem> nexusCatItem(new QTableWidgetItem());

table->setItem(row, 0, idItem.take());
Expand All @@ -236,15 +236,15 @@ void CategoriesDialog::fillTable()

for (auto nexusCat : categories.m_NexusMap) {
QScopedPointer<QListWidgetItem> nexusItem(new QListWidgetItem());
nexusItem->setData(Qt::DisplayRole, nexusCat.second.m_Name);
nexusItem->setData(Qt::UserRole, nexusCat.second.m_ID);
nexusItem->setData(Qt::DisplayRole, nexusCat.second.name());
nexusItem->setData(Qt::UserRole, nexusCat.second.ID());
list->addItem(nexusItem.take());
auto item = table->item(categories.resolveNexusID(nexusCat.first) - 1, 3);
if (item != nullptr) {
auto itemData = item->data(Qt::UserRole).toList();
QVariantList newData;
newData.append(nexusCat.second.m_Name);
newData.append(nexusCat.second.m_ID);
newData.append(nexusCat.second.name());
newData.append(nexusCat.second.ID());
itemData.insert(itemData.length(), newData);
QStringList names;
for (auto cat : itemData) {
Expand Down
2 changes: 1 addition & 1 deletion src/downloadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ void DownloadManager::refreshList()

log::debug("saw {} downloads", m_ActiveDownloads.size());

emit update(-1);
emit update(-1);

} catch (const std::bad_alloc&) {
reportError(tr("Memory allocation error (in refreshing directory)."));
Expand Down
3 changes: 1 addition & 2 deletions src/modlistviewactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,8 +1135,7 @@ void ModListViewActions::remapCategory(const QModelIndexList& indices) const
categoryID = downloadMeta.value("category", 0).toInt();
}
}
unsigned int categoryIndex =
CategoryFactory::instance().resolveNexusID(categoryID);
unsigned int categoryIndex = CategoryFactory::instance().resolveNexusID(categoryID);
if (categoryIndex != 0)
modInfo->setPrimaryCategory(
CategoryFactory::instance().getCategoryID(categoryIndex));
Expand Down

0 comments on commit efdff42

Please sign in to comment.