From eec87db86a8a00db0a9145a3b17c53408f7b1f5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Thu, 4 Aug 2016 21:54:25 +0200 Subject: [PATCH] GH-1635 add filter bar to mod list pages --- api/logic/minecraft/ModList.cpp | 11 +++++ api/logic/minecraft/ModList.h | 3 ++ application/pages/ModFolderPage.cpp | 31 ++++++++++--- application/pages/ModFolderPage.h | 7 ++- application/pages/ModFolderPage.ui | 70 +++++++++++++++++++++-------- 5 files changed, 96 insertions(+), 26 deletions(-) diff --git a/api/logic/minecraft/ModList.cpp b/api/logic/minecraft/ModList.cpp index 36371ee3..ad588f16 100644 --- a/api/logic/minecraft/ModList.cpp +++ b/api/logic/minecraft/ModList.cpp @@ -199,6 +199,17 @@ bool ModList::deleteMods(int first, int last) return true; } +bool ModList::deleteMods(const QVector &indexes) +{ + for (auto i: indexes) + { + Mod &m = mods[i]; + m.destroy(); + } + emit changed(); + return true; +} + int ModList::columnCount(const QModelIndex &parent) const { return 3; diff --git a/api/logic/minecraft/ModList.h b/api/logic/minecraft/ModList.h index 1a42c8f8..24dcdaa4 100644 --- a/api/logic/minecraft/ModList.h +++ b/api/logic/minecraft/ModList.h @@ -89,6 +89,9 @@ public: /// Deletes all the selected mods virtual bool deleteMods(int first, int last); + /// Deletes all the selected mods + virtual bool deleteMods(const QVector &indexes); + void startWatching(); void stopWatching(); diff --git a/application/pages/ModFolderPage.cpp b/application/pages/ModFolderPage.cpp index b838ed10..3926fc46 100644 --- a/application/pages/ModFolderPage.cpp +++ b/application/pages/ModFolderPage.cpp @@ -43,11 +43,17 @@ ModFolderPage::ModFolderPage(BaseInstance *inst, std::shared_ptr mods, m_displayName = displayName; m_iconName = iconName; m_helpName = helpPage; - m_filter = "%1 (*.zip *.jar)"; - ui->modTreeView->setModel(m_mods.get()); + m_fileSelectionFilter = "%1 (*.zip *.jar)"; + m_filterModel = new QSortFilterProxyModel(this); + m_filterModel->setDynamicSortFilter(true); + m_filterModel->setFilterCaseSensitivity(Qt::CaseInsensitive); + m_filterModel->setSourceModel(m_mods.get()); + m_filterModel->setFilterKeyColumn(-1); + ui->modTreeView->setModel(m_filterModel); ui->modTreeView->installEventFilter(this); auto smodel = ui->modTreeView->selectionModel(); connect(smodel, &QItemSelectionModel::currentChanged, this, &ModFolderPage::modCurrent); + connect(ui->filterEdit, &QLineEdit::textChanged, this, &ModFolderPage::on_filterTextChanged ); } void ModFolderPage::opened() @@ -60,6 +66,13 @@ void ModFolderPage::closed() m_mods->stopWatching(); } +void ModFolderPage::on_filterTextChanged(const QString& newContents) +{ + m_viewFilter = newContents; + m_filterModel->setFilterFixedString(m_viewFilter); +} + + CoreModFolderPage::CoreModFolderPage(BaseInstance *inst, std::shared_ptr mods, QString id, QString iconName, QString displayName, QString helpPage, QWidget *parent) @@ -141,7 +154,7 @@ void ModFolderPage::on_addModBtn_clicked() tr("Select %1", "Select whatever type of files the page contains. Example: 'Loader Mods'") .arg(m_displayName), - m_filter.arg(m_displayName), MMC->settings()->get("CentralModsDir").toString(), + m_fileSelectionFilter.arg(m_displayName), MMC->settings()->get("CentralModsDir").toString(), this->parentWidget()); if (!list.empty()) { @@ -159,7 +172,14 @@ void ModFolderPage::on_rmModBtn_clicked() if (!lastfirst(list, first, last)) return; - m_mods->deleteMods(first, last); + + QVector toDelete; + for(int i = first; i <= last; i++) + { + auto index = m_filterModel->mapToSource(m_filterModel->index(i,0,QModelIndex())); + toDelete.append(index.row()); + } + m_mods->deleteMods(toDelete); } void ModFolderPage::on_viewModBtn_clicked() @@ -174,7 +194,8 @@ void ModFolderPage::modCurrent(const QModelIndex ¤t, const QModelIndex &pr ui->frame->clear(); return; } - int row = current.row(); + auto sourceCurrent = m_filterModel->mapToSource(current); + int row = sourceCurrent.row(); Mod &m = m_mods->operator[](row); ui->frame->updateWithMod(m); } diff --git a/application/pages/ModFolderPage.h b/application/pages/ModFolderPage.h index 23b2f67d..4102a1bb 100644 --- a/application/pages/ModFolderPage.h +++ b/application/pages/ModFolderPage.h @@ -39,7 +39,7 @@ public: void setFilter(const QString & filter) { - m_filter = filter; + m_fileSelectionFilter = filter; } virtual QString displayName() const override @@ -72,11 +72,13 @@ protected: private: Ui::ModFolderPage *ui; std::shared_ptr m_mods; + QSortFilterProxyModel *m_filterModel; QString m_iconName; QString m_id; QString m_displayName; QString m_helpName; - QString m_filter; + QString m_fileSelectionFilter; + QString m_viewFilter; public slots: @@ -84,6 +86,7 @@ slots: private slots: + void on_filterTextChanged(const QString & newContents); void on_addModBtn_clicked(); void on_rmModBtn_clicked(); void on_viewModBtn_clicked(); diff --git a/application/pages/ModFolderPage.ui b/application/pages/ModFolderPage.ui index cf991e04..707987c7 100644 --- a/application/pages/ModFolderPage.ui +++ b/application/pages/ModFolderPage.ui @@ -29,27 +29,17 @@ 0 + + + 0 + 0 + + Tab 1 - - - - - - 0 - 0 - - - - true - - - QAbstractItemView::DropOnly - - - - + + @@ -87,7 +77,7 @@ - + @@ -97,6 +87,40 @@ + + + + + + true + + + + + + + Filter: + + + + + + + + 0 + 0 + + + + true + + + QAbstractItemView::DropOnly + + + + + @@ -116,6 +140,14 @@ 1 + + tabWidget + modTreeView + addModBtn + rmModBtn + viewModBtn + filterEdit +