Pārlūkot izejas kodu

Merge pull request #5949 from Opuszek/add_filter_to_hero_spell_widget

Map Editor:Add filter to the herospell widget
Ivan Savenko 3 mēneši atpakaļ
vecāks
revīzija
5c4cbcb130

+ 71 - 0
mapeditor/inspector/herospellwidget.cpp

@@ -98,10 +98,81 @@ void HeroSpellWidget::on_customizeSpells_toggled(bool checked)
 	{
 		hero.removeAllSpells();
 	}
+	ui->filter->clear();
+	ui->filter->setEnabled(checked);
 	ui->tabWidget->setEnabled(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)

+ 5 - 0
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

+ 18 - 0
mapeditor/inspector/herospellwidget.ui

@@ -51,6 +51,9 @@
      </property>
     </widget>
    </item>
+   <item>
+    <widget class="QLineEdit" name="filter"/>
+   </item>
    <item>
     <widget class="QTabWidget" name="tabWidget">
      <property name="sizePolicy">
@@ -99,6 +102,9 @@
          <property name="alternatingRowColors">
           <bool>true</bool>
          </property>
+         <property name="sortingEnabled">
+          <bool>true</bool>
+         </property>
         </widget>
        </item>
       </layout>
@@ -137,6 +143,9 @@
          <property name="alternatingRowColors">
           <bool>true</bool>
          </property>
+         <property name="sortingEnabled">
+          <bool>true</bool>
+         </property>
         </widget>
        </item>
       </layout>
@@ -175,6 +184,9 @@
          <property name="alternatingRowColors">
           <bool>true</bool>
          </property>
+         <property name="sortingEnabled">
+          <bool>true</bool>
+         </property>
         </widget>
        </item>
       </layout>
@@ -213,6 +225,9 @@
          <property name="alternatingRowColors">
           <bool>true</bool>
          </property>
+         <property name="sortingEnabled">
+          <bool>true</bool>
+         </property>
         </widget>
        </item>
       </layout>
@@ -251,6 +266,9 @@
          <property name="alternatingRowColors">
           <bool>true</bool>
          </property>
+         <property name="sortingEnabled">
+          <bool>true</bool>
+         </property>
         </widget>
        </item>
       </layout>