Sandboxie/SandboxiePlus/MiscHelpers/Common/SortFilterProxyModel.h

96 lines
2.5 KiB
C
Raw Normal View History

2021-10-15 16:04:52 +01:00
#pragma once
2020-06-01 17:11:56 +01:00
#include "../mischelpers_global.h"
2021-02-14 19:18:29 +00:00
#include <QSortFilterProxyModel>
#include <QTreeView>
2021-10-13 11:24:12 +01:00
#include "Finder.h"
2020-06-01 17:11:56 +01:00
2021-10-15 16:04:52 +01:00
class MISCHELPERS_EXPORT CSortFilterProxyModel: public QSortFilterProxyModel
{
Q_OBJECT
public:
2022-12-07 16:32:40 +00:00
CSortFilterProxyModel(QObject* parent = 0) : QSortFilterProxyModel(parent)
2021-10-15 16:04:52 +01:00
{
m_bHighLight = false;
m_iColumn = 0;
this->setSortCaseSensitivity(Qt::CaseInsensitive);
}
bool filterAcceptsRow(int source_row, const QModelIndex & source_parent) const
{
if (m_bHighLight)
return true;
// allow the item to pass if any of the child items pass
2022-09-29 17:28:48 +01:00
if(filterRegularExpression().isValid())
2021-10-15 16:04:52 +01:00
{
// get source-model index for current row
QModelIndex source_index = sourceModel()->index(source_row, 0, source_parent);
if(source_index.isValid())
{
// if any of children matches the filter, then current index matches the filter as well
int nb = sourceModel()->rowCount(source_index);
for(int i = 0; i < nb; i++)
{
if(filterAcceptsRow(i, source_index))
return true;
}
// check current index itself
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
}
2022-12-17 20:06:15 +00:00
// default behaviour
2021-10-15 16:04:52 +01:00
return QSortFilterProxyModel::filterAcceptsRow(source_row, source_parent);
}
QVariant data(const QModelIndex &index, int role) const
{
QVariant Data = QSortFilterProxyModel::data(index, role);
if (m_bHighLight && role == (CFinder::GetDarkMode() ? Qt::ForegroundRole : Qt::BackgroundRole))
{
2022-09-29 17:28:48 +01:00
if (filterRegularExpression().isValid())
2021-10-15 16:04:52 +01:00
{
QString Key = QSortFilterProxyModel::data(index, filterRole()).toString();
2022-09-29 17:28:48 +01:00
if (Key.contains(filterRegularExpression()))
2021-10-15 16:04:52 +01:00
return QColor(Qt::yellow);
}
//return QColor(Qt::white);
}
2022-07-29 09:24:32 +01:00
//if (role == Qt::BackgroundRole)
//{
// if (m_bAlternate && !Data.isValid())
// {
// if (0 == index.row() % 2)
// return QColor(226, 237, 253);
// else
// return QColor(Qt::white);
// }
//}
2021-10-15 16:04:52 +01:00
return Data;
}
public slots:
2023-01-08 10:49:09 +00:00
void SetFilter(const QString& Exp, int iOptions, int Col = -1) // -1 = any
2021-10-15 16:04:52 +01:00
{
2023-01-08 10:49:09 +00:00
QString ExpStr = ((iOptions & CFinder::eRegExp) == 0) ? Exp : (".*" + QRegularExpression::escape(Exp) + ".*");
QRegularExpression RegExp(ExpStr, (iOptions & CFinder::eCaseSens) != 0 ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption);
2021-10-15 16:04:52 +01:00
QModelIndex idx;
m_iColumn = Col;
2023-01-08 10:49:09 +00:00
m_bHighLight = (iOptions & CFinder::eHighLight) != 0;
2021-10-15 16:04:52 +01:00
setFilterKeyColumn(Col);
2023-01-08 10:49:09 +00:00
setFilterRegularExpression(RegExp);
2021-10-15 16:04:52 +01:00
if (m_bHighLight)
emit layoutChanged();
}
protected:
bool m_bHighLight;
int m_iColumn;
2022-12-07 16:32:40 +00:00
};
2023-01-12 22:10:50 +00:00