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
|
#include "MOPanelInterface.h"
#include <log.h>
#include <algorithm>
#include <functional>
#include <iterator>
using namespace Qt::Literals::StringLiterals;
MOPanelInterface::MOPanelInterface(MOBase::IOrganizer* organizer,
QMainWindow* mainWindow)
: m_ModList{organizer->modList()}, m_PluginList{organizer->pluginList()},
m_ModListView{mainWindow->findChild<QTreeView*>(u"modList"_s)},
m_PluginListView{mainWindow->findChild<QTreeView*>(u"espList"_s)}
{
if (m_ModListView) {
QObject::connect(m_ModListView, &QTreeView::collapsed, this,
&MOPanelInterface::onModSeparatorCollapsed);
QObject::connect(m_ModListView, &QTreeView::expanded, this,
&MOPanelInterface::onModSeparatorExpanded);
QObject::connect(m_ModListView->selectionModel(),
&QItemSelectionModel::selectionChanged, this,
&MOPanelInterface::onModSelectionChanged);
}
}
MOPanelInterface::~MOPanelInterface() noexcept
{
m_SelectedOriginsChanged.disconnect_all_slots();
}
void MOPanelInterface::assignWidget(QTabWidget* tabWidget, QWidget* panel)
{
QObject::connect(tabWidget, &QTabWidget::currentChanged,
[this, tabWidget, panel](int index) {
QWidget* const currentWidget = tabWidget->widget(index);
if (currentWidget == panel) {
m_PanelActivated();
}
});
}
void MOPanelInterface::setSelectedFiles(const QList<QString>& selectedFiles)
{
if (!m_PluginListView) {
return;
}
QItemSelection selection;
const auto model = m_PluginListView->model();
for (int row = 0, count = model->rowCount(); row < count; ++row) {
const auto index = model->index(row, 0);
if (selectedFiles.contains(index.data(Qt::DisplayRole))) {
const auto end = model->index(row, model->columnCount() - 1);
selection.select(index, end);
}
}
m_PluginListView->selectionModel()->select(selection,
QItemSelectionModel::ClearAndSelect);
}
// FIXME: only works for files in the plugins panel
void MOPanelInterface::displayOriginInformation(const QString& file)
{
const auto model = m_PluginListView->model();
for (int row = 0, count = model->rowCount(); row < count; ++row) {
const auto index = model->index(row, 0);
const auto other = index.data(Qt::DisplayRole).toString();
if (file.compare(other, Qt::CaseInsensitive) == 0) {
m_PluginListView->selectionModel()->select(
QItemSelection(index, model->index(row, model->columnCount() - 1)),
QItemSelectionModel::ClearAndSelect);
m_PluginListView->selectionModel()->setCurrentIndex(index,
QItemSelectionModel::Current);
m_PluginListView->doubleClicked(index);
return;
}
}
MOBase::log::warn("failed to open origin info for \"{}\"", file);
}
bool MOPanelInterface::onPanelActivated(const std::function<void()>& func)
{
auto connection = m_PanelActivated.connect(func);
return connection.connected();
}
bool MOPanelInterface::onSelectedOriginsChanged(
const std::function<void(const QList<QString>&)>& func)
{
auto connection = m_SelectedOriginsChanged.connect(func);
return connection.connected();
}
void MOPanelInterface::setPluginState(const QString& name, bool enable)
{
const auto model = m_PluginListView->model();
for (int i = 0, count = model->rowCount(); i < count; ++i) {
const auto index = model->index(i, 0);
if (index.data().toString().compare(name, Qt::CaseInsensitive) == 0) {
model->setData(index, enable ? Qt::Checked : Qt::Unchecked, Qt::CheckStateRole);
}
}
}
void MOPanelInterface::onModSeparatorCollapsed(const QModelIndex& index)
{
if (m_ModListView->selectionModel()->isSelected(index)) {
onModSelectionChanged();
}
}
void MOPanelInterface::onModSeparatorExpanded(const QModelIndex& index)
{
if (m_ModListView->selectionModel()->isSelected(index)) {
onModSelectionChanged();
}
}
void MOPanelInterface::onModSelectionChanged()
{
QList<QString> origins;
std::function<void(const QModelIndex&)> addOrigins;
addOrigins = [&](const QModelIndex& index) {
if (index.model()->hasChildren(index)) {
if (m_ModListView->isExpanded(index)) {
return;
}
for (int i = 0, count = index.model()->rowCount(index); i < count; ++i) {
addOrigins(index.model()->index(i, 0, index));
}
} else {
origins.append(index.data(Qt::DisplayRole).toString());
}
};
const auto indexes = m_ModListView->selectionModel()->selectedRows();
std::ranges::for_each(indexes, addOrigins);
m_SelectedOriginsChanged(origins);
}
|