mysortfilterproxymodel.cpp
author insilmaril
Fri, 02 Oct 2009 13:24:55 +0000
changeset 802 f076fdec767d
parent 795 6b0a5f4923d3
child 804 14f2b1b15242
permissions -rw-r--r--
More fixes for using proxy
insilmaril@795
     1
/****************************************************************************
insilmaril@795
     2
**
insilmaril@802
     3
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
insilmaril@802
     4
** Contact: Nokia Corporation (qt-info@nokia.com)
insilmaril@795
     5
**
insilmaril@802
     6
** This file is part of the examples of the Qt Toolkit.
insilmaril@795
     7
**
insilmaril@802
     8
** $QT_BEGIN_LICENSE:LGPL$
insilmaril@795
     9
** Commercial Usage
insilmaril@795
    10
** Licensees holding valid Qt Commercial licenses may use this file in
insilmaril@795
    11
** accordance with the Qt Commercial License Agreement provided with the
insilmaril@795
    12
** Software or, alternatively, in accordance with the terms contained in
insilmaril@795
    13
** a written agreement between you and Nokia.
insilmaril@795
    14
**
insilmaril@802
    15
** GNU Lesser General Public License Usage
insilmaril@802
    16
** Alternatively, this file may be used under the terms of the GNU Lesser
insilmaril@802
    17
** General Public License version 2.1 as published by the Free Software
insilmaril@802
    18
** Foundation and appearing in the file LICENSE.LGPL included in the
insilmaril@802
    19
** packaging of this file.  Please review the following information to
insilmaril@802
    20
** ensure the GNU Lesser General Public License version 2.1 requirements
insilmaril@802
    21
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
insilmaril@802
    22
**
insilmaril@802
    23
** In addition, as a special exception, Nokia gives you certain
insilmaril@802
    24
** additional rights. These rights are described in the Nokia Qt LGPL
insilmaril@802
    25
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
insilmaril@802
    26
** package.
insilmaril@795
    27
**
insilmaril@795
    28
** GNU General Public License Usage
insilmaril@795
    29
** Alternatively, this file may be used under the terms of the GNU
insilmaril@802
    30
** General Public License version 3.0 as published by the Free Software
insilmaril@802
    31
** Foundation and appearing in the file LICENSE.GPL included in the
insilmaril@802
    32
** packaging of this file.  Please review the following information to
insilmaril@802
    33
** ensure the GNU General Public License version 3.0 requirements will be
insilmaril@802
    34
** met: http://www.gnu.org/copyleft/gpl.html.
insilmaril@795
    35
**
insilmaril@795
    36
** If you are unsure which license is appropriate for your use, please
insilmaril@802
    37
** contact the sales department at http://www.qtsoftware.com/contact.
insilmaril@802
    38
** $QT_END_LICENSE$
insilmaril@795
    39
**
insilmaril@795
    40
****************************************************************************/
insilmaril@795
    41
insilmaril@795
    42
#include <QtGui>
insilmaril@795
    43
insilmaril@795
    44
#include "mysortfilterproxymodel.h"
insilmaril@795
    45
insilmaril@802
    46
//! [0]
insilmaril@795
    47
MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent)
insilmaril@795
    48
    : QSortFilterProxyModel(parent)
insilmaril@795
    49
{
insilmaril@795
    50
}
insilmaril@802
    51
//! [0]
insilmaril@802
    52
insilmaril@802
    53
//! [1]
insilmaril@795
    54
void MySortFilterProxyModel::setFilterMinimumDate(const QDate &date)
insilmaril@795
    55
{
insilmaril@795
    56
    minDate = date;
insilmaril@795
    57
    invalidateFilter();
insilmaril@795
    58
}
insilmaril@802
    59
//! [1]
insilmaril@802
    60
insilmaril@802
    61
//! [2]
insilmaril@795
    62
void MySortFilterProxyModel::setFilterMaximumDate(const QDate &date)
insilmaril@795
    63
{
insilmaril@795
    64
    maxDate = date;
insilmaril@795
    65
    invalidateFilter();
insilmaril@795
    66
}
insilmaril@802
    67
//! [2]
insilmaril@795
    68
insilmaril@802
    69
//! [3]
insilmaril@795
    70
bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
insilmaril@795
    71
        const QModelIndex &sourceParent) const
insilmaril@795
    72
{
insilmaril@795
    73
    QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
insilmaril@795
    74
    QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
insilmaril@802
    75
    QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
insilmaril@795
    76
insilmaril@802
    77
    return (sourceModel()->data(index0).toString().contains(filterRegExp())
insilmaril@802
    78
            || sourceModel()->data(index1).toString().contains(filterRegExp()))
insilmaril@802
    79
           && dateInRange(sourceModel()->data(index2).toDate());
insilmaril@795
    80
}
insilmaril@802
    81
//! [3]
insilmaril@795
    82
insilmaril@802
    83
//! [4] //! [5]
insilmaril@795
    84
bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
insilmaril@795
    85
                                      const QModelIndex &right) const
insilmaril@795
    86
{
insilmaril@795
    87
    QVariant leftData = sourceModel()->data(left);
insilmaril@795
    88
    QVariant rightData = sourceModel()->data(right);
insilmaril@802
    89
//! [4]
insilmaril@802
    90
insilmaril@802
    91
//! [6]
insilmaril@795
    92
    if (leftData.type() == QVariant::DateTime) {
insilmaril@795
    93
        return leftData.toDateTime() < rightData.toDateTime();
insilmaril@795
    94
    } else {
insilmaril@795
    95
        QRegExp *emailPattern = new QRegExp("([\\w\\.]*@[\\w\\.]*)");
insilmaril@795
    96
insilmaril@795
    97
        QString leftString = leftData.toString();
insilmaril@795
    98
        if(left.column() == 1 && emailPattern->indexIn(leftString) != -1)
insilmaril@795
    99
            leftString = emailPattern->cap(1);
insilmaril@795
   100
insilmaril@795
   101
        QString rightString = rightData.toString();
insilmaril@795
   102
        if(right.column() == 1 && emailPattern->indexIn(rightString) != -1)
insilmaril@795
   103
            rightString = emailPattern->cap(1);
insilmaril@795
   104
insilmaril@795
   105
        return QString::localeAwareCompare(leftString, rightString) < 0;
insilmaril@795
   106
    }
insilmaril@795
   107
}
insilmaril@802
   108
//! [5] //! [6]
insilmaril@802
   109
insilmaril@802
   110
//! [7]
insilmaril@795
   111
bool MySortFilterProxyModel::dateInRange(const QDate &date) const
insilmaril@795
   112
{
insilmaril@795
   113
    return (!minDate.isValid() || date > minDate)
insilmaril@795
   114
           && (!maxDate.isValid() || date < maxDate);
insilmaril@795
   115
}
insilmaril@802
   116
//! [7]