Warning, file /system/kjournald/lib/fieldfilterproxymodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 SPDX-License-Identifier: LGPL-2.1-or-later OR MIT 0003 SPDX-FileCopyrightText: 2021 Andreas Cord-Landwehr <cordlandwehr@kde.org> 0004 */ 0005 0006 #include "fieldfilterproxymodel.h" 0007 #include "journaldviewmodel.h" 0008 #include "kjournaldlib_log_general.h" 0009 #include <QDebug> 0010 #include <QtQml> 0011 0012 FieldFilterProxyModel::FieldFilterProxyModel(QObject *parent) 0013 : QSortFilterProxyModel(parent) 0014 , mComplete(false) 0015 , mFilterRole{JournaldViewModel::Roles::SYSTEMD_UNIT} 0016 { 0017 connect(this, &QSortFilterProxyModel::rowsInserted, this, &FieldFilterProxyModel::countChanged); 0018 connect(this, &QSortFilterProxyModel::rowsRemoved, this, &FieldFilterProxyModel::countChanged); 0019 } 0020 0021 void FieldFilterProxyModel::setField(const QString &field) 0022 { 0023 JournaldViewModel::Roles role = mFilterRole; 0024 if (field == QLatin1String("_SYSTEMD_UNIT")) { 0025 role = JournaldViewModel::Roles::SYSTEMD_UNIT; 0026 } else if (field == QLatin1String("MESSAGE")) { 0027 role = JournaldViewModel::Roles::MESSAGE; 0028 } else if (field == QLatin1String("PRIORITY")) { 0029 role = JournaldViewModel::Roles::PRIORITY; 0030 } else if (field == QLatin1String("_BOOT_ID")) { 0031 role = JournaldViewModel::Roles::BOOT_ID; 0032 } else if (field == QLatin1String("DATE")) { 0033 role = JournaldViewModel::Roles::DATE; 0034 } 0035 if (role == mFilterRole) { 0036 // nothing to do 0037 return; 0038 } 0039 0040 mFilterRole = role; 0041 if (mComplete) { 0042 QSortFilterProxyModel::setFilterRole(mFilterRole); 0043 } 0044 } 0045 0046 QString FieldFilterProxyModel::filterString() const 0047 { 0048 return mFilter; 0049 } 0050 0051 void FieldFilterProxyModel::setFilterString(const QString &filter) 0052 { 0053 mFilter = filter; 0054 setFilterFixedString(filter); 0055 } 0056 0057 QJSValue FieldFilterProxyModel::get(int idx) const 0058 { 0059 QJSEngine *engine = qmlEngine(this); 0060 QJSValue value = engine->newObject(); 0061 if (idx >= 0 && idx < rowCount()) { 0062 QHash<int, QByteArray> roles = roleNames(); 0063 QHashIterator<int, QByteArray> it(roles); 0064 while (it.hasNext()) { 0065 it.next(); 0066 value.setProperty(QString::fromUtf8(it.value()), data(index(idx, 0), it.key()).toString()); 0067 } 0068 } 0069 return value; 0070 } 0071 0072 void FieldFilterProxyModel::classBegin() 0073 { 0074 } 0075 0076 void FieldFilterProxyModel::componentComplete() 0077 { 0078 mComplete = true; 0079 QSortFilterProxyModel::setFilterRole(mFilterRole); 0080 } 0081 0082 int FieldFilterProxyModel::roleKey(const QByteArray &role) const 0083 { 0084 QHash<int, QByteArray> roles = roleNames(); 0085 QHashIterator<int, QByteArray> it(roles); 0086 while (it.hasNext()) { 0087 it.next(); 0088 if (it.value() == role) 0089 return it.key(); 0090 } 0091 return -1; 0092 } 0093 0094 QHash<int, QByteArray> FieldFilterProxyModel::roleNames() const 0095 { 0096 if (QAbstractItemModel *source = sourceModel()) { 0097 return source->roleNames(); 0098 } 0099 return QHash<int, QByteArray>(); 0100 }