| 713 | } |
| 714 | |
| 715 | bool ProjectExplorer::filter(const QModelIndex& index, const QString& text) { |
| 716 | const auto* model = index.model(); |
| 717 | const int rows = model->rowCount(index); |
| 718 | |
| 719 | // if the filter string is empty, just traverse the whole model and make every index visible |
| 720 | if (text.isEmpty()) { |
| 721 | for (int i = 0; i < rows; ++i) { |
| 722 | m_treeView->setRowHidden(i, index, false); |
| 723 | const auto& child = model->index(i, 0, index); |
| 724 | if (model->hasChildren(child)) |
| 725 | filter(child, text); |
| 726 | } |
| 727 | return true; |
| 728 | } |
| 729 | |
| 730 | #if HAS_FUZZY_MATCHER |
| 731 | bool fuzzyFiltering = true; |
| 732 | if (fuzzyMatchingAction && !fuzzyMatchingAction->isChecked()) |
| 733 | fuzzyFiltering = false; |
| 734 | #endif |
| 735 | |
| 736 | bool childVisible = false; |
| 737 | for (int i = 0; i < rows; i++) { |
| 738 | const auto& child = model->index(i, 0, index); |
| 739 | auto* aspect = static_cast<AbstractAspect*>(child.internalPointer()); |
| 740 | bool visible; |
| 741 | #if HAS_FUZZY_MATCHER |
| 742 | if (fuzzyFiltering) |
| 743 | visible = KFuzzyMatcher::matchSimple(text, aspect->name()); |
| 744 | else |
| 745 | #endif |
| 746 | { |
| 747 | bool matchCompleteWord = false; |
| 748 | if (matchCompleteWordAction && matchCompleteWordAction->isChecked()) |
| 749 | matchCompleteWord = true; |
| 750 | |
| 751 | Qt::CaseSensitivity sensitivity = Qt::CaseInsensitive; |
| 752 | if (caseSensitiveAction && caseSensitiveAction->isChecked()) |
| 753 | sensitivity = Qt::CaseSensitive; |
| 754 | |
| 755 | if (matchCompleteWord) |
| 756 | visible = aspect->name().startsWith(text, sensitivity); |
| 757 | else |
| 758 | visible = aspect->name().contains(text, sensitivity); |
| 759 | } |
| 760 | |
| 761 | if (visible) { |
| 762 | // current item is visible -> make all its children visible without applying the filter |
| 763 | for (int j = 0; j < model->rowCount(child); ++j) |
| 764 | m_treeView->setRowHidden(j, child, false); |
| 765 | |
| 766 | childVisible = true; |
| 767 | } else { |
| 768 | // check children items. if one of the children is visible, make the parent (current) item visible too. |
| 769 | visible = filter(child, text); |
| 770 | if (visible) |
| 771 | childVisible = true; |
| 772 | } |