summaryrefslogtreecommitdiff
path: root/src/pluginlistcontextmenu.cpp
blob: 787e5c0cf226eb386b61277cd391f5fd5ae93ac7 (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
127
128
129
130
131
132
#include "pluginlistcontextmenu.h"

#include <report.h>
#include <utility.h>

#include "pluginlistview.h"
#include "organizercore.h"

using namespace MOBase;

PluginListContextMenu::PluginListContextMenu(
  const QModelIndex& index, OrganizerCore& core, PluginListView* view) :
  QMenu(view)
  , m_core(core)
  , m_index(index.model() == view->model() ? view->indexViewToModel(index) : index)
  , m_view(view)
{
  if (view->selectionModel()->hasSelection()) {
    m_selected = view->indexViewToModel(view->selectionModel()->selectedRows());
  }
  else {
    m_selected = { index };
  }

  addAction(tr("Enable selected"), [=]() { m_core.pluginList()->setEnabled(m_selected, true); });
  addAction(tr("Disable selected"), [=]() { m_core.pluginList()->setEnabled(m_selected, false); });

  addSeparator();

  addAction(tr("Enable all"), m_core.pluginList(), &PluginList::enableAll);
  addAction(tr("Disable all"), m_core.pluginList(), &PluginList::disableAll);

  addSeparator();

  addMenu(createSendToContextMenu());
  addSeparator();

  bool hasLocked = false;
  bool hasUnlocked = false;
  for (auto& idx : m_selected) {
    if (m_core.pluginList()->isEnabled(idx.row())) {
      if (m_core.pluginList()->isESPLocked(idx.row())) {
        hasLocked = true;
      }
      else {
        hasUnlocked = true;
      }
    }
  }

  if (hasLocked) {
    addAction(tr("Unlock load order"), [=]() { setESPLock(m_selected, false); });
  }
  if (hasUnlocked) {
    addAction(tr("Lock load order"), [=]() { setESPLock(m_selected, true); });
  }

  addSeparator();

  unsigned int modInfoIndex = ModInfo::getIndex(m_core.pluginList()->origin(m_index.data().toString()));
  // this is to avoid showing the option on game files like skyrim.esm
  if (modInfoIndex != UINT_MAX) {
    addAction(tr("Open Origin in Explorer"), [=]() { openOriginExplorer(m_selected); });
    ModInfo::Ptr modInfo = ModInfo::getByIndex(modInfoIndex);
    std::vector<ModInfo::EFlag> flags = modInfo->getFlags();

    if (!modInfo->isForeign() && m_selected.size() == 1) {
      QAction* infoAction = addAction(tr("Open Origin Info..."), [=]() { openOriginInformation(index); });
      setDefaultAction(infoAction);
    }
  }

}

QMenu* PluginListContextMenu::createSendToContextMenu()
{
  QMenu* menu = new QMenu(m_view);
  menu->setTitle(tr("Send to... "));
  menu->addAction(tr("Top"), [=]() { m_core.pluginList()->sendToPriority(m_selected, 0); });
  menu->addAction(tr("Bottom"), [=]() { m_core.pluginList()->sendToPriority(m_selected, INT_MAX); });
  menu->addAction(tr("Priority..."), [=]() { sendPluginsToPriority(m_selected); });
  return menu;
}

void PluginListContextMenu::sendPluginsToPriority(const QModelIndexList& indices)
{
  bool ok;
  int newPriority = QInputDialog::getInt(m_view->topLevelWidget(),
    tr("Set Priority"), tr("Set the priority of the selected plugins"),
    0, 0, INT_MAX, 1, &ok);
  if (!ok) return;

  m_core.pluginList()->sendToPriority(m_selected, newPriority);
}

void PluginListContextMenu::setESPLock(const QModelIndexList& indices, bool locked)
{
  for (auto& idx : indices) {
    if (m_core.pluginList()->isEnabled(idx.row())) {
      m_core.pluginList()->lockESPIndex(idx.row(), locked);
    }
  }
}

void PluginListContextMenu::openOriginExplorer(const QModelIndexList& indices)
{
  for (auto& idx : indices) {
    QString fileName = idx.data().toString();
    unsigned int modIndex = ModInfo::getIndex(m_core.pluginList()->origin(fileName));
    if (modIndex == UINT_MAX) {
      continue;
    }
    ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex);
    shell::Explore(modInfo->absolutePath());
  }
}

void PluginListContextMenu::openOriginInformation(const QModelIndex& index)
{
  try {
    QString fileName = index.data().toString();
    unsigned int modIndex = ModInfo::getIndex(m_core.pluginList()->origin(fileName));
    ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex);

    if (modInfo->isRegular() || modInfo->isOverwrite()) {
      emit openModInformation(modIndex);
    }
  }
  catch (const std::exception& e) {
    reportError(e.what());
  }
}