MultiMC5/gui/dialogs/LegacyModEditDialog.cpp

394 lines
10 KiB
C++
Raw Normal View History

/* Copyright 2013 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
2013-10-05 23:13:40 +00:00
*
* 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 "MultiMC.h"
#include "LegacyModEditDialog.h"
2013-08-28 02:38:29 +00:00
#include "ModEditDialogCommon.h"
#include "VersionSelectDialog.h"
#include "ProgressDialog.h"
#include "ui_LegacyModEditDialog.h"
#include "logic/ModList.h"
#include "logic/lists/ForgeVersionList.h"
#include "gui/Platform.h"
2013-08-18 18:52:17 +00:00
#include <pathutils.h>
#include <QFileDialog>
//#include <QMessageBox>
#include <QDebug>
#include <QEvent>
#include <QKeyEvent>
2013-10-05 23:13:40 +00:00
LegacyModEditDialog::LegacyModEditDialog(LegacyInstance *inst, QWidget *parent)
: QDialog(parent), ui(new Ui::LegacyModEditDialog), m_inst(inst)
{
MultiMCPlatform::fixWM_CLASS(this);
ui->setupUi(this);
2013-10-05 23:13:40 +00:00
// Jar mods
{
ensureFolderPathExists(m_inst->jarModsDir());
m_jarmods = m_inst->jarModList();
2013-10-05 23:13:40 +00:00
ui->jarModsTreeView->setModel(m_jarmods.get());
#ifndef Q_OS_LINUX
// FIXME: internal DnD causes segfaults later
ui->jarModsTreeView->setDragDropMode(QAbstractItemView::DragDrop);
// FIXME: DnD is glitched with contiguous (we move only first item in selection)
ui->jarModsTreeView->setSelectionMode(QAbstractItemView::SingleSelection);
#endif
2013-10-05 23:13:40 +00:00
ui->jarModsTreeView->installEventFilter(this);
m_jarmods->startWatching();
auto smodel = ui->jarModsTreeView->selectionModel();
connect(smodel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
SLOT(jarCurrent(QModelIndex, QModelIndex)));
}
// Core mods
{
ensureFolderPathExists(m_inst->coreModsDir());
m_coremods = m_inst->coreModList();
2013-10-05 23:13:40 +00:00
ui->coreModsTreeView->setModel(m_coremods.get());
ui->coreModsTreeView->installEventFilter(this);
m_coremods->startWatching();
auto smodel = ui->coreModsTreeView->selectionModel();
connect(smodel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
SLOT(coreCurrent(QModelIndex, QModelIndex)));
}
// Loader mods
{
ensureFolderPathExists(m_inst->loaderModsDir());
m_mods = m_inst->loaderModList();
2013-10-05 23:13:40 +00:00
ui->loaderModTreeView->setModel(m_mods.get());
ui->loaderModTreeView->installEventFilter(this);
m_mods->startWatching();
auto smodel = ui->loaderModTreeView->selectionModel();
connect(smodel, SIGNAL(currentChanged(QModelIndex, QModelIndex)),
SLOT(loaderCurrent(QModelIndex, QModelIndex)));
}
// texture packs
{
2013-08-28 02:38:29 +00:00
ensureFolderPathExists(m_inst->texturePacksDir());
m_texturepacks = m_inst->texturePackList();
2013-10-05 23:13:40 +00:00
ui->texPackTreeView->setModel(m_texturepacks.get());
ui->texPackTreeView->installEventFilter(this);
m_texturepacks->startWatching();
}
}
LegacyModEditDialog::~LegacyModEditDialog()
{
m_mods->stopWatching();
m_coremods->stopWatching();
m_jarmods->stopWatching();
m_texturepacks->stopWatching();
delete ui;
}
2013-10-05 23:13:40 +00:00
bool LegacyModEditDialog::coreListFilter(QKeyEvent *keyEvent)
{
2013-10-05 23:13:40 +00:00
switch (keyEvent->key())
{
2013-10-05 23:13:40 +00:00
case Qt::Key_Delete:
on_rmCoreBtn_clicked();
return true;
case Qt::Key_Plus:
on_addCoreBtn_clicked();
return true;
default:
break;
}
2013-10-05 23:13:40 +00:00
return QDialog::eventFilter(ui->coreModsTreeView, keyEvent);
}
2013-10-05 23:13:40 +00:00
bool LegacyModEditDialog::jarListFilter(QKeyEvent *keyEvent)
{
2013-10-05 23:13:40 +00:00
switch (keyEvent->key())
{
2013-10-05 23:13:40 +00:00
case Qt::Key_Up:
{
if (keyEvent->modifiers() & Qt::ControlModifier)
{
2013-10-05 23:13:40 +00:00
on_moveJarUpBtn_clicked();
return true;
}
2013-10-05 23:13:40 +00:00
break;
}
case Qt::Key_Down:
{
if (keyEvent->modifiers() & Qt::ControlModifier)
{
2013-10-05 23:13:40 +00:00
on_moveJarDownBtn_clicked();
return true;
2013-10-05 23:13:40 +00:00
}
break;
}
2013-10-05 23:13:40 +00:00
case Qt::Key_Delete:
on_rmJarBtn_clicked();
return true;
case Qt::Key_Plus:
on_addJarBtn_clicked();
return true;
default:
break;
}
return QDialog::eventFilter(ui->jarModsTreeView, keyEvent);
}
2013-10-05 23:13:40 +00:00
bool LegacyModEditDialog::loaderListFilter(QKeyEvent *keyEvent)
{
2013-10-05 23:13:40 +00:00
switch (keyEvent->key())
{
2013-10-05 23:13:40 +00:00
case Qt::Key_Delete:
on_rmModBtn_clicked();
return true;
case Qt::Key_Plus:
on_addModBtn_clicked();
return true;
default:
break;
}
2013-10-05 23:13:40 +00:00
return QDialog::eventFilter(ui->loaderModTreeView, keyEvent);
}
2013-10-05 23:13:40 +00:00
bool LegacyModEditDialog::texturePackListFilter(QKeyEvent *keyEvent)
{
2013-10-05 23:13:40 +00:00
switch (keyEvent->key())
{
2013-10-05 23:13:40 +00:00
case Qt::Key_Delete:
on_rmTexPackBtn_clicked();
return true;
case Qt::Key_Plus:
on_addTexPackBtn_clicked();
return true;
default:
break;
}
2013-10-05 23:13:40 +00:00
return QDialog::eventFilter(ui->texPackTreeView, keyEvent);
}
2013-10-05 23:13:40 +00:00
bool LegacyModEditDialog::eventFilter(QObject *obj, QEvent *ev)
{
if (ev->type() != QEvent::KeyPress)
{
2013-10-05 23:13:40 +00:00
return QDialog::eventFilter(obj, ev);
}
2013-10-05 23:13:40 +00:00
QKeyEvent *keyEvent = static_cast<QKeyEvent *>(ev);
if (obj == ui->jarModsTreeView)
return jarListFilter(keyEvent);
2013-10-05 23:13:40 +00:00
if (obj == ui->coreModsTreeView)
return coreListFilter(keyEvent);
2013-10-05 23:13:40 +00:00
if (obj == ui->loaderModTreeView)
return loaderListFilter(keyEvent);
2013-10-05 23:13:40 +00:00
if (obj == ui->texPackTreeView)
2013-08-28 02:38:29 +00:00
return texturePackListFilter(keyEvent);
2013-10-05 23:13:40 +00:00
return QDialog::eventFilter(obj, ev);
}
2013-08-18 18:52:17 +00:00
void LegacyModEditDialog::on_addCoreBtn_clicked()
{
//: Title of core mod selection dialog
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select Core Mods"));
2013-10-05 23:13:40 +00:00
for (auto filename : fileNames)
2013-08-18 18:52:17 +00:00
{
m_coremods->stopWatching();
2013-08-18 18:52:17 +00:00
m_coremods->installMod(QFileInfo(filename));
m_coremods->startWatching();
2013-08-18 18:52:17 +00:00
}
}
void LegacyModEditDialog::on_addForgeBtn_clicked()
{
2013-10-06 12:43:46 +00:00
VersionSelectDialog vselect(MMC->forgelist().get(), tr("Select Forge version"), this);
vselect.setFilter(1, m_inst->intendedVersionId());
if (vselect.exec() && vselect.selectedVersion())
{
2013-10-05 23:13:40 +00:00
ForgeVersionPtr forge =
std::dynamic_pointer_cast<ForgeVersion>(vselect.selectedVersion());
2013-10-05 23:13:40 +00:00
if (!forge)
return;
auto entry = MMC->metacache()->resolveEntry("minecraftforge", forge->filename);
2013-10-05 23:13:40 +00:00
if (entry->stale)
{
NetJob *fjob = new NetJob("Forge download");
fjob->addNetAction(CacheDownload::make(forge->universal_url, entry));
ProgressDialog dlg(this);
dlg.exec(fjob);
2013-10-05 23:13:40 +00:00
if (dlg.result() == QDialog::Accepted)
{
m_jarmods->stopWatching();
m_jarmods->installMod(QFileInfo(entry->getFullPath()));
m_jarmods->startWatching();
}
else
{
// failed to download forge :/
}
}
else
{
m_jarmods->stopWatching();
m_jarmods->installMod(QFileInfo(entry->getFullPath()));
m_jarmods->startWatching();
}
}
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_addJarBtn_clicked()
{
//: Title of jar mod selection dialog
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select Jar Mods"));
2013-10-05 23:13:40 +00:00
for (auto filename : fileNames)
{
m_jarmods->stopWatching();
m_jarmods->installMod(QFileInfo(filename));
m_jarmods->startWatching();
}
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_addModBtn_clicked()
{
//: Title of regular mod selection dialog
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select Loader Mods"));
2013-10-05 23:13:40 +00:00
for (auto filename : fileNames)
{
m_mods->stopWatching();
m_mods->installMod(QFileInfo(filename));
m_mods->startWatching();
}
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_addTexPackBtn_clicked()
{
//: Title of texture pack selection dialog
QStringList fileNames = QFileDialog::getOpenFileNames(this, tr("Select Texture Packs"));
2013-10-05 23:13:40 +00:00
for (auto filename : fileNames)
{
m_texturepacks->stopWatching();
m_texturepacks->installMod(QFileInfo(filename));
m_texturepacks->startWatching();
}
2013-08-18 18:52:17 +00:00
}
2013-08-18 18:52:17 +00:00
void LegacyModEditDialog::on_moveJarDownBtn_clicked()
{
int first, last;
auto list = ui->jarModsTreeView->selectionModel()->selectedRows();
2013-10-05 23:13:40 +00:00
if (!lastfirst(list, first, last))
return;
2013-08-18 18:52:17 +00:00
m_jarmods->moveModsDown(first, last);
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_moveJarUpBtn_clicked()
{
int first, last;
auto list = ui->jarModsTreeView->selectionModel()->selectedRows();
2013-10-05 23:13:40 +00:00
if (!lastfirst(list, first, last))
return;
m_jarmods->moveModsUp(first, last);
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_rmCoreBtn_clicked()
{
int first, last;
auto list = ui->coreModsTreeView->selectionModel()->selectedRows();
2013-10-05 23:13:40 +00:00
if (!lastfirst(list, first, last))
2013-08-18 18:52:17 +00:00
return;
m_coremods->stopWatching();
m_coremods->deleteMods(first, last);
m_coremods->startWatching();
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_rmJarBtn_clicked()
{
int first, last;
auto list = ui->jarModsTreeView->selectionModel()->selectedRows();
2013-10-05 23:13:40 +00:00
if (!lastfirst(list, first, last))
2013-08-18 18:52:17 +00:00
return;
m_jarmods->stopWatching();
m_jarmods->deleteMods(first, last);
m_jarmods->startWatching();
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_rmModBtn_clicked()
{
int first, last;
2013-08-28 02:38:29 +00:00
auto list = ui->loaderModTreeView->selectionModel()->selectedRows();
2013-10-05 23:13:40 +00:00
if (!lastfirst(list, first, last))
2013-08-18 18:52:17 +00:00
return;
m_mods->stopWatching();
m_mods->deleteMods(first, last);
m_mods->startWatching();
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_rmTexPackBtn_clicked()
{
int first, last;
auto list = ui->texPackTreeView->selectionModel()->selectedRows();
2013-10-05 23:13:40 +00:00
if (!lastfirst(list, first, last))
return;
m_texturepacks->stopWatching();
m_texturepacks->deleteMods(first, last);
m_texturepacks->startWatching();
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_viewCoreBtn_clicked()
{
openDirInDefaultProgram(m_inst->coreModsDir(), true);
}
void LegacyModEditDialog::on_viewModBtn_clicked()
{
openDirInDefaultProgram(m_inst->loaderModsDir(), true);
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_viewTexPackBtn_clicked()
{
2013-08-28 02:38:29 +00:00
openDirInDefaultProgram(m_inst->texturePacksDir(), true);
2013-08-18 18:52:17 +00:00
}
void LegacyModEditDialog::on_buttonBox_rejected()
{
close();
}
void LegacyModEditDialog::jarCurrent(QModelIndex current, QModelIndex previous)
{
if (!current.isValid())
{
ui->jarMIFrame->clear();
return;
}
int row = current.row();
Mod &m = m_jarmods->operator[](row);
ui->jarMIFrame->updateWithMod(m);
}
void LegacyModEditDialog::coreCurrent(QModelIndex current, QModelIndex previous)
{
if (!current.isValid())
{
ui->coreMIFrame->clear();
return;
}
int row = current.row();
Mod &m = m_coremods->operator[](row);
ui->coreMIFrame->updateWithMod(m);
}
void LegacyModEditDialog::loaderCurrent(QModelIndex current, QModelIndex previous)
{
if (!current.isValid())
{
ui->loaderMIFrame->clear();
return;
}
int row = current.row();
Mod &m = m_mods->operator[](row);
ui->loaderMIFrame->updateWithMod(m);
}