Warning, file /office/calligra/braindump/src/TreeSortFilter.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 Copyright (c) 2006 Frans Englich <frans.englich@telia.com> 0003 0004 This file is part of the KDE project 0005 0006 This library is free software; you can redistribute it and/or 0007 modify it under the terms of the GNU Library General Public 0008 License as published by the Free Software Foundation; either 0009 version 2 of the License, or(at your option) any later version. 0010 0011 This library is distributed in the hope that it will be useful, 0012 but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 0014 Library General Public License for more details. 0015 0016 You should have received a copy of the GNU Library General Public License 0017 along with this library; see the file COPYING.LIB. If not, write to 0018 the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, 0019 Boston, MA 02110-1301, USA. 0020 */ 0021 0022 #include "TreeSortFilter.h" 0023 0024 TreeSortFilter::TreeSortFilter(QObject *p) : QSortFilterProxyModel(p) 0025 { 0026 Q_ASSERT(p); 0027 } 0028 0029 bool TreeSortFilter::lessThan(const QModelIndex &left, 0030 const QModelIndex &right) const 0031 { 0032 const QVariant leftData(sourceModel()->data(left)); 0033 const QVariant rightData(sourceModel()->data(right)); 0034 0035 return numericLessThan(leftData.toString(), rightData.toString()); 0036 } 0037 0038 bool TreeSortFilter::numericLessThan(const QString &l, const QString &r) const 0039 { 0040 QString ls(l); 0041 QString rs(r); 0042 const int len = (l.length() > r.length() ? r.length() : l.length()); 0043 0044 for(int i = 0; i < len; ++i) { 0045 const QChar li(l.at(i)); 0046 const QChar ri(r.at(i)); 0047 0048 if(li >= QLatin1Char('0') && 0049 li <= QLatin1Char('9') && 0050 ri >= QLatin1Char('0') && 0051 ri <= QLatin1Char('9')) { 0052 ls = l.mid(i); 0053 rs = r.mid(i); 0054 break; 0055 } else if(li != ri) 0056 break; 0057 } 0058 0059 const int ld = ls.toInt(); 0060 const int rd = rs.toInt(); 0061 0062 if(ld == rd) 0063 return ls.localeAwareCompare(rs) < 0; 0064 else 0065 return ld < rd; 0066 } 0067 0068 bool TreeSortFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const 0069 { 0070 if(filterRegExp().isEmpty()) 0071 return true; 0072 0073 QModelIndex current(sourceModel()->index(sourceRow, filterKeyColumn(), sourceParent)); 0074 0075 if(sourceModel()->hasChildren(current)) { 0076 bool atLeastOneValidChild = false; 0077 int i = 0; 0078 while(!atLeastOneValidChild) { 0079 const QModelIndex child(current.child(i, current.column())); 0080 if(!child.isValid()) 0081 // No valid child 0082 break; 0083 0084 atLeastOneValidChild = filterAcceptsRow(i, current); 0085 i++; 0086 } 0087 return atLeastOneValidChild; 0088 } 0089 0090 return sourceModel()->data(current).toString().contains(filterRegExp()); 0091 } 0092 0093 // vim: et:ts=4:sw=4:sts=4