PrismLauncher/launcher/pages/modplatform/legacy_ftb/Page.cpp

370 lines
11 KiB
C++
Raw Normal View History

#include "Page.h"
#include "ui_Page.h"
2018-03-19 01:36:12 +00:00
#include <QInputDialog>
2018-03-19 01:36:12 +00:00
#include "MultiMC.h"
#include "dialogs/CustomMessageBox.h"
#include "dialogs/NewInstanceDialog.h"
#include "modplatform/legacy_ftb/PackFetchTask.h"
#include "modplatform/legacy_ftb/PackInstallTask.h"
#include "modplatform/legacy_ftb/PrivatePackManager.h"
#include "ListModel.h"
2018-03-19 01:36:12 +00:00
namespace LegacyFTB {
Page::Page(NewInstanceDialog* dialog, QWidget *parent)
: QWidget(parent), dialog(dialog), ui(new Ui::Page)
2018-03-19 01:36:12 +00:00
{
ftbFetchTask.reset(new PackFetchTask());
ftbPrivatePacks.reset(new PrivatePackManager());
2018-07-15 13:51:05 +01:00
ui->setupUi(this);
2018-03-19 01:36:12 +00:00
2018-07-15 13:51:05 +01:00
{
publicFilterModel = new FilterModel(this);
publicListModel = new ListModel(this);
2018-07-15 13:51:05 +01:00
publicFilterModel->setSourceModel(publicListModel);
2018-03-19 01:36:12 +00:00
2018-07-15 13:51:05 +01:00
ui->publicPackList->setModel(publicFilterModel);
ui->publicPackList->setSortingEnabled(true);
ui->publicPackList->header()->hide();
ui->publicPackList->setIndentation(0);
ui->publicPackList->setIconSize(QSize(42, 42));
2018-03-19 01:36:12 +00:00
2018-07-15 13:51:05 +01:00
for(int i = 0; i < publicFilterModel->getAvailableSortings().size(); i++)
{
ui->sortByBox->addItem(publicFilterModel->getAvailableSortings().keys().at(i));
}
2018-03-19 01:36:12 +00:00
2018-07-15 13:51:05 +01:00
ui->sortByBox->setCurrentText(publicFilterModel->translateCurrentSorting());
}
2018-03-19 01:36:12 +00:00
2018-07-15 13:51:05 +01:00
{
thirdPartyFilterModel = new FilterModel(this);
thirdPartyModel = new ListModel(this);
2018-07-15 13:51:05 +01:00
thirdPartyFilterModel->setSourceModel(thirdPartyModel);
2018-07-15 13:51:05 +01:00
ui->thirdPartyPackList->setModel(thirdPartyFilterModel);
ui->thirdPartyPackList->setSortingEnabled(true);
ui->thirdPartyPackList->header()->hide();
ui->thirdPartyPackList->setIndentation(0);
ui->thirdPartyPackList->setIconSize(QSize(42, 42));
2018-04-06 14:08:59 +01:00
2018-07-15 13:51:05 +01:00
thirdPartyFilterModel->setSorting(publicFilterModel->getCurrentSorting());
}
2018-03-19 01:36:12 +00:00
{
privateFilterModel = new FilterModel(this);
privateListModel = new ListModel(this);
privateFilterModel->setSourceModel(privateListModel);
ui->privatePackList->setModel(privateFilterModel);
ui->privatePackList->setSortingEnabled(true);
ui->privatePackList->header()->hide();
ui->privatePackList->setIndentation(0);
ui->privatePackList->setIconSize(QSize(42, 42));
privateFilterModel->setSorting(publicFilterModel->getCurrentSorting());
}
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
connect(ui->sortByBox, &QComboBox::currentTextChanged, this, &Page::onSortingSelectionChanged);
connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &Page::onVersionSelectionItemChanged);
connect(ui->publicPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &Page::onPublicPackSelectionChanged);
connect(ui->thirdPartyPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &Page::onThirdPartyPackSelectionChanged);
connect(ui->privatePackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &Page::onPrivatePackSelectionChanged);
connect(ui->addPackBtn, &QPushButton::pressed, this, &Page::onAddPackClicked);
connect(ui->removePackBtn, &QPushButton::pressed, this, &Page::onRemovePackClicked);
2018-03-19 01:36:12 +00:00
connect(ui->tabWidget, &QTabWidget::currentChanged, this, &Page::onTabChanged);
2018-04-05 18:33:31 +01:00
// ui->modpackInfo->setOpenExternalLinks(true);
2018-04-05 18:33:31 +01:00
2018-07-15 13:51:05 +01:00
ui->publicPackList->selectionModel()->reset();
ui->thirdPartyPackList->selectionModel()->reset();
ui->privatePackList->selectionModel()->reset();
onTabChanged(ui->tabWidget->currentIndex());
2018-03-19 01:36:12 +00:00
}
Page::~Page()
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
delete ui;
2018-03-19 01:36:12 +00:00
}
bool Page::shouldDisplay() const
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
return true;
2018-03-19 01:36:12 +00:00
}
void Page::openedImpl()
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
if(!initialized)
{
connect(ftbFetchTask.get(), &PackFetchTask::finished, this, &Page::ftbPackDataDownloadSuccessfully);
connect(ftbFetchTask.get(), &PackFetchTask::failed, this, &Page::ftbPackDataDownloadFailed);
connect(ftbFetchTask.get(), &PackFetchTask::privateFileDownloadFinished, this, &Page::ftbPrivatePackDataDownloadSuccessfully);
connect(ftbFetchTask.get(), &PackFetchTask::privateFileDownloadFailed, this, &Page::ftbPrivatePackDataDownloadFailed);
2018-07-15 13:51:05 +01:00
ftbFetchTask->fetch();
ftbPrivatePacks->load();
ftbFetchTask->fetchPrivate(ftbPrivatePacks->getCurrentPackCodes().toList());
2018-07-15 13:51:05 +01:00
initialized = true;
}
suggestCurrent();
2018-03-19 01:36:12 +00:00
}
void Page::suggestCurrent()
2018-03-19 01:36:12 +00:00
{
if(!isOpened)
2018-07-15 13:51:05 +01:00
{
return;
}
2021-06-20 21:52:10 +01:00
if(selected.broken || selectedVersion.isEmpty())
{
dialog->setSuggestedPack();
return;
}
dialog->setSuggestedPack(selected.name, new PackInstallTask(selected, selectedVersion));
QString editedLogoName;
if(selected.logo.toLower().startsWith("ftb"))
{
editedLogoName = selected.logo;
}
else
{
editedLogoName = "ftb_" + selected.logo;
}
editedLogoName = editedLogoName.left(editedLogoName.lastIndexOf(".png"));
if(selected.type == PackType::Public)
{
publicListModel->getLogo(selected.logo, [this, editedLogoName](QString logo)
2018-07-15 13:51:05 +01:00
{
dialog->setSuggestedIconFromFile(logo, editedLogoName);
});
}
else if (selected.type == PackType::ThirdParty)
{
thirdPartyModel->getLogo(selected.logo, [this, editedLogoName](QString logo)
2018-07-15 13:51:05 +01:00
{
dialog->setSuggestedIconFromFile(logo, editedLogoName);
});
}
else if (selected.type == PackType::Private)
{
privateListModel->getLogo(selected.logo, [this, editedLogoName](QString logo)
{
dialog->setSuggestedIconFromFile(logo, editedLogoName);
});
2018-07-15 13:51:05 +01:00
}
2018-03-19 01:36:12 +00:00
}
void Page::ftbPackDataDownloadSuccessfully(ModpackList publicPacks, ModpackList thirdPartyPacks)
{
2018-07-15 13:51:05 +01:00
publicListModel->fill(publicPacks);
thirdPartyModel->fill(thirdPartyPacks);
}
void Page::ftbPackDataDownloadFailed(QString reason)
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
//TODO: Display the error
2018-03-19 01:36:12 +00:00
}
void Page::ftbPrivatePackDataDownloadSuccessfully(Modpack pack)
{
privateListModel->addPack(pack);
}
void Page::ftbPrivatePackDataDownloadFailed(QString reason, QString packCode)
{
auto reply = QMessageBox::question(
this,
tr("FTB private packs"),
tr("Failed to download pack information for code %1.\nShould it be removed now?").arg(packCode)
);
if(reply == QMessageBox::Yes)
{
ftbPrivatePacks->remove(packCode);
}
}
void Page::onPublicPackSelectionChanged(QModelIndex now, QModelIndex prev)
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
if(!now.isValid())
{
onPackSelectionChanged();
return;
}
Modpack selectedPack = publicFilterModel->data(now, Qt::UserRole).value<Modpack>();
2018-07-15 13:51:05 +01:00
onPackSelectionChanged(&selectedPack);
2018-03-19 01:36:12 +00:00
}
void Page::onThirdPartyPackSelectionChanged(QModelIndex now, QModelIndex prev)
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
if(!now.isValid())
{
onPackSelectionChanged();
return;
}
Modpack selectedPack = thirdPartyFilterModel->data(now, Qt::UserRole).value<Modpack>();
2018-07-15 13:51:05 +01:00
onPackSelectionChanged(&selectedPack);
2018-03-19 01:36:12 +00:00
}
void Page::onPrivatePackSelectionChanged(QModelIndex now, QModelIndex prev)
{
if(!now.isValid())
{
onPackSelectionChanged();
return;
}
Modpack selectedPack = privateFilterModel->data(now, Qt::UserRole).value<Modpack>();
onPackSelectionChanged(&selectedPack);
}
void Page::onPackSelectionChanged(Modpack* pack)
2018-03-19 01:36:12 +00:00
{
ui->versionSelectionBox->clear();
2018-07-15 13:51:05 +01:00
if(pack)
{
currentModpackInfo->setHtml("Pack by <b>" + pack->author + "</b>" +
"<br>Minecraft " + pack->mcVersion + "<br>" + "<br>" + pack->description + "<ul><li>" + pack->mods.replace(";", "</li><li>")
+ "</li></ul>");
2018-07-15 13:51:05 +01:00
bool currentAdded = false;
for(int i = 0; i < pack->oldVersions.size(); i++)
{
if(pack->currentVersion == pack->oldVersions.at(i))
{
currentAdded = true;
}
ui->versionSelectionBox->addItem(pack->oldVersions.at(i));
2018-07-15 13:51:05 +01:00
}
if(!currentAdded)
{
ui->versionSelectionBox->addItem(pack->currentVersion);
2018-07-15 13:51:05 +01:00
}
selected = *pack;
}
else
{
currentModpackInfo->setHtml("");
ui->versionSelectionBox->clear();
if(isOpened)
{
dialog->setSuggestedPack();
}
return;
}
2018-07-15 13:51:05 +01:00
suggestCurrent();
2018-03-19 01:36:12 +00:00
}
void Page::onVersionSelectionItemChanged(QString data)
2018-03-19 01:36:12 +00:00
{
2018-07-15 13:51:05 +01:00
if(data.isNull() || data.isEmpty())
{
selectedVersion = "";
return;
}
selectedVersion = data;
suggestCurrent();
2018-03-19 01:36:12 +00:00
}
void Page::onSortingSelectionChanged(QString data)
2018-03-19 01:36:12 +00:00
{
FilterModel::Sorting toSet = publicFilterModel->getAvailableSortings().value(data);
2018-07-15 13:51:05 +01:00
publicFilterModel->setSorting(toSet);
thirdPartyFilterModel->setSorting(toSet);
privateFilterModel->setSorting(toSet);
2018-03-19 01:36:12 +00:00
}
2018-04-05 18:33:31 +01:00
void Page::onTabChanged(int tab)
2018-04-05 18:33:31 +01:00
{
if(tab == 1)
2018-07-15 13:51:05 +01:00
{
currentModel = thirdPartyFilterModel;
currentList = ui->thirdPartyPackList;
currentModpackInfo = ui->thirdPartyPackDescription;
}
else if(tab == 2)
{
currentModel = privateFilterModel;
currentList = ui->privatePackList;
currentModpackInfo = ui->privatePackDescription;
2018-07-15 13:51:05 +01:00
}
else
{
currentModel = publicFilterModel;
currentList = ui->publicPackList;
currentModpackInfo = ui->publicPackDescription;
2018-07-15 13:51:05 +01:00
}
currentList->selectionModel()->reset();
2018-07-15 13:51:05 +01:00
QModelIndex idx = currentList->currentIndex();
if(idx.isValid())
{
auto pack = currentModel->data(idx, Qt::UserRole).value<Modpack>();
2018-07-15 13:51:05 +01:00
onPackSelectionChanged(&pack);
}
else
{
onPackSelectionChanged();
}
2018-04-05 18:33:31 +01:00
}
void Page::onAddPackClicked()
{
bool ok;
QString text = QInputDialog::getText(
this,
tr("Add FTB pack"),
tr("Enter pack code:"),
QLineEdit::Normal,
QString(),
&ok
);
if(ok && !text.isEmpty())
{
ftbPrivatePacks->add(text);
ftbFetchTask->fetchPrivate({text});
}
}
void Page::onRemovePackClicked()
{
auto index = ui->privatePackList->currentIndex();
if(!index.isValid())
{
return;
}
auto row = index.row();
Modpack pack = privateListModel->at(row);
auto answer = QMessageBox::question(
this,
tr("Remove pack"),
tr("Are you sure you want to remove pack %1?").arg(pack.name),
QMessageBox::Yes | QMessageBox::No
);
if(answer != QMessageBox::Yes)
{
return;
}
ftbPrivatePacks->remove(pack.packCode);
privateListModel->remove(row);
onPackSelectionChanged();
}
}