feat(ui): allow downloading multiple mods in Modrinth at once

This commit is contained in:
flow 2022-02-21 21:34:53 -03:00
parent 9c6727e27f
commit 512395e3f1
No known key found for this signature in database
GPG Key ID: 8D0F221F0A59F469
3 changed files with 130 additions and 106 deletions

View File

@ -34,6 +34,7 @@ ModrinthPage::ModrinthPage(ModDownloadDialog *dialog, BaseInstance *instance)
connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch())); connect(ui->sortByBox, SIGNAL(currentIndexChanged(int)), this, SLOT(triggerSearch()));
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ModrinthPage::onSelectionChanged); connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &ModrinthPage::onSelectionChanged);
connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &ModrinthPage::onVersionSelectionChanged); connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &ModrinthPage::onVersionSelectionChanged);
connect(ui->modSelectionButton, &QPushButton::clicked, this, &ModrinthPage::onModSelected);
} }
ModrinthPage::~ModrinthPage() ModrinthPage::~ModrinthPage()
@ -61,7 +62,7 @@ bool ModrinthPage::shouldDisplay() const
void ModrinthPage::openedImpl() void ModrinthPage::openedImpl()
{ {
suggestCurrent(); updateSelectionButton();
triggerSearch(); triggerSearch();
} }
@ -76,10 +77,6 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
if(!first.isValid()) if(!first.isValid())
{ {
if(isOpened)
{
dialog->setSuggestedMod();
}
return; return;
} }
@ -97,6 +94,10 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
if (!current.versionsLoaded) if (!current.versionsLoaded)
{ {
qDebug() << "Loading Modrinth mod versions"; qDebug() << "Loading Modrinth mod versions";
ui->modSelectionButton->setText(tr("Loading versions..."));
ui->modSelectionButton->setEnabled(false);
auto netJob = new NetJob(QString("Modrinth::ModVersions(%1)").arg(current.name), APPLICATION->network()); auto netJob = new NetJob(QString("Modrinth::ModVersions(%1)").arg(current.name), APPLICATION->network());
std::shared_ptr<QByteArray> response = std::make_shared<QByteArray>(); std::shared_ptr<QByteArray> response = std::make_shared<QByteArray>();
QString addonId = current.addonId; QString addonId = current.addonId;
@ -136,8 +137,9 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(-1)); ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(-1));
} }
suggestCurrent(); updateSelectionButton();
}); });
netJob->start(); netJob->start();
} }
else else
@ -148,33 +150,47 @@ void ModrinthPage::onSelectionChanged(QModelIndex first, QModelIndex second)
if(ui->versionSelectionBox->count() == 0){ if(ui->versionSelectionBox->count() == 0){
ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(-1)); ui->versionSelectionBox->addItem(tr("No Valid Version found !"), QVariant(-1));
} }
suggestCurrent();
updateSelectionButton();
} }
} }
void ModrinthPage::suggestCurrent() void ModrinthPage::updateSelectionButton()
{ {
if(!isOpened) if(!isOpened || selectedVersion < 0){
{ ui->modSelectionButton->setEnabled(false);
return; return;
} }
if (selectedVersion == -1) ui->modSelectionButton->setEnabled(true);
{ auto& version = current.versions[selectedVersion];
dialog->setSuggestedMod(); if(!dialog->isModSelected(current.name, version.fileName)){
return; ui->modSelectionButton->setText(tr("Select mod for download"));
}
else{
ui->modSelectionButton->setText(tr("Deselect mod for download"));
} }
auto version = current.versions[selectedVersion];
dialog->setSuggestedMod(current.name, new ModDownloadTask(version.downloadUrl, version.fileName , dialog->mods));
} }
void ModrinthPage::onVersionSelectionChanged(QString data) void ModrinthPage::onVersionSelectionChanged(QString data)
{ {
if(data.isNull() || data.isEmpty()) if (data.isNull() || data.isEmpty()){
{
selectedVersion = -1; selectedVersion = -1;
return; return;
} }
selectedVersion = ui->versionSelectionBox->currentData().toInt(); selectedVersion = ui->versionSelectionBox->currentData().toInt();
suggestCurrent(); updateSelectionButton();
}
void ModrinthPage::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();
} }

View File

@ -50,12 +50,13 @@ public:
BaseInstance *m_instance; BaseInstance *m_instance;
private: private:
void suggestCurrent(); void updateSelectionButton();
private slots: private slots:
void triggerSearch(); void triggerSearch();
void onSelectionChanged(QModelIndex first, QModelIndex second); void onSelectionChanged(QModelIndex first, QModelIndex second);
void onVersionSelectionChanged(QString data); void onVersionSelectionChanged(QString data);
void onModSelected();
private: private:
Ui::ModrinthPage *ui = nullptr; Ui::ModrinthPage *ui = nullptr;

View File

@ -13,23 +13,7 @@
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="2"> <item row="1" column="0" colspan="2">
<layout class="QGridLayout" name="gridLayout_3"> <layout class="QGridLayout" name="gridLayout_3">
<item row="1" column="0"> <item row="1" column="2">
<widget class="QListView" name="packView">
<property name="iconSize">
<size>
<width>48</width>
<height>48</height>
</size>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QTextBrowser" name="packDescription"> <widget class="QTextBrowser" name="packDescription">
<property name="openExternalLinks"> <property name="openExternalLinks">
<bool>true</bool> <bool>true</bool>
@ -39,26 +23,22 @@
</property> </property>
</widget> </widget>
</item> </item>
</layout> <item row="1" column="0">
</item> <widget class="QListView" name="packView">
<item row="2" column="0" colspan="2"> <property name="horizontalScrollBarPolicy">
<layout class="QGridLayout" name="gridLayout_4" columnstretch="0,0,0" rowminimumheight="0" columnminimumwidth="0,0,0"> <enum>Qt::ScrollBarAlwaysOff</enum>
<item row="0" column="2">
<widget class="QComboBox" name="versionSelectionBox"/>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label">
<property name="text">
<string>Version selected:</string>
</property> </property>
<property name="alignment"> <property name="alternatingRowColors">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set> <bool>true</bool>
</property>
<property name="iconSize">
<size>
<width>48</width>
<height>48</height>
</size>
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="0">
<widget class="QComboBox" name="sortByBox"/>
</item>
</layout> </layout>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
@ -75,6 +55,33 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0" colspan="2">
<layout class="QGridLayout" name="gridLayout_4" columnstretch="0,0,0,0">
<item row="0" column="2">
<widget class="QComboBox" name="versionSelectionBox"/>
</item>
<item row="0" column="1">
<widget class="QLabel" name="label">
<property name="text">
<string>Version selected:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QComboBox" name="sortByBox"/>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="modSelectionButton">
<property name="text">
<string>Select mod for download</string>
</property>
</widget>
</item>
</layout>
</item>
</layout> </layout>
</widget> </widget>
<tabstops> <tabstops>