logplus/ModuleConsole/src/PaiModuleTree.cpp
2026-01-16 17:18:41 +08:00

326 lines
9.8 KiB
C++

/**
* @file PaiModuleTree.h
* @brief 模块树
* @author: shiyuerong
* @date: 2011-8-17
*/
#include "PaiModuleTree.h"
#include "ModuleManager.h"
#include "Module.h"
#include "PaiTreeItemDelegate.h"
#include <iostream>
#include <QTreeWidgetItem>
#include <QTreeWidget>
#include <QList>
#include <QStringList>
#include <QDebug>
#include <QApplication>
#include <QIcon>
#include <QStyle>
#include <QMouseEvent>
#include <QMimeData>
#include <QDrag>
#include <QtDebug>
#include <QMenu>
#include <QHeaderView>
#include "GlobalUtility.h"
// #include "PaiDropEventAnalyser.h"
// #include "Log.h"
#include "ModuleInformation.h"
// #include "PaiTypes.h"
#include "GeometryUtils.h"
using namespace std;
using namespace pai;
using namespace pai::gui;
CPaiModuleTreeControl::CPaiModuleTreeControl(QObject* pParent)
:QObject(pParent),ControlExtension()
{
}
void CPaiModuleTreeControl::BuildTreeItemsFromModuleManager(QList<QTreeWidgetItem*>& toplevelTreeItems)
{
pai::module::CModuleManager* pModuleManager = ::pai::module::GetModuleManager();
pModuleManager->LoadAllModules();
QString qstr; //临时变量
std::vector<std::string> vecCategoryNames; //存储类型名
pModuleManager->GetCategoryNames(vecCategoryNames); //获取系统中模块种类名,赋值于vecCategoryNames
for(uint i = 0; i < vecCategoryNames.size(); ++i)
{
//设定各个项
QTreeWidgetItem* categoryItem = new QTreeWidgetItem();
//设置Icon
QIcon icon(::GetImagePath() + "icon/Modules.png");
categoryItem->setIcon(0, icon);
categoryItem->setText(0, QString::fromStdString(vecCategoryNames[i]));
categoryItem->setData(0, pai::gui::TypeRole, QString("ModuleMaster"));
std::vector<pai::module::CModule*> vecSameCategoryMod;
pModuleManager->GetModulesByCategory(vecCategoryNames[i], vecSameCategoryMod);
for(uint j = 0; j < vecSameCategoryMod.size(); ++j)
{
if(vecSameCategoryMod[j] != NULL)
{
QString subcategoryname=QString::fromStdString(vecSameCategoryMod[j]->GetMetaData()->GetSubCategory());
QTreeWidgetItem* subcategoryitem=categoryItem;
if(subcategoryname!="")//需要进行查找
{
for(int kkk=0;kkk<categoryItem->childCount();kkk++)
{
if(categoryItem->child(kkk)->text(0)==subcategoryname)
{
subcategoryitem=categoryItem->child(kkk);
break;
}
}
if(subcategoryitem==categoryItem){
subcategoryitem= new QTreeWidgetItem(categoryItem);
categoryItem->setIcon(0, icon);
subcategoryitem->setText(0, subcategoryname);
}
}
QTreeWidgetItem* modItem = new QTreeWidgetItem(subcategoryitem);
qstr = QString(QString::fromStdString(vecSameCategoryMod[j]->GetMetaData()->GetName().c_str()));
// std::string strIOType = vecSameCategoryMod[j]->GetMetaData()->GetIOType();
modItem->setIcon(0, QIcon( ::GetImagePath() + "icon/ModuleOne.png"));
modItem->setText(0, qstr);
modItem->setData(0,pai::gui::IDRole,QVariant(QString::fromStdString(vecSameCategoryMod[j]->GetClassName())));
modItem->setData(0,pai::gui::HintRole,QVariant(QString::fromStdString(vecSameCategoryMod[j]->GetMetaData()->GetHelpDoc())));
modItem->setData(0,pai::gui::TypeRole, QString("Module"));
}
}
toplevelTreeItems.push_back(categoryItem);
}
}
void CPaiModuleTreeControl::DragItem(QTreeWidgetItem* pCurrentItem)
{
// if(pCurrentItem->parent()==NULL)
// {
// //分类节点不能拖动
// return;
// }
// QMimeData *pMimeData = new QMimeData;
// pai::objectmodel::PaiDropEventAnalyser analyser;
// QByteArray array;
// DraggedData draggedData;
// draggedData.dataSource = DRAG_MODULETREE;
// draggedData.id = pCurrentItem->data(0,pai::gui::IDRole).toString();
// draggedData.dataName = pCurrentItem->data(0,pai::gui::IDRole).toString();
// draggedData.dataType = pCurrentItem->data(0,pai::gui::TypeRole).toString();
// draggedData.reserve = QString("");
// analyser.Serialize(draggedData,array);
// pMimeData->setData(QString::number(DRAG_MODULETREE),array);
// QDrag *pDrag = new QDrag((QWidget*)(this->parent()));
// pDrag->setMimeData(pMimeData);
// pDrag->exec(Qt::CopyAction);
}
std::string CPaiModuleTreeControl::GetStringID() const
{
return "CPaiModuleTreeControl";
}
CPaiModuleTree::CPaiModuleTree(QWidget* parent) :
pai::gui::PaiTreeWidget(parent),
m_control(this)
{
setContextMenuPolicy(Qt::CustomContextMenu);
connect(this, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(RightMenuRequest(const QPoint &)));
connect(this, SIGNAL(itemExpanded(QTreeWidgetItem*)), this, SLOT(changeFolderToOpen(QTreeWidgetItem*)));
connect(this, SIGNAL(itemCollapsed(QTreeWidgetItem*)), this, SLOT(changeFolderToClose(QTreeWidgetItem*)));
// TODO Auto-generated constructor stub
setDragEnabled(true);
setDropIndicatorShown(true);
setItemDelegate(new PaiTreeItemDelegate(this));
this->setColumnCount(1); //设置TreeWidget的列
this->header()->setVisible(false);
QList<QTreeWidgetItem*> lstCategoryItems;
m_control.BuildTreeItemsFromModuleManager(lstCategoryItems);
this->addTopLevelItems(lstCategoryItems);
scenseModuleSelected = false;
}
CPaiModuleTree::~CPaiModuleTree()
{
// TODO Auto-generated destructor stub
}
void CPaiModuleTree::startDrag(Qt::DropActions /*supportedActions*/)
{
m_control.DragItem(currentItem());
}
void CPaiModuleTree::mouseDoubleClickEvent(QMouseEvent *event)
{
// pai::log::Debug(_FLF("double click"));
if (currentItem() == NULL)
return;
if(currentItem()->parent()==NULL)
{
PaiTreeWidget::mouseDoubleClickEvent(event);
return;
}
//仅当双击事件发生在当前选择的item的区域时才发送item双击信号
if (visualItemRect(currentItem()).contains(event->pos()))
{
emit DeliverModule(currentItem()->data(0,pai::gui::IDRole).toString());
}
}
void CPaiModuleTree::MonkeyClickedModule(const QString& strModuleName)
{
emit DeliverModule(strModuleName);
}
void CPaiModuleTree::RequireDeliverSelectedModules()
{
QList<QTreeWidgetItem*> items = selectedItems();
foreach(QTreeWidgetItem *pItem, items)
{
if(pItem->data(0, pai::gui::TypeRole).toString() == QString("Module"))
{
emit DeliverModule(pItem->data(0, pai::gui::IDRole).toString());
}
}
}
void CPaiModuleTree::changeFolderToOpen(QTreeWidgetItem *pItem)
{
if((pItem != NULL) && (pItem->data(0, pai::gui::TypeRole).toString() == QString("ModuleMaster")))
{
QIcon icon(":/project.png");
pItem->setIcon(0, icon);
}
}
void CPaiModuleTree::changeFolderToClose(QTreeWidgetItem *pItem)
{
if((pItem != NULL) && (pItem->data(0, pai::gui::TypeRole).toString() == QString("ModuleMaster")))
{
QIcon icon(":/Folder_Close.png");
pItem->setIcon(0, icon);
}
}
void CPaiModuleTree::RightMenuRequest(const QPoint &point)
{
// QMenu rightMenu;
//
// QTreeWidgetItem* pItem = itemAt(point);
// if (pItem == NULL)
// {
// //树为空时的菜单
// rightMenu.addAction(tr("Expand All"), this, SLOT(expandAll()));
// rightMenu.exec(QCursor::pos());
// }
// else
// {
// if(pItem->parent() != NULL)
// {
// QAction *addAction = rightMenu.addAction(tr("Add"), this, SLOT( menuSelectedSlot() ));
// addAction->setEnabled(true);
// QAction *addBeforeAction = rightMenu.addAction(tr("Add Up"), this, SLOT(menuSelectedSlot()));
// if( scenseModuleSelected )
// {
// addBeforeAction->setEnabled(true);
// }
// else
// {
// addBeforeAction->setEnabled(false);
// }
// QAction *addIntoFlowAction = rightMenu.addAction(tr("Add Down"), this, SLOT(menuSelectedSlot()));
// if( scenseModuleSelected )
// {
// addIntoFlowAction->setEnabled(true);
// }
// else
// {
// addIntoFlowAction->setEnabled(false);
// }
//
// // QAction *addIntoFlowBeforeAction = rightMenu.addAction(tr("Add Into Flow Before"), this, SLOT());
// // addIntoFlowBeforeAction->setEnabled(false);
//
// rightMenu.addSeparator();
// QAction *copyAction = rightMenu.addAction(tr("Copy"), this, SLOT(menuSelectedSlot()));
// copyAction->setEnabled(false);
// rightMenu.addSeparator();
// QAction *helpAction = rightMenu.addAction(tr("Help"), this, SLOT(menuSelectedSlot()));
// helpAction->setEnabled(false);
// rightMenu.exec(QCursor::pos());
// }
// }
//return;
}
void CPaiModuleTree::moduleSelectedSlot(bool selected)
{
scenseModuleSelected = selected;
}
void CPaiModuleTree::menuSelectedSlot()
{
QAction* pAction = dynamic_cast<QAction*> (sender());
if( pAction != NULL)
{
if ( currentItem() == NULL )
return;
if(currentItem()->parent()==NULL)
return;
QString moduleInfo = currentItem()->data(0,pai::gui::IDRole).toString();
QString menuName = pAction->text();
emit menuClicked(menuName , moduleInfo);
}
}
void CPaiModuleTree::selectModules(const QString& strModuleName)
{
clearSelection();
if(strModuleName.isEmpty())
{
return;
}
QList<QTreeWidgetItem*> lstFindItems = findItems(strModuleName, Qt::MatchExactly|Qt::MatchRecursive);
if (lstFindItems.size() <= 0)
{
lstFindItems = findItems(strModuleName, Qt::MatchStartsWith|Qt::MatchRecursive);
if (lstFindItems.size() <= 0)
lstFindItems = findItems(strModuleName, Qt::MatchContains|Qt::MatchRecursive);
}
foreach(QTreeWidgetItem* pFindItem, lstFindItems)
{
scrollToItem(pFindItem);
pFindItem->setSelected(true);
}
}
void CPaiModuleTree::slotSelectModule(pai::workflow::CModuleInformation* pModule)
{
if (pModule == NULL || pModule->IsBlankModule())
{
clearSelection();
return;
}
QString moduleName = QString::fromStdString(pModule->GetModule()->GetMetaData()->GetName());
if (!moduleName.isEmpty())
{
selectModules(moduleName);
}
}