GridFrame/diagramCavas/source/ctExtraInfoDlg.cpp

369 lines
12 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 "ctExtraInfoDlg.h"
#include "ui_ctExtraInfoDlg.h"
#include "baseProperty.h"
#include "basePropertyManager.h"
#include <QButtonGroup>
#include <QJsonArray>
#include <QJsonDocument>
#include <QMenu>
CtExtraInfoDlg::CtExtraInfoDlg(QWidget *parent)
: BaseContentDlg(parent)
, ui(new Ui::ctExtraInfoDlg)
{
ui->setupUi(this);
_stateGroup_ct = new QButtonGroup(this);
_stateGroup_ct->addButton(ui->rb_tpt_ct,1);
_stateGroup_ct->addButton(ui->rb_zst_ct,0);
connect(ui->btn_add_ct,&QPushButton::clicked,this,&CtExtraInfoDlg::onAddClicked);
ui->tb_ct->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
_count = 1;
ui->tb_ct->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui->tb_ct, &QTableWidget::customContextMenuRequested, this, &CtExtraInfoDlg::onTableCustomContextMenuRequested);
}
CtExtraInfoDlg::~CtExtraInfoDlg()
{
delete ui;
}
void CtExtraInfoDlg::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> CtExtraInfoDlg::getPropertyValue(BaseProperty* pPro)
{
QMap<QString,PropertyStateInfo> map;
for(auto &pro:_mapPro)
{
PropertyStateInfo info;
info.tagName = pro.proTag;
info.type = pro.proType;
info.name = pro.proName;
if(info.name == "额定电流(A)" || info.tagName == "in_a") //此处应为类型名
{
info.defaultValue = ui->le_ratedCurrent->text();
}
else if(info.name == "工频耐压(V/1min)" || info.tagName == "uac_v_1min")
{
info.defaultValue = ui->le_pfwv_ct->text();
}
else if(info.name == "冲击耐压(V)" || info.tagName == "uimp_v")
{
info.defaultValue = ui->le_iwv_ct->text();
}
else if(info.name == "动稳定电流(A)" || info.tagName == "dsc_a")
{
info.defaultValue = ui->le_dsc_ct->text();
}
else if(info.name == "仪表保安系数" || info.tagName == "fs")
{
info.defaultValue = ui->le_isf->text();
}
else if(info.name == "热稳定电流(A)" || info.tagName == "ith_a")
{
info.defaultValue = ui->le_sttc->text();
}
else if(info.name == "额定频率(Hz)" || info.tagName == "fn_hz")
{
info.defaultValue = ui->le_rf_ct->text();
}
else if(info.name == "相数" || info.tagName == "phase_num")
{
if(ui->rb_tpt_ct->isChecked())
info.defaultValue = QVariant(1);
else
info.defaultValue = QVariant(0);
}
else if(info.name == "CT绕组" || info.tagName == "ct_winding")
{
QJsonObject object;
QJsonArray arr;
for(auto &info:_mapCT)
{
QJsonObject obj;
obj["index"] = info.index;
obj["scope"] = info.scope;
obj["accuracy"] = info.accuracy;
obj["volume"] = info.volume;
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 CtExtraInfoDlg::setPropertyValue(QVariant var)
{
QMap<QString,PropertyStateInfo> map = var.value<QMap<QString,PropertyStateInfo>>();
for(auto &info:map)
{
if(info.name == "额定电流(A)" || info.tagName == "in_a") //此处应为类型名
{
ui->le_ratedCurrent->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "工频耐压(V/1min)" || info.tagName == "uac_v_1min")
{
ui->le_pfwv_ct->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "冲击耐压(V)" || info.tagName == "uimp_v")
{
ui->le_iwv_ct->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "动稳定电流(A)" || info.tagName == "dsc_a")
{
ui->le_dsc_ct->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "仪表保安系数" || info.tagName == "fs")
{
if(info.defaultValue.toString() == "null")
ui->le_isf->setText(0);
ui->le_isf->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "热稳定电流(A)" || info.tagName == "ith_a")
{
ui->le_sttc->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "额定频率(Hz)" || info.tagName == "fn_hz")
{
ui->le_rf_ct->setText(QString::number(info.defaultValue.toDouble()));
}
else if(info.name == "相数" || info.tagName == "phase_num")
{
if(info.defaultValue.toInt() == 1)
ui->rb_tpt_ct->setChecked(true);
else
ui->rb_zst_ct->setChecked(true);
}
else if(info.name == "CT绕组" || info.tagName == "ct_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 sRatioRange = node["scope"].toString();
QString sAccuracy = node["accuracy"].toString();
QString sVolume = node["volume"].toString();
double dRatio = node["ratio"].toDouble();
bool bPolarity = node["polarity"].toBool();
addTableRow(sRatioRange,sAccuracy,sVolume,dRatio,bPolarity,index);
}
}
}
}
void CtExtraInfoDlg::onAddClicked()
{
QString sRatioRange = ui->le_tr_range_ct->text();
QString sAccuracy= ui->le_ac_ct->text();
QString sVolume = ui->le_slc_ct->text();
double dRatio = ui->le_tr_ct->text().toDouble();
bool bPolarity = ui->cb_polarity->checkState();
int index = -1;
if(ui->rb_zst_ct->isChecked())
index = 0;
addTableRow(sRatioRange,sAccuracy,sVolume,dRatio,bPolarity,index);
}
void CtExtraInfoDlg::onTableCustomContextMenuRequested(const QPoint &pos) {
QModelIndex index = ui->tb_ct->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_ct->viewport()->mapToGlobal(pos));
}
void CtExtraInfoDlg::addTableRow(QString sRatioRange,QString sAccuracy,QString sVolume,double dRatio,bool bPolarity,int index)
{
/*if(_mapCT.contains(QString::number(index)))
{
return;
}
CtExtraInfo info;
if(index == -1){ //缺省id时新建否则加载
info.index = _count;
_count += 1;
}
else{
info.index = index;
}
int row = ui->tb_ct->rowCount();
ui->tb_ct->insertRow(row);
//index
QTableWidgetItem *item = new QTableWidgetItem(QString::number(info.index));
item->setData(Qt::UserRole,info.index);
ui->tb_ct->setItem(row, 0, item);
//变比范围
ui->tb_ct->setItem(row, 1, new QTableWidgetItem(sRatioRange));
//精度等级
ui->tb_ct->setItem(row, 2, new QTableWidgetItem(sAccuracy));
//二次负载容量
ui->tb_ct->setItem(row, 3, new QTableWidgetItem(sVolume));
//变比
ui->tb_ct->setItem(row, 4, new QTableWidgetItem(QString::number(dRatio)));
//极性
ui->tb_ct->setItem(row, 5, new QTableWidgetItem(QString::number(bPolarity? 1 : -1)));
info.scope = sRatioRange;
info.accuracy = sAccuracy;
info.volume = sVolume;
info.ratio = dRatio;
info.polarity = bPolarity? 1 : -1;
_mapCT.insert(QString::number(info.index),info);
updateLables();*/
if (index != -1) {
if (_mapCT.contains(QString::number(index)))
return;
}
int row = ui->tb_ct->rowCount();
ui->tb_ct->insertRow(row);
// index 永远等于 row + 1
int newIndex = row + 1;
CtExtraInfo info;
info.index = newIndex;
info.scope = sRatioRange;
info.accuracy = sAccuracy;
info.volume = sVolume;
info.ratio = dRatio;
info.polarity = bPolarity ? 1 : -1;
// 表格显示
auto* idItem = new QTableWidgetItem(QString::number(newIndex));
idItem->setData(Qt::UserRole, newIndex);
ui->tb_ct->setItem(row, 0, idItem);
ui->tb_ct->setItem(row, 1, new QTableWidgetItem(sRatioRange));
ui->tb_ct->setItem(row, 2, new QTableWidgetItem(sAccuracy));
ui->tb_ct->setItem(row, 3, new QTableWidgetItem(sVolume));
ui->tb_ct->setItem(row, 4, new QTableWidgetItem(QString::number(dRatio)));
ui->tb_ct->setItem(row, 5, new QTableWidgetItem(QString::number(info.polarity)));
// map 同步
_mapCT.insert(QString::number(newIndex), info);
updateLables();
}
void CtExtraInfoDlg::updateShowLabel(QStringList lst)
{
_curLabels = lst;
ui->label_title_ct->setText(_curLabels.join("\n"));
int h = ui->label_title_ct->heightForWidth(ui->label_title_ct->width());
ui->label_title_ct->setFixedHeight(h);
}
void CtExtraInfoDlg::updateLables()
{
QStringList lst; //更新显示标签
for(auto& info:_mapCT){
QString sLabel = info.scope+" "+info.accuracy+" "+info.volume;
lst.append(sLabel);
}
updateShowLabel(lst);
}
void CtExtraInfoDlg::deleteRowWithReindex(int row) {
// 1. 获取要删除的ID
/*QTableWidgetItem* pFirstItem = ui->tb_ct->item(row, 0);
if (!pFirstItem) return;
int deletedId = pFirstItem->data(Qt::UserRole).toInt();
QString deletedKey = QString::number(deletedId);
// 2. 从表格中删除行
ui->tb_ct->removeRow(row);
// 3. 从_mapCT中删除对应项
if (_mapCT.contains(deletedKey)) {
_mapCT.remove(deletedKey);
}
// 4. 重新排序和更新index
reorderMapAndUpdateIndices(row);*/
if (row < 0 || row >= ui->tb_ct->rowCount())
return;
// 1. 删除行
ui->tb_ct->removeRow(row);
// 2. 清空旧数据
_mapCT.clear();
// 3. 按当前表格顺序重建 index 和 map
int totalRows = ui->tb_ct->rowCount();
for (int r = 0; r < totalRows; ++r)
{
int newIndex = r + 1;
CtExtraInfo info;
info.index = newIndex;
info.scope = ui->tb_ct->item(r, 1)->text();
info.accuracy = ui->tb_ct->item(r, 2)->text();
info.volume = ui->tb_ct->item(r, 3)->text();
info.ratio = ui->tb_ct->item(r, 4)->text().toDouble();
info.polarity = (ui->tb_ct->item(r, 5)->text().toInt() > 0);
QString key = QString::number(newIndex);
_mapCT[key] = info;
// 更新显示
auto* idItem = ui->tb_ct->item(r, 0);
if (idItem) {
idItem->setText(key);
idItem->setData(Qt::UserRole, newIndex);
}
}
updateLables();
}