Merge pull request #3442 from jamierocks/ftb-sorts

NOISSUE Add sorting options to FTB pack install page
This commit is contained in:
Petr Mrázek 2020-11-30 00:13:41 +01:00 committed by GitHub
commit 58d168bda2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 164 additions and 38 deletions

View File

@ -117,7 +117,7 @@ void PackInstallTask::install()
if(file.serverOnly) continue; if(file.serverOnly) continue;
auto relpath = FS::PathCombine("minecraft", file.path, file.name); auto relpath = FS::PathCombine("minecraft", file.path, file.name);
auto path = FS::PathCombine(m_stagingPath , relpath); auto path = FS::PathCombine(m_stagingPath, relpath);
qDebug() << "Will download" << file.url << "to" << path; qDebug() << "Will download" << file.url << "to" << path;
auto dl = Net::Download::makeFile(file.url, path); auto dl = Net::Download::makeFile(file.url, path);

View File

@ -124,8 +124,10 @@ SET(MULTIMC_SOURCES
# GUI - platform pages # GUI - platform pages
pages/modplatform/VanillaPage.cpp pages/modplatform/VanillaPage.cpp
pages/modplatform/VanillaPage.h pages/modplatform/VanillaPage.h
pages/modplatform/ftb/FtbModel.cpp pages/modplatform/ftb/FtbFilterModel.cpp
pages/modplatform/ftb/FtbModel.h pages/modplatform/ftb/FtbFilterModel.h
pages/modplatform/ftb/FtbListModel.cpp
pages/modplatform/ftb/FtbListModel.h
pages/modplatform/ftb/FtbPage.cpp pages/modplatform/ftb/FtbPage.cpp
pages/modplatform/ftb/FtbPage.h pages/modplatform/ftb/FtbPage.h
pages/modplatform/legacy_ftb/Page.cpp pages/modplatform/legacy_ftb/Page.cpp

View File

@ -0,0 +1,64 @@
#include "FtbFilterModel.h"
#include <QDebug>
#include "modplatform/modpacksch/FTBPackManifest.h"
#include <MMCStrings.h>
namespace Ftb {
FilterModel::FilterModel(QObject *parent) : QSortFilterProxyModel(parent)
{
currentSorting = Sorting::ByPlays;
sortings.insert(tr("Sort by plays"), Sorting::ByPlays);
sortings.insert(tr("Sort by installs"), Sorting::ByInstalls);
sortings.insert(tr("Sort by name"), Sorting::ByName);
}
const QMap<QString, FilterModel::Sorting> FilterModel::getAvailableSortings()
{
return sortings;
}
QString FilterModel::translateCurrentSorting()
{
return sortings.key(currentSorting);
}
void FilterModel::setSorting(Sorting sorting)
{
currentSorting = sorting;
invalidate();
}
FilterModel::Sorting FilterModel::getCurrentSorting()
{
return currentSorting;
}
bool FilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
return true;
}
bool FilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
ModpacksCH::Modpack leftPack = sourceModel()->data(left, Qt::UserRole).value<ModpacksCH::Modpack>();
ModpacksCH::Modpack rightPack = sourceModel()->data(right, Qt::UserRole).value<ModpacksCH::Modpack>();
if (currentSorting == ByPlays) {
return leftPack.plays < rightPack.plays;
}
else if (currentSorting == ByInstalls) {
return leftPack.installs < rightPack.installs;
}
else if (currentSorting == ByName) {
return Strings::naturalCompare(leftPack.name, rightPack.name, Qt::CaseSensitive) >= 0;
}
// Invalid sorting set, somehow...
qWarning() << "Invalid sorting set!";
return true;
}
}

View File

@ -0,0 +1,33 @@
#pragma once
#include <QtCore/QSortFilterProxyModel>
namespace Ftb {
class FilterModel : public QSortFilterProxyModel
{
Q_OBJECT
public:
FilterModel(QObject* parent = Q_NULLPTR);
enum Sorting {
ByPlays,
ByInstalls,
ByName,
};
const QMap<QString, Sorting> getAvailableSortings();
QString translateCurrentSorting();
void setSorting(Sorting sorting);
Sorting getCurrentSorting();
protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
private:
QMap<QString, Sorting> sortings;
Sorting currentSorting;
};
}

View File

