aboutsummaryrefslogtreecommitdiff
path: root/launcher/ui/pages/instance/WorldListPage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'launcher/ui/pages/instance/WorldListPage.cpp')
-rw-r--r--launcher/ui/pages/instance/WorldListPage.cpp177
1 files changed, 71 insertions, 106 deletions
diff --git a/launcher/ui/pages/instance/WorldListPage.cpp b/launcher/ui/pages/instance/WorldListPage.cpp
index b2200b1a..fe477616 100644
--- a/launcher/ui/pages/instance/WorldListPage.cpp
+++ b/launcher/ui/pages/instance/WorldListPage.cpp
@@ -36,45 +36,42 @@
*/
#include "WorldListPage.h"
+#include "minecraft/WorldList.h"
#include "ui/dialogs/CustomMessageBox.h"
#include "ui_WorldListPage.h"
-#include "minecraft/WorldList.h"
+#include <QClipboard>
#include <QEvent>
-#include <QMenu>
+#include <QInputDialog>
#include <QKeyEvent>
-#include <QClipboard>
+#include <QMenu>
#include <QMessageBox>
#include <QSortFilterProxyModel>
#include <QTreeView>
-#include <QInputDialog>
#include <Qt>
-#include "tools/MCEditTool.h"
#include "FileSystem.h"
+#include "tools/MCEditTool.h"
-#include "ui/GuiUtil.h"
#include "DesktopServices.h"
+#include "ui/GuiUtil.h"
#include "Application.h"
-
-class WorldListProxyModel : public QSortFilterProxyModel
-{
+class WorldListProxyModel : public QSortFilterProxyModel {
Q_OBJECT
-public:
- WorldListProxyModel(QObject *parent) : QSortFilterProxyModel(parent) {}
+ public:
+ WorldListProxyModel(QObject* parent) : QSortFilterProxyModel(parent) {}
- virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
+ 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());
+ if (index.column() == 0 && role == Qt::DecorationRole) {
+ WorldList* worlds = qobject_cast<WorldList*>(sourceModel());
auto iconFile = worlds->data(sourceIndex, WorldList::IconFileRole).toString();
- if(iconFile.isNull()) {
+ if (iconFile.isNull()) {
// NOTE: Minecraft uses the same placeholder for servers AND worlds
return APPLICATION->getThemedIcon("unknown_server");
}
@@ -85,15 +82,14 @@ public:
}
};
-
-WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr<WorldList> worlds, QWidget *parent)
+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);
+ WorldListProxyModel* proxy = new WorldListProxyModel(this);
proxy->setSortCaseSensitivity(Qt::CaseInsensitive);
proxy->setSourceModel(m_worlds.get());
proxy->setSortRole(Qt::UserRole);
@@ -101,7 +97,7 @@ WorldListPage::WorldListPage(BaseInstance *inst, std::shared_ptr<WorldList> worl
ui->worldTreeView->setModel(proxy);
ui->worldTreeView->installEventFilter(this);
ui->worldTreeView->setContextMenuPolicy(Qt::CustomContextMenu);
- ui->worldTreeView->setIconSize(QSize(64,64));
+ ui->worldTreeView->setIconSize(QSize(64, 64));
connect(ui->worldTreeView, &QTreeView::customContextMenuRequested, this, &WorldListPage::ShowContextMenu);
auto head = ui->worldTreeView->header();
@@ -146,10 +142,10 @@ void WorldListPage::ShowContextMenu(const QPoint& pos)
delete menu;
}
-QMenu * WorldListPage::createPopupMenu()
+QMenu* WorldListPage::createPopupMenu()
{
QMenu* filteredMenu = QMainWindow::createPopupMenu();
- filteredMenu->removeAction( ui->toolBar->toggleViewAction() );
+ filteredMenu->removeAction(ui->toolBar->toggleViewAction());
return filteredMenu;
}
@@ -163,26 +159,24 @@ void WorldListPage::retranslate()
ui->retranslateUi(this);
}
-bool WorldListPage::worldListFilter(QKeyEvent *keyEvent)
+bool WorldListPage::worldListFilter(QKeyEvent* keyEvent)
{
- switch (keyEvent->key())
- {
- case Qt::Key_Delete:
- on_actionRemove_triggered();
- return true;
- default:
- break;
+ 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)
+bool WorldListPage::eventFilter(QObject* obj, QEvent* ev)
{
- if (ev->type() != QEvent::KeyPress)
- {
+ if (ev->type() != QEvent::KeyPress) {
return QWidget::eventFilter(obj, ev);
}
- QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
+ QKeyEvent* keyEvent = static_cast<QKeyEvent*>(ev);
if (obj == ui->worldTreeView)
return worldListFilter(keyEvent);
return QWidget::eventFilter(obj, ev);
@@ -192,7 +186,7 @@ void WorldListPage::on_actionRemove_triggered()
{
auto proxiedIndex = getSelectedWorld();
- if(!proxiedIndex.isValid())
+ if (!proxiedIndex.isValid())
return;
auto result = CustomMessageBox::selectable(this, tr("Confirm Deletion"),
@@ -203,8 +197,7 @@ void WorldListPage::on_actionRemove_triggered()
QMessageBox::Warning, QMessageBox::Yes | QMessageBox::No, QMessageBox::No)
->exec();
- if(result != QMessageBox::Yes)
- {
+ if (result != QMessageBox::Yes) {
return;
}
m_worlds->stopWatching();
@@ -221,12 +214,11 @@ void WorldListPage::on_actionDatapacks_triggered()
{
QModelIndex index = getSelectedWorld();
- if (!index.isValid())
- {
+ if (!index.isValid()) {
return;
}
- if(!worldSafetyNagQuestion(tr("Open World Datapacks Folder")))
+ if (!worldSafetyNagQuestion(tr("Open World Datapacks Folder")))
return;
auto fullPath = m_worlds->data(index, WorldList::FolderRole).toString();
@@ -234,25 +226,23 @@ void WorldListPage::on_actionDatapacks_triggered()
DesktopServices::openDirectory(FS::PathCombine(fullPath, "datapacks"), true);
}
-
void WorldListPage::on_actionReset_Icon_triggered()
{
auto proxiedIndex = getSelectedWorld();
- if(!proxiedIndex.isValid())
+ if (!proxiedIndex.isValid())
return;
- if(m_worlds->resetIcon(proxiedIndex.row())) {
+ 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();
+ auto proxy = (QSortFilterProxyModel*)ui->worldTreeView->model();
return proxy->mapToSource(index);
}
@@ -260,8 +250,7 @@ void WorldListPage::on_actionCopy_Seed_triggered()
{
QModelIndex index = getSelectedWorld();
- if (!index.isValid())
- {
+ if (!index.isValid()) {
return;
}
int64_t seed = m_worlds->data(index, WorldList::SeedRole).toLongLong();
@@ -270,7 +259,7 @@ void WorldListPage::on_actionCopy_Seed_triggered()
void WorldListPage::on_actionMCEdit_triggered()
{
- if(m_mceditStarting)
+ if (m_mceditStarting)
return;
auto mcedit = APPLICATION->mcedit();
@@ -279,81 +268,66 @@ void WorldListPage::on_actionMCEdit_triggered()
QModelIndex index = getSelectedWorld();
- if (!index.isValid())
- {
+ if (!index.isValid()) {
return;
}
- if(!worldSafetyNagQuestion(tr("Open World in MCEdit")))
+ if (!worldSafetyNagQuestion(tr("Open World in MCEdit")))
return;
auto fullPath = m_worlds->data(index, WorldList::FolderRole).toString();
auto program = mcedit->getProgramPath();
- if(program.size())
- {
+ if (program.size()) {
#ifdef Q_OS_WIN32
- if(!QProcess::startDetached(program, {fullPath}, mceditPath))
- {
+ 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->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.")
- );
+ } 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.")
- );
+ 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)
- {
+ switch (state) {
case LoggedProcess::NotRunning:
case LoggedProcess::Starting:
return;
case LoggedProcess::FailedToStart:
case LoggedProcess::Crashed:
- case LoggedProcess::Aborted:
- {
+ case LoggedProcess::Aborted: {
failed = true;
}
/* fallthrough */
case LoggedProcess::Running:
- case LoggedProcess::Finished:
- {
+ case LoggedProcess::Finished: {
m_mceditStarting = false;
break;
}
}
- if(failed)
- {
+ if (failed) {
mceditError();
}
}
-void WorldListPage::worldChanged(const QModelIndex &current, const QModelIndex &previous)
+void WorldListPage::worldChanged(const QModelIndex& current, const QModelIndex& previous)
{
QModelIndex index = getSelectedWorld();
bool enable = index.isValid();
@@ -369,15 +343,11 @@ void WorldListPage::worldChanged(const QModelIndex &current, const QModelIndex &
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())
- {
+ 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)
- {
+ for (auto filename : list) {
m_worlds->installWorld(QFileInfo(filename));
}
m_worlds->startWatching();
@@ -389,38 +359,35 @@ bool WorldListPage::isWorldSafe(QModelIndex)
return !m_inst->isRunning();
}
-bool WorldListPage::worldSafetyNagQuestion(const QString &actionType)
+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)
- {
+ 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())
- {
+ if (!index.isValid()) {
return;
}
- if(!worldSafetyNagQuestion(tr("Copy World")))
+ if (!worldSafetyNagQuestion(tr("Copy World")))
return;
auto worldVariant = m_worlds->data(index, WorldList::ObjectRole);
- auto world = (World *) worldVariant.value<void *>();
+ 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);
+ 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)
- {
+ if (ok && name.length() > 0) {
world->install(m_worlds->dir().absolutePath(), name);
}
}
@@ -428,22 +395,20 @@ void WorldListPage::on_actionCopy_triggered()
void WorldListPage::on_actionRename_triggered()
{
QModelIndex index = getSelectedWorld();
- if (!index.isValid())
- {
+ if (!index.isValid()) {
return;
}
- if(!worldSafetyNagQuestion(tr("Rename World")))
+ if (!worldSafetyNagQuestion(tr("Rename World")))
return;
auto worldVariant = m_worlds->data(index, WorldList::ObjectRole);
- auto world = (World *) worldVariant.value<void *>();
+ 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)
- {
+ if (ok && name.length() > 0) {
world->rename(name);
}
}