/
lmaxyz
/
telegraf
Обзор
Документация
Войти
/
lmaxyz
/
telegraf
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/data/rolefiltermodel.cpp
151 строка
4 KB
Nikolay Sinyov
Добавил все не залитые изменения по форуму
31 авг 2025, 14:20
31 авг 2025, 14:20
edd4b2a
Код
Авторство
О чём код?
#include "rolefiltermodel.h" #include "../debuglog.h" RoleFilterModel::RoleFilterModel(QObject *parent) : QSortFilterProxyModel(parent), m_comparisonType(BoolEquals) { setDynamicSortFilter(true); } void RoleFilterModel::setSource(QObject *model) { setSourceModel(qobject_cast<QAbstractItemModel*>(model)); } void RoleFilterModel::setSourceModel(QAbstractItemModel *model) { if (sourceModel() != model) { QSortFilterProxyModel::setSourceModel(model); updateFilterRole(); emit sourceChanged(); } } QString RoleFilterModel::filterRoleName() const { return m_filterRoleName; } void RoleFilterModel::setFilterRoleName(QString role) { if (m_filterRoleName != role) { m_filterRoleName = role; updateFilterRole(); emit filterRoleNameChanged(); } } QVariant RoleFilterModel::filterValue() const { return m_filterValue; } void RoleFilterModel::setFilterValue(QVariant value) { if (value.isNull() || !value.isValid()) { qWarning() << "Invalid filter value!"; return; } if(m_filterValue != value) { m_filterValue = value; invalidateFilter(); emit filterValueChanged(); } } RoleFilterModel::ComparisonType RoleFilterModel::comparisonType() const { return m_comparisonType; } void RoleFilterModel::setComparisonType(ComparisonType type) { if(m_comparisonType != type) { m_comparisonType = type; invalidateFilter(); emit comparisonTypeChanged(); } } int RoleFilterModel::mapRowFromSource(int i, int fallbackDirection) { QModelIndex myIndex = mapFromSource(sourceModel()->index(i, 0)); if(myIndex.isValid()) { return myIndex.row(); } if(fallbackDirection > 0) { int max = sourceModel()->rowCount(); i += 1; while (i < max) { myIndex = mapFromSource(sourceModel()->index(i, 0)); if(myIndex.isValid()) { return myIndex.row(); } i += 1; } } else if(fallbackDirection < 0) { i -= 1; while (i > -1) { myIndex = mapFromSource(sourceModel()->index(i, 0)); if(myIndex.isValid()) { return myIndex.row(); } i -= 1; } } return myIndex.row(); } int RoleFilterModel::mapRowToSource(int i) { QModelIndex sourceIndex = mapToSource(index(i, 0)); return sourceIndex.row(); } bool RoleFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { QModelIndex index = sourceModel()->index(sourceRow, 0, sourceParent); QVariant value = index.data(m_filterRole); return compareValues(value); } bool RoleFilterModel::compareValues(const QVariant &sourceValue) const { switch(m_comparisonType) { case BoolEquals: return sourceValue.toBool() == m_filterValue.toBool(); case StringEquals: return sourceValue.toString() == m_filterValue.toString(); case IntEquals: return sourceValue.toLongLong() == m_filterValue.toLongLong(); } return false; } int RoleFilterModel::findRole(QAbstractItemModel *model, QString role) { if (model && !role.isEmpty()) { const QByteArray roleName(role.toUtf8()); const QHash<int,QByteArray> roleMap(model->roleNames()); const QList<int> roles(roleMap.keys()); const int n = roles.count(); for (int i = 0; i < n; i++) { const QByteArray name(roleMap.value(roles.at(i))); if (name == roleName) { return roles.at(i); } } } return -1; } void RoleFilterModel::updateFilterRole() { const int role = findRole(sourceModel(), m_filterRoleName); m_filterRole = (role >= 0) ? role : Qt::DisplayRole; invalidateFilter(); }