@ -1,4 +1,4 @@
#include "FtbModel.h" #include "FtbListModel.h"
#include "BuildConfig.h" #include "BuildConfig.h"
#include "Env.h" #include "Env.h"
@ -211,12 +211,13 @@ void ListModel::packRequestFinished()
if (pack.versions.empty()) if (pack.versions.empty())
{ {
qWarning() << "FTB Pack " << pack.id << " ignored. reason: lacking any versions"; qWarning() << "FTB Pack " << pack.id << " ignored. reason: lacking any versions";
return;
} }
else
{
beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size()); beginInsertRows(QModelIndex(), modpacks.size(), modpacks.size());
modpacks.append(pack); modpacks.append(pack);
endInsertRows(); endInsertRows();
}
if(!remainingPacks.isEmpty()) { if(!remainingPacks.isEmpty()) {
currentPack = remainingPacks.at(0); currentPack = remainingPacks.at(0);

View File

@ -10,12 +10,26 @@ FtbPage::FtbPage(NewInstanceDialog* dialog, QWidget *parent)
: QWidget(parent), ui(new Ui::FtbPage), dialog(dialog) : QWidget(parent), ui(new Ui::FtbPage), dialog(dialog)
{ {
ui->setupUi(this); ui->setupUi(this);
connect(ui->searchButton, &QPushButton::clicked, this, &FtbPage::triggerSearch);
ui->searchEdit->installEventFilter(this);
model = new Ftb::ListModel(this);
ui->packView->setModel(model);
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FtbPage::onSelectionChanged);
filterModel = new Ftb::FilterModel(this);
listModel = new Ftb::ListModel(this);
filterModel->setSourceModel(listModel);
ui->packView->setModel(filterModel);
ui->packView->setSortingEnabled(true);
ui->packView->header()->hide();
ui->packView->setIndentation(0);
ui->searchEdit->installEventFilter(this);
for(int i = 0; i < filterModel->getAvailableSortings().size(); i++)
{
ui->sortByBox->addItem(filterModel->getAvailableSortings().keys().at(i));
}
ui->sortByBox->setCurrentText(filterModel->translateCurrentSorting());
connect(ui->searchButton, &QPushButton::clicked, this, &FtbPage::triggerSearch);
connect(ui->sortByBox, &QComboBox::currentTextChanged, this, &FtbPage::onSortingSelectionChanged);
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FtbPage::onSelectionChanged);
connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FtbPage::onVersionSelectionChanged); connect(ui->versionSelectionBox, &QComboBox::currentTextChanged, this, &FtbPage::onVersionSelectionChanged);
} }
@ -59,7 +73,7 @@ void FtbPage::suggestCurrent()
QString editedLogoName; QString editedLogoName;
editedLogoName = selected.name; editedLogoName = selected.name;
model->getLogo(selected.name, art.url, [this, editedLogoName](QString logo) listModel->getLogo(selected.name, art.url, [this, editedLogoName](QString logo)
{ {
dialog->setSuggestedIconFromFile(logo + ".small", editedLogoName); dialog->setSuggestedIconFromFile(logo + ".small", editedLogoName);
}); });
@ -70,7 +84,13 @@ void FtbPage::suggestCurrent()
void FtbPage::triggerSearch() void FtbPage::triggerSearch()
{ {
model->searchWithTerm(ui->searchEdit->text()); listModel->searchWithTerm(ui->searchEdit->text());
}
void FtbPage::onSortingSelectionChanged(QString data)
{
auto toSet = filterModel->getAvailableSortings().value(data);
filterModel->setSorting(toSet);
} }
void FtbPage::onSelectionChanged(QModelIndex first, QModelIndex second) void FtbPage::onSelectionChanged(QModelIndex first, QModelIndex second)
@ -86,7 +106,7 @@ void FtbPage::onSelectionChanged(QModelIndex first, QModelIndex second)
return; return;
} }
selected = model->data(first, Qt::UserRole).value<ModpacksCH::Modpack>(); selected = filterModel->data(first, Qt::UserRole).value<ModpacksCH::Modpack>();
// reverse foreach, so that the newest versions are first // reverse foreach, so that the newest versions are first
for (auto i = selected.versions.size(); i--;) { for (auto i = selected.versions.size(); i--;) {

View File

@ -15,7 +15,8 @@
#pragma once #pragma once
#include "FtbModel.h" #include "FtbFilterModel.h"
#include "FtbListModel.h"
#include <QWidget> #include <QWidget>
@ -64,13 +65,15 @@ private:
private slots: private slots:
void triggerSearch(); void triggerSearch();
void onSortingSelectionChanged(QString data);
void onSelectionChanged(QModelIndex first, QModelIndex second); void onSelectionChanged(QModelIndex first, QModelIndex second);
void onVersionSelectionChanged(QString data); void onVersionSelectionChanged(QString data);
private: private:
Ui::FtbPage *ui = nullptr; Ui::FtbPage *ui = nullptr;
NewInstanceDialog* dialog = nullptr; NewInstanceDialog* dialog = nullptr;
Ftb::ListModel* model = nullptr; Ftb::ListModel* listModel = nullptr;
Ftb::FilterModel* filterModel = nullptr;
ModpacksCH::Modpack selected; ModpacksCH::Modpack selected;
QString selectedVersion; QString selectedVersion;

View File

@ -11,22 +11,6 @@
</rect> </rect>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="1" column="0" colspan="2">
<widget class="QListView" name="packView">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="iconSize">
<size>
<width>48</width>
<height>48</height>
</size>
</property>
<property name="uniformItemSizes">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QPushButton" name="searchButton"> <widget class="QPushButton" name="searchButton">
<property name="text"> <property name="text">
@ -38,19 +22,38 @@
<widget class="QLineEdit" name="searchEdit"/> <widget class="QLineEdit" name="searchEdit"/>
</item> </item>
<item row="2" column="0" colspan="2"> <item row="2" column="0" colspan="2">
<layout class="QGridLayout" name="gridLayout_4" columnstretch="0,0" rowminimumheight="0" columnminimumwidth="0,0"> <layout class="QGridLayout" name="gridLayout_4" columnstretch="0,0,0" rowminimumheight="0" columnminimumwidth="0,0,0">
<item row="0" column="1"> <item row="0" column="2">
<widget class="QComboBox" name="versionSelectionBox"/> <widget class="QComboBox" name="versionSelectionBox"/>
</item> </item>
<item row="0" column="0"> <item row="0" column="1">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
<string>Version selected:</string> <string>Version selected:</string>
</property> </property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget> </widget>
</item> </item>
<item row="0" column="0">
<widget class="QComboBox" name="sortByBox"/>
</item>
</layout> </layout>
</item> </item>
<item row="1" column="0" colspan="2">
<widget class="QTreeView" name="packView">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="iconSize">
<size>
<width>48</width>
<height>48</height>
</size>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<tabstops> <tabstops>