mysortfilterproxymodel.cpp
author convert-repo
Fri, 23 Jul 2010 16:43:49 +0000
changeset 849 988f1908a7c4
parent 822 c2ce9944148c
permissions -rw-r--r--
update tags
     1 //#include <QtGui>
     2 
     3 #include "mysortfilterproxymodel.h"
     4 
     5 MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent)
     6     : QSortFilterProxyModel(parent)
     7 {
     8 }
     9 //! [0]
    10 
    11 /*
    12 //! [1]
    13 void MySortFilterProxyModel::setFilterMinimumDate(const QDate &date)
    14 {
    15     minDate = date;
    16     invalidateFilter();
    17 }
    18 //! [1]
    19 
    20 //! [2]
    21 void MySortFilterProxyModel::setFilterMaximumDate(const QDate &date)
    22 {
    23     maxDate = date;
    24     invalidateFilter();
    25 }
    26 */
    27 
    28 bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,	// FIXME-3 find a way to show _all_ rows which match, independent of parent
    29         const QModelIndex &sourceParent) const
    30 {
    31     QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
    32     QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
    33     QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
    34 
    35     return (sourceModel()->data(index0).toString().contains(filterRegExp())
    36             || sourceModel()->data(index1).toString().contains(filterRegExp()));
    37 //           && dateInRange(sourceModel()->data(index2).toDate());
    38 }
    39 
    40 /*
    41 bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
    42                                       const QModelIndex &right) const
    43 {
    44     QVariant leftData = sourceModel()->data(left);
    45     QVariant rightData = sourceModel()->data(right);
    46 //! [4]
    47 
    48 //! [6]
    49     if (leftData.type() == QVariant::DateTime) {
    50         return leftData.toDateTime() < rightData.toDateTime();
    51     } else {
    52         QRegExp *emailPattern = new QRegExp("([\\w\\.]*@[\\w\\.]*)");
    53 
    54         QString leftString = leftData.toString();
    55         if(left.column() == 1 && emailPattern->indexIn(leftString) != -1)
    56             leftString = emailPattern->cap(1);
    57 
    58         QString rightString = rightData.toString();
    59         if(right.column() == 1 && emailPattern->indexIn(rightString) != -1)
    60             rightString = emailPattern->cap(1);
    61 
    62         return QString::localeAwareCompare(leftString, rightString) < 0;
    63     }
    64 }
    65 //! [5] //! [6]
    66 
    67 //! [7]
    68 bool MySortFilterProxyModel::dateInRange(const QDate &date) const
    69 {
    70     return (!minDate.isValid() || date > minDate)
    71            && (!maxDate.isValid() || date < maxDate);
    72 }
    73 //! [7]
    74 */