File indexing completed on 2026-10-11 05:40:40

0001 /*
0002     SPDX-FileCopyrightText: 2012 Daniel Nicoletti <dantti12@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "PrinterSortFilterModel.h"
0008 
0009 #include "PrinterModel.h"
0010 #include "kcupslib_log.h"
0011 
0012 PrinterSortFilterModel::PrinterSortFilterModel(QObject *parent)
0013     : QSortFilterProxyModel(parent)
0014 {
0015     setDynamicSortFilter(true);
0016     setSortCaseSensitivity(Qt::CaseInsensitive);
0017     setFilterCaseSensitivity(Qt::CaseInsensitive);
0018     sort(0);
0019 
0020     connect(this, &PrinterSortFilterModel::rowsInserted, this, &PrinterSortFilterModel::countChanged);
0021     connect(this, &PrinterSortFilterModel::rowsRemoved, this, &PrinterSortFilterModel::countChanged);
0022     connect(this, &PrinterSortFilterModel::modelReset, this, &PrinterSortFilterModel::countChanged);
0023 }
0024 
0025 void PrinterSortFilterModel::setModel(QAbstractItemModel *model)
0026 {
0027     if (model == sourceModel()) {
0028         return;
0029     }
0030 
0031     QSortFilterProxyModel::setSourceModel(model);
0032     Q_EMIT sourceModelChanged(model);
0033 }
0034 
0035 void PrinterSortFilterModel::setFilteredPrinters(const QString &printers)
0036 {
0037     qCDebug(LIBKCUPS) << rowCount() << printers << printers.split(QLatin1Char('|'));
0038     if (printers.isEmpty()) {
0039         m_filteredPrinters.clear();
0040     } else {
0041         m_filteredPrinters = printers.split(QLatin1Char('|'));
0042     }
0043     invalidateFilter();
0044     Q_EMIT filteredPrintersChanged();
0045 }
0046 
0047 QString PrinterSortFilterModel::filteredPrinters() const
0048 {
0049     return m_filteredPrinters.join(QLatin1String("|"));
0050 }
0051 
0052 int PrinterSortFilterModel::count() const
0053 {
0054     return rowCount();
0055 }
0056 
0057 bool PrinterSortFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0058 {
0059     QModelIndex index = sourceModel()->index(source_row, 0, source_parent);
0060 
0061     // check if the printer is on the blacklist
0062     if (!m_filteredPrinters.isEmpty()) {
0063         return m_filteredPrinters.contains(index.data(PrinterModel::DestName).toString());
0064     }
0065 
0066     return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
0067 }
0068 
0069 // bool PrinterSortFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
0070 //{
0071 //     bool leftIsRemote = sourceModel()->data(left, PrinterModel::DestRemote).toBool();
0072 //     bool rightIsRemote = sourceModel()->data(right, PrinterModel::DestRemote).toBool();
0073 //     bool leftDefault = sourceModel()->data(left, PrinterModel::DestIsDefault).toBool();
0074 //     bool rightDefault = sourceModel()->data(right, PrinterModel::DestIsDefault).toBool();
0075 
0076 //    if (leftDefault != rightDefault) {
0077 //        return leftDefault;
0078 //    }
0079 
0080 //    if (leftIsRemote != rightIsRemote) {
0081 //        // If the right item is a remote the left should move right
0082 //        return rightIsRemote;
0083 //    }
0084 
0085 //    return QSortFilterProxyModel::lessThan(left, right);
0086 //}
0087 
0088 #include "moc_PrinterSortFilterModel.cpp"