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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#include "modinfodialogtextfiles.h"
#include "modinfodialog.h"
#include "settings.h"
#include "ui_modinfodialog.h"
#include <QMessageBox>
class FileListModel : public QAbstractItemModel
{
public:
void clear()
{
beginResetModel();
m_files.clear();
endResetModel();
}
QModelIndex index(int row, int col, const QModelIndex& = {}) const override
{
return createIndex(row, col);
}
QModelIndex parent(const QModelIndex&) const override { return {}; }
int rowCount(const QModelIndex& index = {}) const override
{
// no child nodes
if (index.isValid())
return 0;
return static_cast<int>(m_files.size());
}
int columnCount(const QModelIndex& = {}) const override { return 1; }
QVariant data(const QModelIndex& index, int role) const override
{
if (role == Qt::DisplayRole) {
const auto row = index.row();
if (row < 0) {
return {};
}
const auto i = static_cast<std::size_t>(row);
if (i >= m_files.size()) {
return {};
}
return m_files[i].text;
}
return {};
}
void add(const QString& rootPath, QString fullPath)
{
QString text = fullPath.mid(rootPath.length() + 1);
m_files.emplace_back(std::move(fullPath), std::move(text));
}
void finished()
{
beginResetModel();
std::sort(m_files.begin(), m_files.end(), [](const auto& a, const auto& b) {
return (naturalCompare(a.text, b.text) < 0);
});
endResetModel();
}
QString fullPath(const QModelIndex& index) const
{
const auto row = index.row();
if (row < 0) {
return {};
}
const auto i = static_cast<std::size_t>(row);
if (i >= m_files.size()) {
return {};
}
return m_files[i].fullPath;
}
private:
struct File
{
QString fullPath;
QString text;
File(QString fp, QString t) : fullPath(std::move(fp)), text(std::move(t)) {}
};
std::deque<File> m_files;
};
GenericFilesTab::GenericFilesTab(ModInfoDialogTabContext cx, QListView* list,
QSplitter* sp, TextEditor* e, QLineEdit* filter)
: ModInfoDialogTab(std::move(cx)), m_list(list), m_editor(e), m_splitter(sp),
m_model(new FileListModel)
{
m_list->setModel(m_model);
m_editor->setupToolbar();
m_splitter->setSizes({200, 1});
m_splitter->setStretchFactor(0, 0);
m_splitter->setStretchFactor(1, 1);
m_filter.setEdit(filter);
m_filter.setList(m_list);
QObject::connect(m_list->selectionModel(), &QItemSelectionModel::currentRowChanged,
[&](auto current, auto previous) {
onSelection(current, previous);
});
}
void GenericFilesTab::clear()
{
m_model->clear();
select({});
setHasData(false);
}
bool GenericFilesTab::canClose()
{
if (!m_editor->dirty()) {
return true;
}
setFocus();
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)
{
if (wantsFile(rootPath, fullPath)) {
m_model->add(rootPath, fullPath);
return true;
}
return false;
}
void GenericFilesTab::update()
{
m_model->finished();
setHasData(m_model->rowCount() > 0);
}
void GenericFilesTab::saveState(Settings& s)
{
s.geometry().saveState(m_splitter);
}
void GenericFilesTab::restoreState(const Settings& s)
{
s.geometry().restoreState(m_splitter);
}
void GenericFilesTab::onSelection(const QModelIndex& current,
const QModelIndex& previous)
{
if (!canClose()) {
m_list->selectionModel()->select(previous, QItemSelectionModel::Current);
return;
}
select(current);
}
void GenericFilesTab::select(const QModelIndex& index)
{
if (!index.isValid()) {
m_editor->clear();
m_editor->setEnabled(false);
return;
}
m_editor->setEnabled(true);
m_editor->load(m_model->fullPath(m_filter.mapToSource(index)));
}
TextFilesTab::TextFilesTab(ModInfoDialogTabContext cx)
: GenericFilesTab(cx, cx.ui->textFileList, cx.ui->tabTextSplitter,
cx.ui->textFileEditor, cx.ui->textFileFilter)
{}
bool TextFilesTab::wantsFile(const QString& rootPath, const QString& fullPath) const
{
static const QString extensions[] = {".txt", ".json", ".cfg", ".log", ".toml"};
for (const auto& e : extensions) {
if (fullPath.endsWith(e, Qt::CaseInsensitive)) {
return true;
}
}
return false;
}
IniFilesTab::IniFilesTab(ModInfoDialogTabContext cx)
: GenericFilesTab(cx, cx.ui->iniFileList, cx.ui->tabIniSplitter,
cx.ui->iniFileEditor, cx.ui->iniFileFilter)
{}
bool IniFilesTab::wantsFile(const QString& rootPath, const QString& fullPath) const
{
static const QString extensions[] = {".ini"};
static const QString meta("meta.ini");
for (const auto& e : extensions) {
if (fullPath.endsWith(e, Qt::CaseInsensitive)) {
if (!fullPath.endsWith(meta)) {
return true;
}
}
}
return false;
}
|