summaryrefslogtreecommitdiff
path: root/src/modinfodialogcategories.cpp
blob: 69c7c6b5718ab93c71044f264f0d3d8ac7461fd3 (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
124
125
126
#include "modinfodialogcategories.h"
#include "ui_modinfodialog.h"
#include "categories.h"
#include "modinfo.h"

CategoriesTab::CategoriesTab(
  OrganizerCore& oc, PluginContainer& plugin,
  QWidget* parent, Ui::ModInfoDialog* ui)
    : ModInfoDialogTab(oc, plugin, parent, ui)
{
  connect(
    ui->categories, &QTreeWidget::itemChanged,
    [&](auto* item, int col){ onCategoryChanged(item, col); });

  connect(
    ui->primaryCategories,
    static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged),
    [&](int index){ onPrimaryChanged(index); });
}

void CategoriesTab::clear()
{
  ui->categories->clear();
  ui->primaryCategories->clear();
}

void CategoriesTab::update()
{
  clear();

  add(
    CategoryFactory::instance(), mod()->getCategories(),
    ui->categories->invisibleRootItem(), 0);

  updatePrimary();
}

void CategoriesTab::add(
  const CategoryFactory &factory, const std::set<int>& enabledCategories,
  QTreeWidgetItem* root, int rootLevel)
{
  for (int i=0; i<static_cast<int>(factory.numCategories()); ++i) {
    if (factory.getParentID(i) != rootLevel) {
      continue;
    }

    int categoryID = factory.getCategoryID(i);

    QTreeWidgetItem *newItem
      = new QTreeWidgetItem(QStringList(factory.getCategoryName(i)));

    newItem->setFlags(newItem->flags() | Qt::ItemIsUserCheckable);

    newItem->setCheckState(0, enabledCategories.find(categoryID)
      != enabledCategories.end()
      ? Qt::Checked
      : Qt::Unchecked);

    newItem->setData(0, Qt::UserRole, categoryID);

    if (factory.hasChildren(i)) {
      add(factory, enabledCategories, newItem, categoryID);
    }

    root->addChild(newItem);
  }
}

void CategoriesTab::updatePrimary()
{
  ui->primaryCategories->clear();

  int primaryCategory = mod()->getPrimaryCategory();

  addChecked(ui->categories->invisibleRootItem());

  for (int i = 0; i < ui->primaryCategories->count(); ++i) {
    if (ui->primaryCategories->itemData(i).toInt() == primaryCategory) {
      ui->primaryCategories->setCurrentIndex(i);
      break;
    }
  }
}

void CategoriesTab::addChecked(QTreeWidgetItem* tree)
{
  for (int i = 0; i < tree->childCount(); ++i) {
    QTreeWidgetItem *child = tree->child(i);
    if (child->checkState(0) == Qt::Checked) {
      ui->primaryCategories->addItem(child->text(0), child->data(0, Qt::UserRole));
      addChecked(child);
    }
  }
}

void CategoriesTab::save(QTreeWidgetItem* currentNode)
{
  for (int i = 0; i < currentNode->childCount(); ++i) {
    QTreeWidgetItem *childNode = currentNode->child(i);

    mod()->setCategory(
      childNode->data(0, Qt::UserRole).toInt(), childNode->checkState(0));

    save(childNode);
  }
}

void CategoriesTab::onCategoryChanged(QTreeWidgetItem* item, int)
{
  QTreeWidgetItem *parent = item->parent();

  while ((parent != nullptr) && ((parent->flags() & Qt::ItemIsUserCheckable) != 0) && (parent->checkState(0) == Qt::Unchecked)) {
    parent->setCheckState(0, Qt::Checked);
    parent = parent->parent();
  }

  updatePrimary();
  save(ui->categories->invisibleRootItem());
}

void CategoriesTab::onPrimaryChanged(int index)
{
  if (index != -1) {
    mod()->setPrimaryCategory(ui->primaryCategories->itemData(index).toInt());
  }
}