summaryrefslogtreecommitdiff
path: root/src/pluginlistview.cpp
blob: 70b3ebc194dd7a2db93ae4005cbe170a7b8928f3 (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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "pluginlistview.h"

#include <QUrl>
#include <QMimeData>

#include <widgetutility.h>

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "organizercore.h"
#include "pluginlistsortproxy.h"
#include "genericicondelegate.h"
#include "modelutils.h"

PluginListView::PluginListView(QWidget *parent)
  : QTreeView(parent)
  , m_Scrollbar(new ViewMarkingScrollBar(this->model(), this))
{
  setVerticalScrollBar(m_Scrollbar);
  MOBase::setCustomizableColumns(this);
}

void PluginListView::setModel(QAbstractItemModel *model)
{
  QTreeView::setModel(model);
  setVerticalScrollBar(new ViewMarkingScrollBar(model, this));
}

int PluginListView::sortColumn() const
{
  return m_sortProxy ? m_sortProxy->sortColumn() : -1;
}

QModelIndex PluginListView::indexModelToView(const QModelIndex& index) const
{
  return ::indexModelToView(index, this);
}

QModelIndexList PluginListView::indexModelToView(const QModelIndexList& index) const
{
  return ::indexModelToView(index, this);
}

QModelIndex PluginListView::indexViewToModel(const QModelIndex& index) const
{
  return ::indexViewToModel(index, m_core->pluginList());
}

QModelIndexList PluginListView::indexViewToModel(const QModelIndexList& index) const
{
  return ::indexViewToModel(index, m_core->pluginList());
}

void PluginListView::updatePluginCount()
{
  int activeMasterCount = 0;
  int activeLightMasterCount = 0;
  int activeRegularCount = 0;
  int masterCount = 0;
  int lightMasterCount = 0;
  int regularCount = 0;
  int activeVisibleCount = 0;

  PluginList* list = m_core->pluginList();
  QString filter = ui.filter->text();

  for (QString plugin : list->pluginNames()) {
    bool active = list->isEnabled(plugin);
    bool visible = m_sortProxy->filterMatchesPlugin(plugin);
    if (list->isLight(plugin) || list->isLightFlagged(plugin)) {
      lightMasterCount++;
      activeLightMasterCount += active;
      activeVisibleCount += visible && active;
    }
    else if (list->isMaster(plugin)) {
      masterCount++;
      activeMasterCount += active;
      activeVisibleCount += visible && active;
    }
    else {
      regularCount++;
      activeRegularCount += active;
      activeVisibleCount += visible && active;
    }
  }

  int activeCount = activeMasterCount + activeLightMasterCount + activeRegularCount;
  int totalCount = masterCount + lightMasterCount + regularCount;

  ui.counter->display(activeVisibleCount);
  ui.counter->setToolTip(tr("<table cellspacing=\"6\">"
    "<tr><th>Type</th><th>Active      </th><th>Total</th></tr>"
    "<tr><td>All plugins:</td><td align=right>%1    </td><td align=right>%2</td></tr>"
    "<tr><td>ESMs:</td><td align=right>%3    </td><td align=right>%4</td></tr>"
    "<tr><td>ESPs:</td><td align=right>%7    </td><td align=right>%8</td></tr>"
    "<tr><td>ESMs+ESPs:</td><td align=right>%9    </td><td align=right>%10</td></tr>"
    "<tr><td>ESLs:</td><td align=right>%5    </td><td align=right>%6</td></tr>"
    "</table>")
    .arg(activeCount).arg(totalCount)
    .arg(activeMasterCount).arg(masterCount)
    .arg(activeLightMasterCount).arg(lightMasterCount)
    .arg(activeRegularCount).arg(regularCount)
    .arg(activeMasterCount + activeRegularCount).arg(masterCount + regularCount)
  );
}

void PluginListView::onFilterChanged(const QString& filter)
{
  if (!filter.isEmpty()) {
    setStyleSheet("QTreeView { border: 2px ridge #f00; }");
    ui.counter->setStyleSheet("QLCDNumber { border: 2px ridge #f00; }");
  }
  else {
    setStyleSheet("");
    ui.counter->setStyleSheet("");
  }
  updatePluginCount();
}

std::pair<QModelIndex, QModelIndexList> PluginListView::selected() const
{
  return { indexViewToModel(currentIndex()), indexViewToModel(selectionModel()->selectedRows()) };
}

void PluginListView::setSelected(const QModelIndex& current, const QModelIndexList& selected)
{
  setCurrentIndex(indexModelToView(current));
  for (auto idx : selected) {
    selectionModel()->select(indexModelToView(idx), QItemSelectionModel::Select | QItemSelectionModel::Rows);
  }
}


void PluginListView::setup(OrganizerCore& core, MainWindow* mw, Ui::MainWindow* mwui)
{
  m_core = &core;
  ui = { mwui->activePluginsCounter, mwui->espFilterEdit };

  m_sortProxy = new PluginListSortProxy(&core);
  m_sortProxy->setSourceModel(core.pluginList());
  setModel(m_sortProxy);

  sortByColumn(PluginList::COL_PRIORITY, Qt::AscendingOrder);
  setItemDelegateForColumn(PluginList::COL_FLAGS, new GenericIconDelegate(this));

  connect(ui.filter, &QLineEdit::textChanged, m_sortProxy, &PluginListSortProxy::updateFilter);
  connect(ui.filter, &QLineEdit::textChanged, this, &PluginListView::onFilterChanged);

}

bool PluginListView::moveSelection(int key)
{
  auto [cindex, sourceRows] = selected();

  int offset = key == Qt::Key_Up ? -1 : 1;
  if (m_sortProxy->sortOrder() == Qt::DescendingOrder) {
    offset = -offset;
  }

  m_core->pluginList()->shiftPluginsPriority(sourceRows, offset);

  // reset the selection and the index
  setSelected(cindex, sourceRows);

  return true;
}

bool PluginListView::toggleSelectionState()
{
  if (!selectionModel()->hasSelection()) {
    return true;
  }
  m_core->pluginList()->toggleState(indexViewToModel(selectionModel()->selectedRows()));
  return true;
}

bool PluginListView::event(QEvent* event)
{
  Profile* profile = m_core->currentProfile();
  if (event->type() == QEvent::KeyPress && profile) {
    QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);

    if (keyEvent->modifiers() == Qt::ControlModifier
      && (sortColumn() == PluginList::COL_PRIORITY || sortColumn() == PluginList::COL_MODINDEX)
      && (keyEvent->key() == Qt::Key_Up || keyEvent->key() == Qt::Key_Down)) {
      return moveSelection(keyEvent->key());
    }
    else if (keyEvent->key() == Qt::Key_Space) {
      return toggleSelectionState();
    }
    return QTreeView::event(event);
  }
  return QTreeView::event(event);
}