File indexing completed on 2026-10-11 05:40:37
0001 /* 0002 SPDX-FileCopyrightText: 2012-2013 Daniel Nicoletti <dantti12@gmail.com> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "JobSortFilterModel.h" 0008 0009 #include "JobModel.h" 0010 #include "kcupslib_log.h" 0011 0012 JobSortFilterModel::JobSortFilterModel(QObject *parent) 0013 : QSortFilterProxyModel(parent) 0014 { 0015 setDynamicSortFilter(true); 0016 setSortCaseSensitivity(Qt::CaseInsensitive); 0017 sort(0); 0018 0019 connect(this, &JobSortFilterModel::dataChanged, this, &JobSortFilterModel::activeCountChanged); 0020 connect(this, &JobSortFilterModel::rowsInserted, this, &JobSortFilterModel::activeCountChanged); 0021 connect(this, &JobSortFilterModel::rowsRemoved, this, &JobSortFilterModel::activeCountChanged); 0022 connect(this, &JobSortFilterModel::modelReset, this, &JobSortFilterModel::activeCountChanged); 0023 connect(this, &JobSortFilterModel::dataChanged, this, &JobSortFilterModel::countChanged); 0024 connect(this, &JobSortFilterModel::rowsInserted, this, &JobSortFilterModel::countChanged); 0025 connect(this, &JobSortFilterModel::rowsRemoved, this, &JobSortFilterModel::countChanged); 0026 connect(this, &JobSortFilterModel::modelReset, this, &JobSortFilterModel::countChanged); 0027 } 0028 0029 void JobSortFilterModel::setModel(QAbstractItemModel *model) 0030 { 0031 if (model == sourceModel()) { 0032 return; 0033 } 0034 0035 QSortFilterProxyModel::setSourceModel(model); 0036 Q_EMIT sourceModelChanged(model); 0037 } 0038 0039 void JobSortFilterModel::setFilteredPrinters(const QString &printers) 0040 { 0041 qCDebug(LIBKCUPS) << rowCount() << printers << printers.split(QLatin1Char('|')); 0042 if (printers.isEmpty()) { 0043 m_filteredPrinters.clear(); 0044 } else { 0045 m_filteredPrinters = printers.split(QLatin1Char('|')); 0046 } 0047 invalidateFilter(); 0048 Q_EMIT filteredPrintersChanged(); 0049 } 0050 0051 QString JobSortFilterModel::filteredPrinters() const 0052 { 0053 return m_filteredPrinters.join(QLatin1String("|")); 0054 } 0055 0056 int JobSortFilterModel::activeCount() const 0057 { 0058 int active = 0; 0059 for (int i = 0; i < rowCount(); ++i) { 0060 QModelIndex item = index(i, 0); 0061 if (weightForState(item.data(JobModel::RoleJobState).toInt())) { 0062 ++active; 0063 } 0064 } 0065 return active; 0066 } 0067 0068 int JobSortFilterModel::count() const 0069 { 0070 return rowCount(); 0071 } 0072 0073 bool JobSortFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const 0074 { 0075 QModelIndex index = sourceModel()->index(source_row, 0, source_parent); 0076 0077 // check if the printer is on the blacklist 0078 if (!m_filteredPrinters.isEmpty()) { 0079 return m_filteredPrinters.contains(index.data(JobModel::RoleJobPrinter).toString()); 0080 } 0081 0082 return true; 0083 } 0084 0085 bool JobSortFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const 0086 { 0087 if (left.column() == 0) { 0088 int leftWeight = weightForState(left.data(JobModel::RoleJobState).toInt()); 0089 int rightWeight = weightForState(right.data(JobModel::RoleJobState).toInt()); 0090 0091 if (leftWeight != rightWeight) { 0092 return leftWeight > rightWeight; 0093 } else { 0094 int leftId = left.data(JobModel::RoleJobId).toInt(); 0095 int rightId = right.data(JobModel::RoleJobId).toInt(); 0096 0097 if (leftWeight) { 0098 return leftId < rightId; 0099 } 0100 0101 return leftId > rightId; 0102 } 0103 } 0104 return QSortFilterProxyModel::lessThan(left, right); 0105 } 0106 0107 int JobSortFilterModel::weightForState(int state) const 0108 { 0109 switch (state) { 0110 case IPP_JOB_PROCESSING: 0111 return 3; 0112 break; 0113 case IPP_JOB_PENDING: 0114 return 2; 0115 break; 0116 case IPP_JOB_HELD: 0117 return 1; 0118 break; 0119 default: 0120 return 0; 0121 break; 0122 } 0123 } 0124 0125 #include "moc_JobSortFilterModel.cpp"