File indexing completed on 2024-04-28 17:02:22
0001 /* 0002 This file is part of Massif Visualizer 0003 0004 Copyright 2010 Milian Wolff <mail@milianw.de> 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Lesser General Public 0008 License as published by the Free Software Foundation; either 0009 version 2.1 of the License, or (at your option) version 3, or any 0010 later version accepted by the membership of KDE e.V. (or its 0011 successor approved by the membership of KDE e.V.), which shall 0012 act as a proxy defined in Section 6 of version 3 of the license. 0013 0014 This library is distributed in the hope that it will be useful, 0015 but WITHOUT ANY WARRANTY; without even the implied warranty of 0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0017 Lesser General Public License for more details. 0018 0019 You should have received a copy of the GNU Lesser General Public 0020 License along with this library. If not, see <http://www.gnu.org/licenses/>. 0021 */ 0022 0023 #include "filtereddatatreemodel.h" 0024 #include "datatreemodel.h" 0025 0026 using namespace Massif; 0027 0028 FilteredDataTreeModel::FilteredDataTreeModel(DataTreeModel* parent) 0029 : QSortFilterProxyModel(parent) 0030 { 0031 setDynamicSortFilter(true); 0032 setSourceModel(parent); 0033 m_timer.setSingleShot(true); 0034 m_timer.setInterval(150); 0035 connect(&m_timer, &QTimer::timeout, this, &FilteredDataTreeModel::timeout); 0036 } 0037 0038 void FilteredDataTreeModel::setFilter(const QString& needle) 0039 { 0040 m_needle = needle; 0041 m_timer.start(); 0042 } 0043 0044 void FilteredDataTreeModel::timeout() 0045 { 0046 invalidateFilter(); 0047 } 0048 0049 bool FilteredDataTreeModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const 0050 { 0051 if (m_needle.isEmpty()) { 0052 return true; 0053 } 0054 0055 const QModelIndex& dataIdx = sourceModel()->index(source_row, 0, source_parent); 0056 Q_ASSERT(dataIdx.isValid()); 0057 if (sourceModel()->data(dataIdx, DataTreeModel::RawLabelRole).toString().contains(m_needle, Qt::CaseInsensitive)) { 0058 return true; 0059 } else { 0060 const int rows = sourceModel()->rowCount(dataIdx); 0061 for ( int i = 0; i < rows; ++i ) { 0062 if ( filterAcceptsRow(i, dataIdx) ) { 0063 return true; 0064 } 0065 } 0066 return false; 0067 } 0068 } 0069 0070 bool FilteredDataTreeModel::filterAcceptsColumn(int, const QModelIndex&) const 0071 { 0072 return true; 0073 } 0074 0075 void FilteredDataTreeModel::setSourceModel(QAbstractItemModel* sourceModel) 0076 { 0077 QSortFilterProxyModel::setSourceModel(sourceModel); 0078 }