Skip to content

Commit

Permalink
add version select for curseforge files
Browse files Browse the repository at this point in the history
  • Loading branch information
kaniol-lck committed Jun 4, 2024
1 parent 4ad2268 commit a8c6af4
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 28 deletions.
5 changes: 4 additions & 1 deletion src/curseforge/curseforgeapi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,15 @@ Reply<CurseforgeFileInfo> CurseforgeAPI::getFileInfo(int id, int FileID)
} };
}

Reply<QList<CurseforgeFileInfo>, int> CurseforgeAPI::getFiles(int id, int index)
Reply<QList<CurseforgeFileInfo>, int> CurseforgeAPI::getFiles(int id, int index, GameVersion version)
{
//TODO: page
QUrl url = PREFIX + "/v1/mods/" + QString::number(id) + "/files";
//url query
QUrlQuery urlQuery;
//version
if(version != GameVersion::Any)
urlQuery.addQueryItem("gameVersion", version.toString());
//index
urlQuery.addQueryItem("index", QString::number(index));
//page size
Expand Down
2 changes: 1 addition & 1 deletion src/curseforge/curseforgeapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CurseforgeAPI : public QObject
Reply<QString> getChangelog(int id, int FileID);
Reply<QString> getDownloadUrl(int id, int FileID);
Reply<CurseforgeFileInfo> getFileInfo(int id, int FileID);
Reply<QList<CurseforgeFileInfo>, int> getFiles(int id, int index);
Reply<QList<CurseforgeFileInfo>, int> getFiles(int id, int index, GameVersion version = GameVersion::Any);
Reply<CurseforgeModInfo> getInfo(int id);
Reply<QString> getTimestamp();
Reply<QList<GameVersion> > getMinecraftVersionList();
Expand Down
11 changes: 7 additions & 4 deletions src/curseforge/curseforgemod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,17 @@ void CurseforgeMod::acquireDescription()
});
}

