GridFrame/diagramCavas/source/ptExtraInfoDlg.cpp

346 lines
11 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "ptExtraInfoDlg.h"
#include "ui_ptExtraInfoDlg.h"
#include "baseProperty.h"
#include "basePropertyManager.h"
#include <QButtonGroup>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMenu>
PtExtraInfoDlg::PtExtraInfoDlg(QWidget *parent)
: BaseContentDlg(parent)
, ui(new Ui::ptExtraInfoDlg)
{
ui->setupUi(this);
_stateGroup_pt = new QButtonGroup(this);
_stateGroup_pt->addButton(ui->rb_tpt_pt,1);
_stateGroup_pt->addButton(ui->rb_spt_pt,0);
connect(ui->btn_add_pt,&QPushButton::clicked,this,&PtExtraInfoDlg::onAddClicked);
ui->tb_pt->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
_count = 1;
ui->tb_pt->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->tb_pt, &QTableWidget::customContextMenuRequested, this, &PtExtraInfoDlg::onTableCustomContextMenuRequested);
}
PtExtraInfoDlg::~PtExtraInfoDlg()
{
delete ui;
}
void PtExtraInfoDlg::createGroupView(GroupStateInfo infos)
{
for(auto& info:infos.info) {
PropertyContentInfo inf;
inf.proTag = info.tagName;
inf.proName = info.name;
inf.proType = info.type;
_mapPro.insert(info.tagName,inf);
}
}
QMap<QString,PropertyStateInfo> PtExtraInfoDlg::getPropertyValue(BaseProperty* pPro)
{
QMap<QString,PropertyStateInfo> map;
for(auto &pro:_mapPro)
{
PropertyStateInfo info;
info.type = pro.proType;
info.name = pro.proName;
info.tagName = pro.proTag;
if(info.name == "额定电压(V)" || info.tagName == "un_v")
{
info.defaultValue = ui->le_ratedVol->text();
}
else if(info.name == "工频耐压(V/1min)" || info.tagName == "uac_v_1min")
{
info.defaultValue = ui->le_pfwv_pt->text();
}
else if(info.name == "冲击耐压(V)" || info.tagName == "uimp_v")
{
info.defaultValue = ui->le_iwv_pt->text();
}
else if(info.name == "额定电压因数" || info.tagName == "rvf")
{
info.defaultValue = ui->le_ratedVolFactor->text();
}
else if(info.name == "一次绕组接线接地方式" || info.tagName == "pwcc")
{
info.defaultValue = ui->le_pwwgm->text();
}
else if(info.name == "额定频率(Hz)" || info.tagName == "fn_hz")
{
info.defaultValue = ui->le_rf_pt->text();
}
else if(info.name == "相数" || info.tagName == "phase_num")
{
if(ui->rb_tpt_pt->isChecked())
info.defaultValue = 1;
else
info.defaultValue = 0;
}
else if(info.name == "PT二次绕组" || info.tagName == "pt_sec_winding")
{
QJsonObject object;
QJsonArray arr;
for(auto &info:_mapPT)
{
QJsonObject obj;
obj["index"] = info.index;
obj["scope"] = info.scope;
obj["accuracy"] = info.accuracy;
obj["volume"] = info.volume;
obj["star"] = info.star;
obj["ratio"] = info.ratio;
obj["polarity"] = info.polarity;
arr.push_back(obj);
}
object["winding"] = arr;
info.defaultValue = object;
}
map.insert(pro.proTag,info);
}
pPro->setDataChanged(true);
return map;
}
void PtExtraInfoDlg::setPropertyValue(QVariant var)
{
QMap<QString,PropertyStateInfo> map = var.value<QMap<QString,PropertyStateInfo>>();
for(auto &info:map)
{
if(info.name == "额定电压(V)" || info.tagName == "un_v")
{
ui->le_ratedVol->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "工频耐压(V/1min)" || info.tagName == "uac_v_1min")
{
ui->le_pfwv_pt->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "冲击耐压(V)" || info.tagName == "uimp_v")
{
ui->le_iwv_pt->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "额定电压因数" || info.tagName == "rvf")
{
ui->le_ratedVolFactor->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "一次绕组接线接地方式" || info.tagName == "pwcc")
{
int nIndex = 0;
if(info.defaultValue.toString() != "null")
nIndex = info.defaultValue.toInt();
ui->le_pwwgm->setText(QString::number(nIndex));
}
else if(info.name == "额定频率(Hz)" || info.tagName == "fn_hz")
{
ui->le_rf_pt->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "相数" || info.tagName == "phase_num")
{
if(info.defaultValue.toInt() == 1)
ui->rb_tpt_pt->setChecked(true);
else
ui->rb_spt_pt->setChecked(true);
}
else if(info.name == "PT二次绕组" || info.tagName == "pt_sec_winding")
{
QString jsonString = info.defaultValue.toString();
QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toUtf8().data());
QJsonObject jsonObject = jsonDocument.object();
QJsonObject object = jsonObject;
QJsonArray arr = object["winding"].toArray();
for (QJsonValueRef jsonObj : arr)
{
QJsonObject node = jsonObj.toObject();
int index = node["index"].toInt();
QString scope = node["scope"].toString();
QString accuracy = node["accuracy"].toString();
QString volume = node["volume"].toString();
QString star = node["star"].toString();
double ratio = node["ratio"].toDouble();
int polarity = node["polarity"].toInt();
addTableRow(scope,accuracy,volume,star,ratio,polarity,index);
}
}
}
}
void PtExtraInfoDlg::onAddClicked()
{
QString sRatioRange = ui->le_tr_range_pt->text();
QString sAccuracy= ui->le_ac_pt->text();
QString sVolume = ui->le_slc_pt->text();
QString sStar = ui->cb_wcm->currentText();
double dRatio = ui->le_tr_pt->text().toDouble();
bool bPolarity = ui->cB_polarity->checkState();
int index = -1;
if(ui->rb_spt_pt->isChecked())
index = 0;
addTableRow(sRatioRange,sAccuracy,sVolume,sStar,dRatio,bPolarity,index);
}
void PtExtraInfoDlg::onTableCustomContextMenuRequested(const QPoint &pos) {
QModelIndex index = ui->tb_pt->indexAt(pos);
if (!index.isValid()) {
return;
}
int row = index.row();
// 创建右键菜单
QMenu menu(this);
QAction *deleteAction = menu.addAction("删除此行");
// 连接删除操作
connect(deleteAction, &QAction::triggered, this, [this, row]() {
deleteRowWithReindex(row);
//updateLables();
});
menu.exec(ui->tb_pt->viewport()->mapToGlobal(pos));
}
void PtExtraInfoDlg::addTableRow(QString sRatioRange,QString sAccuracy,QString sVolume,QString sStar,double dRatio,bool bPolarity,int index)
{
/*if(_mapPT.contains(QString::number(index)))
{
return;
}
PtExtraInfo info;
if(index == -1){ //缺省id时新建否则加载
info.index = _count;
_count += 1;
}
else{
info.index = index;
}
int row = ui->tb_pt->rowCount();
ui->tb_pt->insertRow(row);
// index
QTableWidgetItem *item = new QTableWidgetItem(QString::number(info.index));
item->setData(Qt::UserRole,info.index);
ui->tb_pt->setItem(row, 0, item);
//变比范围
ui->tb_pt->setItem(row, 1, new QTableWidgetItem(sRatioRange));
//精度等级
ui->tb_pt->setItem(row, 2, new QTableWidgetItem(sAccuracy));
//二次负载容量
ui->tb_pt->setItem(row, 3, new QTableWidgetItem(sVolume));
// 变比
ui->tb_pt->setItem(row, 4, new QTableWidgetItem(QString::number(dRatio)));
// 极性
ui->tb_pt->setItem(row, 5, new QTableWidgetItem(QString::number(bPolarity? 1 : -1)));
//接线
ui->tb_pt->setItem(row, 6, new QTableWidgetItem(sStar));
info.scope = sRatioRange;
info.accuracy = sAccuracy;
info.volume = sVolume;
info.star = sStar;
info.ratio = dRatio;
info.polarity = bPolarity? 1 : -1;
_mapPT.insert(QString::number(info.index),info);*/
if (index != -1) {
if (_mapPT.contains(QString::number(index)))
return;
}
int row = ui->tb_pt->rowCount();
ui->tb_pt->insertRow(row);
int newIndex = row + 1;
PtExtraInfo info;
info.index = newIndex;
info.scope = sRatioRange;
info.accuracy = sAccuracy;
info.volume = sVolume;
info.star = sStar;
info.ratio = dRatio;
info.polarity = bPolarity ? 1 : -1;
// index 列
auto* idItem = new QTableWidgetItem(QString::number(newIndex));
idItem->setData(Qt::UserRole, newIndex);
ui->tb_pt->setItem(row, 0, idItem);
ui->tb_pt->setItem(row, 1, new QTableWidgetItem(sRatioRange));
ui->tb_pt->setItem(row, 2, new QTableWidgetItem(sAccuracy));
ui->tb_pt->setItem(row, 3, new QTableWidgetItem(sVolume));
ui->tb_pt->setItem(row, 4, new QTableWidgetItem(QString::number(dRatio)));
ui->tb_pt->setItem(row, 5, new QTableWidgetItem(QString::number(info.polarity)));
ui->tb_pt->setItem(row, 6, new QTableWidgetItem(sStar));
_mapPT.insert(QString::number(newIndex), info);
}
void PtExtraInfoDlg::deleteRowWithReindex(int row) {
// 1. 获取要删除的ID
/*QTableWidgetItem* pFirstItem = ui->tb_pt->item(row, 0);
if (!pFirstItem) return;
int deletedId = pFirstItem->data(Qt::UserRole).toInt();
QString deletedKey = QString::number(deletedId);
// 2. 从表格中删除行
ui->tb_pt->removeRow(row);
// 3. 从_mapCT中删除对应项
if (_mapPT.contains(deletedKey)) {
_mapPT.remove(deletedKey);
}
// 4. 重新排序和更新index
reorderMapAndUpdateIndices(row);*/
if (row < 0 || row >= ui->tb_pt->rowCount())
return;
// 1. 删除行
ui->tb_pt->removeRow(row);
// 2. 清空旧 map
_mapPT.clear();
// 3. 按表格顺序重建
int totalRows = ui->tb_pt->rowCount();
for (int r = 0; r < totalRows; ++r)
{
int newIndex = r + 1;
PtExtraInfo info;
info.index = newIndex;
info.scope = ui->tb_pt->item(r, 1)->text();
info.accuracy = ui->tb_pt->item(r, 2)->text();
info.volume = ui->tb_pt->item(r, 3)->text();
info.ratio = ui->tb_pt->item(r, 4)->text().toDouble();
info.polarity = (ui->tb_pt->item(r, 5)->text().toInt() > 0);
info.star = ui->tb_pt->item(r, 6)->text();
QString key = QString::number(newIndex);
_mapPT[key] = info;
// 更新显示
auto* idItem = ui->tb_pt->item(r, 0);
if (idItem) {
idItem->setText(key);
idItem->setData(Qt::UserRole, newIndex);
}
}
}