123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #include "DialogWaitSelection.h"
- #include "GvlManager.h"
- #include "TaskManager.h"
- DialogWaitSelection::DialogWaitSelection(QWidget *parent)
- : QDialog(parent)
- , m_selVariable(nullptr)
- {
- ui.setupUi(this);
- // 对话框初始化
- this->initUI();
- // 初始化所有合适的触发事件
- this->initAllTriggerEvents();
- }
- DialogWaitSelection::~DialogWaitSelection()
- {
- }
- /// <summary>
- /// 对话框初始化
- /// </summary>
- void DialogWaitSelection::initUI()
- {
- // 设置对话框标题
- this->setWindowTitle(("Wait Events Selection"));
- this->setAttribute(Qt::WA_QuitOnClose);
- this->setWindowModality(Qt::ApplicationModal);
- // Tree
- // 1列
- ui.treeEvent->setColumnCount(1);
- // 隐藏表头
- ui.treeEvent->setHeaderHidden(true);
- // 显示虚线
- ui.treeEvent->setStyle(QStyleFactory::create("windows"));
- // 槽函数
- connect(ui.selectButton, SIGNAL(clicked()), this, SLOT(onButtonSelectClicked()));
- connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
- // 设置固定窗体大小
- this->setFixedSize(485, 490);
- }
- /// <summary>
- /// 初始化所有合适的触发事件
- /// </summary>
- void DialogWaitSelection::initAllTriggerEvents()
- {
- // 首先获取所有的系统内置触发事件
- QTreeWidgetItem* rootItem = this->AddRootItem(GROUP_NAME_SYSTEMEVENT);
- for (EVENT* pEvent : g_pTaskManager->m_sysEvents)
- {
- this->AddChildItem(rootItem, pEvent);
- }
- // 然后获取所有带有触发性质的变量
- QHash<QString, VARIABLE*> eventVars = g_pGvlManager->getAllEventVariables();
- QHash<QString, VARIABLE*>::const_iterator it = eventVars.constBegin();
- while (it != eventVars.constEnd())
- {
- VARIABLE* pVar = it.value();
- // 变量组名字
- QString strGroupName = pVar->eventTrigger->groupName();
- // 查找是否已经存在此节点了
- QList<QTreeWidgetItem*> items = ui.treeEvent->findItems(strGroupName, Qt::MatchFlag::MatchExactly);
- // 如果存在,则直接添加子节点
- if (items.size() > 0)
- {
- this->AddChildItem(items[0], pVar);
- }
- // 如果不存在,需要先创建一个根节点
- else
- {
- rootItem = this->AddRootItem(strGroupName);
- this->AddChildItem(rootItem, pVar);
- }
- ++it;
- }
- // 将树形结构展开
- ui.treeEvent->expandAll();
- }
- /// <summary>
- /// 添加根节点
- /// </summary>
- /// <param name="toolName"></param>
- /// <returns></returns>
- QTreeWidgetItem* DialogWaitSelection::AddRootItem(const QString& nodeName)
- {
- QTreeWidgetItem* newItem = new QTreeWidgetItem(QStringList(nodeName));
- newItem->setIcon(0, QIcon(":/image/tool_tree_item.png"));
- // 使父节点不可选中
- newItem->setFlags(Qt::ItemIsEnabled);
- ui.treeEvent->addTopLevelItem(newItem);
- return newItem;
- }
- /// <summary>
- /// 添加子节点
- /// </summary>
- /// <param name="pToolItem"></param>
- /// <param name="pVar"></param>
- void DialogWaitSelection::AddChildItem(QTreeWidgetItem* pRootItem, const VARIABLE* pVar)
- {
- QTreeWidgetItem* newItem = new QTreeWidgetItem(pRootItem, QStringList(pVar->strName));
- newItem->setIcon(0, QIcon(":/image/Input.png"));
- pRootItem->addChild(newItem);
- }
- /// <summary>
- /// 按名字查找某一个节点
- /// </summary>
- /// <param name="nodeName"></param>
- /// <returns></returns>
- QTreeWidgetItem* DialogWaitSelection::findItem(const QString& nodeName)
- {
- QTreeWidgetItemIterator it(ui.treeEvent);
- while (*it)
- {
- // 如果节点名字相同则返回当前节点
- if ((*it)->text(0) == nodeName)
- {
- return *it;
- }
-
- it++;
- }
- return nullptr;
- }
- /// <summary>
- /// select按钮
- /// </summary>
- void DialogWaitSelection::onButtonSelectClicked()
- {
- // 获取用户输入,并转换成对应的Variable
- QList<QTreeWidgetItem*> selItems = ui.treeEvent->selectedItems();
- if (selItems.size() <= 0)
- {
- // 如果选择的节点无效,则不允许关闭
- Utility::VPCriticalMessageBox(" The event selected is invalid!");
- return;
- }
- // 获取用户选择的变量
- QString strName = selItems[0]->text(0);
- QString strGroupName = selItems[0]->parent()->text(0);
- // 首先检查是否是系统内置事件
- m_selVariable = g_pTaskManager->getSysEventByName(strName);
- // 如果不是的话,再判断是否是某一个变量
- if (m_selVariable == nullptr)
- {
- // 转换成对应的Variable
- m_selVariable = g_pGvlManager->getVariableByName(strGroupName, strName);
- // 校验有效性
- if (m_selVariable == nullptr)
- {
- Utility::VPCriticalMessageBox(" The event selected is nullptr!");
- return;
- }
- }
- vDebug() << "Select variable [" << m_selVariable->strFullName << "].";
- this->accept();
- }
|