File indexing completed on 2024-04-28 05:48:38

0001 /***************************************************************************
0002  *   This file is part of Kate build plugin                                *
0003  *   SPDX-FileCopyrightText: 2022 Kåre Särs <kare.sars@iki.fi>             *
0004  *                                                                         *
0005  *   SPDX-License-Identifier: LGPL-2.0-or-later
0006  ***************************************************************************/
0007 #include "TargetFilterProxyModel.h"
0008 
0009 #include <QDebug>
0010 
0011 TargetFilterProxyModel::TargetFilterProxyModel(QObject *parent)
0012     : QSortFilterProxyModel(parent)
0013 {
0014 }
0015 
0016 bool TargetFilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0017 {
0018     QModelIndex srcIndex = sourceModel()->index(sourceRow, 0, sourceParent);
0019     if (!srcIndex.isValid()) {
0020         qDebug() << "srcIndex is invalid";
0021         return false;
0022     }
0023 
0024     if (m_filter.isEmpty()) {
0025         return true;
0026     }
0027 
0028     QString name = srcIndex.data().toString();
0029     if (name.contains(m_filter, Qt::CaseInsensitive)) {
0030         return true;
0031     }
0032 
0033     for (int row = 0; row < sourceModel()->rowCount(srcIndex); ++row) {
0034         if (filterAcceptsRow(row, srcIndex)) {
0035             return true;
0036         }
0037     }
0038     return false;
0039 }
0040 
0041 void TargetFilterProxyModel::setFilter(const QString &filter)
0042 {
0043     m_filter = filter;
0044     invalidateFilter();
0045 }
0046 
0047 bool TargetFilterProxyModel::setData(const QModelIndex &index, const QVariant &value, int role)
0048 {
0049     QModelIndex srcIndex = mapToSource(index);
0050     return sourceModel()->setData(srcIndex, value, role);
0051 }