2022-03-03 05:47:10 +05:30
|
|
|
#include "ModPage.h"
|
|
|
|
#include "ui_ModPage.h"
|
|
|
|
|
|
|
|
#include <QKeyEvent>
|
2022-04-03 03:03:50 +05:30
|
|
|
#include <memory>
|
2022-03-03 05:47:10 +05:30
|
|
|
|
2022-03-08 03:59:59 +05:30
|
|
|
#include "minecraft/MinecraftInstance.h"
|
|
|
|
#include "minecraft/PackProfile.h"
|
2022-03-03 05:47:10 +05:30
|
|
|
#include "ui/dialogs/ModDownloadDialog.h"
|
|
|
|
|
2022-03-06 22:24:05 +05:30
|
|
|
ModPage::ModPage(ModDownloadDialog* dialog, BaseInstance* instance, ModAPI* api)
|
2022-04-03 03:03:50 +05:30
|
|
|
: QWidget(dialog)
|
|
|
|
, m_instance(instance)
|
|
|
|
, ui(new Ui::ModPage)
|
|
|
|
, dialog(dialog)
|
2022-04-14 18:57:03 +05:30
|
|
|
, filter_widget(static_cast<MinecraftInstance*>(instance)->getPackProfile()->getComponentVersion("net.minecraft"), this)
|
2022-04-03 03:03:50 +05:30
|
|
|
, api(api)
|
2022-03-03 05:47:10 +05:30
|
|
|
{
|
|
|
|
ui->setupUi(this);
|
|
|
|
connect(ui->searchButton, &QPushButton::clicked, this, &ModPage::triggerSearch);
|
2022-04-03 03:03:50 +05:30
|
|
|
connect(ui->modFilterButton, &QPushButton::clicked, this, &ModPage::filterMods);
|
2022-03-03 05:47:10 +05:30
|
|
|
ui->searchEdit->installEventFilter(this);
|
|
|
|
|
|
|
|
ui->versionSelectionBox->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
|
|
|
ui->versionSelectionBox->view()->parentWidget()->setMaximumHeight(300);
|
|
|
|
|
2022-04-14 18:57:03 +05:30
|
|
|
ui->gridLayout_3->addWidget(&filter_widget, 0, 0, 1, ui->gridLayout_3->columnCount());
|
|
|
|
|
|
|
|
filter_widget.setInstance(static_cast<MinecraftInstance*>(m_instance));
|
|
|
|
m_filter = filter_widget.getFilter();
|
2022-04-14 19:22:23 +05:30
|
|
|
|
|
|
|
connect(&filter_widget, &ModFilterWidget::filterChanged, this, [&]{
|
|
|
|
ui->searchButton->setStyleSheet("text-decoration: underline");
|
|
|
|
});
|
|
|
|
connect(&filter_widget, &ModFilterWidget::filterUnchanged, this, [&]{
|
|
|
|
ui->searchButton->setStyleSheet("text-decoration: none");
|
|
|
|
});
|
2022-03-03 05:47:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
ModPage::~ModPage()
|
|
|
|
{
|
|
|
|
delete ui;
|
|
|
|
}
|
|
|
|
|
2022-03-08 03:59:59 +05:30
|
|
|
|
|
|
|
/******** Qt things ********/
|
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
void ModPage::openedImpl()
|
|
|
|
{
|
|
|
|
updateSelectionButton();
|
|
|
|
triggerSearch();
|
|
|
|
}
|
|
|
|
|
2022-03-08 19:42:35 +05:30
|
|
|
auto ModPage::eventFilter(QObject* watched, QEvent* event) -> bool
|
2022-03-03 05:47:10 +05:30
|
|
|
{
|
|
|
|
if (watched == ui->searchEdit && event->type() == QEvent::KeyPress) {
|
2022-03-08 19:42:35 +05:30
|
|
|
auto* keyEvent = dynamic_cast<QKeyEvent*>(event);
|
2022-03-03 05:47:10 +05:30
|
|
|
if (keyEvent->key() == Qt::Key_Return) {
|
|
|
|
triggerSearch();
|
|
|
|
keyEvent->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QWidget::eventFilter(watched, event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-08 03:59:59 +05:30
|
|
|
/******** Callbacks to events in the UI (set up in the derived classes) ********/
|
2022-03-03 05:47:10 +05:30
|
|
|
|
2022-04-03 03:03:50 +05:30
|
|
|
void ModPage::filterMods()
|
|
|
|
{
|
2022-04-14 18:57:03 +05:30
|
|
|
filter_widget.setHidden(!filter_widget.isHidden());
|
2022-04-03 03:03:50 +05:30
|
|
|
}
|
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
void ModPage::triggerSearch()
|
|
|
|
{
|
2022-04-14 18:57:03 +05:30
|
|
|
auto changed = filter_widget.changed();
|
|
|
|
m_filter = filter_widget.getFilter();
|
|
|
|
|
|
|
|
if(changed){
|
|
|
|
ui->packView->clearSelection();
|
|
|
|
ui->packDescription->clear();
|
|
|
|
ui->versionSelectionBox->clear();
|
|
|
|
updateSelectionButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
listModel->searchWithTerm(ui->searchEdit->text(), ui->sortByBox->currentIndex(), changed);
|
2022-03-03 05:47:10 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void ModPage::onSelectionChanged(QModelIndex first, QModelIndex second)
|
|
|
|
{
|
|
|
|
ui->versionSelectionBox->clear();
|
|
|
|
|
|
|
|
if (!first.isValid()) { return; }
|
|
|
|
|
|
|
|
current = listModel->data(first, Qt::UserRole).value<ModPlatform::IndexedPack>();
|
|
|
|
QString text = "";
|
|
|
|
QString name = current.name;
|
|
|
|
|
|
|
|
if (current.websiteUrl.isEmpty())
|
|
|
|
text = name;
|
|
|
|
else
|
|
|
|
text = "<a href=\"" + current.websiteUrl + "\">" + name + "</a>";
|
2022-04-15 01:25:03 +05:30
|
|
|
|
2022-03-03 05:47:10 +05:30
|
|
|
if (!current.authors.empty()) {
|
2022-03-08 19:42:35 +05:30
|
|
|
auto authorToStr = [](ModPlatform::ModpackAuthor& author) -> QString {
|
2022-03-03 05:47:10 +05:30
|
|
|
if (author.url.isEmpty()) { return author.name; }
|
|
|
|
return QString("<a href=\"%1\">%2</a>").arg(author.url, author.name);
|
|
|
|
};
|
|
|
|
QStringList authorStrs;
|
|
|
|
for (auto& author : current.authors) {
|
|
|
|
authorStrs.push_back(authorToStr(author));
|
|
|
|
}
|
|
|
|
text += "<br>" + tr(" by ") + authorStrs.join(", ");
|
|
|
|
}
|
|
|
|
text += "<br><br>";
|
|
|
|
|
|
|
|
ui->packDescription->setHtml(text + current.description);
|
|
|
|
|
|
|
|
if (!current.versionsLoaded) {
|
|
|
|
qDebug() << QString("Loading %1 mod versions").arg(debugName());
|
|
|
|
|
|
|
|
ui->modSelectionButton->setText(tr("Loading versions..."));
|
|
|
|
ui->modSelectionButton->setEnabled(false);
|
|
|
|
|
2022-03-07 00:34:24 +05:30
|
|
|
listModel->requestModVersions(current);
|
2022-03-03 05:47:10 +05:30
|
|
|
} else {
|
|
|
|
for (int i = 0; i < current.versions.size(); i++) {
|
|
|
|
ui->versionSelectionBox->addItem(current.versions[i].version, QVariant(i));
|
|
|
|
}
|
2022-03-25 02:54:51 +05:30
|
|
|
if (ui->versionSelectionBox->count() == 0) { ui->versionSelectionBox->addItem(tr("No valid version found."), QVariant(-1)); }
|
2022-03-03 05:47:10 +05:30
|
|
|
|
|
|
|
updateSelectionButton();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModPage::onVersionSelectionChanged(QString data)
|
|
|
|
{
|
|
|
|
if (data.isNull() || data.isEmpty()) {
|
|
|
|
selectedVersion = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
selectedVersion = ui->versionSelectionBox->currentData().toInt();
|
|
|
|
updateSelectionButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ModPage::onModSelected()
|
|
|
|
{
|
|
|
|
auto& version = current.versions[selectedVersion];
|
|
|
|
if (dialog->isModSelected(current.name, version.fileName)) {
|
|
|
|
dialog->removeSelectedMod(current.name);
|
|
|
|
} else {
|
|
|
|
dialog->addSelectedMod(current.name, new ModDownloadTask(version.downloadUrl, version.fileName, dialog->mods));
|
|
|
|
}
|
|
|
|
|
|
|
|
updateSelectionButton();
|
|
|
|
}
|
2022-03-08 03:59:59 +05:30
|
|
|
|
|
|
|
|
|
|
|
/******** Make changes to the UI ********/
|
|
|
|
|
2022-03-25 02:54:51 +05:30
|
|
|
void ModPage::retranslate()
|
|
|
|
{
|
2022-04-15 01:25:03 +05:30
|
|
|
ui->retranslateUi(this);
|
2022-03-25 02:54:51 +05:30
|
|
|
}
|
|
|
|
|
2022-04-03 18:51:48 +05:30
|
|
|
void ModPage::updateModVersions(int prev_count)
|
2022-03-08 03:59:59 +05:30
|
|
|
{
|
2022-03-08 19:42:35 +05:30
|
|
|
auto packProfile = (dynamic_cast<MinecraftInstance*>(m_instance))->getPackProfile();
|
2022-03-08 03:59:59 +05:30
|
|
|
|
|
|
|
QString mcVersion = packProfile->getComponentVersion("net.minecraft");
|
2022-02-21 01:25:26 +05:30
|
|
|
|
2022-04-15 01:25:03 +05:30
|
|
|
QString loaderString = ModAPI::getModLoaderString(packProfile->getModLoader());
|
2022-03-08 03:59:59 +05:30
|
|
|
|
|
|
|
for (int i = 0; i < current.versions.size(); i++) {
|
|
|
|
auto version = current.versions[i];
|
2022-04-03 03:03:50 +05:30
|
|
|
bool valid = false;
|
|
|
|
for(auto& mcVer : m_filter->versions){
|
2022-04-03 04:38:37 +05:30
|
|
|
//NOTE: Flame doesn't care about loaderString, so passing it changes nothing.
|
2022-04-03 03:03:50 +05:30
|
|
|
if (validateVersion(version, mcVer.toString(), loaderString)) {
|
|
|
|
valid = true;
|
|
|
|
break;
|
|
|
|
}
|
2022-03-08 03:59:59 +05:30
|
|
|
}
|
2022-04-03 04:38:37 +05:30
|
|
|
if(valid || m_filter->versions.size() == 0)
|
2022-04-03 03:03:50 +05:30
|
|
|
ui->versionSelectionBox->addItem(version.version, QVariant(i));
|
2022-03-08 03:59:59 +05:30
|
|
|
}
|
2022-04-03 18:51:48 +05:30
|
|
|
if (ui->versionSelectionBox->count() == 0 && prev_count != 0) {
|
|
|
|
ui->versionSelectionBox->addItem(tr("No valid version found!"), QVariant(-1));
|
|
|
|
ui->modSelectionButton->setText(tr("Cannot select invalid version :("));
|
|
|
|
}
|
2022-03-08 03:59:59 +05:30
|
|
|
|
|
|
|
updateSelectionButton();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ModPage::updateSelectionButton()
|
|
|
|
{
|
|
|
|
if (!isOpened || selectedVersion < 0) {
|
|
|
|
ui->modSelectionButton->setEnabled(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui->modSelectionButton->setEnabled(true);
|
|
|
|
auto& version = current.versions[selectedVersion];
|
|
|
|
if (!dialog->isModSelected(current.name, version.fileName)) {
|
|
|
|
ui->modSelectionButton->setText(tr("Select mod for download"));
|
|
|
|
} else {
|
|
|
|
ui->modSelectionButton->setText(tr("Deselect mod for download"));
|
|
|
|
}
|
|
|
|
}
|