insilmaril@795: /**************************************************************************** insilmaril@795: ** insilmaril@802: ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). insilmaril@802: ** Contact: Nokia Corporation (qt-info@nokia.com) insilmaril@795: ** insilmaril@802: ** This file is part of the examples of the Qt Toolkit. insilmaril@795: ** insilmaril@802: ** $QT_BEGIN_LICENSE:LGPL$ insilmaril@795: ** Commercial Usage insilmaril@795: ** Licensees holding valid Qt Commercial licenses may use this file in insilmaril@795: ** accordance with the Qt Commercial License Agreement provided with the insilmaril@795: ** Software or, alternatively, in accordance with the terms contained in insilmaril@795: ** a written agreement between you and Nokia. insilmaril@795: ** insilmaril@802: ** GNU Lesser General Public License Usage insilmaril@802: ** Alternatively, this file may be used under the terms of the GNU Lesser insilmaril@802: ** General Public License version 2.1 as published by the Free Software insilmaril@802: ** Foundation and appearing in the file LICENSE.LGPL included in the insilmaril@802: ** packaging of this file. Please review the following information to insilmaril@802: ** ensure the GNU Lesser General Public License version 2.1 requirements insilmaril@802: ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. insilmaril@802: ** insilmaril@802: ** In addition, as a special exception, Nokia gives you certain insilmaril@802: ** additional rights. These rights are described in the Nokia Qt LGPL insilmaril@802: ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this insilmaril@802: ** package. insilmaril@795: ** insilmaril@795: ** GNU General Public License Usage insilmaril@795: ** Alternatively, this file may be used under the terms of the GNU insilmaril@802: ** General Public License version 3.0 as published by the Free Software insilmaril@802: ** Foundation and appearing in the file LICENSE.GPL included in the insilmaril@802: ** packaging of this file. Please review the following information to insilmaril@802: ** ensure the GNU General Public License version 3.0 requirements will be insilmaril@802: ** met: http://www.gnu.org/copyleft/gpl.html. insilmaril@795: ** insilmaril@795: ** If you are unsure which license is appropriate for your use, please insilmaril@802: ** contact the sales department at http://www.qtsoftware.com/contact. insilmaril@802: ** $QT_END_LICENSE$ insilmaril@795: ** insilmaril@795: ****************************************************************************/ insilmaril@795: insilmaril@795: #include insilmaril@795: insilmaril@795: #include "mysortfilterproxymodel.h" insilmaril@795: insilmaril@802: //! [0] insilmaril@795: MySortFilterProxyModel::MySortFilterProxyModel(QObject *parent) insilmaril@795: : QSortFilterProxyModel(parent) insilmaril@795: { insilmaril@795: } insilmaril@802: //! [0] insilmaril@802: insilmaril@802: //! [1] insilmaril@795: void MySortFilterProxyModel::setFilterMinimumDate(const QDate &date) insilmaril@795: { insilmaril@795: minDate = date; insilmaril@795: invalidateFilter(); insilmaril@795: } insilmaril@802: //! [1] insilmaril@802: insilmaril@802: //! [2] insilmaril@795: void MySortFilterProxyModel::setFilterMaximumDate(const QDate &date) insilmaril@795: { insilmaril@795: maxDate = date; insilmaril@795: invalidateFilter(); insilmaril@795: } insilmaril@802: //! [2] insilmaril@795: insilmaril@802: //! [3] insilmaril@795: bool MySortFilterProxyModel::filterAcceptsRow(int sourceRow, insilmaril@795: const QModelIndex &sourceParent) const insilmaril@795: { insilmaril@795: QModelIndex index0 = sourceModel()->index(sourceRow, 0, sourceParent); insilmaril@795: QModelIndex index1 = sourceModel()->index(sourceRow, 1, sourceParent); insilmaril@802: QModelIndex index2 = sourceModel()->index(sourceRow, 2, sourceParent); insilmaril@795: insilmaril@802: return (sourceModel()->data(index0).toString().contains(filterRegExp()) insilmaril@802: || sourceModel()->data(index1).toString().contains(filterRegExp())) insilmaril@802: && dateInRange(sourceModel()->data(index2).toDate()); insilmaril@795: } insilmaril@802: //! [3] insilmaril@795: insilmaril@802: //! [4] //! [5] insilmaril@795: bool MySortFilterProxyModel::lessThan(const QModelIndex &left, insilmaril@795: const QModelIndex &right) const insilmaril@795: { insilmaril@795: QVariant leftData = sourceModel()->data(left); insilmaril@795: QVariant rightData = sourceModel()->data(right); insilmaril@802: //! [4] insilmaril@802: insilmaril@802: //! [6] insilmaril@795: if (leftData.type() == QVariant::DateTime) { insilmaril@795: return leftData.toDateTime() < rightData.toDateTime(); insilmaril@795: } else { insilmaril@795: QRegExp *emailPattern = new QRegExp("([\\w\\.]*@[\\w\\.]*)"); insilmaril@795: insilmaril@795: QString leftString = leftData.toString(); insilmaril@795: if(left.column() == 1 && emailPattern->indexIn(leftString) != -1) insilmaril@795: leftString = emailPattern->cap(1); insilmaril@795: insilmaril@795: QString rightString = rightData.toString(); insilmaril@795: if(right.column() == 1 && emailPattern->indexIn(rightString) != -1) insilmaril@795: rightString = emailPattern->cap(1); insilmaril@795: insilmaril@795: return QString::localeAwareCompare(leftString, rightString) < 0; insilmaril@795: } insilmaril@795: } insilmaril@802: //! [5] //! [6] insilmaril@802: insilmaril@802: //! [7] insilmaril@795: bool MySortFilterProxyModel::dateInRange(const QDate &date) const insilmaril@795: { insilmaril@795: return (!minDate.isValid() || date > minDate) insilmaril@795: && (!maxDate.isValid() || date < maxDate); insilmaril@795: } insilmaril@802: //! [7]