#include "DialogNewVariable.h"
#include "Common.h"
#include "GvlManager.h"
DialogNewVariable::DialogNewVariable(GVL_MODE gvlType, QWidget *parent)
: QDialog(parent)
, m_gvlType(gvlType)
{
ui.setupUi(this);
// 对话框初始化
initUI();
}
DialogNewVariable::~DialogNewVariable()
{
}
///
/// 对话框初始化
///
void DialogNewVariable::initUI()
{
// this->setWindowTitle(("Add New Variable"));
this->setAttribute(Qt::WA_QuitOnClose);
this->setWindowModality(Qt::ApplicationModal);
ui.checkSerialized->setChecked(true);
ui.editValue->setText("0");
ui.editValue->setAlignment(Qt::AlignLeading);
// 初始化类型列表(根据不同的变量模式)
this->initTypeList();
ui.comboType->setCurrentIndex(1);
ui.editName->setPlaceholderText("Name is required.");
ui.editComment->setPlaceholderText("Type here to input comment.");
// 槽函数
connect(ui.createButton, SIGNAL(clicked()), this, SLOT(onButtonCreateClicked()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
// 设置固定窗体大小
this->setFixedSize(495, 450);
//this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
}
///
/// 2022-2-18,初始化类型列表(根据不同的变量模式添加不同的候选项)
///
void DialogNewVariable::initTypeList()
{
QString strItem;
// 基础模式
if (m_gvlType == GVL_MODE::GVL_BASIC)
{
for (int i = 0; i < GVL_BASIC_TYPE_COUNT; i++)
{
strItem = Utility::getTypeString((VALUE_TYPE)i);
ui.comboType->addItem(strItem);
}
}
else
{
for (int i = 0; i < GVL_STANDARD_TYPE_COUNT; i++)
{
strItem = Utility::getTypeString((VALUE_TYPE)i);
ui.comboType->addItem(strItem);
}
}
}
///
/// Create按钮
///
void DialogNewVariable::onButtonCreateClicked()
{
// 将控件设置为正常状态
Utility::setControlNormal(ui.editName);
Utility::setControlNormal(ui.editValue);
m_bSerialized = ui.checkSerialized->checkState() == Qt::Checked;
m_strName = ui.editName->text();
m_strType = ui.comboType->currentText();
m_strValue = ui.editValue->text();
m_strComment = ui.editComment->text();
// 检查变量名
if (m_strName.isEmpty())
{
Utility::VPCriticalMessageBox("Variable name is required!");
Utility::setControlError(ui.editName);
return;
}
// 检查页面是否重复
if( g_pGvlManager->isDuplicated(m_strGroup, m_strName))
{
Utility::VPCriticalMessageBox("Variable name is duplicated!");
Utility::setControlError(ui.editName);
return;
}
// 检查变量值是否和变量类型吻合
if (!g_pGvlManager->isValueValid(m_strType, m_strValue))
{
Utility::VPCriticalMessageBox("Invalid value - type: " + m_strType + " .");
Utility::setControlError(ui.editValue);
return;
}
this->accept();
}