/
erhoof
/
telegraf
Обзор
Документация
Войти
/
erhoof
/
telegraf
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/data/boolfiltermodel.cpp
125 строк
3 KB
Nikolay Sinyov
Добавил все не залитые изменения по форуму
31 авг 2025, 14:20
31 авг 2025, 14:20
edd4b2a
Код
Авторство
О чём код?
#include "boolfiltermodel.h" #include "../debuglog.h" BoolFilterModel::BoolFilterModel(QObject *parent) : QSortFilterProxyModel(parent) { setDynamicSortFilter(true); filterValue = true; } QHash<int, QByteArray> BoolFilterModel::roleNames() const { if(sourceModel()) { return sourceModel()->roleNames(); } return QSortFilterProxyModel::roleNames(); } void BoolFilterModel::setSource(QObject *model) { setSourceModel(qobject_cast<QAbstractItemModel*>(model)); } void BoolFilterModel::setSourceModel(QAbstractItemModel *model) { if (sourceModel() != model) { QSortFilterProxyModel::setSourceModel(model); updateFilterRole(); emit sourceChanged(); } } QString BoolFilterModel::getFilterRoleName() const { return filterRoleName; } void BoolFilterModel::setFilterRoleName(QString role) { if (filterRoleName != role) { filterRoleName = role; updateFilterRole(); emit filterRoleNameChanged(); } } bool BoolFilterModel::getFilterValue() const { return filterValue; } void BoolFilterModel::setFilterValue(bool value) { if(value != filterValue) { filterValue = value; invalidateFilter(); } } int BoolFilterModel::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(); // may still be -1 } int BoolFilterModel::mapRowToSource(int i) { QModelIndex sourceIndex = mapToSource(index(i, 0)); return sourceIndex.row(); } bool BoolFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const { return sourceModel()->index(sourceRow, 0, sourceParent.child(sourceRow, 0)).data(filterRole()).toBool() == filterValue; } int BoolFilterModel::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 BoolFilterModel::updateFilterRole() { const int role = findRole(sourceModel(), filterRoleName); setFilterRole((role >= 0) ? role : Qt::DisplayRole); }