419 lines
15 KiB
C++
419 lines
15 KiB
C++
#include <QMessageBox>
|
||
#include "diagramEditor/diagramEditorTransSettingDlg.h"
|
||
#include "diagramEditor/diagramEditorWizard.h"
|
||
#include "diagramEditor/diagramEditorBaseBlock.h"
|
||
#include "ui_diagramEditorTransSettingDlg.h"
|
||
#include "diagramEditor/diagramEditorStructContainer.h"
|
||
#include "diagramEditor/wizardBayContentDlg.h"
|
||
#include "common/core_model/constants.h"
|
||
#include <QTimer>
|
||
#include "titleBar.h"
|
||
|
||
DiagramEditorTransSettingDlg::DiagramEditorTransSettingDlg(QWidget *parent)
|
||
: QDialog(parent)
|
||
, ui(new Ui::diagramEditorTransSettingDlg)
|
||
,_curOperateBlock(nullptr)
|
||
{
|
||
ui->setupUi(this);
|
||
this->setWindowFlags(Qt::FramelessWindowHint | windowFlags());
|
||
this->setWindowModality(Qt::WindowModal);
|
||
initial();
|
||
}
|
||
|
||
DiagramEditorTransSettingDlg::~DiagramEditorTransSettingDlg()
|
||
{
|
||
delete ui;
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::showDlg()
|
||
{
|
||
_curModel = 0;
|
||
show();
|
||
addNewTrans();
|
||
if(_pWizard){
|
||
ui->le_name->setText("#主变");
|
||
}
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::showDlg(DiagramEditorTransformerBlock* p)
|
||
{
|
||
_curModel = 1;
|
||
if(_curOperateBlock == nullptr)
|
||
_curOperateBlock = p;
|
||
show();
|
||
addNewTrans();
|
||
if(_pWizard){
|
||
ui->le_name->setText(p->getName());
|
||
if(p){
|
||
auto lstCon = p->getConnect();
|
||
/*QStringList lst;
|
||
for(auto& conId:lstCon){
|
||
if(_pWizard->getConnection().contains(conId)){
|
||
auto con = _pWizard->getConnection().value(conId);
|
||
QString sOpposite = con.getOppositeName(p->getName());
|
||
lst.append(sOpposite);
|
||
}
|
||
}
|
||
ui->listWidget->addItems(lst);*/
|
||
|
||
for(auto& conId:lstCon){
|
||
if(_pWizard->getConnection().contains(conId)){
|
||
auto con = _pWizard->getConnection().value(conId);
|
||
QString sOpposite = con.getOpposite(p->getName()).sName;
|
||
|
||
QString sPos;
|
||
if(con.nPara == 0){
|
||
sPos = "高压侧";
|
||
}
|
||
else if(con.nPara == 1){
|
||
sPos = "中压侧";
|
||
}
|
||
else if(con.nPara == 2){
|
||
sPos = "低压侧";
|
||
}
|
||
|
||
int row = ui->tableWidget->rowCount();
|
||
ui->tableWidget->insertRow(row);
|
||
|
||
//名称
|
||
int bayType = 0;
|
||
auto pBlock = _pWizard->getBlockByName_all(sOpposite);
|
||
if(pBlock){
|
||
auto pBay = dynamic_cast<DiagramEditorBayBlock*>(pBlock);
|
||
if(pBay){
|
||
bayType = int(pBay->getBayType());
|
||
}
|
||
}
|
||
QTableWidgetItem* nameItem = new QTableWidgetItem(sOpposite);
|
||
nameItem->setData(Qt::UserRole, bayType);
|
||
ui->tableWidget->setItem(row, 0, nameItem);
|
||
|
||
//位置
|
||
QTableWidgetItem* posItem = new QTableWidgetItem(sPos);
|
||
ui->tableWidget->setItem(row, 1, posItem);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::addNewTrans()
|
||
{
|
||
ui->cb_type->setCurrentIndex(0);
|
||
ui->cb_level->clear();
|
||
ui->cb_target->clear();
|
||
ui->tableWidget->clearContents();
|
||
ui->tableWidget->setRowCount(0);
|
||
if(_pWizard){
|
||
auto mapAllCon = _pWizard->getContainerStruct();
|
||
for(auto iter = mapAllCon.begin();iter != mapAllCon.end();++iter){
|
||
if(iter.key() == Constants::TRANSFORMER_LEVEL)
|
||
continue;
|
||
ui->cb_level->addItem("第"+QString::number(iter.key()+1)+"层",iter.key());
|
||
}
|
||
}
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::initial()
|
||
{
|
||
m_titleBar = new TitleBar(this);
|
||
m_titleBar->setTitle("变压器设置");
|
||
ui->verticalLayout_3->insertWidget(0,m_titleBar);
|
||
|
||
_curModel = 0;
|
||
connect(ui->btn_add,&QPushButton::clicked,this,&DiagramEditorTransSettingDlg::onAddClicked);
|
||
connect(ui->btn_delete,&QPushButton::clicked,this,&DiagramEditorTransSettingDlg::onDeleteClicked);
|
||
connect(ui->btn_ok,&QPushButton::clicked,this,&DiagramEditorTransSettingDlg::onOkClicked);
|
||
connect(ui->btn_cancel,&QPushButton::clicked,this,&DiagramEditorTransSettingDlg::onCancelClicked);
|
||
connect(ui->cb_level,&QComboBox::currentTextChanged,this,&DiagramEditorTransSettingDlg::onConnectLevelChanged);
|
||
connect(ui->cb_bayType,&QComboBox::currentIndexChanged,this,&DiagramEditorTransSettingDlg::onBayTypeChanged);
|
||
connect(ui->tableWidget, &QTableWidget::itemSelectionChanged,this, &DiagramEditorTransSettingDlg::onTableItemSelected);
|
||
ui->cb_bayType->setItemData(0,3); //将类型与item关联
|
||
ui->cb_bayType->setItemData(1,4);
|
||
|
||
QStringList headerText;
|
||
headerText<<"间隔名称"<<"连接对象";
|
||
ui->tableWidget->setContextMenuPolicy(Qt::CustomContextMenu);
|
||
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection);
|
||
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
|
||
ui->tableWidget->setColumnCount(headerText.count());
|
||
ui->tableWidget->setHorizontalHeaderLabels(headerText);
|
||
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
|
||
ui->tableWidget->verticalHeader()->setVisible(false);
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::onAddClicked()
|
||
{
|
||
QString str = ui->cb_target->currentText();
|
||
|
||
for(int i = 0;i < ui->tableWidget->rowCount();++i){
|
||
QString sName = ui->tableWidget->item(i,0)->text();
|
||
if(str == sName)
|
||
return;
|
||
}
|
||
|
||
int row = ui->tableWidget->rowCount();
|
||
ui->tableWidget->insertRow(row);
|
||
|
||
//名称
|
||
QTableWidgetItem* nameItem = new QTableWidgetItem(str);
|
||
nameItem->setData(Qt::UserRole, ui->cb_bayType->currentData().toInt());
|
||
ui->tableWidget->setItem(row, 0, nameItem);
|
||
|
||
//位置
|
||
QTableWidgetItem* posItem = new QTableWidgetItem(ui->cb_toPos->currentText());
|
||
ui->tableWidget->setItem(row, 1, posItem);
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::onDeleteClicked()
|
||
{
|
||
// 获取当前选中的索引
|
||
QModelIndexList selectedIndexes = ui->tableWidget->selectionModel()->selectedRows();
|
||
if (selectedIndexes.isEmpty()) {
|
||
return; // 没有选中任何行
|
||
}
|
||
|
||
// 获取当前选中的第一项索引
|
||
QModelIndex index = selectedIndexes.first();
|
||
if (!index.isValid()) {
|
||
return;
|
||
}
|
||
|
||
QModelIndex indexName = index.sibling(index.row(),0);
|
||
QString sName = indexName.data().toString();
|
||
if(!_prepareDisconnectBlock.contains(sName))
|
||
_prepareDisconnectBlock.append(sName);
|
||
/*if(_pWizard){
|
||
//delete con?
|
||
}*/
|
||
|
||
int currentRow = ui->tableWidget->currentRow();
|
||
if (currentRow == -1) {
|
||
return; // 没有选中行
|
||
}
|
||
|
||
ui->tableWidget->removeRow(currentRow);
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::onOkClicked()
|
||
{
|
||
QString sName = ui->le_name->text();
|
||
TransformerType nType = TransformerType(ui->cb_type->currentIndex());
|
||
if(_curModel == 0){
|
||
auto lstBlock = _pWizard->getTargetLevelBlocks(Constants::TRANSFORMER_LEVEL,3);
|
||
for(auto bay:lstBlock)
|
||
{
|
||
if(bay->getName() == sName){ //判断间隔存在
|
||
QMessageBox::information(NULL, QString("提示"), QString::fromWCharArray(L"变压器已存在"));
|
||
return;
|
||
}
|
||
}
|
||
|
||
QList<EditorTransConnection> lst;
|
||
for(int i = 0;i < ui->tableWidget->rowCount();++i){
|
||
QString sPos = ui->tableWidget->item(i,1)->text();
|
||
int nVal = 0;
|
||
if(sPos == "高压侧"){
|
||
nVal = 0;
|
||
}
|
||
else if(sPos == "中压侧")
|
||
{
|
||
nVal = 1;
|
||
}
|
||
else if(sPos == "低压侧")
|
||
{
|
||
nVal = 2;
|
||
}
|
||
EditorTransConnection con;
|
||
con.sName = ui->tableWidget->item(i,0)->text();
|
||
con.nPara = nVal;
|
||
lst.append(con);
|
||
}
|
||
|
||
DiagramEditorWizardTransformerInfo info;
|
||
info.sName = sName;
|
||
info.nType = nType;
|
||
info.lstBindObj = lst;
|
||
|
||
_pWizard->onAddTransFinished(info);
|
||
}
|
||
else if(_curModel == 1){
|
||
if(_curOperateBlock){
|
||
_curOperateBlock->setName(sName);
|
||
_curOperateBlock->setTransType(nType);
|
||
_curOperateBlock->clearConnect();
|
||
|
||
for(int i = 0;i < ui->tableWidget->rowCount();++i){
|
||
QString sPos = ui->tableWidget->item(i,1)->text();
|
||
int nVal = 0;
|
||
if(sPos == "高压侧"){
|
||
nVal = 0;
|
||
}
|
||
else if(sPos == "中压侧")
|
||
{
|
||
nVal = 1;
|
||
}
|
||
else if(sPos == "低压侧")
|
||
{
|
||
nVal = 2;
|
||
}
|
||
QString sTargetBay = ui->tableWidget->item(i,0)->text();
|
||
QUuid uid = _pWizard->addConnection(_curOperateBlock->getName(),sTargetBay,3,2,nVal);
|
||
auto pBlock = _pWizard->getBlockByName_all(sTargetBay); //连接的双方都保存连接(todo:修改时删除对面保存的连接)
|
||
if(pBlock){
|
||
pBlock->addConnect(uid);
|
||
}
|
||
_curOperateBlock->addConnect(uid);
|
||
}
|
||
//todo: may delete
|
||
|
||
if(_prepareDisconnectBlock.size()){
|
||
|
||
for(auto& sTargetBay:_prepareDisconnectBlock){
|
||
QUuid uid = _pWizard->findConnection(_curOperateBlock->getName(),sTargetBay);
|
||
if(!uid.isNull())
|
||
{
|
||
_pWizard->removeConnection(uid); //从记录中删除链接
|
||
auto pBlock = _pWizard->getBlockByName_all(sTargetBay); //连接的双方都保存连接(todo:修改时删除对面保存的连接)
|
||
if(pBlock){
|
||
pBlock->removeConnect(uid);//从各自block中删除链接
|
||
|
||
auto pBay = dynamic_cast<DiagramEditorBayBlock*>(pBlock);
|
||
if(pBay){
|
||
auto& mapComponent = pBay->getBayInfo().mapComponent;
|
||
for(auto& comp:mapComponent){
|
||
if(comp.sBindParent == _curOperateBlock->getName()){ //block中设备信息断开
|
||
comp.sBindParent.clear();
|
||
comp.sBindObj.clear();
|
||
break;
|
||
}
|
||
}
|
||
|
||
auto& mapRoute = pBay->getBayInfo().mapRoute;
|
||
for(auto& route:mapRoute){ //block中线路信息断开
|
||
for(auto& comp:route.lstCompo){
|
||
if(comp.sBindParent == _curOperateBlock->getName()){
|
||
comp.sBindParent.clear();
|
||
comp.sBindObj.clear();
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
_curOperateBlock->removeConnect(uid); //从各自block中删除链接
|
||
}
|
||
}
|
||
_prepareDisconnectBlock.clear();
|
||
}
|
||
|
||
_pWizard->flushTransPage();
|
||
_curOperateBlock = nullptr;
|
||
}
|
||
}
|
||
hide();
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::onCancelClicked()
|
||
{
|
||
if(_prepareDisconnectBlock.size())
|
||
_prepareDisconnectBlock.clear();
|
||
hide();
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::onConnectLevelChanged(const QString& str)
|
||
{
|
||
if(_pWizard){
|
||
int n = ui->cb_level->findText(str);
|
||
BayType nType = BayType(ui->cb_bayType->currentData().toInt());
|
||
if(n != -1){
|
||
ui->cb_target->clear();
|
||
int nIndex = ui->cb_level->itemData(n).toInt();
|
||
auto lstBlock = _pWizard->getTargetLevelBlocks(nIndex,2);
|
||
for(auto& block:lstBlock){
|
||
auto pBay = dynamic_cast<DiagramEditorBayBlock*>(block);
|
||
if(pBay){
|
||
BayType tpe = pBay->getBayType();
|
||
if(nType == tpe)
|
||
ui->cb_target->addItem(pBay->getName());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::onBayTypeChanged(int idx)
|
||
{
|
||
BayType nType = BayType(ui->cb_bayType->currentData().toInt());
|
||
int n = ui->cb_level->currentIndex();
|
||
if(n != -1){
|
||
ui->cb_target->clear();
|
||
int nIndex = ui->cb_level->itemData(n).toInt();
|
||
auto lstBlock = _pWizard->getTargetLevelBlocks(nIndex,2);
|
||
for(auto& block:lstBlock){
|
||
auto pBay = dynamic_cast<DiagramEditorBayBlock*>(block);
|
||
if(pBay){
|
||
BayType tpe = pBay->getBayType();
|
||
if(nType == tpe)
|
||
ui->cb_target->addItem(pBay->getName());
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
void DiagramEditorTransSettingDlg::onTableItemSelected()
|
||
{
|
||
auto items = ui->tableWidget->selectedItems();
|
||
if (items.isEmpty())
|
||
return;
|
||
|
||
// 取第一列(名称)
|
||
QTableWidgetItem* nameItem = ui->tableWidget->item(items.first()->row(), 0);
|
||
if (!nameItem)
|
||
return;
|
||
|
||
QString targetName = nameItem->text();
|
||
int bayTypeValue = nameItem->data(Qt::UserRole).toInt();
|
||
BayType targetBayType = BayType(bayTypeValue);
|
||
|
||
if (!_pWizard)
|
||
return;
|
||
|
||
// 遍历所有 level
|
||
for (int i = 0; i < ui->cb_level->count(); ++i)
|
||
{
|
||
int levelIndex = ui->cb_level->itemData(i).toInt();
|
||
auto lstBlock = _pWizard->getTargetLevelBlocks(levelIndex, 2);
|
||
|
||
for (auto& block : lstBlock)
|
||
{
|
||
auto pBay = dynamic_cast<DiagramEditorBayBlock*>(block);
|
||
if (!pBay)
|
||
continue;
|
||
|
||
if (pBay->getName() == targetName &&
|
||
pBay->getBayType() == targetBayType)
|
||
{
|
||
// ✅ 1. 设置 bayType(必须先)
|
||
for (int j = 0; j < ui->cb_bayType->count(); ++j)
|
||
{
|
||
if (ui->cb_bayType->itemData(j).toInt() == bayTypeValue)
|
||
{
|
||
ui->cb_bayType->setCurrentIndex(j);
|
||
break;
|
||
}
|
||
}
|
||
|
||
// ✅ 2. 设置 level
|
||
ui->cb_level->setCurrentIndex(i);
|
||
|
||
// ✅ 3. 等待 onConnectLevelChanged 刷新 cb_target
|
||
QTimer::singleShot(0, this, [=] {
|
||
ui->cb_target->setCurrentText(targetName);
|
||
});
|
||
|
||
return;
|
||
}
|
||
}
|
||
}
|
||
}
|