1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 **   * Redistributions of source code must retain the above copyright
15 **     notice, this list of conditions and the following disclaimer.
16 **   * Redistributions in binary form must reproduce the above copyright
17 **     notice, this list of conditions and the following disclaimer in
18 **     the documentation and/or other materials provided with the
19 **     distribution.
20 **   * Neither the name of The Qt Company Ltd nor the names of its
21 **     contributors may be used to endorse or promote products derived
22 **     from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #include <QtGui>
42 
43 #include "mysortfilterproxymodel.h"
44 
45 //! [0]
MySortFilterProxyModel(QObject * parent)46 MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent)
47     : QSortFilterProxyModel(parent)
48 {
49 }
50 //! [0]
51 
52 //! [1]
setFilterMinimumDate(const QDate & date)53 void MySortFilterProxyModel::setFilterMinimumDate(const QDate &date)
54 {
55     minDate = date;
56     invalidateFilter();
57 }
58 //! [1]
59 
60 //! [2]
setFilterMaximumDate(const QDate & date)61 void MySortFilterProxyModel::setFilterMaximumDate(const QDate &date)
62 {
63     maxDate = date;
64     invalidateFilter();
65 }
66 //! [2]
67 
68 //! [3]
filterAcceptsRow(int sourceRow,const QModelIndex & sourceParent) const69 bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow,
70         const QModelIndex &sourceParent) const
71 {
72     QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent);
73     QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent);
74     QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent);
75 
76     return (sourceModel()->data(index0).toString().contains(filterRegExp())
77             || sourceModel()->data(index1).toString().contains(filterRegExp()))
78            && dateInRange(sourceModel()->data(index2).toDate());
79 }
80 //! [3]
81 
82 //! [4] //! [5]
lessThan(const QModelIndex & left,const QModelIndex & right) const83 bool MySortFilterProxyModel::lessThan(const QModelIndex &left,
84                                       const QModelIndex &right) const
85 {
86     QVariant leftData = sourceModel()->data(left);
87     QVariant rightData = sourceModel()->data(right);
88 //! [4]
89 
90 //! [6]
91     if (leftData.type() == QVariant::DateTime) {
92         return leftData.toDateTime() < rightData.toDateTime();
93     } else {
94         QRegExp *emailPattern = new QRegExp("([\\w\\.]*@[\\w\\.]*)");
95 
96         QString leftString = leftData.toString();
97         if(left.column() == 1 && emailPattern->indexIn(leftString) != -1)
98             leftString = emailPattern->cap(1);
99 
100         QString rightString = rightData.toString();
101         if(right.column() == 1 && emailPattern->indexIn(rightString) != -1)
102             rightString = emailPattern->cap(1);
103 
104         return QString::localeAwareCompare(leftString, rightString) < 0;
105     }
106 }
107 //! [5] //! [6]
108 
109 //! [7]
dateInRange(const QDate & date) const110 bool MySortFilterProxyModel::dateInRange(const QDate &date) const
111 {
112     return (!minDate.isValid() || date > minDate)
113            && (!maxDate.isValid() || date < maxDate);
114 }
115 //! [7]
116