std::shared_ptr<Reply<QList<CurseforgeFileInfo>, int>> CurseforgeMod::acquireMoreFileList()
std::shared_ptr<Reply<QList<CurseforgeFileInfo>, int>> CurseforgeMod::acquireMoreFileList(GameVersion version, bool clear)
{
if(allFileListGetter_ && allFileListGetter_->isRunning()) return allFileListGetter_;
allFileListGetter_ = api_->getFiles(modInfo_.id(), modInfo().allFileList().size()).asUnique();
allFileListGetter_ = api_->getFiles(modInfo_.id(), clear? 0: modInfo().allFileList().size(), version).asUnique();
allFileListGetter_->setOnFinished(this, [=](const QList<CurseforgeFileInfo> &fileList, int count){
modInfo_.allFileList_ << fileList;
if(clear)
modInfo_.allFileList_ = fileList;
else
modInfo_.allFileList_ << fileList;
modInfo_.totalFileCount_ = count;
emit moreFileListReady(fileList);
emit moreFileListReady(modInfo_.allFileList_);
}, [=](auto){
emit moreFileListReady({});
});
Expand Down
2 changes: 1 addition & 1 deletion src/curseforge/curseforgemod.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CurseforgeMod : public QObject, public Tagable
void acquireBasicInfo();
void acquireIcon();
void acquireDescription();
std::shared_ptr<Reply<QList<CurseforgeFileInfo>, int>> acquireMoreFileList();
std::shared_ptr<Reply<QList<CurseforgeFileInfo>, int>> acquireMoreFileList(GameVersion version = GameVersion::Any, bool clear = false);

const CurseforgeModInfo &modInfo() const;
void download(const CurseforgeFileInfo &fileInfo, LocalModPath *downloadPath = nullptr);
Expand Down
74 changes: 65 additions & 9 deletions src/ui/curseforge/curseforgefilelistwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,38 @@

CurseforgeFileListWidget::CurseforgeFileListWidget(CurseforgeModBrowser *parent) :
QWidget(parent),
ui(new Ui::CurseforgeFileListWidget)
ui(new Ui::CurseforgeFileListWidget),
versionMenu_(new QMenu(this))
{
ui->setupUi(this);
ui->versionSelect->setDefaultAction(versionMenu_->menuAction());
ui->versionSelect->setPopupMode(QToolButton::InstantPopup);
ui->fileListView->setModel(&model_);
ui->fileListView->setVerticalScrollBar(new SmoothScrollBar(this));
ui->fileListView->setProperty("class", "ModList");
connect(ui->fileListView->verticalScrollBar(), &QAbstractSlider::valueChanged, this , &CurseforgeFileListWidget::updateIndexWidget);
connect(ui->fileListView->verticalScrollBar(), &QSlider::valueChanged, this, &CurseforgeFileListWidget::onListSliderChanged);
updateVersionList();
connect(VersionManager::manager(), &VersionManager::curseforgeVersionListUpdated, this, &CurseforgeFileListWidget::updateVersionList);
ui->downloadPathSelect->hide();
downloadPathSelectMenu_ = parent->downloadPathSelectMenu();
}

CurseforgeFileListWidget::CurseforgeFileListWidget(QWidget *parent, LocalMod *localMod) :
QWidget(parent),
ui(new Ui::CurseforgeFileListWidget)
ui(new Ui::CurseforgeFileListWidget),
versionMenu_(new QMenu(this))
{
ui->setupUi(this);
ui->versionSelect->setDefaultAction(versionMenu_->menuAction());
ui->versionSelect->setPopupMode(QToolButton::InstantPopup);
ui->fileListView->setModel(&model_);
ui->fileListView->setVerticalScrollBar(new SmoothScrollBar(this));
ui->fileListView->setProperty("class", "ModList");
connect(ui->fileListView->verticalScrollBar(), &QAbstractSlider::valueChanged, this, &CurseforgeFileListWidget::updateIndexWidget);
connect(ui->fileListView->verticalScrollBar(), &QSlider::valueChanged, this, &CurseforgeFileListWidget::onListSliderChanged);
updateVersionList();
connect(VersionManager::manager(), &VersionManager::curseforgeVersionListUpdated, this, &CurseforgeFileListWidget::updateVersionList);
ui->downloadPathSelect->hide();
downloadPathSelectMenu_ = new DownloadPathSelectMenu(this);
ui->downloadPathSelect->setDefaultAction(downloadPathSelectMenu_->menuAction());
Expand All @@ -59,10 +69,7 @@ void CurseforgeFileListWidget::setMod(CurseforgeMod *mod)
connect(mod_, &QObject::destroyed, this, [=]{ setMod(nullptr); })));

updateFileList();
if(!mod_->modInfo().fileCompleted()){
ui->fileListView->setCursor(Qt::BusyCursor);
mod_->acquireMoreFileList();
}
search();
}

