Sandboxie/SandboxiePlus/MiscHelpers/Common/Finder.cpp

296 lines
7.5 KiB
C++
Raw Normal View History

2021-10-15 16:04:52 +01:00
#include "stdafx.h"
#include "Finder.h"
bool CFinder::m_DarkMode = false;
2023-04-22 21:59:42 +01:00
QString CFinder::m_CaseInsensitive = "Case Sensitive";
QString CFinder::m_RegExpStr = "RegExp";
QString CFinder::m_Highlight = "Highlight";
QString CFinder::m_CloseStr = "Close";
QString CFinder::m_FindStr = "&Find ...";
QString CFinder::m_AllColumns = "All columns";
2023-01-12 22:10:50 +00:00
QWidget* CFinder::AddFinder(QTreeView* pTree, QObject* pFilterTarget, int iOptions, CFinder** ppFinder)
2021-10-15 16:04:52 +01:00
{
QWidget* pWidget = new QWidget();
QVBoxLayout* pLayout = new QVBoxLayout();
2022-09-29 17:28:48 +01:00
pLayout->setContentsMargins(0,0,0,0);
2021-10-15 16:04:52 +01:00
pWidget->setLayout(pLayout);
2023-01-12 22:10:50 +00:00
pLayout->addWidget(pTree);
2023-01-08 10:49:09 +00:00
CFinder* pFinder = new CFinder(pFilterTarget, pWidget, iOptions);
2023-01-12 22:10:50 +00:00
pFinder->SetTree(pTree);
2021-10-15 16:04:52 +01:00
pLayout->addWidget(pFinder);
if (ppFinder)
*ppFinder = pFinder;
return pWidget;
}
2023-01-08 10:49:09 +00:00
CFinder::CFinder(QObject* pFilterTarget, QWidget *parent, int iOptions)
2021-10-15 16:04:52 +01:00
:QWidget(parent)
{
m_pSearchLayout = new QHBoxLayout();
2022-09-29 17:28:48 +01:00
m_pSearchLayout->setContentsMargins(0,0,0,0);
2021-10-15 16:04:52 +01:00
m_pSearchLayout->setSpacing(3);
m_pSearchLayout->setAlignment(Qt::AlignLeft);
m_pSearch = new QLineEdit();
2023-07-01 17:54:53 +01:00
m_pSearch->setMinimumWidth(200);
//m_pSearch->setMaximumWidth(400);
2021-10-15 16:04:52 +01:00
m_pSearchLayout->addWidget(m_pSearch);
QObject::connect(m_pSearch, SIGNAL(textChanged(QString)), this, SLOT(OnText()));
QObject::connect(m_pSearch, SIGNAL(returnPressed()), this, SLOT(OnReturn()));
2023-01-08 10:49:09 +00:00
if ((iOptions & eCaseSens) != 0)
{
2023-04-22 21:59:42 +01:00
m_pCaseSensitive = new QCheckBox(m_CaseInsensitive);
2023-01-08 10:49:09 +00:00
m_pSearchLayout->addWidget(m_pCaseSensitive);
connect(m_pCaseSensitive, SIGNAL(stateChanged(int)), this, SLOT(OnUpdate()));
}
else
m_pCaseSensitive = NULL;
2021-10-15 16:04:52 +01:00
2023-01-08 10:49:09 +00:00
if ((iOptions & eRegExp) != 0)
{
2023-04-22 21:59:42 +01:00
m_pRegExp = new QCheckBox(m_RegExpStr);
2023-01-08 10:49:09 +00:00
m_pSearchLayout->addWidget(m_pRegExp);
connect(m_pRegExp, SIGNAL(stateChanged(int)), this, SLOT(OnUpdate()));
}
else
m_pRegExp = NULL;
2021-10-15 16:04:52 +01:00
2023-01-12 22:10:50 +00:00
m_pTree = NULL;
2023-01-13 15:53:53 +00:00
m_pModel = qobject_cast<QAbstractItemModel*>(pFilterTarget);
2021-10-15 16:28:20 +01:00
2023-01-12 22:10:50 +00:00
if (m_pModel) {
2021-10-15 16:28:20 +01:00
m_pColumn = new QComboBox();
m_pSearchLayout->addWidget(m_pColumn);
connect(m_pColumn, SIGNAL(currentIndexChanged(int)), this, SLOT(OnUpdate()));
m_pColumn->setVisible(false);
}
else
m_pColumn = NULL;
2021-10-15 16:04:52 +01:00
2023-01-08 10:49:09 +00:00
if ((iOptions & eHighLight) != 0)
2021-10-15 16:04:52 +01:00
{
2023-04-22 21:59:42 +01:00
m_pHighLight = new QCheckBox(m_Highlight);
2023-01-12 22:10:50 +00:00
if ((iOptions & eHighLightDefault) == eHighLightDefault)
m_pHighLight->setChecked(true);
2021-10-15 16:04:52 +01:00
m_pSearchLayout->addWidget(m_pHighLight);
connect(m_pHighLight, SIGNAL(stateChanged(int)), this, SLOT(OnUpdate()));
}
else
m_pHighLight = NULL;
QToolButton* pClose = new QToolButton(this);
pClose->setIcon(QIcon(":/close.png"));
pClose->setAutoRaise(true);
2023-04-22 21:59:42 +01:00
pClose->setText(m_CloseStr);
2021-10-15 16:04:52 +01:00
m_pSearchLayout->addWidget(pClose);
QObject::connect(pClose, SIGNAL(clicked()), this, SLOT(Close()));
QWidget* pSpacer = new QWidget();
pSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
m_pSearchLayout->addWidget(pSpacer);
setLayout(m_pSearchLayout);
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
//setMaximumHeight(30);
hide();
if (parent)
{
2023-04-22 21:59:42 +01:00
QAction* pFind = new QAction(m_FindStr, parent);
2021-10-15 16:04:52 +01:00
pFind->setShortcut(QKeySequence::Find);
pFind->setShortcutContext(Qt::WidgetWithChildrenShortcut);
parent->addAction(pFind);
QObject::connect(pFind, SIGNAL(triggered()), this, SLOT(Open()));
}
if (pFilterTarget) {
2023-01-08 10:49:09 +00:00
QObject::connect(this, SIGNAL(SetFilter(const QString&, int, int)), pFilterTarget, SLOT(SetFilter(const QString&, int, int)));
2023-01-12 22:10:50 +00:00
//QObject::connect(this, SIGNAL(SelectNext()), pFilterTarget, SLOT(SelectNext()));
2021-10-15 16:04:52 +01:00
}
m_pTimer = new QTimer(this);
m_pTimer->setSingleShot(true);
m_pTimer->setInterval(500);
connect(m_pTimer, SIGNAL(timeout()), SLOT(OnUpdate()));
this->installEventFilter(this);
}
CFinder::~CFinder()
{
}
2023-01-12 22:10:50 +00:00
void CFinder::SetTree(QTreeView* pTree)
{
m_pTree = pTree;
2023-02-04 13:42:45 +00:00
QObject::connect(this, SIGNAL(SelectNext()), this, SLOT(OnSelectNext()));
2023-01-12 22:10:50 +00:00
}
2021-10-15 16:04:52 +01:00
bool CFinder::eventFilter(QObject* source, QEvent* event)
{
if (event->type() == QEvent::KeyPress && ((QKeyEvent*)event)->key() == Qt::Key_Escape
&& ((QKeyEvent*)event)->modifiers() == Qt::NoModifier)
{
Close();
return true; // cancel event
}
return QWidget::eventFilter(source, event);
}
void CFinder::Open()
{
2023-02-04 13:42:45 +00:00
if (m_pColumn && m_pColumn->count() == 0)
2021-10-15 16:04:52 +01:00
{
2023-04-22 21:59:42 +01:00
m_pColumn->addItem(m_AllColumns, -1);
2023-01-12 22:10:50 +00:00
for (int i = 0; i < m_pModel->columnCount(); i++)
m_pColumn->addItem(m_pModel->headerData(i, Qt::Horizontal, Qt::DisplayRole).toString(), i);
2021-10-15 16:04:52 +01:00
m_pColumn->setVisible(true);
}
show();
m_pSearch->setFocus(Qt::OtherFocusReason);
m_pSearch->selectAll();
OnUpdate();
}
void CFinder::OnUpdate()
{
m_pTimer->stop();
2023-01-08 10:49:09 +00:00
if (!isVisible() || m_pSearch->text().isEmpty())
SetFilter(QString(), 0, GetColumn());
int iOptions = 0;
if (GetRegExp())
iOptions |= eRegExp;
if (GetCaseSensitive())
iOptions |= eCaseSens;
if (GetHighLight())
iOptions |= eHighLight;
2023-01-12 22:10:50 +00:00
QString Exp = m_pSearch->text();
2023-03-12 15:36:22 +00:00
QString ExpStr = ((iOptions & CFinder::eRegExp) != 0) ? Exp : (".*" + QRegularExpression::escape(Exp).replace("\\","\\\\") + ".*");
2023-01-12 22:10:50 +00:00
m_RegExp = QRegularExpression(ExpStr, (iOptions & CFinder::eCaseSens) != 0 ? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption);
SetFilter(Exp, iOptions, GetColumn());
2021-10-15 16:04:52 +01:00
}
void CFinder::OnText()
{
m_pTimer->stop();
m_pTimer->start();
}
void CFinder::OnReturn()
{
OnUpdate();
2023-01-13 15:53:53 +00:00
if (!m_pHighLight || m_pHighLight->isChecked())
2021-10-15 16:04:52 +01:00
emit SelectNext();
}
void CFinder::Close()
{
2023-01-08 10:49:09 +00:00
emit SetFilter(QString());
2021-10-15 16:04:52 +01:00
hide();
2023-01-12 22:10:50 +00:00
}
////////////////////////////////////////////////////////////////
//
bool CFinder::MatchString(const QString& value)
{
return value.contains(m_RegExp);
}
bool CFinder::MatchCell(QModelIndex idx, int column)
{
QModelIndex tmp = idx.sibling(idx.row(), column);
QString str = m_pModel->data(tmp, Qt::DisplayRole).toString();
return MatchString(str);
}
bool CFinder::MatchRow(QModelIndex idx)
{
int iColumn = GetColumn();
if (iColumn != -1)
return MatchCell(idx, iColumn);
for(int col = 0; col < m_pModel->columnCount(idx); col++) {
if (MatchCell(idx, col))
return true;
}
return false;
}
2023-02-05 17:33:18 +00:00
QModelIndex CFinder::FindRow(QModelIndex par, int start, bool reverse)
2023-01-12 22:10:50 +00:00
{
2023-02-05 17:33:18 +00:00
int numRows = m_pModel->rowCount(par);
for (int row = start; row < numRows && row >= 0; row += (reverse ? -1 : 1)) {
QModelIndex cur = m_pModel->index(row, 0, par);
if (MatchRow(cur))
return cur;
if (m_pModel->hasChildren(cur)) {
QModelIndex child = FindRow(cur, reverse ? m_pModel->rowCount(cur) - 1 : 0, reverse);
if (child.isValid())
return child;
2023-01-12 22:10:50 +00:00
}
}
return QModelIndex();
}
2023-02-05 17:33:18 +00:00
QModelIndex CFinder::FindRow(bool reverse)
2023-01-12 22:10:50 +00:00
{
2023-02-05 17:33:18 +00:00
bool next = true;
QModelIndex idx = m_pTree->currentIndex();
if (!(next = idx.isValid()))
idx = m_pModel->index(0, 0);
2023-01-12 22:10:50 +00:00
2023-02-05 17:33:18 +00:00
next_sibling:
if (idx.isValid() && m_pModel->hasChildren(idx)) {
QModelIndex child = FindRow(idx, reverse ? m_pModel->rowCount(idx) - 1 : 0, reverse);
if (child.isValid())
return child;
2023-01-12 22:10:50 +00:00
}
2023-02-11 11:58:34 +00:00
QModelIndex cur = FindRow(m_pModel->parent(idx), idx.row() + (next ? (reverse ? -1 : 1) : 0), reverse);
if (cur.isValid())
return cur;
2023-01-12 22:10:50 +00:00
2023-02-05 17:33:18 +00:00
next_parent:
QModelIndex parent = m_pModel->parent(idx);
if (parent.isValid()) {
QModelIndex sibling = parent.siblingAtRow(parent.row() + (reverse ? -1 : 1));
if (sibling.isValid()) {
idx = sibling;
next = false;
goto next_sibling;
} else {
idx = parent;
goto next_parent;
}
2023-01-12 22:10:50 +00:00
}
return QModelIndex();
}
void CFinder::OnSelectNext()
{
2023-02-04 13:42:45 +00:00
if (!m_pModel)
return;
2023-02-05 17:33:18 +00:00
QModelIndex idx = FindRow(QApplication::keyboardModifiers() & Qt::ShiftModifier);
2023-01-12 22:10:50 +00:00
if (idx.isValid())
m_pTree->setCurrentIndex(idx);
else
QApplication::beep();
2020-06-01 17:11:56 +01:00
}