File indexing completed on 2024-04-21 05:45:05

0001 /***************************************************************************
0002  *   Copyright (C) 2012 by Daniel Nicoletti                                *
0003  *   dantti12@gmail.com                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  *                                                                         *
0010  *   This program is distributed in the hope that it will be useful,       *
0011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0013  *   GNU General Public License for more details.                          *
0014  *                                                                         *
0015  *   You should have received a copy of the GNU General Public License     *
0016  *   along with this program; see the file COPYING. If not, write to       *
0017  *   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,  *
0018  *   Boston, MA 02110-1301, USA.                                           *
0019  ***************************************************************************/
0020 #include "ApplicationSortFilterModel.h"
0021 
0022 #include "PackageModel.h"
0023 
0024 //#include <KDebug>
0025 
0026 ApplicationSortFilterModel::ApplicationSortFilterModel(QObject *parent) :
0027     QSortFilterProxyModel(parent),
0028     m_info(Transaction::InfoUnknown),
0029     m_applicationsOnly(false)
0030 {
0031     setDynamicSortFilter(true);
0032     setSortCaseSensitivity(Qt::CaseInsensitive);
0033     setSortRole(PackageModel::SortRole);
0034 }
0035 
0036 PackageModel *ApplicationSortFilterModel::sourcePkgModel() const
0037 {
0038     return qobject_cast<PackageModel*>(sourceModel());
0039 }
0040 
0041 void ApplicationSortFilterModel::setSourcePkgModel(PackageModel *packageModel)
0042 {
0043     setSourceModel(packageModel);
0044 }
0045 
0046 Transaction::Info ApplicationSortFilterModel::infoFilter() const
0047 {
0048     return m_info;
0049 }
0050 
0051 bool ApplicationSortFilterModel::applicationFilter() const
0052 {
0053     return m_applicationsOnly;
0054 }
0055 
0056 void ApplicationSortFilterModel::setInfoFilter(Transaction::Info info)
0057 {
0058     m_info = info;
0059     invalidate();
0060 }
0061 
0062 void ApplicationSortFilterModel::setApplicationFilter(bool enable)
0063 {
0064     m_applicationsOnly = enable;
0065     invalidate();
0066 }
0067 
0068 bool ApplicationSortFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0069 {
0070     QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
0071 
0072     // If we are filtering by Info check if the info matches
0073     if (m_info != Transaction::InfoUnknown &&
0074             m_info != index.data(PackageModel::InfoRole).value<Transaction::Info>()) {
0075         return false;
0076     }
0077 
0078     // If we are filtering by Applications check if it is an application
0079     if (m_applicationsOnly && index.data(PackageModel::IsPackageRole).toBool()) {
0080         return false;
0081     }
0082 
0083     // Return true if no filter was applied
0084     return true;
0085 }
0086 
0087 bool ApplicationSortFilterModel::lessThan(const QModelIndex &left,
0088                                           const QModelIndex &right) const
0089 {
0090     bool leftIsPackage = left.data(PackageModel::IsPackageRole).toBool();
0091     bool rightIsPackage = left.data(PackageModel::IsPackageRole).toBool();
0092 
0093     if (leftIsPackage != rightIsPackage) {
0094         // If the right item is a package the left should move right
0095         return rightIsPackage;
0096     }
0097 
0098     return QSortFilterProxyModel::lessThan(left, right);
0099 }
0100 
0101 void ApplicationSortFilterModel::sortNow()
0102 {
0103     sort(0);
0104 }
0105 
0106 #include "moc_ApplicationSortFilterModel.cpp"