From 6aada8adf7df5075d5838512670a0b2cc2bc12a1 Mon Sep 17 00:00:00 2001 From: Jannis Lübke Date: Thu, 2 Aug 2018 00:52:31 +0200 Subject: NOISSUE FTB pack code implementation, cleaned up --- application/pages/modplatform/FTBPage.cpp | 203 +++++++++++++++++++++---- application/pages/modplatform/FTBPage.h | 24 ++- application/pages/modplatform/FTBPage.ui | 159 ++++++++++--------- application/pages/modplatform/FtbListModel.cpp | 103 ++++++++++--- application/pages/modplatform/FtbListModel.h | 5 + 5 files changed, 364 insertions(+), 130 deletions(-) (limited to 'application/pages') diff --git a/application/pages/modplatform/FTBPage.cpp b/application/pages/modplatform/FTBPage.cpp index 1884bbfb..dca86efd 100644 --- a/application/pages/modplatform/FTBPage.cpp +++ b/application/pages/modplatform/FTBPage.cpp @@ -1,20 +1,23 @@ #include "FTBPage.h" #include "ui_FTBPage.h" +#include + #include "MultiMC.h" #include "dialogs/CustomMessageBox.h" #include "dialogs/NewInstanceDialog.h" #include "modplatform/ftb/FtbPackFetchTask.h" #include "modplatform/ftb/FtbPackInstallTask.h" +#include "modplatform/ftb/FtbPrivatePackManager.h" #include "FtbListModel.h" FTBPage::FTBPage(NewInstanceDialog* dialog, QWidget *parent) : QWidget(parent), dialog(dialog), ui(new Ui::FTBPage) { - ftbFetchTask = new FtbPackFetchTask(); + ftbFetchTask.reset(new FtbPackFetchTask()); + ftbPrivatePacks.reset(new FtbPrivatePackManager()); ui->setupUi(this); - ui->tabWidget->tabBar()->hide(); { publicFilterModel = new FtbFilterModel(this); @@ -49,29 +52,47 @@ FTBPage::FTBPage(NewInstanceDialog* dialog, QWidget *parent) thirdPartyFilterModel->setSorting(publicFilterModel->getCurrentSorting()); } - ui->packVersionSelection->view()->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); - ui->packVersionSelection->view()->parentWidget()->setMaximumHeight(300); + { + privateFilterModel = new FtbFilterModel(this); + privateListModel = new FtbListModel(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, &FTBPage::onSortingSelectionChanged); - connect(ui->packVersionSelection, &QComboBox::currentTextChanged, this, &FTBPage::onVersionSelectionItemChanged); + connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FTBPage::onVersionSelectionItemChanged); connect(ui->publicPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onPublicPackSelectionChanged); connect(ui->thirdPartyPackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onThirdPartyPackSelectionChanged); + connect(ui->privatePackList->selectionModel(), &QItemSelectionModel::currentChanged, this, &FTBPage::onPrivatePackSelectionChanged); + + connect(ui->addPackBtn, &QPushButton::pressed, this, &FTBPage::onAddPackClicked); + connect(ui->removePackBtn, &QPushButton::pressed, this, &FTBPage::onRemovePackClicked); - connect(ui->ftbTabWidget, &QTabWidget::currentChanged, this, &FTBPage::onTabChanged); + connect(ui->tabWidget, &QTabWidget::currentChanged, this, &FTBPage::onTabChanged); - ui->modpackInfo->setOpenExternalLinks(true); + // ui->modpackInfo->setOpenExternalLinks(true); ui->publicPackList->selectionModel()->reset(); ui->thirdPartyPackList->selectionModel()->reset(); + ui->privatePackList->selectionModel()->reset(); + + onTabChanged(ui->tabWidget->currentIndex()); } FTBPage::~FTBPage() { delete ui; - if(ftbFetchTask) { - ftbFetchTask->deleteLater(); - } } bool FTBPage::shouldDisplay() const @@ -83,9 +104,15 @@ void FTBPage::openedImpl() { if(!initialized) { - connect(ftbFetchTask, &FtbPackFetchTask::finished, this, &FTBPage::ftbPackDataDownloadSuccessfully); - connect(ftbFetchTask, &FtbPackFetchTask::failed, this, &FTBPage::ftbPackDataDownloadFailed); + connect(ftbFetchTask.get(), &FtbPackFetchTask::finished, this, &FTBPage::ftbPackDataDownloadSuccessfully); + connect(ftbFetchTask.get(), &FtbPackFetchTask::failed, this, &FTBPage::ftbPackDataDownloadFailed); + + connect(ftbFetchTask.get(), &FtbPackFetchTask::privateFileDownloadFinished, this, &FTBPage::ftbPrivatePackDataDownloadSuccessfully); + connect(ftbFetchTask.get(), &FtbPackFetchTask::privateFileDownloadFailed, this, &FTBPage::ftbPrivatePackDataDownloadFailed); + ftbFetchTask->fetch(); + ftbPrivatePacks->load(); + ftbFetchTask->fetchPrivate(ftbPrivatePacks->getCurrentPackCodes().toList()); initialized = true; } suggestCurrent(); @@ -98,13 +125,37 @@ void FTBPage::suggestCurrent() if(!selected.broken) { dialog->setSuggestedPack(selected.name, new FtbPackInstallTask(selected, selectedVersion)); - if(selected.type == FtbPackType::Public) { - publicListModel->getLogo(selected.logo, [this](QString logo){ - dialog->setSuggestedIconFromFile(logo, "ftb_" + selected.name); + 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 == FtbPackType::Public) + { + publicListModel->getLogo(selected.logo, [this, editedLogoName](QString logo) + { + dialog->setSuggestedIconFromFile(logo, editedLogoName); }); - } else if (selected.type == FtbPackType::ThirdParty) { - thirdPartyModel->getLogo(selected.logo, [this](QString logo){ - dialog->setSuggestedIconFromFile(logo, "ftb_" + selected.name); + } + else if (selected.type == FtbPackType::ThirdParty) + { + thirdPartyModel->getLogo(selected.logo, [this, editedLogoName](QString logo) + { + dialog->setSuggestedIconFromFile(logo, editedLogoName); + }); + } + else if (selected.type == FtbPackType::Private) + { + privateListModel->getLogo(selected.logo, [this, editedLogoName](QString logo) + { + dialog->setSuggestedIconFromFile(logo, editedLogoName); }); } } @@ -126,6 +177,24 @@ void FTBPage::ftbPackDataDownloadFailed(QString reason) //TODO: Display the error } +void FTBPage::ftbPrivatePackDataDownloadSuccessfully(FtbModpack pack) +{ + privateListModel->addPack(pack); +} + +void FTBPage::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 FTBPage::onPublicPackSelectionChanged(QModelIndex now, QModelIndex prev) { if(!now.isValid()) @@ -148,13 +217,25 @@ void FTBPage::onThirdPartyPackSelectionChanged(QModelIndex now, QModelIndex prev onPackSelectionChanged(&selectedPack); } +void FTBPage::onPrivatePackSelectionChanged(QModelIndex now, QModelIndex prev) +{ + if(!now.isValid()) + { + onPackSelectionChanged(); + return; + } + FtbModpack selectedPack = privateFilterModel->data(now, Qt::UserRole).value(); + onPackSelectionChanged(&selectedPack); +} + void FTBPage::onPackSelectionChanged(FtbModpack* pack) { - ui->packVersionSelection->clear(); + ui->versionSelectionBox->clear(); if(pack) { - ui->modpackInfo->setHtml("Pack by " + pack->author + "" + "
Minecraft " + pack->mcVersion + "
" - "
" + pack->description + "
  • " + pack->mods.replace(";", "
  • ") + "
