DialogTaskPou.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include "DialogTaskPou.h"
  2. #include "PouManager.h"
  3. // 根节点的名称
  4. #define ROOT_NAME "ALL_POUS"
  5. DialogTaskPou::DialogTaskPou(QWidget *parent)
  6. : QDialog(parent)
  7. {
  8. ui.setupUi(this);
  9. // 对话框界面初始化
  10. init();
  11. // 添加所有的Pou到树形结构中
  12. this->AddPousToTree();
  13. }
  14. DialogTaskPou::~DialogTaskPou()
  15. {
  16. }
  17. /// <summary>
  18. /// 对话框初始化
  19. /// </summary>
  20. void DialogTaskPou::init()
  21. {
  22. // 设置对话框标题
  23. this->setWindowTitle(("POU Selection ( Multi Selection )"));
  24. this->setAttribute(Qt::WA_QuitOnClose);
  25. this->setWindowModality(Qt::ApplicationModal);
  26. // Tree
  27. // 1列
  28. ui.treePou->setColumnCount(1);
  29. // 隐藏表头
  30. ui.treePou->setHeaderHidden(true);
  31. // 显示虚线
  32. ui.treePou->setStyle(QStyleFactory::create("windows"));
  33. // 多选
  34. ui.treePou->setSelectionMode(QAbstractItemView::MultiSelection);
  35. // 槽函数
  36. connect(ui.selectButton, SIGNAL(clicked()), this, SLOT(onButtonSelectClicked()));
  37. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
  38. // 设置固定窗体大小
  39. this->setFixedSize(463, 460);
  40. //this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
  41. }
  42. /// <summary>
  43. /// 添加所有的Pou到树形结构中
  44. /// </summary>
  45. void DialogTaskPou::AddPousToTree()
  46. {
  47. // 首先添加一个父节点
  48. QTreeWidgetItem* rootItem = new QTreeWidgetItem(QStringList(ROOT_NAME));
  49. rootItem->setIcon(0, QIcon(":/image/tool_tree_item.png"));
  50. // 使父节点不可选中
  51. rootItem->setFlags(Qt::ItemIsEnabled);
  52. ui.treePou->addTopLevelItem(rootItem);
  53. // 循环添加所有的Pou
  54. // 从公共区域获取全部变量
  55. const QMap<QString, POU*>& allPous = g_pPouManager->getAllPous();
  56. // 遍历所有的Pou信息
  57. QMapIterator<QString, POU*> it(allPous);
  58. while (it.hasNext())
  59. {
  60. // 以Pou分组名称为节点添加
  61. QString strPouGroupName = it.next().key();
  62. // 需要跳过Hardware以及功能块类型的Pou
  63. if (strPouGroupName == GROUP_NAME_HARDWARE)
  64. {
  65. continue;
  66. }
  67. POU* pPou = it.value();
  68. // 加上工具数量
  69. int nToolCount = pPou->GetStandardToolsCount();
  70. strPouGroupName += " (" + QString::number(nToolCount) + " tools)";
  71. QTreeWidgetItem* pouItem = new QTreeWidgetItem(rootItem, QStringList(strPouGroupName));
  72. pouItem->setIcon(0, QIcon(":/image/tree_item.png"));
  73. rootItem->addChild(pouItem);
  74. // 将新加入的item和对应的Interface绑定
  75. m_ItemPous.insert(pouItem, it.value());
  76. }
  77. // 展开树状节点
  78. ui.treePou->expandAll();
  79. }
  80. /// <summary>
  81. /// select按钮
  82. /// </summary>
  83. void DialogTaskPou::onButtonSelectClicked()
  84. {
  85. // 清空当前选择
  86. m_selPous.clear();
  87. // 获取选择项(多选)
  88. QList<QTreeWidgetItem*> selectedItemList = ui.treePou->selectedItems();
  89. // 如果选择的数量为0,则提示重新选择
  90. if (selectedItemList.size() <= 0)
  91. {
  92. Utility::VPCriticalMessageBox("Please select a valid pou.");
  93. return;
  94. }
  95. // 找到对应的数据结构项
  96. for (QTreeWidgetItem * pSelItem : selectedItemList)
  97. {
  98. if (m_ItemPous.contains(pSelItem))
  99. {
  100. m_selPous.push_back(m_ItemPous[pSelItem]);
  101. }
  102. }
  103. this->accept();
  104. }