From 0e4ca9aa88eb7b9351bb563dcd2df59c15744aa5 Mon Sep 17 00:00:00 2001 From: Juergen Spitzmueller Date: Sun, 9 Oct 2022 16:01:49 +0200 Subject: [PATCH] GuiLyXFiles: Hide empty categories on filtering (#12584) --- src/frontends/qt/GuiLyXFiles.cpp | 40 ++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/src/frontends/qt/GuiLyXFiles.cpp b/src/frontends/qt/GuiLyXFiles.cpp index 302c567eda..c9ab003dad 100644 --- a/src/frontends/qt/GuiLyXFiles.cpp +++ b/src/frontends/qt/GuiLyXFiles.cpp @@ -26,6 +26,7 @@ #include "support/qstring_helpers.h" #include "support/Package.h" +#include #include #include @@ -509,12 +510,43 @@ void GuiLyXFiles::filterLabels() { Qt::CaseSensitivity cs = csFindCB->isChecked() ? Qt::CaseSensitive : Qt::CaseInsensitive; + // Collect "active" categories (containing entries + // that match the filter) + QVector activeCats; QTreeWidgetItemIterator it(filesLW); while (*it) { - (*it)->setHidden( - (*it)->childCount() == 0 - && !(*it)->text(0).contains(filter_->text(), cs) - ); + if ((*it)->childCount() > 0) { + // Unhide parents (will be hidden + // below if necessary) + (*it)->setHidden(false); + ++it; + continue; + } + bool const match = (*it)->text(0).contains(filter_->text(), cs); + if (match) { + // Register parents of matched entries + // so we don't hide those later. + QTreeWidgetItem * twi = *it; + while (true) { + if (!twi->parent()) + break; + activeCats << twi->parent(); + // ascend further up if possible + twi = twi->parent(); + } + } + (*it)->setHidden(!match); + ++it; + } + // Iterate through parents once more + // to hide empty categories + it = QTreeWidgetItemIterator(filesLW); + while (*it) { + if ((*it)->childCount() == 0) { + ++it; + continue; + } + (*it)->setHidden(!activeCats.contains(*it)); ++it; } } -- 2.39.5