2025-03-17 11:05:48 +08:00

127 lines
3.3 KiB
C++

#include "CDevTreeView.h"
#include "CDevTreeModel.h"
#include <QContextMenuEvent>
#include <QMenu>
CDevTreeView::CDevTreeView(QWidget *parent)
: QTreeView(parent)
{
}
void CDevTreeView::filter(const QString &text)
{
CDevTreeModel * pModel = dynamic_cast<CDevTreeModel *>(model());
if(Q_NULLPTR == pModel)
{
return;
}
for(int nIndex(0); nIndex<pModel->rowCount(); nIndex++)
{
filter(pModel->index(nIndex, 0), pModel, text);
}
}
void CDevTreeView::setShowCheckedOnly(bool checked)
{
CDevTreeModel * pModel = dynamic_cast<CDevTreeModel *>(model());
if(Q_NULLPTR == pModel)
{
return;
}
for(int nIndex(0); nIndex<pModel->rowCount(); nIndex++)
{
setShowCheckedOnly(pModel->index(nIndex, 0), pModel, checked);
}
}
bool CDevTreeView::filter(const QModelIndex &curIndex, CDevTreeModel *pModel, const QString &text)
{
bool isHidden = true;
QString temp = pModel->data(curIndex).toString();
if(text.isEmpty() || temp.contains(text))
{
setAllChildShow(curIndex, pModel);
isHidden = false;
}
else
{
if(pModel->rowCount(curIndex) > 0)
{
for(int nIndex(0); nIndex<pModel->rowCount(curIndex); nIndex++)
{
if(!filter(pModel->index(nIndex, 0, curIndex), pModel, text))
{
isHidden = false;
}
}
}
else
{
isHidden = true;
}
}
setRowHidden(curIndex.row(), pModel->parent(curIndex), isHidden);
return isHidden;
}
bool CDevTreeView::setShowCheckedOnly(const QModelIndex &curIndex, CDevTreeModel *pModel, bool checked)
{
bool isHidden = true;
if(pModel->rowCount(curIndex) > 0)
{
for(int nIndex(0); nIndex<pModel->rowCount(curIndex); nIndex++)
{
if(!setShowCheckedOnly(pModel->index(nIndex, 0, curIndex), pModel, checked))
{
isHidden = false;
}
}
}
else
{
if(checked && pModel->data(curIndex, Qt::CheckStateRole).toInt() == Qt::Unchecked)
{
isHidden = true;
}
else
{
isHidden = false;
}
}
setRowHidden(curIndex.row(), pModel->parent(curIndex), isHidden);
return isHidden;
}
void CDevTreeView::setAllChildShow(const QModelIndex &parent, CDevTreeModel *pModel)
{
int childCount = pModel->rowCount(parent);
for(int nIndex(0); nIndex < childCount; nIndex++)
{
setRowHidden(nIndex, parent, false);
setAllChildShow(pModel->index(nIndex, 0, parent), pModel);
}
}
void CDevTreeView::contextMenuEvent(QContextMenuEvent *event)
{
CDevTreeModel * pModel = dynamic_cast<CDevTreeModel *>(model());
if(Q_NULLPTR != pModel)
{
if(pModel->currentType() == Item_Location_Type)
{
return;
}
QTreeWidgetItem * pItem = static_cast<QTreeWidgetItem*>(indexAt(event->pos()).internalPointer());
if(Q_NULLPTR != pItem)
{
QMenu menu;
menu.addAction(tr("全选"), [=](){ pModel->setChildrenCheckState(indexAt(event->pos()), Qt::Checked); });
menu.addAction(tr("清空"), [=](){ pModel->setChildrenCheckState(indexAt(event->pos()), Qt::Unchecked); });
menu.exec(event->globalPos());
}
}
return;
}