summaryrefslogtreecommitdiff
path: root/src/modflagicondelegate.cpp
blob: e656e3905b9090c8eadb806f31e31632c23a5ac6 (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
#include "modflagicondelegate.h"
#include "modlist.h"
#include "modlistview.h"
#include <QList>
#include <log.h>

using namespace MOBase;

ModFlagIconDelegate::ModFlagIconDelegate(ModListView* view, int column, int compactSize)
    : IconDelegate(view, column, compactSize), m_view(view)
{}

QList<QString> ModFlagIconDelegate::getIconsForFlags(std::vector<ModInfo::EFlag> flags,
                                                     bool compact)
{
  QList<QString> result;

  // Don't do flags for overwrite
  if (std::find(flags.begin(), flags.end(), ModInfo::FLAG_OVERWRITE) != flags.end())
    return result;

  for (auto iter = flags.begin(); iter != flags.end(); ++iter) {
    auto iconPath = getFlagIcon(*iter);
    if (!iconPath.isEmpty())
      result.append(iconPath);
  }

  return result;
}

QList<QString> ModFlagIconDelegate::getIcons(const QModelIndex& index) const
{
  QVariant modid = index.data(ModList::IndexRole);

  if (modid.isValid()) {
    bool compact;
    auto flags = m_view->modFlags(index, &compact);
    return getIconsForFlags(flags, compact || this->compact());
  }

  return {};
}

QString ModFlagIconDelegate::getFlagIcon(ModInfo::EFlag flag)
{
  switch (flag) {
  case ModInfo::FLAG_BACKUP:
    return QStringLiteral(":/MO/gui/emblem_backup");
  case ModInfo::FLAG_INVALID:
    return QStringLiteral(":/MO/gui/problem");
  case ModInfo::FLAG_NOTENDORSED:
    return QStringLiteral(":/MO/gui/emblem_notendorsed");
  case ModInfo::FLAG_NOTES:
    return QStringLiteral(":/MO/gui/emblem_notes");
  case ModInfo::FLAG_HIDDEN_FILES:
    return QStringLiteral(":/MO/gui/emblem_hidden_files");
  case ModInfo::FLAG_ALTERNATE_GAME:
    return QStringLiteral(":/MO/gui/alternate_game");
  case ModInfo::FLAG_FOREIGN:
    return QString();
  case ModInfo::FLAG_SEPARATOR:
    return QString();
  case ModInfo::FLAG_OVERWRITE:
    return QString();
  case ModInfo::FLAG_PLUGIN_SELECTED:
    return QString();
  case ModInfo::FLAG_TRACKED:
    return QStringLiteral(":/MO/gui/tracked");
  default:
    log::warn("ModInfo flag {} has no defined icon", flag);
    return QString();
  }
}

size_t ModFlagIconDelegate::getNumIcons(const QModelIndex& index) const
{
  unsigned int modIdx = index.data(ModList::IndexRole).toInt();
  if (modIdx < ModInfo::getNumMods()) {
    ModInfo::Ptr info                 = ModInfo::getByIndex(modIdx);
    std::vector<ModInfo::EFlag> flags = info->getFlags();
    return flags.size();
  } else {
    return 0;
  }
}

QSize ModFlagIconDelegate::sizeHint(const QStyleOptionViewItem& option,
                                    const QModelIndex& modelIndex) const
{
  size_t count       = getNumIcons(modelIndex);
  unsigned int index = modelIndex.data(ModList::IndexRole).toInt();
  QSize result;
  if (index < ModInfo::getNumMods()) {
    result = QSize(static_cast<int>(count) * 40, 20);
  } else {
    result = QSize(1, 20);
  }
  if (option.rect.width() > 0) {
    result.setWidth(std::min(option.rect.width(), result.width()));
  }
  return result;
}