Merge pull request #4177 from jamierocks/mch-search

NOISSUE Fix modpacks.ch search
This commit is contained in:
Petr Mrázek 2021-10-24 01:01:38 +02:00 committed by GitHub
commit e6cb7b7710
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 62 deletions

View File

@ -36,9 +36,21 @@ FilterModel::Sorting FilterModel::getCurrentSorting()
return currentSorting; return currentSorting;
} }
void FilterModel::setSearchTerm(const QString& term)
{
searchTerm = term.trimmed();
invalidate();
}
bool FilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const bool FilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{ {
return true; if (searchTerm.isEmpty()) {
return true;
}
auto index = sourceModel()->index(sourceRow, 0, sourceParent);
auto pack = sourceModel()->data(index, Qt::UserRole).value<ModpacksCH::Modpack>();
return pack.name.contains(searchTerm, Qt::CaseInsensitive);
} }
bool FilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const bool FilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const

View File

@ -19,6 +19,7 @@ public:
QString translateCurrentSorting(); QString translateCurrentSorting();
void setSorting(Sorting sorting); void setSorting(Sorting sorting);
Sorting getCurrentSorting(); Sorting getCurrentSorting();
void setSearchTerm(const QString& term);
protected: protected:
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
@ -27,6 +28,7 @@ protected:
private: private:
QMap<QString, Sorting> sortings; QMap<QString, Sorting> sortings;
Sorting currentSorting; Sorting currentSorting;
QString searchTerm { "" };
}; };

View File

@ -74,24 +74,6 @@ QVariant ListModel::data(const QModelIndex &index, int role) const
return QVariant(); return QVariant();
} }
void ListModel::performSearch()
{
auto *netJob = new NetJob("Ftb::Search");
QString searchUrl;
if(currentSearchTerm.isEmpty()) {
searchUrl = BuildConfig.MODPACKSCH_API_BASE_URL + "public/modpack/all";
}
else {
searchUrl = QString(BuildConfig.MODPACKSCH_API_BASE_URL + "public/modpack/search/25?term=%1")
.arg(currentSearchTerm);
}
netJob->addNetAction(Net::Download::makeByteArray(QUrl(searchUrl), &response));
jobPtr = netJob;
jobPtr->start();
QObject::connect(netJob, &NetJob::succeeded, this, &ListModel::searchRequestFinished);
QObject::connect(netJob, &NetJob::failed, this, &ListModel::searchRequestFailed);
}
void ListModel::getLogo(const QString &logo, const QString &logoUrl, LogoCallback callback) void ListModel::getLogo(const QString &logo, const QString &logoUrl, LogoCallback callback)
{ {
if(m_logoMap.contains(logo)) if(m_logoMap.contains(logo))
@ -104,28 +86,23 @@ void ListModel::getLogo(const QString &logo, const QString &logoUrl, LogoCallbac
} }
} }
void ListModel::searchWithTerm(const QString &term) void ListModel::request()
{ {
if(searchState != Failed && currentSearchTerm == term && currentSearchTerm.isNull() == term.isNull()) {
// unless the search has failed, then there is no need to perform an identical search.
return;
}
currentSearchTerm = term;
if(jobPtr) {
jobPtr->abort();
jobPtr.reset();
}
beginResetModel(); beginResetModel();
modpacks.clear(); modpacks.clear();
endResetModel(); endResetModel();
searchState = None;
performSearch(); auto *netJob = new NetJob("Ftb::Request");
auto url = QString(BuildConfig.MODPACKSCH_API_BASE_URL + "public/modpack/all");
netJob->addNetAction(Net::Download::makeByteArray(QUrl(url), &response));
jobPtr = netJob;
jobPtr->start();
QObject::connect(netJob, &NetJob::succeeded, this, &ListModel::requestFinished);
QObject::connect(netJob, &NetJob::failed, this, &ListModel::requestFailed);
} }
void ListModel::searchRequestFinished() void ListModel::requestFinished()
{ {
jobPtr.reset(); jobPtr.reset();
remainingPacks.clear(); remainingPacks.clear();
@ -150,12 +127,10 @@ void ListModel::searchRequestFinished()
} }
} }
void ListModel::searchRequestFailed(QString reason) void ListModel::requestFailed(QString reason)
{ {
jobPtr.reset(); jobPtr.reset();
remainingPacks.clear(); remainingPacks.clear();
searchState = Failed;
} }
void ListModel::requestPack() void ListModel::requestPack()

View File

