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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
|
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2022 Jamie Mansfield <jmansfield@cadixdev.org>
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
* Copyright (C) 2022 TheKodeToad <TheKodeToad@proton.me>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2013-2021 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "WorldListPage.h"
#include "ui/dialogs/CustomMessageBox.h"
#include "ui_WorldListPage.h"
#include "minecraft/WorldList.h"
#include <QEvent>
#include <QMenu>
#include <QKeyEvent>
#include <QClipboard>
#include <QMessageBox>
#include <QSortFilterProxyModel>
#include <QTreeView>
#include <QInputDialog>
#include <Qt>
#include "tools/MCEditTool.h"
#include "FileSystem.h"
#include "ui/GuiUtil.h"
#include "DesktopServices.h"
#include "Application.h"
class WorldListProxyModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
WorldListProxyModel(QObject *parent) : QSortFilterProxyModel(parent) {}
virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
{
QModelIndex sourceIndex = mapToSource(index);
if (index.column() == 0 && role == Qt::DecorationRole)
{
WorldList *worlds = qobject_cast<WorldList *>(sourceModel());
auto iconFile = worlds->data(sourceIndex, WorldList::IconFileRole).toString();
if(iconFile.isNull()) {
// NOTE: Minecraft uses the same placeholder for servers AND worlds
return APPLICATION->getThemedIcon("unknown_server");
}
return QIcon(iconFile);
}
return sourceIndex.data(role);
}
};
WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr<WorldList> worlds, QWidget *parent)
: QMainWindow(parent), m_inst(inst), ui(new Ui::WorldListPage), m_worlds(worlds)
{
ui->setupUi(this);
ui->toolBar->insertSpacer(ui->actionRefresh);
WorldListProxyModel * proxy = new WorldListProxyModel(this);
proxy->setSortCaseSensitivity(Qt::CaseInsensitive);
proxy->setSourceModel(m_worlds.get());
proxy->setSortRole(Qt::UserRole);
ui->worldTreeView->setSortingEnabled(true);
ui->worldTreeView->setModel(proxy);
ui->worldTreeView->installEventFilter(this);
ui->worldTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
ui->worldTreeView->setIconSize(QSize(64,64));
connect(ui->worldTreeView, &QTreeView::customContextMenuRequested, this, &WorldListPage::ShowContextMenu);
auto head = ui->worldTreeView->header();
head->setSectionResizeMode(0, QHeaderView::Stretch);
head->setSectionResizeMode(1, QHeaderView::ResizeToContents);
head->setSectionResizeMode(4, QHeaderView::ResizeToContents);
connect(ui->worldTreeView->selectionModel(), &QItemSelectionModel::currentChanged, this, &WorldListPage::worldChanged);
worldChanged(QModelIndex(), QModelIndex());
}
void WorldListPage::openedImpl()
{
m_worlds->startWatching();
auto const setting_name = QString("WideBarVisibility_%1").arg(id());
if (!APPLICATION->settings()->contains(setting_name))
m_wide_bar_setting = APPLICATION->settings()->registerSetting(setting_name);
else
m_wide_bar_setting = APPLICATION->settings()->getSetting(setting_name);
ui->toolBar->setVisibilityState(m_wide_bar_setting->get().toByteArray());
}
void WorldListPage::closedImpl()
{
m_worlds->stopWatching();
m_wide_bar_setting->set(ui->toolBar->getVisibilityState());
}
WorldListPage::~WorldListPage()
{
m_worlds->stopWatching();
delete ui;
}
void WorldListPage::ShowContextMenu(const QPoint& pos)
{
auto menu = ui->toolBar->createContextMenu(this, tr("Context menu"));
menu->exec(ui->worldTreeView->mapToGlobal(pos));
delete menu;
}
QMenu * WorldListPage::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();
filteredMenu->removeAction( ui->toolBar->toggleViewAction() );
return filteredMenu;
}
bool WorldListPage::shouldDisplay() const
{
return true;
}
void WorldListPage::retranslate()
{
ui->retranslateUi(this);
}
bool WorldListPage::worldListFilter(QKeyEvent *keyEvent)
{
switch (keyEvent->key())
{
case Qt::Key_Delete:
on_actionRemove_triggered();
return true;
default:
break;
}
return QWidget::eventFilter(ui->worldTreeView, keyEvent);
}
bool WorldListPage::eventFilter(QObject *obj, QEvent *ev)
{
if (ev->type() != QEvent::KeyPress)
{
return QWidget::eventFilter(obj, ev);
}
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
if (obj == ui->worldTreeView)
return worldListFilter(keyEvent);
return QWidget::eventFilter(obj, ev);
}
void WorldListPage::on_actionRemove_triggered()
{
auto proxiedIndex = getSelectedWorld();
if(!proxiedIndex.isValid())
return;
auto result = CustomMessageBox::selectable(this, tr("Confirm Deletion"),
tr("You are about to delete \"%1\".\n"
"The world may be gone forever (A LONG TIME).\n\n"
"Are you sure?")
.arg(m_worlds->allWorlds().at(proxiedIndex.row()).name()),
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec();
if(result != QMessageBox::Yes)
{
return;
}
m_worlds->stopWatching();
m_worlds->deleteWorld(proxiedIndex.row());
m_worlds->startWatching();
}
void WorldListPage::on_actionView_Folder_triggered()
{
DesktopServices::openDirectory(m_worlds->dir().absolutePath(), true);
}
void WorldListPage::on_actionDatapacks_triggered()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid())
{
return;
}
if(!worldSafetyNagQuestion(tr("Open World Datapacks Folder")))
return;
auto fullPath = m_worlds->data(index, WorldList::FolderRole).toString();
DesktopServices::openDirectory(FS::PathCombine(fullPath, "datapacks"), true);
}
void WorldListPage::on_actionReset_Icon_triggered()
{
auto proxiedIndex = getSelectedWorld();
if(!proxiedIndex.isValid())
return;
if(m_worlds->resetIcon(proxiedIndex.row())) {
ui->actionReset_Icon->setEnabled(false);
}
}
QModelIndex WorldListPage::getSelectedWorld()
{
auto index = ui->worldTreeView->selectionModel()->currentIndex();
auto proxy = (QSortFilterProxyModel *) ui->worldTreeView->model();
return proxy->mapToSource(index);
}
void WorldListPage::on_actionCopy_Seed_triggered()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid())
{
return;
}
int64_t seed = m_worlds->data(index, WorldList::SeedRole).toLongLong();
APPLICATION->clipboard()->setText(QString::number(seed));
}
void WorldListPage::on_actionMCEdit_triggered()
{
if(m_mceditStarting)
return;
auto mcedit = APPLICATION->mcedit();
const QString mceditPath = mcedit->path();
QModelIndex index = getSelectedWorld();
if (!index.isValid())
{
return;
}
if(!worldSafetyNagQuestion(tr("Open World in MCEdit")))
return;
auto fullPath = m_worlds->data(index, WorldList::FolderRole).toString();
auto program = mcedit->getProgramPath();
if(program.size())
{
#ifdef Q_OS_WIN32
if(!QProcess::startDetached(program, {fullPath}, mceditPath))
{
mceditError();
}
#else
m_mceditProcess.reset(new LoggedProcess());
m_mceditProcess->setDetachable(true);
connect(m_mceditProcess.get(), &LoggedProcess::stateChanged, this, &WorldListPage::mceditState);
m_mceditProcess->start(program, {fullPath});
m_mceditProcess->setWorkingDirectory(mceditPath);
m_mceditStarting = true;
#endif
}
else
{
QMessageBox::warning(
this->parentWidget(),
tr("No MCEdit found or set up!"),
tr("You do not have MCEdit set up or it was moved.\nYou can set it up in the global settings.")
);
}
}
void WorldListPage::mceditError()
{
QMessageBox::warning(
this->parentWidget(),
tr("MCEdit failed to start!"),
tr("MCEdit failed to start.\nIt may be necessary to reinstall it.")
);
}
void WorldListPage::mceditState(LoggedProcess::State state)
{
bool failed = false;
switch(state)
{
case LoggedProcess::NotRunning:
case LoggedProcess::Starting:
return;
case LoggedProcess::FailedToStart:
case LoggedProcess::Crashed:
case LoggedProcess::Aborted:
{
failed = true;
}
/* fallthrough */
case LoggedProcess::Running:
case LoggedProcess::Finished:
{
m_mceditStarting = false;
break;
}
}
if(failed)
{
mceditError();
}
}
void WorldListPage::worldChanged(const QModelIndex ¤t, const QModelIndex &previous)
{
QModelIndex index = getSelectedWorld();
bool enable = index.isValid();
ui->actionCopy_Seed->setEnabled(enable);
ui->actionMCEdit->setEnabled(enable);
ui->actionRemove->setEnabled(enable);
ui->actionCopy->setEnabled(enable);
ui->actionRename->setEnabled(enable);
ui->actionDatapacks->setEnabled(enable);
bool hasIcon = !index.data(WorldList::IconFileRole).isNull();
ui->actionReset_Icon->setEnabled(enable && hasIcon);
}
void WorldListPage::on_actionAdd_triggered()
{
auto list = GuiUtil::BrowseForFiles(
displayName(),
tr("Select a Minecraft world zip"),
tr("Minecraft World Zip File (*.zip)"), QString(), this->parentWidget());
if (!list.empty())
{
m_worlds->stopWatching();
for (auto filename : list)
{
m_worlds->installWorld(QFileInfo(filename));
}
m_worlds->startWatching();
}
}
bool WorldListPage::isWorldSafe(QModelIndex)
{
return !m_inst->isRunning();
}
bool WorldListPage::worldSafetyNagQuestion(const QString &actionType)
{
if(!isWorldSafe(getSelectedWorld()))
{
auto result = QMessageBox::question(this, actionType, tr("Changing a world while Minecraft is running is potentially unsafe.\nDo you wish to proceed?"));
if(result == QMessageBox::No)
{
return false;
}
}
return true;
}
void WorldListPage::on_actionCopy_triggered()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid())
{
return;
}
if(!worldSafetyNagQuestion(tr("Copy World")))
return;
auto worldVariant = m_worlds->data(index, WorldList::ObjectRole);
auto world = (World *) worldVariant.value<void *>();
bool ok = false;
QString name = QInputDialog::getText(this, tr("World name"), tr("Enter a new name for the copy."), QLineEdit::Normal, world->name(), &ok);
if (ok && name.length() > 0)
{
world->install(m_worlds->dir().absolutePath(), name);
}
}
void WorldListPage::on_actionRename_triggered()
{
QModelIndex index = getSelectedWorld();
if (!index.isValid())
{
return;
}
if(!worldSafetyNagQuestion(tr("Rename World")))
return;
auto worldVariant = m_worlds->data(index, WorldList::ObjectRole);
auto world = (World *) worldVariant.value<void *>();
bool ok = false;
QString name = QInputDialog::getText(this, tr("World name"), tr("Enter a new world name."), QLineEdit::Normal, world->name(), &ok);
if (ok && name.length() > 0)
{
world->rename(name);
}
}
void WorldListPage::on_actionRefresh_triggered()
{
m_worlds->update();
}
#include "WorldListPage.moc"
|