#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()
{
}
///
/// 对话框初始化
///
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);
}
///
/// 初始化所有合适的触发事件
///
void DialogWaitSelection::initAllTriggerEvents()
{
// 首先获取所有的系统内置触发事件
QTreeWidgetItem* rootItem = this->AddRootItem(GROUP_NAME_SYSTEMEVENT);
for (EVENT* pEvent : g_pTaskManager->m_sysEvents)
{
this->AddChildItem(rootItem, pEvent);
}
// 然后获取所有带有触发性质的变量
QHash eventVars = g_pGvlManager->getAllEventVariables();
QHash::const_iterator it = eventVars.constBegin();
while (it != eventVars.constEnd())
{
VARIABLE* pVar = it.value();
// 变量组名字
QString strGroupName = pVar->eventTrigger->groupName();
// 查找是否已经存在此节点了
QList 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();
}
///
/// 添加根节点
///
///
///
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;
}
///
/// 添加子节点
///
///
///
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);
}
///
/// 按名字查找某一个节点
///
///
///
QTreeWidgetItem* DialogWaitSelection::findItem(const QString& nodeName)
{
QTreeWidgetItemIterator it(ui.treeEvent);
while (*it)
{
// 如果节点名字相同则返回当前节点
if ((*it)->text(0) == nodeName)
{
return *it;
}
it++;
}
return nullptr;
}
///
/// select按钮
///
void DialogWaitSelection::onButtonSelectClicked()
{
// 获取用户输入,并转换成对应的Variable
QList 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();
}