@ -30,13 +30,13 @@ public:
int columnCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override;
QVariant data(const QModelIndex &index, int role) const override; QVariant data(const QModelIndex &index, int role) const override;
void request();
void getLogo(const QString &logo, const QString &logoUrl, LogoCallback callback); void getLogo(const QString &logo, const QString &logoUrl, LogoCallback callback);
void searchWithTerm(const QString & term);
private slots: private slots:
void performSearch(); void requestFinished();
void searchRequestFinished(); void requestFailed(QString reason);
void searchRequestFailed(QString reason);
void requestPack(); void requestPack();
void packRequestFinished(); void packRequestFinished();
@ -52,14 +52,6 @@ private:
QList<ModpacksCH::Modpack> modpacks; QList<ModpacksCH::Modpack> modpacks;
LogoMap m_logoMap; LogoMap m_logoMap;
QString currentSearchTerm;
enum SearchState {
None,
CanPossiblyFetchMore,
ResetRequested,
Finished,
Failed,
} searchState = None;
NetJobPtr jobPtr; NetJobPtr jobPtr;
int currentPack; int currentPack;
QList<int> remainingPacks; QList<int> remainingPacks;

View File

@ -32,7 +32,7 @@ FtbPage::FtbPage(NewInstanceDialog* dialog, QWidget *parent)
} }
ui->sortByBox->setCurrentText(filterModel->translateCurrentSorting()); ui->sortByBox->setCurrentText(filterModel->translateCurrentSorting());
connect(ui->searchButton, &QPushButton::clicked, this, &FtbPage::triggerSearch); connect(ui->searchEdit, &QLineEdit::textChanged, this, &FtbPage::triggerSearch);
connect(ui->sortByBox, &QComboBox::currentTextChanged, this, &FtbPage::onSortingSelectionChanged); connect(ui->sortByBox, &QComboBox::currentTextChanged, this, &FtbPage::onSortingSelectionChanged);
connect(ui->packView->selectionModel(), &QItemSelectionModel::currentChanged, this, &FtbPage::onSelectionChanged); 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);
@ -63,7 +63,12 @@ bool FtbPage::shouldDisplay() const
void FtbPage::openedImpl() void FtbPage::openedImpl()
{ {
triggerSearch(); if(!initialised)
{
listModel->request();
initialised = true;
}
suggestCurrent(); suggestCurrent();
} }
@ -96,7 +101,7 @@ void FtbPage::suggestCurrent()
void FtbPage::triggerSearch() void FtbPage::triggerSearch()
{ {
listModel->searchWithTerm(ui->searchEdit->text()); filterModel->setSearchTerm(ui->searchEdit->text());
} }
void FtbPage::onSortingSelectionChanged(QString data) void FtbPage::onSortingSelectionChanged(QString data)

View File

@ -65,6 +65,7 @@ private:
private slots: private slots:
void triggerSearch(); void triggerSearch();
void onSortingSelectionChanged(QString data); void onSortingSelectionChanged(QString data);
void onSelectionChanged(QModelIndex first, QModelIndex second); void onSelectionChanged(QModelIndex first, QModelIndex second);
void onVersionSelectionChanged(QString data); void onVersionSelectionChanged(QString data);
@ -77,4 +78,6 @@ private:
ModpacksCH::Modpack selected; ModpacksCH::Modpack selected;
QString selectedVersion; QString selectedVersion;
bool initialised { false };
}; };

View File

@ -36,12 +36,8 @@
<property name="placeholderText"> <property name="placeholderText">
<string>Search and filter ...</string> <string>Search and filter ...</string>
</property> </property>
</widget> <property name="clearButtonEnabled">
</item> <bool>true</bool>
<item row="0" column="1">
<widget class="QPushButton" name="searchButton">
<property name="text">
<string>Search</string>
</property> </property>
</widget> </widget>
</item> </item>
@ -49,15 +45,15 @@
<layout class="QGridLayout" name="gridLayout_3"> <layout class="QGridLayout" name="gridLayout_3">
<item row="0" column="0"> <item row="0" column="0">
<widget class="QTreeView" name="packView"> <widget class="QTreeView" name="packView">
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="iconSize"> <property name="iconSize">
<size> <size>
<width>48</width> <width>48</width>
<height>48</height> <height>48</height>
</size> </size>
</property> </property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
</widget> </widget>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
@ -76,7 +72,6 @@
</widget> </widget>
<tabstops> <tabstops>
<tabstop>searchEdit</tabstop> <tabstop>searchEdit</tabstop>
<tabstop>searchButton</tabstop>
<tabstop>versionSelectionBox</tabstop> <tabstop>versionSelectionBox</tabstop>
</tabstops> </tabstops>
<resources/> <resources/>