| 1 | /* |
| 2 | * Copyright (C) 2022 Damir Porobic <[email protected]> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | */ |
| 19 | |
| 20 | #include <functional> |
| 21 | |
| 22 | #include <QAbstractButton> |
| 23 | #include <QComboBox> |
| 24 | #include <QLabel> |
| 25 | #include <QLineEdit> |
| 26 | |
| 27 | #include "SettingsFilter.h" |
| 28 | |
| 29 | void SettingsFilter::filterSettings( |
| 30 | const QString &filterString, |
| 31 | QTreeWidget *treeWidget, |
| 32 | std::function<QWidget*(QTreeWidgetItem*)> getSettingsPageFunc) const |
| 33 | { |
| 34 | if (filterString.isEmpty()) { |
| 35 | for (auto topLevelItemIndex = 0; topLevelItemIndex < treeWidget->topLevelItemCount(); ++topLevelItemIndex) { |
| 36 | auto topLevelItem = treeWidget->topLevelItem(topLevelItemIndex); |
| 37 | for (auto childIndex = 0; childIndex < topLevelItem->childCount(); ++childIndex) { |
| 38 | topLevelItem->child(childIndex)->setHidden(false); |
| 39 | } |
| 40 | topLevelItem->setHidden(false); |
| 41 | } |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | for (auto index = 0; index < treeWidget->topLevelItemCount(); ++index) { |
| 46 | filterNavigatorItem(treeWidget->topLevelItem(index), filterString, getSettingsPageFunc); |
| 47 | } |
| 48 | |
| 49 | for (auto topLevelItemIndex = 0; topLevelItemIndex < treeWidget->topLevelItemCount(); ++topLevelItemIndex) { |
| 50 | if (!treeWidget->topLevelItem(topLevelItemIndex)->isHidden()) { |
| 51 | treeWidget->setCurrentItem(treeWidget->topLevelItem(topLevelItemIndex)); |
| 52 | return; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | treeWidget->clearSelection(); |
| 57 | } |
| 58 | |
| 59 | bool SettingsFilter::filterNavigatorItem( |
| 60 | QTreeWidgetItem *navigatorItem, |
| 61 | const QString &filterString, |
| 62 | std::function<QWidget*(QTreeWidgetItem*)> getSettingsPageFunc) const |
| 63 | { |
| 64 | bool isFiltered{true}; |
| 65 | |
| 66 | if (navigatorItem->text(0).contains(filterString, Qt::CaseInsensitive)) { |
| 67 | navigatorItem->setDisabled(false); |
| 68 | for (auto index = 0; index < navigatorItem->childCount(); ++index) { |
| 69 | filterNavigatorItem(navigatorItem->child(index), filterString, getSettingsPageFunc); |
| 70 | } |
| 71 | isFiltered = false; |
| 72 | } else { |
| 73 | isFiltered = !settingsPageContainsFilterString(getSettingsPageFunc(navigatorItem), filterString); |
| 74 | |
| 75 | for (auto index = 0; index < navigatorItem->childCount(); ++index) { |
| 76 | isFiltered &= filterNavigatorItem(navigatorItem->child(index), filterString, getSettingsPageFunc); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | navigatorItem->setHidden(isFiltered); |
| 81 | return isFiltered; |
| 82 | } |
| 83 | |
| 84 | bool SettingsFilter::settingsPageContainsFilterString(QWidget *settingsPage, const QString &filterString) const |
| 85 | { |
| 86 | foreach (auto button, settingsPage->findChildren<QAbstractButton*>()) { |
| 87 | if (button->text().contains(filterString, Qt::CaseInsensitive)) { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | foreach (auto label, settingsPage->findChildren<QLabel*>()) { |
| 93 | if (label->text().contains(filterString, Qt::CaseInsensitive)) { |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | foreach (auto lineEdit, settingsPage->findChildren<QLineEdit*>()) { |
| 99 | if (lineEdit->text().contains(filterString, Qt::CaseInsensitive)) { |
| 100 | return true; |
| 101 | } |
| 102 | if (lineEdit->placeholderText().contains(filterString, Qt::CaseInsensitive)) { |
| 103 | return true; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | foreach (auto comboBox, settingsPage->findChildren<QComboBox*>()) { |
| 108 | for (int index = 0; index < comboBox->count(); ++index) { |
| 109 | if (comboBox->itemText(index).contains(filterString, Qt::CaseInsensitive)) { |
| 110 | return true; |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return false; |
| 116 | } |