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
|
#include "modinfodialogtextfiles.h"
#include "ui_modinfodialog.h"
#include <QMessageBox>
class FileListItem : public QListWidgetItem
{
public:
FileListItem(const QString& rootPath, QString fullPath)
: m_fullPath(std::move(fullPath))
{
setText(m_fullPath.mid(rootPath.length() + 1));
}
const QString& fullPath() const
{
return m_fullPath;
}
private:
QString m_fullPath;
};
GenericFilesTab::GenericFilesTab(
OrganizerCore& oc, PluginContainer& plugin,
QWidget* parent, Ui::ModInfoDialog* ui, int id,
QListWidget* list, QSplitter* sp, TextEditor* e)
: ModInfoDialogTab(oc, plugin, parent, ui, id), m_list(list), m_editor(e)
{
m_editor->setupToolbar();
sp->setSizes({200, 1});
sp->setStretchFactor(0, 0);
sp->setStretchFactor(1, 1);
QObject::connect(
m_list, &QListWidget::currentItemChanged,
[&](auto* current, auto* previous){ onSelection(current, previous); });
}
void GenericFilesTab::clear()
{
m_list->clear();
select(nullptr);
setHasData(false);
}
bool GenericFilesTab::canClose()
{
if (!m_editor->dirty()) {
return true;
}
const int res = QMessageBox::question(
parentWidget(),
QObject::tr("Save changes?"),
QObject::tr("Save changes to \"%1\"?").arg(m_editor->filename()),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);
if (res == QMessageBox::Cancel) {
return false;
}
if (res == QMessageBox::Yes) {
m_editor->save();
}
return true;
}
bool GenericFilesTab::feedFile(const QString& rootPath, const QString& fullPath)
{
static constexpr const char* extensions[] = {
".txt"
};
for (const auto* e : extensions) {
if (wantsFile(rootPath, fullPath)) {
m_list->addItem(new FileListItem(rootPath, fullPath));
setHasData(true);
return true;
}
}
return false;
}
void GenericFilesTab::onSelection(
QListWidgetItem* current, QListWidgetItem* previous)
{
auto* item = dynamic_cast<FileListItem*>(current);
if (!item) {
qCritical("TextFilesTab: item is not a FileListItem");
return;
}
if (!canClose()) {
m_list->setCurrentItem(previous, QItemSelectionModel::Current);
return;
}
select(item);
}
void GenericFilesTab::select(FileListItem* item)
{
if (item) {
m_editor->setEnabled(true);
m_editor->load(item->fullPath());
} else {
m_editor->setEnabled(false);
}
}
TextFilesTab::TextFilesTab(
OrganizerCore& oc, PluginContainer& plugin,
QWidget* parent, Ui::ModInfoDialog* ui, int id)
: GenericFilesTab(
oc, plugin, parent, ui, id,
ui->textFileList, ui->tabTextSplitter, ui->textFileEditor)
{
}
bool TextFilesTab::wantsFile(const QString& rootPath, const QString& fullPath) const
{
static constexpr const char* extensions[] = {
".txt"
};
for (const auto* e : extensions) {
if (fullPath.endsWith(e, Qt::CaseInsensitive)) {
return true;
}
}
return false;
}
IniFilesTab::IniFilesTab(
OrganizerCore& oc, PluginContainer& plugin,
QWidget* parent, Ui::ModInfoDialog* ui, int id)
: GenericFilesTab(
oc, plugin, parent, ui, id,
ui->iniFileList, ui->tabIniSplitter, ui->iniFileEditor)
{
}
bool IniFilesTab::wantsFile(const QString& rootPath, const QString& fullPath) const
{
static constexpr const char* extensions[] = {
".ini", ".cfg"
};
for (const auto* e : extensions) {
if (fullPath.endsWith(e, Qt::CaseInsensitive)) {
if (!fullPath.endsWith("meta.ini")) {
return true;
}
}
}
return false;
}
|