DialogWaitSelection.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "DialogWaitSelection.h"
  2. #include "GvlManager.h"
  3. #include "TaskManager.h"
  4. DialogWaitSelection::DialogWaitSelection(QWidget *parent)
  5. : QDialog(parent)
  6. , m_selVariable(nullptr)
  7. {
  8. ui.setupUi(this);
  9. // 对话框初始化
  10. this->initUI();
  11. // 初始化所有合适的触发事件
  12. this->initAllTriggerEvents();
  13. }
  14. DialogWaitSelection::~DialogWaitSelection()
  15. {
  16. }
  17. /// <summary>
  18. /// 对话框初始化
  19. /// </summary>
  20. void DialogWaitSelection::initUI()
  21. {
  22. // 设置对话框标题
  23. this->setWindowTitle(("Wait Events Selection"));
  24. this->setAttribute(Qt::WA_QuitOnClose);
  25. this->setWindowModality(Qt::ApplicationModal);
  26. // Tree
  27. // 1列
  28. ui.treeEvent->setColumnCount(1);
  29. // 隐藏表头
  30. ui.treeEvent->setHeaderHidden(true);
  31. // 显示虚线
  32. ui.treeEvent->setStyle(QStyleFactory::create("windows"));
  33. // 槽函数
  34. connect(ui.selectButton, SIGNAL(clicked()), this, SLOT(onButtonSelectClicked()));
  35. connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
  36. // 设置固定窗体大小
  37. this->setFixedSize(485, 490);
  38. }
  39. /// <summary>
  40. /// 初始化所有合适的触发事件
  41. /// </summary>
  42. void DialogWaitSelection::initAllTriggerEvents()
  43. {
  44. // 首先获取所有的系统内置触发事件
  45. QTreeWidgetItem* rootItem = this->AddRootItem(GROUP_NAME_SYSTEMEVENT);
  46. for (EVENT* pEvent : g_pTaskManager->m_sysEvents)
  47. {
  48. this->AddChildItem(rootItem, pEvent);
  49. }
  50. // 然后获取所有带有触发性质的变量
  51. QHash<QString, VARIABLE*> eventVars = g_pGvlManager->getAllEventVariables();
  52. QHash<QString, VARIABLE*>::const_iterator it = eventVars.constBegin();
  53. while (it != eventVars.constEnd())
  54. {
  55. VARIABLE* pVar = it.value();
  56. // 变量组名字
  57. QString strGroupName = pVar->eventTrigger->groupName();
  58. // 查找是否已经存在此节点了
  59. QList<QTreeWidgetItem*> items = ui.treeEvent->findItems(strGroupName, Qt::MatchFlag::MatchExactly);
  60. // 如果存在,则直接添加子节点
  61. if (items.size() > 0)
  62. {
  63. this->AddChildItem(items[0], pVar);
  64. }
  65. // 如果不存在,需要先创建一个根节点
  66. else
  67. {
  68. rootItem = this->AddRootItem(strGroupName);
  69. this->AddChildItem(rootItem, pVar);
  70. }
  71. ++it;
  72. }
  73. // 将树形结构展开
  74. ui.treeEvent->expandAll();
  75. }
  76. /// <summary>
  77. /// 添加根节点
  78. /// </summary>
  79. /// <param name="toolName"></param>
  80. /// <returns></returns>
  81. QTreeWidgetItem* DialogWaitSelection::AddRootItem(const QString& nodeName)
  82. {
  83. QTreeWidgetItem* newItem = new QTreeWidgetItem(QStringList(nodeName));
  84. newItem->setIcon(0, QIcon(":/image/tool_tree_item.png"));
  85. // 使父节点不可选中
  86. newItem->setFlags(Qt::ItemIsEnabled);
  87. ui.treeEvent->addTopLevelItem(newItem);
  88. return newItem;
  89. }
  90. /// <summary>
  91. /// 添加子节点
  92. /// </summary>
  93. /// <param name="pToolItem"></param>
  94. /// <param name="pVar"></param>
  95. void DialogWaitSelection::AddChildItem(QTreeWidgetItem* pRootItem, const VARIABLE* pVar)
  96. {
  97. QTreeWidgetItem* newItem = new QTreeWidgetItem(pRootItem, QStringList(pVar->strName));
  98. newItem->setIcon(0, QIcon(":/image/Input.png"));
  99. pRootItem->addChild(newItem);
  100. }
  101. /// <summary>
  102. /// 按名字查找某一个节点
  103. /// </summary>
  104. /// <param name="nodeName"></param>
  105. /// <returns></returns>
  106. QTreeWidgetItem* DialogWaitSelection::findItem(const QString& nodeName)
  107. {
  108. QTreeWidgetItemIterator it(ui.treeEvent);
  109. while (*it)
  110. {
  111. // 如果节点名字相同则返回当前节点
  112. if ((*it)->text(0) == nodeName)
  113. {
  114. return *it;
  115. }
  116. it++;
  117. }
  118. return nullptr;
  119. }
  120. /// <summary>
  121. /// select按钮
  122. /// </summary>
  123. void DialogWaitSelection::onButtonSelectClicked()
  124. {
  125. // 获取用户输入,并转换成对应的Variable
  126. QList<QTreeWidgetItem*> selItems = ui.treeEvent->selectedItems();
  127. if (selItems.size() <= 0)
  128. {
  129. // 如果选择的节点无效,则不允许关闭
  130. Utility::VPCriticalMessageBox(" The event selected is invalid!");
  131. return;
  132. }
  133. // 获取用户选择的变量
  134. QString strName = selItems[0]->text(0);
  135. QString strGroupName = selItems[0]->parent()->text(0);
  136. // 首先检查是否是系统内置事件
  137. m_selVariable = g_pTaskManager->getSysEventByName(strName);
  138. // 如果不是的话,再判断是否是某一个变量
  139. if (m_selVariable == nullptr)
  140. {
  141. // 转换成对应的Variable
  142. m_selVariable = g_pGvlManager->getVariableByName(strGroupName, strName);
  143. // 校验有效性
  144. if (m_selVariable == nullptr)
  145. {
  146. Utility::VPCriticalMessageBox(" The event selected is nullptr!");
  147. return;
  148. }
  149. }
  150. vDebug() << "Select variable [" << m_selVariable->strFullName << "].";
  151. this->accept();
  152. }