DialogGotoSelection.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "DialogGotoSelection.h"
  2. #include "Pou.h"
  3. DialogGotoSelection::DialogGotoSelection(const POU* pou, QWidget *parent)
  4. : QDialog(parent)
  5. , m_pou(pou)
  6. {
  7. ui.setupUi(this);
  8. // 对话框初始化
  9. this->initUI();
  10. }
  11. DialogGotoSelection::~DialogGotoSelection()
  12. {
  13. }
  14. /// <summary>
  15. /// 对话框初始化
  16. /// </summary>
  17. void DialogGotoSelection::initUI()
  18. {
  19. // 设置对话框标题
  20. this->setWindowTitle(("Goto Tools Selection"));
  21. this->setAttribute(Qt::WA_QuitOnClose);
  22. this->setWindowModality(Qt::ApplicationModal);
  23. // 添加分组信息(目前就固定一个分组)
  24. ui.comboValueGroup->addItem(m_pou->pouName());
  25. ui.comboValueGroup->setCurrentIndex(0);
  26. this->onComboGroupChanged(0);
  27. // Tree
  28. // 1列
  29. ui.treeTool->setColumnCount(1);
  30. // 隐藏表头
  31. ui.treeTool->setHeaderHidden(true);
  32. // 显示虚线
  33. ui.treeTool->setStyle(QStyleFactory::create("windows"));
  34. // 槽函数
  35. connect(ui.selectButton, SIGNAL(clicked()), this, SLOT(onButtonSelectClicked()));
  36. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
  37. connect(ui.comboValueGroup, SIGNAL(currentIndexChanged(int)), this, SLOT(onComboGroupChanged(int)));
  38. // 设置固定窗体大小
  39. this->setFixedSize(485, 490);
  40. }
  41. /// <summary>
  42. /// 添加兼容的Pou工具到树形结构中
  43. /// </summary>
  44. /// <param name="pPou"></param>
  45. void DialogGotoSelection::AddPouToolsToTree(const POU* pPou)
  46. {
  47. // 添加根节点
  48. QTreeWidgetItem* pRootItem = AddRootTreeItem("Tools");
  49. // 添加适配的工具
  50. const QVector<TOOL*>& allTools = pPou->GetAllStandardTools();
  51. QVectorIterator<TOOL*> i(allTools);
  52. while (i.hasNext())
  53. {
  54. const TOOL* pTool = i.next();
  55. // 此处只要最标准的Tool
  56. if (pTool->isStandardTool())
  57. {
  58. // 添加工具到列表中
  59. this->AddToolTreeItem(pRootItem, pTool);
  60. }
  61. }
  62. // 将树形结构展开
  63. ui.treeTool->expandAll();
  64. }
  65. /// <summary>
  66. /// // 添加根节点
  67. /// </summary>
  68. /// <param name="toolName"></param>
  69. /// <returns></returns>
  70. QTreeWidgetItem* DialogGotoSelection::AddRootTreeItem(const QString& toolName)
  71. {
  72. QTreeWidgetItem* newItem = new QTreeWidgetItem(QStringList(toolName));
  73. newItem->setIcon(0, QIcon(":/image/tool_tree_item.png"));
  74. // 使父节点不可选中
  75. newItem->setFlags(Qt::ItemIsEnabled);
  76. ui.treeTool->addTopLevelItem(newItem);
  77. return newItem;
  78. }
  79. /// <summary>
  80. /// 添加Tool子节点
  81. /// </summary>
  82. /// <param name="pToolItem"></param>
  83. /// <param name="pTool"></param>
  84. void DialogGotoSelection::AddToolTreeItem(QTreeWidgetItem* pRootItem, const TOOL* pTool)
  85. {
  86. QTreeWidgetItem* newItem = new QTreeWidgetItem(pRootItem, QStringList(pTool->strInstanceName));
  87. newItem->setIcon(0, QIcon(":/image/Input.png"));
  88. pRootItem->addChild(newItem);
  89. }
  90. /// <summary>
  91. /// select按钮
  92. /// </summary>
  93. void DialogGotoSelection::onButtonSelectClicked()
  94. {
  95. // 获取用户选择的节点
  96. QList<QTreeWidgetItem*> selItems = ui.treeTool->selectedItems();
  97. if (selItems.size() <= 0)
  98. {
  99. // 如果选择的节点无效,则不允许关闭
  100. Utility::VPCriticalMessageBox(" The tool selected is invalid!");
  101. return;
  102. }
  103. // 获取用户选择的Tool
  104. m_selToolInstName = selItems[0]->text(0);
  105. this->accept();
  106. }
  107. /// <summary>
  108. /// Combo框选择变更时,需要切换对应的工具列表(当前仅一个分组)
  109. /// </summary>
  110. /// <param name="nIndex"></param>
  111. void DialogGotoSelection::onComboGroupChanged(int nIndex)
  112. {
  113. Q_UNUSED(nIndex);
  114. // 先将当前的树形控件清空
  115. ui.treeTool->clear();
  116. // 重新添加节点和数据结构
  117. AddPouToolsToTree(m_pou);
  118. }