void CurseforgeFileListWidget::updateUi()
Expand Down Expand Up @@ -136,9 +143,58 @@ void CurseforgeFileListWidget::updateIndexWidget()
void CurseforgeFileListWidget::onListSliderChanged(int i)
{
if(i >= ui->fileListView->verticalScrollBar()->maximum() - 1000){
if(!mod_->modInfo().fileCompleted()){
ui->fileListView->setCursor(Qt::BusyCursor);
mod_->acquireMoreFileList();
search();
}
}

void CurseforgeFileListWidget::updateVersionList()
{
versionMenu_->clear();
versionMenu_->setTitle(tr("Select Game Version"));
versionMenu_->setStyleSheet("QMenu { menu-scrollable: 1; }");
auto anyVersionAction = versionMenu_->addAction(tr("Any"));
connect(anyVersionAction, &QAction::triggered, this, [=]{
currentGameVersion_ = GameVersion::Any;
versionMenu_->setTitle(tr("Game Version : %1").arg(tr("Any")));
versionMenu_->setIcon(QIcon());
search(true);
});
versionMenu_->addSeparator();
QMap<QString, QMenu*> submenus;
QList<QString> keys; //to keep order
for(auto &&version : GameVersion::curseforgeVersionList()){
if(!submenus.contains(version.majorVersion())){
auto submenu = new QMenu(version.majorVersion());
submenus[version.majorVersion()] = submenu;
keys << version.majorVersion();
}
}
for(auto &&version : GameVersion::curseforgeVersionList()){
if(submenus.contains(version.majorVersion())){
auto submenu = submenus[version.majorVersion()];
if(submenu->actions().size() == 1)
if(GameVersion version = submenu->actions().at(0)->text(); version == version.majorVersion())
submenu->addSeparator();
submenu->addAction(version, this, [=]{
currentGameVersion_ = version;
versionMenu_->setTitle(tr("Game Version : %1").arg(version));
search(true);
});
}
}
for(const auto &key : qAsConst(keys)){
auto submenu = submenus[key];
if(submenu->actions().size() == 1)
versionMenu_->addActions(submenu->actions());
else
versionMenu_->addMenu(submenu);
}
}

void CurseforgeFileListWidget::search(bool changed)
{
if(changed || !mod_->modInfo().fileCompleted()){
ui->fileListView->setCursor(Qt::BusyCursor);
mod_->acquireMoreFileList(currentGameVersion_, changed);
}
}
8 changes: 8 additions & 0 deletions src/ui/curseforge/curseforgefilelistwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
#include <QStandardItemModel>
#include <QWidget>

#include "gameversion.h"

class CurseforgeMod;
class CurseforgeModBrowser;
class LocalModPath;
class DownloadPathSelectMenu;
class LocalMod;

class QMenu;
namespace Ui {
class CurseforgeFileListWidget;
}
Expand Down Expand Up @@ -38,6 +42,8 @@ private slots:
void updateFileList();
void updateIndexWidget();
void onListSliderChanged(int i);
void updateVersionList();
void search(bool changed = false);

protected:
void paintEvent(QPaintEvent *event) override;
Expand All @@ -48,6 +54,8 @@ private slots:
QStandardItemModel model_;
CurseforgeMod *mod_ = nullptr;
LocalMod *localMod_ = nullptr;
QMenu *versionMenu_ = nullptr;
GameVersion currentGameVersion_;
};

#endif // CURSEFORGEFILELISTWIDGET_H
44 changes: 33 additions & 11 deletions src/ui/curseforge/curseforgefilelistwidget.ui
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,29 @@
</rect>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="0">
<widget class="QListView" name="fileListView">
<property name="styleSheet">
<string notr="true">QListView{
background-color: transparent;
}</string>
<item row="2" column="0">
<widget class="QToolButton" name="versionSelect">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
<property name="popupMode">
<enum>QToolButton::InstantPopup</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
<property name="toolButtonStyle">
<enum>Qt::ToolButtonTextBesideIcon</enum>
</property>
<property name="autoRaise">
<bool>true</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<item row="2" column="1">
<widget class="QComboBox" name="loaderTypeSelecct"/>
</item>
<item row="0" column="0" colspan="2">
<widget class="QToolButton" name="downloadPathSelect">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Fixed">
Expand All @@ -45,6 +52,21 @@
</property>
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QListView" name="fileListView">
<property name="styleSheet">
<string notr="true">QListView{
background-color: transparent;
}</string>
</property>
<property name="frameShape">
<enum>QFrame::NoFrame</enum>
</property>
<property name="verticalScrollMode">
<enum>QAbstractItemView::ScrollPerPixel</enum>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
Expand Down
1 change: 0 additions & 1 deletion src/ui/downloadpathselectmenu.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ private slots:
void onAboutToShow();

private:
QMenu menu_;
LocalModPath *downloadPath_ = nullptr;
};

Expand Down

0 comments on commit a8c6af4

Please sign in to comment.