#include "DialogGotoSelection.h"
#include "Pou.h"
DialogGotoSelection::DialogGotoSelection(const POU* pou, QWidget *parent)
: QDialog(parent)
, m_pou(pou)
{
ui.setupUi(this);
// 对话框初始化
this->initUI();
}
DialogGotoSelection::~DialogGotoSelection()
{
}
///
/// 对话框初始化
///
void DialogGotoSelection::initUI()
{
// 设置对话框标题
this->setWindowTitle(("Goto Tools Selection"));
this->setAttribute(Qt::WA_QuitOnClose);
this->setWindowModality(Qt::ApplicationModal);
// 添加分组信息(目前就固定一个分组)
ui.comboValueGroup->addItem(m_pou->pouName());
ui.comboValueGroup->setCurrentIndex(0);
this->onComboGroupChanged(0);
// Tree
// 1列
ui.treeTool->setColumnCount(1);
// 隐藏表头
ui.treeTool->setHeaderHidden(true);
// 显示虚线
ui.treeTool->setStyle(QStyleFactory::create("windows"));
// 槽函数
connect(ui.selectButton, SIGNAL(clicked()), this, SLOT(onButtonSelectClicked()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
connect(ui.comboValueGroup, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboGroupChanged(int)));
// 设置固定窗体大小
this->setFixedSize(485, 490);
}
///
/// 添加兼容的Pou工具到树形结构中
///
///
void DialogGotoSelection::AddPouToolsToTree(const POU* pPou)
{
// 添加根节点
QTreeWidgetItem* pRootItem = AddRootTreeItem("Tools");
// 添加适配的工具
const QVector& allTools = pPou->GetAllStandardTools();
QVectorIterator i(allTools);
while (i.hasNext())
{
const TOOL* pTool = i.next();
// 此处只要最标准的Tool
if (pTool->isStandardTool())
{
// 添加工具到列表中
this->AddToolTreeItem(pRootItem, pTool);
}
}
// 将树形结构展开
ui.treeTool->expandAll();
}
///
/// // 添加根节点
///
///
///
QTreeWidgetItem* DialogGotoSelection::AddRootTreeItem(const QString& toolName)
{
QTreeWidgetItem* newItem = new QTreeWidgetItem(QStringList(toolName));
newItem->setIcon(0, QIcon(":/image/tool_tree_item.png"));
// 使父节点不可选中
newItem->setFlags(Qt::ItemIsEnabled);
ui.treeTool->addTopLevelItem(newItem);
return newItem;
}
///
/// 添加Tool子节点
///
///
///
void DialogGotoSelection::AddToolTreeItem(QTreeWidgetItem* pRootItem, const TOOL* pTool)
{
QTreeWidgetItem* newItem = new QTreeWidgetItem(pRootItem, QStringList(pTool->strInstanceName));
newItem->setIcon(0, QIcon(":/image/Input.png"));
pRootItem->addChild(newItem);
}
///
/// select按钮
///
void DialogGotoSelection::onButtonSelectClicked()
{
// 获取用户选择的节点
QList selItems = ui.treeTool->selectedItems();
if (selItems.size() <= 0)
{
// 如果选择的节点无效,则不允许关闭
Utility::VPCriticalMessageBox(" The tool selected is invalid!");
return;
}
// 获取用户选择的Tool
m_selToolInstName = selItems[0]->text(0);
this->accept();
}
///
/// Combo框选择变更时,需要切换对应的工具列表(当前仅一个分组)
///
///
void DialogGotoSelection::onComboGroupChanged(int nIndex)
{
Q_UNUSED(nIndex);
// 先将当前的树形控件清空
ui.treeTool->clear();
// 重新添加节点和数据结构
AddPouToolsToTree(m_pou);
}