#include "DialogBlockProperty.h"
#include "WindowAppBlockStandard.h"
#include "WindowAppItemInterface.h"
#include "Pou.h"
// 对话框默认标题
#define DEFAULT_WINDOW_TITLE QString("Property Sheets - ")
DialogBlockProperty::DialogBlockProperty(TOOL* tool, POU* Pou, QWidget *parent)
: QDialog(parent)
, m_toolInfo(tool)
, m_pPou(Pou)
{
ui.setupUi(this);
// 初始化界面
this->initUI();
}
DialogBlockProperty::~DialogBlockProperty()
{
}
///
/// 对话框初始化
///
void DialogBlockProperty::initUI()
{
// 根据工具名字设置窗口标题
this->setWindowTitle(DEFAULT_WINDOW_TITLE + m_toolInfo->strInstanceName);
// 设置默认Sheet
ui.tabSettings->setCurrentIndex(0);
// 根据工具的信息填充UI界面
ui.editInfo->setText(m_toolInfo->strInfo);
ui.editName->setText(m_toolInfo->strInstanceName);
ui.editBeforeDelay->setText(QString::number(m_toolInfo->execParams.nPreDelay));
ui.editAfterDelay->setText(QString::number(m_toolInfo->execParams.nPostDelay));
// 保存工具的旧名字备用
blockSettings.strOldInstanceName = m_toolInfo->strInstanceName;
// 设置工具是否可用
if (m_toolInfo->bEnable)
{
ui.checkEnableTool->setChecked(true);
}
else
{
ui.checkEnableTool->setChecked(false);
}
QRegExp regExp("[0-9]+$");//只能输入数字
ui.editBeforeDelay->setValidator(new QRegExpValidator(regExp, this));
ui.editAfterDelay->setValidator(new QRegExpValidator(regExp, this));
// 绘制工具图像
this->initToolBlock();
// 初始化设置工具Index的ComboBox
this->initComboToolIndex();
// 槽函数
connect(ui.okButton, SIGNAL(clicked()), this, SLOT(onButtonOkClicked()));
connect(ui.cancelButton, SIGNAL(clicked()), this, SLOT(reject()));
// 设置固定窗体大小
this->setFixedSize(538, 391);
//this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinMaxButtonsHint);
}
///
/// 绘制工具的图像(以及CheckBox)
///
void DialogBlockProperty::initToolBlock()
{
// 设置为不可交互
// ui.viewBlockProperty->setInteractive(false);
// 设置不传递任何消息到子控件
// ui.viewBlockProperty->setAttribute(Qt::WA_TransparentForMouseEvents, true);
// 初始化scene
QGraphicsScene* blockScene = new QGraphicsScene(this);
blockScene->setSceneRect(QRectF(0, 0, 0, 0));
ui.viewBlockProperty->setScene(blockScene);
// 新建一个功能块信息
WindowAppBlockStandard* newItem = new WindowAppBlockStandard(m_toolInfo, m_pPou, true);
// scene中添加功能块
blockScene->addItem(newItem);
// 在界面中心位置添加此Block
newItem->setPos(QPoint(0,0));
// 添加功能块接口
newItem->addItemInterfaces();
// 根据接口情况动态添加checkbox
for (int i = 0; i < newItem->m_itemInterfaces.size(); i++)
{
WindowAppItemInterface* pInfItem = newItem->m_itemInterfaces[i];
// 跳过ToolStart接口, 并行接口,goto 接口,非标准接口
if (pInfItem->m_infInfo->isToolStart()
|| pInfItem->m_infInfo->isParallelToolStart()
|| pInfItem->m_infInfo->isParallelToolEnd()
|| pInfItem->m_infInfo->isGotoToolEnd()
|| pInfItem->m_infInfo->Type != INF_TYPE::INF_TYPE_STANDARD
)
{
continue;
}
// 获取本接口的真实线段位置
QLineF realLine = QLineF(
pInfItem->mapToScene(pInfItem->line().p1()),
pInfItem->mapToScene(pInfItem->line().p2())
);
// 添加Checkbox
QCheckBox* pNewCheckBox = new QCheckBox();
if (pInfItem->m_infInfo->nRefCount > 0)
{
pNewCheckBox->setText(QString::number(pInfItem->m_infInfo->nRefCount));
pNewCheckBox->setEnabled(false);
}
// 添加到scene中
QGraphicsProxyWidget* pCheckWidget = blockScene->addWidget(pNewCheckBox);
if (pInfItem->m_infInfo->isDirInput())
{
// 微调一下偏移
QPointF pos = QPointF(
realLine.p1().x() - pNewCheckBox->geometry().width() - 5,
realLine.p1().y() - pNewCheckBox->geometry().height() / 2
);
pCheckWidget->setPos(pos);
}
else
{
// 微调一下偏移
QPointF pos = QPointF(
realLine.p1().x() + pNewCheckBox->geometry().width() + 5,
realLine.p1().y() - pNewCheckBox->geometry().height() / 2
);
pCheckWidget->setPos(pos);
}
// 根据接口启用与否打钩
pNewCheckBox->setChecked(pInfItem->m_infInfo->bEnable);
infCheckBoxs.push_back(pNewCheckBox);
}
}
///
/// 2022-9-28 初始化设置工具Index的ComboBox
///
void DialogBlockProperty::initComboToolIndex()
{
// 取出工具当前Index
int nCurrentIndex = this->m_toolInfo->nIndex;
// 取出当前Pou所有工具的Index
int nTotalIndex = this->m_pPou->GetIndexedToolsCount();
// ComboBox中的数字为当前工具所有可以选的Index合集
for (int i = 1; i <= nTotalIndex; i++)
{
ui.comboIndex->addItem(QString::number(i));
}
// 将当前Tool的Index设置为ComboBox的默认文字
ui.comboIndex->setCurrentIndex(nCurrentIndex);
}
///
/// 保存用户的设置项
///
void DialogBlockProperty::saveBlockSettings()
{
for (int i = 0; i < infCheckBoxs.size(); i++)
{
blockSettings.infEnables.push_back(infCheckBoxs[i]->isChecked());
}
blockSettings.bToolEnable = ui.checkEnableTool->isChecked();
blockSettings.strNewInstanceName = ui.editName->text();
blockSettings.strInfo = ui.editInfo->toPlainText();
blockSettings.nInDelay = ui.editBeforeDelay->text().toInt();
blockSettings.nOutDelay = ui.editAfterDelay->text().toInt();
// 2022-9-28,保存用户输入的新索引号,注意这里值要-1,因为界面上的值是从1开始的,实际功能块是从0开始的
blockSettings.nNewIndex = ui.comboIndex->currentText().toInt() - 1;
}
///
/// Ok按钮
///
void DialogBlockProperty::onButtonOkClicked()
{
// 首先检查用户设置的实例名称是否重名,要是有重名的则报错
if ( blockSettings.strOldInstanceName!=ui.editName->text() &&
m_pPou->isInstanceNameDuplicated(ui.editName->text())
)
{
Utility::VPCriticalMessageBox(QString(ui.editName->text() + "is duplicated!"));
return;
}
// 保存用户的设置项
saveBlockSettings();
// 关闭对话框
this->accept();
}