File indexing completed on 2024-04-21 04:58:24

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Omat Holding B.V. <info@omat.nl>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "ksortfilterproxymodel.h"
0008 
0009 /**
0010  * Private class that helps to provide binary compatibility between releases.
0011  * @internal
0012  */
0013 //@cond PRIVATE
0014 class KSortFilterProxyModelPrivate
0015 {
0016 public:
0017     KSortFilterProxyModelPrivate()
0018     {
0019         showAllChildren = false;
0020     }
0021     ~KSortFilterProxyModelPrivate() {}
0022 
0023     bool showAllChildren;
0024 };
0025 
0026 KSortFilterProxyModel::KSortFilterProxyModel(QObject *parent)
0027     : QSortFilterProxyModel(parent), d_ptr(new KSortFilterProxyModelPrivate)
0028 {
0029 }
0030 
0031 KSortFilterProxyModel::~KSortFilterProxyModel()
0032 {
0033     delete d_ptr;
0034 }
0035 
0036 bool KSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0037 {
0038     if (filterRegularExpression().pattern().isEmpty()) {
0039         return true;    //Shortcut for common case
0040     }
0041 
0042     if (QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent)) {
0043         return true;
0044     }
0045 
0046     //one of our children might be accepted, so accept this row if one of our children are accepted.
0047     QModelIndex source_index = sourceModel()->index(source_row, 0, source_parent);
0048     for (int i = 0; i < sourceModel()->rowCount(source_index); i++) {
0049         if (filterAcceptsRow(i, source_index)) {
0050             return true;
0051         }
0052     }
0053 
0054     //one of our parents might be accepted, so accept this row if one of our parents is accepted.
0055     if (d_ptr->showAllChildren) {
0056         QModelIndex parent_index = source_parent;
0057         while (parent_index.isValid()) {
0058             int row = parent_index.row();
0059             parent_index = parent_index.parent();
0060             if (QSortFilterProxyModel::filterAcceptsRow(row, parent_index)) {
0061                 return true;
0062             }
0063         }
0064     }
0065 
0066     return false;
0067 }
0068 
0069 bool KSortFilterProxyModel::showAllChildren() const
0070 {
0071     return d_ptr->showAllChildren;
0072 }
0073 void KSortFilterProxyModel::setShowAllChildren(bool showAllChildren)
0074 {
0075     if (showAllChildren == d_ptr->showAllChildren) {
0076         return;
0077     }
0078     d_ptr->showAllChildren = showAllChildren;
0079     invalidateFilter();
0080 }
0081