GuiLyXFiles: Hide empty categories on filtering (#12584)

This commit is contained in:
Juergen Spitzmueller 2022-10-09 16:01:49 +02:00
parent 5ee0ba6403
commit 0e4ca9aa88

View File

@ -26,6 +26,7 @@
#include "support/qstring_helpers.h" #include "support/qstring_helpers.h"
#include "support/Package.h" #include "support/Package.h"
#include <QVector>
#include <QDirIterator> #include <QDirIterator>
#include <QTreeWidget> #include <QTreeWidget>
@ -509,12 +510,43 @@ void GuiLyXFiles::filterLabels()
{ {
Qt::CaseSensitivity cs = csFindCB->isChecked() ? Qt::CaseSensitivity cs = csFindCB->isChecked() ?
Qt::CaseSensitive : Qt::CaseInsensitive; Qt::CaseSensitive : Qt::CaseInsensitive;
// Collect "active" categories (containing entries
// that match the filter)
QVector<QTreeWidgetItem*> activeCats;
QTreeWidgetItemIterator it(filesLW); QTreeWidgetItemIterator it(filesLW);
while (*it) { while (*it) {
(*it)->setHidden( if ((*it)->childCount() > 0) {
(*it)->childCount() == 0 // Unhide parents (will be hidden
&& !(*it)->text(0).contains(filter_->text(), cs) // 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; ++it;
} }
} }