From 5bdc110b827ea0b117a84e846bf463325e4a044d Mon Sep 17 00:00:00 2001 From: Opuszek Date: Fri, 25 Jul 2025 18:17:13 +0200 Subject: [PATCH 1/3] Add filter to the herospell widget --- mapeditor/inspector/herospellwidget.cpp | 64 +++++++++++++++++++++++++ mapeditor/inspector/herospellwidget.h | 5 ++ mapeditor/inspector/herospellwidget.ui | 18 +++++++ 3 files changed, 87 insertions(+) diff --git a/mapeditor/inspector/herospellwidget.cpp b/mapeditor/inspector/herospellwidget.cpp index b73d8b54b..1813a247a 100644 --- a/mapeditor/inspector/herospellwidget.cpp +++ b/mapeditor/inspector/herospellwidget.cpp @@ -103,6 +103,70 @@ void HeroSpellWidget::on_customizeSpells_toggled(bool checked) initSpellLists(); } +void HeroSpellWidget::on_filter_textChanged(const QString & keyword) +{ + if (keyword.toStdString().find_first_not_of(' ') == std::string::npos) + { + const auto exists = QString::fromStdString(".*"); + showItemIfMatches(exists); + } + else + { + const auto doesNotContainKeyword = QString::fromStdString("^((?!") + keyword + QString::fromStdString(").)*$"); + hideItemIfMatches(doesNotContainKeyword); + + const auto containsKeyword = QString::fromStdString(".*") + keyword + QString::fromStdString(".*"); + showItemIfMatches(containsKeyword); + } + + hideEmptySpellLists(); +} + +void HeroSpellWidget::showItemIfMatches(const QString & match) +{ + toggleHiddenForItemIfMatches(match, false); +} + +void HeroSpellWidget::hideItemIfMatches(const QString & match) +{ + toggleHiddenForItemIfMatches(match, true); +} + +void HeroSpellWidget::toggleHiddenForItemIfMatches(const QString & match, bool hidden) +{ + QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 }; + for (const QListWidget * list : spellLists) { + const auto items = list->findItems(match, Qt::MatchRegularExpression); + for (QListWidgetItem * item : items) { + item->setHidden(hidden); + } + } +} + +void HeroSpellWidget::hideEmptySpellLists() +{ + + QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 }; + auto toggleSpellListVisibility = [&](const QListWidget * list, bool visible) { + auto * parent = list->parentWidget(); + int index = ui->tabWidget->indexOf(parent); + ui->tabWidget->setTabVisible(index, visible); + }; + + for (const QListWidget * list : spellLists) { + const auto allItems = list->findItems("*", Qt::MatchWildcard); + bool isListEmpty = true; + for (QListWidgetItem * item : allItems) { + if (!item->isHidden()) + { + isListEmpty = false; + break; + } + } + toggleSpellListVisibility(list, !isListEmpty); + } +} + HeroSpellDelegate::HeroSpellDelegate(CGHeroInstance & h) : BaseInspectorItemDelegate() , hero(h) diff --git a/mapeditor/inspector/herospellwidget.h b/mapeditor/inspector/herospellwidget.h index 3fb88700d..321a69ca3 100644 --- a/mapeditor/inspector/herospellwidget.h +++ b/mapeditor/inspector/herospellwidget.h @@ -31,6 +31,7 @@ public: private slots: void on_customizeSpells_toggled(bool checked); + void on_filter_textChanged(const QString & keyword); private: Ui::HeroSpellWidget * ui; @@ -38,6 +39,10 @@ private: CGHeroInstance & hero; void initSpellLists(); + void showItemIfMatches(const QString & match); + void hideItemIfMatches(const QString & match); + void toggleHiddenForItemIfMatches(const QString & match, bool hidden); + void hideEmptySpellLists(); }; class HeroSpellDelegate : public BaseInspectorItemDelegate diff --git a/mapeditor/inspector/herospellwidget.ui b/mapeditor/inspector/herospellwidget.ui index 8eb5cdb87..4bafbcf30 100644 --- a/mapeditor/inspector/herospellwidget.ui +++ b/mapeditor/inspector/herospellwidget.ui @@ -51,6 +51,9 @@ + + + @@ -99,6 +102,9 @@ true + + true + @@ -137,6 +143,9 @@ true + + true + @@ -175,6 +184,9 @@ true + + true + @@ -213,6 +225,9 @@ true + + true + @@ -251,6 +266,9 @@ true + + true + From 49a0545e40657abdd70e2d471a1e357f9f023e2c Mon Sep 17 00:00:00 2001 From: Opuszek Date: Fri, 25 Jul 2025 18:28:06 +0200 Subject: [PATCH 2/3] formatting --- mapeditor/inspector/herospellwidget.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/mapeditor/inspector/herospellwidget.cpp b/mapeditor/inspector/herospellwidget.cpp index 1813a247a..1754d60f0 100644 --- a/mapeditor/inspector/herospellwidget.cpp +++ b/mapeditor/inspector/herospellwidget.cpp @@ -135,9 +135,11 @@ void HeroSpellWidget::hideItemIfMatches(const QString & match) void HeroSpellWidget::toggleHiddenForItemIfMatches(const QString & match, bool hidden) { QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 }; - for (const QListWidget * list : spellLists) { + for (const QListWidget * list : spellLists) + { const auto items = list->findItems(match, Qt::MatchRegularExpression); - for (QListWidgetItem * item : items) { + for (QListWidgetItem * item : items) + { item->setHidden(hidden); } } @@ -147,16 +149,19 @@ void HeroSpellWidget::hideEmptySpellLists() { QListWidget * spellLists[] = { ui->spellList1, ui->spellList2, ui->spellList3, ui->spellList4, ui->spellList5 }; - auto toggleSpellListVisibility = [&](const QListWidget * list, bool visible) { + auto toggleSpellListVisibility = [&](const QListWidget * list, bool visible) + { auto * parent = list->parentWidget(); int index = ui->tabWidget->indexOf(parent); ui->tabWidget->setTabVisible(index, visible); }; - for (const QListWidget * list : spellLists) { + for (const QListWidget * list : spellLists) + { const auto allItems = list->findItems("*", Qt::MatchWildcard); bool isListEmpty = true; - for (QListWidgetItem * item : allItems) { + for (QListWidgetItem * item : allItems) + { if (!item->isHidden()) { isListEmpty = false; From dbbe2f244c4da0cee50ffc63e4c3e3700772493e Mon Sep 17 00:00:00 2001 From: Opuszek Date: Fri, 25 Jul 2025 21:01:42 +0200 Subject: [PATCH 3/3] clears and disables filter if customized spells unchecked --- mapeditor/inspector/herospellwidget.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mapeditor/inspector/herospellwidget.cpp b/mapeditor/inspector/herospellwidget.cpp index 1754d60f0..1aa731373 100644 --- a/mapeditor/inspector/herospellwidget.cpp +++ b/mapeditor/inspector/herospellwidget.cpp @@ -99,6 +99,8 @@ void HeroSpellWidget::on_customizeSpells_toggled(bool checked) hero.removeSpellFromSpellbook(SpellID::PRESET); hero.removeSpellbook(); } + ui->filter->clear(); + ui->filter->setEnabled(checked); ui->tabWidget->setEnabled(checked); initSpellLists(); }