/tests/auto/xmlpatternsview/view/TreeSortFilter.cpp

https://bitbucket.org/ultra_iter/qt-vtl
C++ | 122 lines | 63 code | 17 blank | 42 comment | 11 complexity | 2415cf6bb75919e6f002a1f7d9ce4ec2 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the test suite of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <QtDebug>
  42. #include "TreeSortFilter.h"
  43. using namespace QPatternistSDK;
  44. TreeSortFilter::TreeSortFilter(QObject *p) : QSortFilterProxyModel(p)
  45. {
  46. Q_ASSERT(p);
  47. }
  48. bool TreeSortFilter::lessThan(const QModelIndex &left,
  49. const QModelIndex &right) const
  50. {
  51. const QVariant leftData(sourceModel()->data(left));
  52. const QVariant rightData(sourceModel()->data(right));
  53. return numericLessThan(leftData.toString(), rightData.toString());
  54. }
  55. bool TreeSortFilter::numericLessThan(const QString &l, const QString &r) const
  56. {
  57. QString ls(l);
  58. QString rs(r);
  59. const int len = (l.length() > r.length() ? r.length() : l.length());
  60. for(int i = 0;i < len; ++i)
  61. {
  62. const QChar li(l.at(i));
  63. const QChar ri(r.at(i));
  64. if(li >= QLatin1Char('0') &&
  65. li <= QLatin1Char('9') &&
  66. ri >= QLatin1Char('0') &&
  67. ri <= QLatin1Char('9'))
  68. {
  69. ls = l.mid(i);
  70. rs = r.mid(i);
  71. break;
  72. }
  73. else if(li != ri)
  74. break;
  75. }
  76. const int ld = ls.toInt();
  77. const int rd = rs.toInt();
  78. if(ld == rd)
  79. return ls.localeAwareCompare(rs) < 0;
  80. else
  81. return ld < rd;
  82. }
  83. bool TreeSortFilter::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
  84. {
  85. if(filterRegExp().isEmpty())
  86. return true;
  87. QModelIndex current(sourceModel()->index(sourceRow, filterKeyColumn(), sourceParent));
  88. if(sourceModel()->hasChildren(current))
  89. {
  90. bool atLeastOneValidChild = false;
  91. int i = 0;
  92. while(!atLeastOneValidChild)
  93. {
  94. const QModelIndex child(current.child(i, current.column()));
  95. if(!child.isValid())
  96. // No valid child
  97. break;
  98. atLeastOneValidChild = filterAcceptsRow(i, current);
  99. i++;
  100. }
  101. return atLeastOneValidChild;
  102. }
  103. return sourceModel()->data(current).toString().contains(filterRegExp());
  104. }
  105. // vim: et:ts=4:sw=4:sts=4