348 lines
11 KiB
C++
348 lines
11 KiB
C++
#include "kbdapcanawidget.h"
|
|
#include <QVBoxLayout>
|
|
#include "kbdtable.h"
|
|
#include "../model_common/common.h"
|
|
#include "pub_widget/MessageBox.h"
|
|
#include "kbdtabledatamgr.h"
|
|
#include <QDebug>
|
|
#include "kbdpropertypanel.h"
|
|
#include <QToolBar>
|
|
#include "kbddbdesign.h"
|
|
|
|
KbdApcAnaWidget::KbdApcAnaWidget(QWidget *parent) : QWidget(parent)
|
|
{
|
|
initLayout();
|
|
}
|
|
|
|
KbdApcAnaWidget::~KbdApcAnaWidget()
|
|
{
|
|
|
|
}
|
|
|
|
void KbdApcAnaWidget::initLayout()
|
|
{
|
|
QToolBar *bar = new QToolBar(this);
|
|
|
|
QAction* act = nullptr;
|
|
act = bar->addAction(tr("添加记录"),this,SLOT(onAddRow()));
|
|
bar->widgetForAction(act)->setObjectName("icon_add");
|
|
act = bar->addAction(tr("删除记录"),this,SLOT(onRemoveRow()));
|
|
bar->widgetForAction(act)->setObjectName("icon_delete");
|
|
|
|
m_table = new KbdTable(this,EXTERN_COMMIT);
|
|
m_table->setModel(KbdTableDataMgr::getInstance()->createModel("APC_ANALOG.xml"));
|
|
m_table->setColumnWidth(0,150);
|
|
m_table->setColumnWidth(1,50);
|
|
m_table->setColumnWidth(2,50);
|
|
m_table->setColumnWidth(3,150);
|
|
m_table->setColumnWidth(4,150);
|
|
|
|
QVBoxLayout *mainLayout = new QVBoxLayout;
|
|
mainLayout->addWidget(bar);
|
|
mainLayout->addWidget(m_table);
|
|
setLayout(mainLayout);
|
|
|
|
connect(m_table,SIGNAL(doubleClicked(const QModelIndex &)),this,SLOT(onModifyRow()));
|
|
|
|
}
|
|
|
|
void KbdApcAnaWidget::changeProperty(KbdPropertyDlg &dlg, const QString &sqlName, const QString &text)
|
|
{
|
|
if(dlg.panel()->currentModel()->getTableName().compare("APC_ANALOG",Qt::CaseInsensitive) == 0)
|
|
{
|
|
if(sqlName == CN_KEY_ID_TAG)
|
|
{
|
|
QStringList keyIdTagList = text.split(".");
|
|
if(keyIdTagList.size() != 5)
|
|
{
|
|
N_MessageBox::information(NULL,tr("消息"),tr("输出测点 %1 格式不正确,请重新输入").arg(keyIdTagList.join(".")));
|
|
return;
|
|
}
|
|
QString tagName, tableName;
|
|
QStringList locationIdList;
|
|
QStringList subSystemList;
|
|
QStringList descList;
|
|
tableName = keyIdTagList.first();
|
|
keyIdTagList.removeFirst();
|
|
keyIdTagList.removeLast();
|
|
tagName = keyIdTagList.join(".");
|
|
QString sql = QString("SELECT LOCATION_ID,SUB_SYSTEM,DESCRIPTION FROM %1 WHERE TAG_NAME = '%2';");
|
|
KbdDbDesign ds;
|
|
if(!ds.getThreeColumnResult(sql.arg(tableName).arg(tagName),locationIdList,subSystemList,descList))
|
|
{
|
|
N_MessageBox::information(NULL,tr("消息"),tr("数据库查询错误"));
|
|
return;
|
|
}
|
|
if(locationIdList.size() != 1 || subSystemList.size() != 1)
|
|
{
|
|
N_MessageBox::information(NULL,tr("消息"),tr("输出测点%1 不存在对应的车站和专业信息").arg(tagName));
|
|
return;
|
|
}
|
|
|
|
dlg.panel()->setPropertyValue(CN_LOCATION_ID,locationIdList.first());
|
|
dlg.panel()->setPropertyValue(CN_SUB_SYSTEM,subSystemList.first());
|
|
dlg.panel()->setPropertyValue(CN_DESC,descList.first());
|
|
}
|
|
else if(sqlName == CN_TIMEOUT_OUTPUT_DI)
|
|
{
|
|
if(text.isEmpty())
|
|
return;
|
|
|
|
QStringList keyIdTagList = text.split(".");
|
|
if(keyIdTagList.size() != 5)
|
|
{
|
|
N_MessageBox::information(NULL,tr("消息"),tr("超时输出测点 %1 格式不正确").arg(keyIdTagList.join(".")));
|
|
return;
|
|
}
|
|
QString tagName, tableName;
|
|
QStringList locationIdList;
|
|
QStringList subSystemList;
|
|
tableName = keyIdTagList.first();
|
|
keyIdTagList.removeFirst();
|
|
keyIdTagList.removeLast();
|
|
tagName = keyIdTagList.join(".");
|
|
QString sql = QString("SELECT LOCATION_ID,SUB_SYSTEM FROM %1 WHERE TAG_NAME = '%2';");
|
|
KbdDbDesign ds;
|
|
if(!ds.getTwoColumnResult(sql.arg(tableName).arg(tagName),locationIdList,subSystemList))
|
|
{
|
|
N_MessageBox::information(NULL,tr("消息"),tr("数据库查询错误"));
|
|
return;
|
|
}
|
|
if(locationIdList.size() != 1 || subSystemList.size() != 1)
|
|
{
|
|
N_MessageBox::information(NULL,tr("消息"),tr("超时输出测点%1 不存在对应的车站和专业信息").arg(tagName));
|
|
return;
|
|
}
|
|
const ColMgrDataList& modelCols = dlg.panel()->currentModel()->getHeadCols();
|
|
QString oldLocation = dlg.panel()->getPropertyData()
|
|
.at(modelCols.findCol(CN_LOCATION_ID));
|
|
|
|
QString oldSubSystem = dlg.panel()->getPropertyData()
|
|
.at(modelCols.findCol(CN_SUB_SYSTEM));
|
|
|
|
if(oldLocation != locationIdList.first() || oldSubSystem != subSystemList.first())
|
|
{
|
|
N_MessageBox::information(NULL,tr("消息"),tr("超时输出测点与输出测点不是同一个车站和专业"));
|
|
return;
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
bool KbdApcAnaWidget::check(const QString &keyIdTag, const QString &timeoutput)
|
|
{
|
|
//检查是否是同一位置和专业的测点
|
|
QStringList keyIdTagList = keyIdTag.split(".");
|
|
QStringList timeOutPutDiList = timeoutput.split(".");
|
|
if(keyIdTagList.size() != 5 || timeOutPutDiList.size() != 5)
|
|
{
|
|
onMsg(tr("输出测点或者超时状态输出DI点 格式不正确,请重新输入"));
|
|
return false;
|
|
}
|
|
QString tagName, tableName;
|
|
QStringList locationIdList1;
|
|
QStringList subSystemList1;
|
|
QStringList locationIdList2;
|
|
QStringList subSystemList2;
|
|
tableName = keyIdTagList.first();
|
|
keyIdTagList.removeFirst();
|
|
keyIdTagList.removeLast();
|
|
tagName = keyIdTagList.join(".");
|
|
QString sql = QString("SELECT LOCATION_ID,SUB_SYSTEM FROM %1 WHERE TAG_NAME = '%2';");
|
|
KbdDbDesign ds;
|
|
if(!ds.getTwoColumnResult(sql.arg(tableName).arg(tagName),locationIdList1,subSystemList1))
|
|
{
|
|
onMsg(tr("数据库查询错误"));
|
|
return false;
|
|
}
|
|
if(locationIdList1.size() != 1 || subSystemList1.size() != 1)
|
|
{
|
|
onMsg(tr("不存在对应的车站和专业信息"));
|
|
return false;
|
|
}
|
|
|
|
tableName = timeOutPutDiList.first();
|
|
timeOutPutDiList.removeFirst();
|
|
timeOutPutDiList.removeLast();
|
|
tagName = timeOutPutDiList.join(".");
|
|
sql = QString("SELECT LOCATION_ID,SUB_SYSTEM FROM %1 WHERE TAG_NAME = '%2';");
|
|
if(!ds.getTwoColumnResult(sql.arg(tableName).arg(tagName),locationIdList2,subSystemList2))
|
|
{
|
|
onMsg(tr("数据库查询错误"));
|
|
return false;
|
|
}
|
|
if(locationIdList2.size() != 1 || subSystemList2.size() != 1)
|
|
{
|
|
onMsg(tr("不存在对应的车站和专业信息"));
|
|
return false;
|
|
}
|
|
|
|
if(locationIdList1.first() != locationIdList2.first())
|
|
{
|
|
onMsg(tr("所选的输出测点和超时输出测点不是同一个车站"));
|
|
return false;
|
|
}
|
|
|
|
if(subSystemList1.first() != subSystemList2.first())
|
|
{
|
|
onMsg(tr("所选的输出测点和超时输出测点不是同一个专业"));
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void KbdApcAnaWidget::onAddRow()
|
|
{
|
|
KbdTableModel *model = qobject_cast<KbdTableModel *>(m_table->model());
|
|
if(model == NULL)
|
|
return;
|
|
|
|
KbdPropertyDlg dlg(false, this);
|
|
dlg.panel()->setDisplayRow(model,model->rowCount());
|
|
connect(dlg.panel(),&KbdPropertyPanel::signalPropertyChanged,
|
|
[&dlg](const QString &sqlName, const QString &text)
|
|
{
|
|
changeProperty(dlg,sqlName,text);
|
|
});
|
|
if(dlg.exec() == QDialog::Rejected)
|
|
return;
|
|
|
|
QStringList rowDatas = dlg.panel()->getPropertyData();
|
|
if(rowDatas.isEmpty())
|
|
return;
|
|
const ColMgrDataList& modelCols = model->getHeadCols();
|
|
if(!check(rowDatas.at(modelCols.findCol(CN_KEY_ID_TAG)),rowDatas.at(modelCols.findCol(CN_TIMEOUT_OUTPUT_DI))))
|
|
return;
|
|
|
|
|
|
bool success = false;
|
|
QString retStr = KbdTableDataMgr::getInstance()->startTransaction();
|
|
if(retStr.isEmpty())
|
|
{
|
|
success = model->addRow(rowDatas,true);
|
|
|
|
if(KbdTableDataMgr::getInstance()->commit()?false:((success=false)||true))
|
|
onCommitError();
|
|
}
|
|
else
|
|
onStartTransactionError(retStr);
|
|
|
|
if(!success)
|
|
{
|
|
onMsg(tr("添加失败"));
|
|
return;
|
|
}
|
|
|
|
|
|
}
|
|
|
|
void KbdApcAnaWidget::onRemoveRow()
|
|
{
|
|
if(N_MessageBox::No == N_MessageBox::information(this,tr("消息"),
|
|
tr("是否删除?"),
|
|
N_MessageBox::Yes,N_MessageBox::No))
|
|
return;
|
|
|
|
KbdTableModel *model = qobject_cast<KbdTableModel *>(m_table->model());
|
|
if(model == NULL)
|
|
return;
|
|
|
|
QList<int> list;
|
|
foreach (QModelIndex index, m_table->selectionModel()->selectedRows()) {
|
|
list<< index.row();
|
|
}
|
|
if(list.isEmpty())
|
|
return;
|
|
|
|
//删除表同时删除对应的列
|
|
bool success = false;
|
|
QString retStr = KbdTableDataMgr::getInstance()->startTransaction();
|
|
if(retStr.isEmpty())
|
|
{
|
|
success = model->removeTableRows(list);
|
|
|
|
if(KbdTableDataMgr::getInstance()->commit()?false:((success=false)||true))
|
|
onCommitError();
|
|
}
|
|
else
|
|
onStartTransactionError(retStr);
|
|
|
|
if(!success)
|
|
{
|
|
onMsg(tr("删除失败"));
|
|
return;
|
|
}
|
|
|
|
}
|
|
|
|
void KbdApcAnaWidget::onModifyRow()
|
|
{
|
|
KbdTableModel *model = qobject_cast<KbdTableModel *>(m_table->model());
|
|
if(model == NULL)
|
|
return;
|
|
QModelIndex index = m_table->currentIndex();
|
|
if(!index.isValid())
|
|
return;
|
|
|
|
KbdPropertyDlg dlg(false, this);
|
|
dlg.panel()->setDisplayRow(model,index.row());
|
|
connect(dlg.panel(),&KbdPropertyPanel::signalPropertyChanged,
|
|
[&dlg](const QString &sqlName, const QString &text)
|
|
{
|
|
changeProperty(dlg,sqlName,text);
|
|
});
|
|
if(dlg.exec() == QDialog::Rejected)
|
|
return;
|
|
|
|
QStringList rowDatas = dlg.panel()->getPropertyData();
|
|
if(rowDatas.isEmpty())
|
|
return;
|
|
|
|
const ColMgrDataList& modelCols = model->getHeadCols();
|
|
if(!check(rowDatas.at(modelCols.findCol(CN_KEY_ID_TAG)),rowDatas.at(modelCols.findCol(CN_TIMEOUT_OUTPUT_DI))))
|
|
return;
|
|
|
|
//需要判断名称是否已修改
|
|
bool success = true;
|
|
QString retStr = KbdTableDataMgr::getInstance()->startTransaction();
|
|
if(retStr.isEmpty())
|
|
{
|
|
success = model->modifyRow(index.row(),rowDatas,true);
|
|
|
|
if(KbdTableDataMgr::getInstance()->commit()?false:((success=false)||true))
|
|
onCommitError();
|
|
}
|
|
else
|
|
onStartTransactionError(retStr);
|
|
|
|
if(!success)
|
|
{
|
|
onMsg(tr("修改失败"));
|
|
return;
|
|
}
|
|
}
|
|
|
|
void KbdApcAnaWidget::onStartTransactionError(const QString&erroStr)
|
|
{
|
|
N_MessageBox::information(this,tr("消息"),erroStr);
|
|
}
|
|
|
|
void KbdApcAnaWidget::onMsg(const QString &text)
|
|
{
|
|
N_MessageBox::information(this,tr("消息"),text);
|
|
}
|
|
|
|
void KbdApcAnaWidget::onCommitError()
|
|
{
|
|
N_MessageBox::information(this,tr("消息"),tr("此次修改失败,已启动回滚!"));
|
|
}
|
|
|
|
void KbdApcAnaWidget::onRollbackError()
|
|
{
|
|
N_MessageBox::information(this,tr("消息"),tr("回滚事务失败!"));
|
|
}
|
|
|