#include "CDevTreeView.h" #include "CDevTreeModel.h" #include #include CDevTreeView::CDevTreeView(QWidget *parent) : QTreeView(parent) { } void CDevTreeView::filter(const QString &text) { CDevTreeModel * pModel = dynamic_cast(model()); if(Q_NULLPTR == pModel) { return; } for(int nIndex(0); nIndexrowCount(); nIndex++) { filter(pModel->index(nIndex, 0), pModel, text); } } void CDevTreeView::setShowCheckedOnly(bool checked) { CDevTreeModel * pModel = dynamic_cast(model()); if(Q_NULLPTR == pModel) { return; } for(int nIndex(0); nIndexrowCount(); 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); nIndexrowCount(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); nIndexrowCount(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(model()); if(Q_NULLPTR != pModel) { if(pModel->currentType() == Item_Location_Type) { return; } QTreeWidgetItem * pItem = static_cast(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; }