#include "WindowAppTreeTool.h"
#include "ToolDepository.h"
#include "DragMimeData.h"
extern ToolDepository toolDepository;
WindowAppTreeTool::WindowAppTreeTool(TREETOOL_MODE mode, QTreeWidget* parent)
: QTreeWidget(parent)
, m_Mode(mode)
{
// 创建界面布局
createLayouts();
// 创建节点的右键菜单
createMenu();
// 加载工具
toolDepository.LoadTools();
// 添加所有工具到树形结构中
initTreeTools();
}
WindowAppTreeTool::~WindowAppTreeTool()
{
}
///
/// 清空工具箱
///
void WindowAppTreeTool::clear()
{
m_RootItems.clear();
m_CateItems.clear();
m_ItemTools.clear();
QTreeWidget::clear();
}
///
/// 创建界面布局
///
void WindowAppTreeTool::createLayouts()
{
// 1列
this->setColumnCount(1);
// 隐藏表头
this->setHeaderHidden(true);
// 显示虚线
this->setStyle(QStyleFactory::create("windows"));
// 添加右键菜单策略
this->setContextMenuPolicy(Qt::CustomContextMenu);
// 设置根节点有连接线
this->setRootIsDecorated(true);
// 设置节点展开
this->setItemsExpandable(true);
//设置树节点允许拖拽(只允许拖出,不允许拖入)
this->setDragEnabled(true);
// 下面这种方式也可以
//this->setDragDropMode(QAbstractItemView::DragOnly);
//拖拽节点的移动
//this->setDefaultDropAction(Qt::MoveAction);
}
///
/// 创建节点的右键菜单
///
void WindowAppTreeTool::createMenu()
{
// 绑定槽函数
// connect(nodeMenus[NODE_GVL], SIGNAL(triggered(QAction*)), this, SLOT(onMenuGVL(QAction*)));
// 节点菜单的Action
actionAdd = new QAction(("Add"), this);
actionInfo = new QAction(("ToolInfo"), this);
m_menuTool = new QMenu(this);
m_menuTool->addAction(actionAdd);
m_menuTool->addAction(actionInfo);
connect(m_menuTool, SIGNAL(triggered(QAction*)), this, SLOT(onMenuAdd(QAction*)));
connect(m_menuTool, SIGNAL(triggered(QAction*)), this, SLOT(onMenuinfo(QAction*)));
// connect(this, SIGNAL(itemClicked(QTreeWidgetItem*, int)), this, SLOT(checkself(QTreeWidgetItem*, int)));
// 右键菜单
connect(this, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(popMenu(const QPoint&)));
}
///
/// 添加所有工具到树形结构中
///
void WindowAppTreeTool::initTreeTools(TREETOOL_MODE mode)
{
clear();
m_Mode = mode;
for (int i = 0; i < toolDepository.m_TotalTools.size(); i++)
{
const TOOL_CATEGORY& cate = toolDepository.m_TotalTools[i];
// 2021-6-8增加,区分Pou和Hardware
if (m_Mode == TREETOOL_MODE::MODE_HARDWARE &&
cate.strName != CATEGORY_TOOL_HARDWARE)
{
this->expandAll();
continue;
}
AddCateItem(cate.strName);
for (int j = 0; j < cate.staticTools.size(); j++)
{
const STATIC_TOOL& tool = cate.staticTools[j];
AddToolItem(cate.strName, tool);
}
}
}
///
/// 添加分类父节点
///
///
void WindowAppTreeTool::AddCateItem(const QString& cateName)
{
QTreeWidgetItem* newItem = new QTreeWidgetItem(this, QStringList(cateName));
newItem->setIcon(0, QIcon(":/image/tree_folder.png"));
// 2021-3-23增加,使父节点不可选中
newItem->setFlags(Qt::ItemIsEnabled);
m_RootItems.append(newItem);
m_CateItems.insert(cateName, newItem);
}
///
/// 添加Tool子节点
///
///
///
void WindowAppTreeTool::AddToolItem(const QString& cateName, const STATIC_TOOL& tool)
{
QTreeWidgetItem* newItem = new QTreeWidgetItem(m_CateItems.value(cateName), QStringList(tool.strName));
newItem->setIcon(0, QIcon(":/image/tree_item.png"));
m_CateItems[cateName]->addChild(newItem);
// 将新加入的item和对应的tool绑定
m_ItemTools.insert(newItem, &tool);
}
///
/// 显示右键菜单
///
///
void WindowAppTreeTool::popMenu(const QPoint&)
{
QTreeWidgetItem* curItem = this->currentItem(); //获取当前被点击的节点
// 点击到空白处则不显示菜单
if (curItem == nullptr)
{
return;
}
m_menuTool->exec(QCursor::pos()); //在当前鼠标位置显示
}
///
/// 右键菜单槽函数 - Add
///
///
void WindowAppTreeTool::onMenuAdd(QAction* action)
{
Q_UNUSED(action);
}
///
/// 右键菜单槽函数 - Info
///
///
void WindowAppTreeTool::onMenuinfo(QAction* action)
{
Q_UNUSED(action);
}
///
/// 鼠标按下时获取用户拖拽的节点
///
///
void WindowAppTreeTool::mousePressEvent(QMouseEvent* event)
{
if (event->button() & Qt::LeftButton)
{
// 获取拖拽的节点
QTreeWidgetItem* dragItem = this->itemAt(event->pos());
// 2021-3-23 如果是父节点,则不允许选中
const STATIC_TOOL* pTool = m_ItemTools.value(dragItem);
if (pTool != nullptr)
{
// 拖动时的图标(实际应该是一个功能块的缩略图)
QPixmap pixmap(":/image/tree_item.png");
// 设置拖拽信息
DragMimeData* mimeData = new DragMimeData();
mimeData->setDragData(FORMAT_DRAG_TOOL, pTool);
// 执行拖动
QDrag* drag = new QDrag(this);
drag->setMimeData(mimeData);
drag->setPixmap(pixmap);
drag->exec(Qt::CopyAction);
}
}
QTreeWidget::mousePressEvent(event);
}