同步操作将从 无脚鸟/PointLabProjects 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "TreeModel.h"#include "PointLabCore/IPointCloud.h"#include <osg/Group>#include <QIcon>#include <cassert>#include <QHeaderView>TreeModel::TreeModel(QTreeView* view, QObject* parent) :QAbstractItemModel(parent){mTreeViewModelManager = view;mTreeViewModelManager->setModel(this);mTreeViewModelManager->header()->hide();mTreeViewModelManager->setDragEnabled(true);mTreeViewModelManager->setAcceptDrops(true);mTreeViewModelManager->setDropIndicatorShown(true);mDeleteSelectedEntities = new QAction(QString(QStringLiteral("ɾ")), this);connect(mDeleteSelectedEntities, &QAction::triggered, this, &TreeModel::deleteSelectedEntities);connect(mTreeViewModelManager->selectionModel(), &QItemSelectionModel::selectionChanged, this, &TreeModel::changeSelection);connect(mTreeViewModelManager, &QTreeView::customContextMenuRequested, this, &TreeModel::showContextMenu);}TreeModel::~TreeModel(){}void TreeModel::setRootNode(osg::Node *node){beginResetModel();mRootNode = node;endResetModel();}osg::Node* TreeModel::getRootNode(){return mRootNode;}void TreeModel::addElement(osg::Node* node, bool autoExpand /*= true*/){if (!node){return;}osg::Group* parentNode;if (node->getNumParents() > 0){parentNode = node->getParent(0);}else{parentNode = mRootNode->asGroup();mRootNode->asGroup()->addChild(node);}QModelIndex insertNodeIndex = index(parentNode);int childPos = parentNode->getChildIndex(node);beginInsertRows(insertNodeIndex, childPos, childPos);endInsertRows();if (autoExpand){mTreeViewModelManager->expand(index(parentNode));mTreeViewModelManager->expand(index(node));}else{mTreeViewModelManager->expand(insertNodeIndex);}}void TreeModel::removeElement(osg::Node* node){if (!node){return;}osg::Group* parentNode = node->getParent(0);if (!parentNode){return;}int childPos = parentNode->getChildIndex(node);assert(childPos > 0);{beginRemoveRows(index(parentNode), childPos, childPos);parentNode->removeChild(node);endRemoveRows();}}QModelIndex TreeModel::index(int row, int column, const QModelIndex &parent /*= QModelIndex()*/) const{if (!hasIndex(row, column, parent))return QModelIndex();osg::Node *node = (parent.isValid() ? static_cast<osg::Node*>(parent.internalPointer()) : mRootNode);if (!node)return QModelIndex();if (!node->asGroup()){return QModelIndex();}osg::Node *childNode = node->asGroup()->getChild(row);if (childNode)return createIndex(row, column, childNode);elsereturn QModelIndex();}QModelIndex TreeModel::index(osg::Node* node){if (node == mRootNode){return QModelIndex();}osg::Group* parentNode = node->getParent(0);if (!parentNode){return QModelIndex();}int pos = parentNode->getChildIndex(node);return createIndex(pos, 0, node);}QModelIndex TreeModel::parent(const QModelIndex &child) const{if (!child.isValid())return QModelIndex();osg::Node *childNode = static_cast<osg::Node*>(child.internalPointer());if (!childNode)return QModelIndex();osg::Node*parentNode = childNode->getParent(0);if (!parentNode || parentNode == mRootNode)return QModelIndex();osg::Group* parentParentNode = parentNode->getParent(0);return createIndex(parentParentNode ? parentParentNode->getChildIndex(parentNode) : -1, 0, parentNode);}int TreeModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const{osg::Node* parentNode = 0;if (!parent.isValid())parentNode = mRootNode;elseparentNode = static_cast<osg::Node*>(parent.internalPointer());if (parentNode&&parentNode->asGroup())return parentNode->asGroup()->getNumChildren();elsereturn 0;}int TreeModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const{return 1;}QVariant TreeModel::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const{if (!index.isValid())return QVariant();const osg::Node *node = static_cast<osg::Node*>(index.internalPointer());if (!node)return QVariant();if (role == Qt::DisplayRole){QString baseName = QString::fromLocal8Bit(node->getName().c_str());if (baseName.isEmpty())baseName = QStringLiteral("no name");//specific casereturn QVariant(baseName);}if (role == Qt::EditRole){QString baseName = QString::fromLocal8Bit(node->getName().c_str());return QVariant(baseName);}else if (role == Qt::DecorationRole){return QIcon();}else if (role == Qt::CheckStateRole){return (node->getNodeMask() == 0 ? Qt::Unchecked : Qt::Checked);}return QVariant();}bool TreeModel::setData(const QModelIndex &index, const QVariant &value, int role /*= Qt::EditRole*/){if (index.isValid()){if (role == Qt::EditRole){if (value.toString().isEmpty())return false;osg::Node *item = static_cast<osg::Node*>(index.internalPointer());assert(item);if (item){item->setName(value.toString().toLocal8Bit().data());emit dataChanged(index, index);}return true;}else if (role == Qt::CheckStateRole){osg::Node *item = static_cast<osg::Node*>(index.internalPointer());assert(item);if (item){if (value == Qt::Checked)item->setNodeMask(true);elseitem->setNodeMask(false);}return true;}}return false;}Qt::ItemFlags TreeModel::flags(const QModelIndex &index) const{if (!index.isValid())return 0;Qt::ItemFlags defaultFlags = QAbstractItemModel::flags(index);//common flagsdefaultFlags |= (Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable);return defaultFlags;}void TreeModel::changeSelection(const QItemSelection & selected, const QItemSelection & deselected){//first unselectQModelIndexList deselectedItems = deselected.indexes();{for (int i = 0; i < deselectedItems.count(); ++i){osg::Node* element = static_cast<osg::Node*>(deselectedItems.at(i).internalPointer());assert(element);if (element){}}}//then selectQModelIndexList selectedItems = selected.indexes();{for (int i = 0; i < selectedItems.count(); ++i){osg::Node* element = static_cast<osg::Node*>(selectedItems.at(i).internalPointer());assert(element);if (element){}}}emit selectionChanged();}PointLabCore::Uint TreeModel::getSelectedEntities(std::vector<osg::Node*>& selectedEntities){selectedEntities.clear();QItemSelectionModel* qism = mTreeViewModelManager->selectionModel();QModelIndexList selectedIndexes = qism->selectedIndexes();try{int selCount = selectedIndexes.size();for (int i = 0; i < selCount; ++i){osg::Node* node = static_cast<osg::Node*>(selectedIndexes[i].internalPointer());if (node){selectedEntities.push_back(node);}}}catch (const std::bad_alloc&){//not enough memory!}return selectedEntities.size();}void TreeModel::showContextMenu(const QPoint& menuPos){QMenu menu;QModelIndex index = mTreeViewModelManager->indexAt(menuPos);if (index.isValid()){QItemSelectionModel* qism = mTreeViewModelManager->selectionModel();QModelIndexList selectedIndexes = qism->selectedIndexes();int selCount = selectedIndexes.size();if (selCount){for (int i = 0; i < selCount; ++i){osg::Node* item = static_cast<osg::Node*>(selectedIndexes[i].internalPointer());if (!item){assert(false);continue;}}menu.addAction(mDeleteSelectedEntities);menu.exec(mTreeViewModelManager->mapToGlobal(menuPos));}}}void TreeModel::deleteSelectedEntities(){QItemSelectionModel* qism = mTreeViewModelManager->selectionModel();QModelIndexList selectedIndexes = qism->selectedIndexes();if (selectedIndexes.size() < 1)return;unsigned selCount = static_cast<unsigned>(selectedIndexes.size());bool verticesWarningIssued = false;std::vector<osg::Node*> toBeDeleted;for (unsigned i = 0; i < selCount; ++i){osg::Node* node = static_cast<osg::Node*>(selectedIndexes[i].internalPointer());if (node){toBeDeleted.push_back(node);}}qism->clear();while (!toBeDeleted.empty()){osg::Node* node = toBeDeleted.back();assert(node);toBeDeleted.pop_back();osg::Group* parentNode = node->getParent(0);if (!parentNode){return;}int childPos = parentNode->getChildIndex(node);assert(childPos >= 0);{beginRemoveRows(index(parentNode), childPos, childPos);parentNode->removeChild(node);endRemoveRows();}}}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。