"); + currentModpackInfo->setHtml("Pack by " + pack->author + "" + + "
Minecraft " + pack->mcVersion + "
" + "
" + pack->description + "
  • " + pack->mods.replace(";", "
  • ") + + "
"); bool currentAdded = false; for(int i = 0; i < pack->oldVersions.size(); i++) @@ -163,15 +244,25 @@ void FTBPage::onPackSelectionChanged(FtbModpack* pack) { currentAdded = true; } - ui->packVersionSelection->addItem(pack->oldVersions.at(i)); + ui->versionSelectionBox->addItem(pack->oldVersions.at(i)); } if(!currentAdded) { - ui->packVersionSelection->addItem(pack->currentVersion); + ui->versionSelectionBox->addItem(pack->currentVersion); } selected = *pack; } + else + { + currentModpackInfo->setHtml(""); + ui->versionSelectionBox->clear(); + if(isOpened) + { + dialog->setSuggestedPack(); + } + return; + } suggestCurrent(); } @@ -192,22 +283,31 @@ void FTBPage::onSortingSelectionChanged(QString data) FtbFilterModel::Sorting toSet = publicFilterModel->getAvailableSortings().value(data); publicFilterModel->setSorting(toSet); thirdPartyFilterModel->setSorting(toSet); + privateFilterModel->setSorting(toSet); } void FTBPage::onTabChanged(int tab) { - FtbFilterModel* currentModel = nullptr; - QTreeView* currentList = nullptr; - if (tab == 0) + if(tab == 1) { - currentModel = publicFilterModel; - currentList = ui->publicPackList; + currentModel = thirdPartyFilterModel; + currentList = ui->thirdPartyPackList; + currentModpackInfo = ui->thirdPartyPackDescription; + } + else if(tab == 2) + { + currentModel = privateFilterModel; + currentList = ui->privatePackList; + currentModpackInfo = ui->privatePackDescription; } else { - currentModel = thirdPartyFilterModel; - currentList = ui->thirdPartyPackList; + currentModel = publicFilterModel; + currentList = ui->publicPackList; + currentModpackInfo = ui->publicPackDescription; } + + currentList->selectionModel()->reset(); QModelIndex idx = currentList->currentIndex(); if(idx.isValid()) { @@ -219,3 +319,46 @@ void FTBPage::onTabChanged(int tab) onPackSelectionChanged(); } } + +void FTBPage::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 FTBPage::onRemovePackClicked() +{ + auto index = ui->privatePackList->currentIndex(); + if(!index.isValid()) + { + return; + } + auto row = index.row(); + FtbModpack 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(); +} diff --git a/application/pages/modplatform/FTBPage.h b/application/pages/modplatform/FTBPage.h index 46c14c30..6467decc 100644 --- a/application/pages/modplatform/FTBPage.h +++ b/application/pages/modplatform/FTBPage.h @@ -16,12 +16,15 @@ #pragma once #include +#include +#include #include "pages/BasePage.h" #include #include "tasks/Task.h" #include "modplatform/ftb/PackHelpers.h" #include "modplatform/ftb/FtbPackFetchTask.h" +#include "QObjectPtr.h" namespace Ui { @@ -31,6 +34,9 @@ class FTBPage; class FtbListModel; class FtbFilterModel; class NewInstanceDialog; +class FtbPrivatePackListModel; +class FtbPrivatePackFilterModel; +class FtbPrivatePackManager; class FTBPage : public QWidget, public BasePage { @@ -66,15 +72,26 @@ private slots: void ftbPackDataDownloadSuccessfully(FtbModpackList publicPacks, FtbModpackList thirdPartyPacks); void ftbPackDataDownloadFailed(QString reason); + void ftbPrivatePackDataDownloadSuccessfully(FtbModpack pack); + void ftbPrivatePackDataDownloadFailed(QString reason, QString packCode); + void onSortingSelectionChanged(QString data); void onVersionSelectionItemChanged(QString data); void onPublicPackSelectionChanged(QModelIndex first, QModelIndex second); void onThirdPartyPackSelectionChanged(QModelIndex first, QModelIndex second); + void onPrivatePackSelectionChanged(QModelIndex first, QModelIndex second); void onTabChanged(int tab); + void onAddPackClicked(); + void onRemovePackClicked(); + private: + FtbFilterModel* currentModel = nullptr; + QTreeView* currentList = nullptr; + QTextBrowser* currentModpackInfo = nullptr; + bool initialized = false; FtbModpack selected; QString selectedVersion; @@ -85,7 +102,12 @@ private: FtbListModel *thirdPartyModel = nullptr; FtbFilterModel *thirdPartyFilterModel = nullptr; - FtbPackFetchTask *ftbFetchTask = nullptr; + FtbListModel *privateListModel = nullptr; + FtbFilterModel *privateFilterModel = nullptr; + + unique_qobject_ptr ftbFetchTask; + std::unique_ptr ftbPrivatePacks; + NewInstanceDialog* dialog = nullptr; Ui::FTBPage *ui = nullptr; diff --git a/application/pages/modplatform/FTBPage.ui b/application/pages/modplatform/FTBPage.ui index cb085af2..e5ed78cb 100644 --- a/application/pages/modplatform/FTBPage.ui +++ b/application/pages/modplatform/FTBPage.ui @@ -11,18 +11,6 @@ - - 0 - - - 0 - - - 0 - - - 0 - @@ -30,86 +18,107 @@ - + Public - - - - - Qt::ScrollBarAsNeeded + + + + + + 250 + 16777215 + - - - - Version selected: + + + + + + + + 3rd Party + + + + + + + + + + 250 + 16777215 + - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignVCenter + + + + + + + Private + + + + + + + 250 + 16777215 + - - + + + + Add pack + + - - - - - - - 0 - 0 - - - - QTabWidget::Rounded - - - 0 + + + Remove selected pack - - - Public Packs - - - - - - Qt::ScrollBarAsNeeded - - - Qt::ScrollBarAlwaysOff - - - - - - - - 3rd Party Packs - - - - - - Qt::ScrollBarAsNeeded - - - Qt::ScrollBarAlwaysOff - - - - - + + + + + + + + + Version selected: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + + + + 265 + 0 + + + + + + diff --git a/application/pages/modplatform/FtbListModel.cpp b/application/pages/modplatform/FtbListModel.cpp index 0d0d4d5e..f4311afb 100644 --- a/application/pages/modplatform/FtbListModel.cpp +++ b/application/pages/modplatform/FtbListModel.cpp @@ -72,15 +72,17 @@ FtbListModel::~FtbListModel() QString FtbListModel::translatePackType(FtbPackType type) const { - if(type == FtbPackType::Public) { - return tr("Public Modpack"); - } else if(type == FtbPackType::ThirdParty) { - return tr("Third Party Modpack"); - } else if(type == FtbPackType::Private) { - return tr("Private Modpack"); - } else { - return tr("Unknown Type"); + switch(type) + { + case FtbPackType::Public: + return tr("Public Modpack"); + case FtbPackType::ThirdParty: + return tr("Third Party Modpack"); + case FtbPackType::Private: + return tr("Private Modpack"); } + qWarning() << "Unknown FTB modpack type:" << int(type); + return QString(); } int FtbListModel::rowCount(const QModelIndex &parent) const @@ -96,15 +98,20 @@ int FtbListModel::columnCount(const QModelIndex &parent) const QVariant FtbListModel::data(const QModelIndex &index, int role) const { int pos = index.row(); - if(pos >= modpacks.size() || pos < 0 || !index.isValid()) { + if(pos >= modpacks.size() || pos < 0 || !index.isValid()) + { return QString("INVALID INDEX %1").arg(pos); } FtbModpack pack = modpacks.at(pos); - if(role == Qt::DisplayRole) { + if(role == Qt::DisplayRole) + { return pack.name + "\n" + translatePackType(pack.type); - } else if (role == Qt::ToolTipRole) { - if(pack.description.length() > 100) { + } + else if (role == Qt::ToolTipRole) + { + if(pack.description.length() > 100) + { //some magic to prevent to long tooltips and replace html linebreaks QString edit = pack.description.left(97); edit = edit.left(edit.lastIndexOf("
")).left(edit.lastIndexOf(" ")).append("..."); @@ -112,23 +119,33 @@ QVariant FtbListModel::data(const QModelIndex &index, int role) const } return pack.description; - } else if(role == Qt::DecorationRole) { - if(m_logoMap.contains(pack.logo)) { + } + else if(role == Qt::DecorationRole) + { + if(m_logoMap.contains(pack.logo)) + { return (m_logoMap.value(pack.logo)); } QIcon icon = MMC->getThemedIcon("screenshot-placeholder"); ((FtbListModel *)this)->requestLogo(pack.logo); return icon; - } else if(role == Qt::TextColorRole) { - if(pack.broken) { + } + else if(role == Qt::TextColorRole) + { + if(pack.broken) + { //FIXME: Hardcoded color return QColor(255, 0, 50); - } else if(pack.bugged) { + } + else if(pack.bugged) + { //FIXME: Hardcoded color //bugged pack, currently only indicates bugged xml return QColor(244, 229, 66); } - } else if(role == Qt::UserRole) { + } + else if(role == Qt::UserRole) + { QVariant v; v.setValue(pack); return v; @@ -144,11 +161,37 @@ void FtbListModel::fill(FtbModpackList modpacks) endResetModel(); } +void FtbListModel::addPack(FtbModpack modpack) +{ + beginResetModel(); + this->modpacks.append(modpack); + endResetModel(); +} + +void FtbListModel::clear() +{ + beginResetModel(); + modpacks.clear(); + endResetModel(); +} + FtbModpack FtbListModel::at(int row) { return modpacks.at(row); } +void FtbListModel::remove(int row) +{ + if(row < 0 || row >= modpacks.size()) + { + qWarning() << "Attempt to remove FTB modpacks with invalid row" << row; + return; + } + beginRemoveRows(QModelIndex(), row, row); + modpacks.removeAt(row); + endRemoveRows(); +} + void FtbListModel::logoLoaded(QString logo, QIcon out) { m_loadingLogos.removeAll(logo); @@ -164,7 +207,8 @@ void FtbListModel::logoFailed(QString logo) void FtbListModel::requestLogo(QString file) { - if(m_loadingLogos.contains(file) || m_failedLogos.contains(file)) { + if(m_loadingLogos.contains(file) || m_failedLogos.contains(file)) + { return; } @@ -173,14 +217,17 @@ void FtbListModel::requestLogo(QString file) job->addNetAction(Net::Download::makeCached(QUrl(QString("https://ftb.cursecdn.com/FTB2/static/%1").arg(file)), entry)); auto fullPath = entry->getFullPath(); - QObject::connect(job, &NetJob::finished, this, [this, file, fullPath]{ + QObject::connect(job, &NetJob::finished, this, [this, file, fullPath] + { emit logoLoaded(file, QIcon(fullPath)); - if(waitingCallbacks.contains(file)) { + if(waitingCallbacks.contains(file)) + { waitingCallbacks.value(file)(fullPath); } }); - QObject::connect(job, &NetJob::failed, this, [this, file]{ + QObject::connect(job, &NetJob::failed, this, [this, file] + { emit logoFailed(file); }); @@ -191,9 +238,17 @@ void FtbListModel::requestLogo(QString file) void FtbListModel::getLogo(const QString &logo, LogoCallback callback) { - if(m_logoMap.contains(logo)) { + if(m_logoMap.contains(logo)) + { callback(ENV.metacache()->resolveEntry("FTBPacks", QString("logos/%1").arg(logo.section(".", 0, 0)))->getFullPath()); - } else { + } + else + { requestLogo(logo); } } + +Qt::ItemFlags FtbListModel::flags(const QModelIndex &index) const +{ + return QAbstractListModel::flags(index); +} diff --git a/application/pages/modplatform/FtbListModel.h b/application/pages/modplatform/FtbListModel.h index c85c04fa..98301fd7 100644 --- a/application/pages/modplatform/FtbListModel.h +++ b/application/pages/modplatform/FtbListModel.h @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -60,8 +61,12 @@ public: int rowCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; + Qt::ItemFlags flags(const QModelIndex &index) const override; void fill(FtbModpackList modpacks); + void addPack(FtbModpack modpack); + void clear(); + void remove(int row); FtbModpack at(int row); void getLogo(const QString &logo, LogoCallback callback); -- cgit