sneedmc/application/pages/modplatform/flame/FlamePage.cpp

112 lines
2.9 KiB
C++
Raw Normal View History

#include "FlamePage.h"
#include "ui_FlamePage.h"
2020-03-31 01:13:19 +00:00
#include "MultiMC.h"
#include "dialogs/NewInstanceDialog.h"
#include <InstanceImportTask.h>
#include "FlameModel.h"
2020-03-31 01:13:19 +00:00
#include <QKeyEvent>
FlamePage::FlamePage(NewInstanceDialog* dialog, QWidget *parent)
: QWidget(parent), ui(new Ui::FlamePage), dialog(dialog)
2020-03-31 01:13:19 +00:00
{
ui->setupUi(this);
connect(ui->searchButton, &QPushButton::clicked, this, &FlamePage::triggerSearch);
2020-03-31 01:13:19 +00:00
ui->searchEdit->installEventFilter(this);
model = new Flame::ListModel(this);
2020-03-31 01:13:19 +00:00
ui->packView->setModel(model);
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FlamePage::onSelectionChanged);
2020-03-31 01:13:19 +00:00
}
FlamePage::~FlamePage()
2020-03-31 01:13:19 +00:00
{
delete ui;
}
bool FlamePage::eventFilter(QObject* watched, QEvent* event)
2020-03-31 01:13:19 +00:00
{
if (watched == ui->searchEdit && event->type() == QEvent::KeyPress) {
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
if (keyEvent->key() == Qt::Key_Return) {
triggerSearch();
keyEvent->accept();
return true;
}
}
return QWidget::eventFilter(watched, event);
}
bool FlamePage::shouldDisplay() const
2020-03-31 01:13:19 +00:00
{
return true;
}
void FlamePage::openedImpl()
2020-03-31 01:13:19 +00:00
{
suggestCurrent();
}
void FlamePage::triggerSearch()
2020-03-31 01:13:19 +00:00
{
model->searchWithTerm(ui->searchEdit->text());
}
void FlamePage::onSelectionChanged(QModelIndex first, QModelIndex second)
2020-03-31 01:13:19 +00:00
{
if(!first.isValid())
{
if(isOpened)
{
dialog->setSuggestedPack();
}
ui->frame->clear();
2020-03-31 01:13:19 +00:00
return;
}
current = model->data(first, Qt::UserRole).value<Flame::Modpack>();
QString text = "";
QString name = current.name;
if (current.websiteUrl.isEmpty())
text = name;
else
text = "<a href=\"" + current.websiteUrl + "\">" + name + "</a>";
if (!current.authors.empty()) {
auto authorToStr = [](Flame::ModpackAuthor & author) {
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 += tr(" by ") + authorStrs.join(", ");
}
ui->frame->setModText(text);
ui->frame->setModDescription(current.description);
2020-03-31 01:13:19 +00:00
suggestCurrent();
}
void FlamePage::suggestCurrent()
2020-03-31 01:13:19 +00:00
{
if(!isOpened)
{
return;
}
if(current.broken)
{
dialog->setSuggestedPack();
}
dialog->setSuggestedPack(current.name, new InstanceImportTask(current.latestFile.downloadUrl));
QString editedLogoName;
editedLogoName = "curseforge_" + current.logoName.section(".", 0, 0);
2020-03-31 01:13:19 +00:00
model->getLogo(current.logoName, current.logoUrl, [this, editedLogoName](QString logo)
{
dialog->setSuggestedIconFromFile(logo, editedLogoName);
});
}