2025-03-13 15:19:51 +08:00

372 lines
12 KiB
C++

#include <cmath>
#include "kbdctrlactwidget.h"
#include "CustomWidget/kbdtreeview.h"
#include "CustomWidget/kbdsqltreemodel.h"
#include <QSplitter>
#include <QVBoxLayout>
#include <QToolBar>
#include "model_common/common.h"
#include "kbdtable.h"
#include "kbdtabledatamgr.h"
#include "pub_widget/MessageBox.h"
#include "kbdpropertydlg.h"
#include "kbdpropertypanel.h"
#include "kbdmsgcontrl.h"
KbdCtrlActWidget::KbdCtrlActWidget(QWidget *parent) : QWidget(parent)
{
initLayout();
}
void KbdCtrlActWidget::showEvent(QShowEvent *event)
{
m_tree->reload();
m_tree->expandAll();
m_table->setModel(NULL);
if(m_tree->model()->index(0,0).child(0,0).isValid())
{
m_tree->setCurrentIndex(m_tree->model()->index(0,0).child(0,0));
onTreeClick(m_tree->currentIndex());
}
QWidget::showEvent(event);
}
void KbdCtrlActWidget::initLayout()
{
QSplitter *hSplitter = new QSplitter(Qt::Horizontal,this);
QWidget *widgetTree = new QWidget(hSplitter);
QVBoxLayout *vLayout1 = new QVBoxLayout;
QToolBar *bar1 = new QToolBar(widgetTree);
m_tree = new KbdTreeView(widgetTree);
m_treeModel = new KbdSqlTreeModel(Common::getTreePath("ctrlActDefine.xml"),this);
m_tree->setModel(m_treeModel);
vLayout1->addWidget(bar1);
vLayout1->addWidget(m_tree);
vLayout1->setMargin(0);
widgetTree->setLayout(vLayout1);
QWidget *widgetDetail = new QWidget(hSplitter);
QVBoxLayout *vlayout2 = new QVBoxLayout;
QToolBar *bar2 = new QToolBar(widgetDetail);
m_table = new KbdTable(widgetDetail,REWRITE_COMMIT);
vlayout2->addWidget(bar2);
vlayout2->addWidget(m_table);
vlayout2->setMargin(0);
widgetDetail->setLayout(vlayout2);
hSplitter->addWidget(widgetTree);
hSplitter->addWidget(widgetDetail);
hSplitter->setSizes(QList<int>()<<200<<600);
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->setMargin(0);
mainLayout->addWidget(hSplitter);
setLayout(mainLayout);
QAction* act = nullptr;
act = bar1->addAction(tr("添加记录"),this,SLOT(onAddTree()));
bar1->widgetForAction(act)->setObjectName("icon_add");
act = bar1->addAction(tr("修改记录"),this,SLOT(onModifyTree()));
bar1->widgetForAction(act)->setObjectName("icon_edit");
act = bar1->addAction(tr("删除记录"),this,SLOT(onRemoveTree()));
bar1->widgetForAction(act)->setObjectName("icon_delete");
act = bar2->addAction(tr("添加记录"),this,SLOT(onAddTable()));
bar2->widgetForAction(act)->setObjectName("icon_add");
act = bar2->addAction(tr("删除记录"),m_table,SLOT(onRemoveRowNotCommit()));
bar2->widgetForAction(act)->setObjectName("icon_delete");
act = bar2->addAction(tr("撤销更改"),m_table,SLOT(onUndo()));
bar2->widgetForAction(act)->setObjectName("icon_undo");
act = bar2->addAction(tr("保存"),this,SLOT(onSaveTable()));
bar2->widgetForAction(act)->setObjectName("icon_save");
connect(m_tree,SIGNAL(clicked(const QModelIndex &)),this,SLOT(onTreeClick(const QModelIndex &)));
connect(m_tree,SIGNAL(doubleClicked(const QModelIndex &)),this,SLOT(onModifyTree()));
}
void KbdCtrlActWidget::onTreeClick(const QModelIndex &index)
{
//当前处在叶子节点,树的结构必须只有两级
if(index.isValid() && (index.parent().isValid()))
{
QString filter = QString("%1='%2' ORDER BY CTRL_GRP_NO").arg(CN_CTRL_GRP_NAME).arg(index.data(Common::IDRole).toString());
KbdTableModel *model = KbdTableDataMgr::getInstance()
->createModel(Common::createViewName(TBL_OPT_CTRL_ACT_DEFINE,filter));
m_table->setModel(model);
}
else
{
m_table->setModel(NULL);
}
}
void KbdCtrlActWidget::onAddTree()
{
QModelIndex index = m_tree->currentIndex();
if(!index.isValid())
{
onMsg(tr("请选中要添加的专业"));
return;
}
//确保index是专业节点
if(index.parent().isValid())
index = index.parent();
KbdTableModel *model = KbdTableDataMgr::getInstance()->createModel(TBL_OPT_CTRL_ACT_DEFINE);
if(model == NULL)
return;
const ColMgrDataList& modelCols = model->getHeadCols();
//要显示的列的索引
KbdPropertyDlg dlg(false, this);
dlg.panel()->setDisplayRow(model,model->rowCount(), modelCols.getGroup(), modelCols.getShowCol(true));
dlg.panel()->setPropertyValue(CN_SUB_SYSTEM,index.data(Common::IDRole).toString());
dlg.panel()->setPropertyEditable(CN_CTRL_GRP_NAME,true);
dlg.panel()->setPropertyEditable(CN_VALUE_NUM,true);
if(dlg.exec() != QDialog::Accepted)
return;
QStringList panelDatas = dlg.panel()->getPropertyData();
bool success = false;
QString retStr = KbdTableDataMgr::getInstance()->startTransaction();
if(retStr.isEmpty())
{
success = model->addRow(panelDatas);
if(success)
{
//根据分量数,自动生成其他分量
int valueNum = panelDatas.at(modelCols.findCol(CN_VALUE_NUM)).toInt();
int colTargetValue = modelCols.findCol(CN_TARGET_VALUE);
int colCtrlGrpNo = modelCols.findCol(CN_CTRL_GRP_NO);
int colCtrlActName = modelCols.findCol(CN_CTRL_ACT_NAME);
int colCtrlIndex = modelCols.findCol(CN_CTRL_INDEX);
//第一个已经添加
for(int i = 1; i < valueNum; i++)
{
panelDatas.replace(colTargetValue,QString::number(pow(2,i)));
panelDatas.replace(colCtrlGrpNo,
QString::number(panelDatas.at(colCtrlGrpNo).toInt() + 1));
panelDatas.replace(colCtrlActName,tr("动作名%1").arg(i+1));
panelDatas.replace(colCtrlIndex,
QString::number(panelDatas.at(colCtrlIndex).toInt() + 1));
success = model->addRow(panelDatas);
if(!success)
break;
}
}
if(KbdTableDataMgr::getInstance()->commit()?false:((success=false)||true))
onCommitError();
}
else
onStartTransactionError(retStr);
if(!success)
return;
//刷新控件
int row = index.row();
m_tree->reload();
m_tree->expand(m_treeModel->index(row,0));
m_table->setModel(NULL);
}
void KbdCtrlActWidget::onModifyTree()
{
//只允许修改动作组名称
QModelIndex index = m_tree->currentIndex();
if(!index.isValid())
return;
//确保选中数字量文本的节点
if(!index.parent().isValid())
return;
KbdTableModel *model = KbdTableDataMgr::getInstance()->createModel(TBL_OPT_CTRL_ACT_DEFINE);
if(model == NULL)
return;
const ColMgrDataList& modelCols = model->getHeadCols();
//直接设置属性对话框的值
KbdPropertyDlg dlg(false, this);
dlg.panel()->setCheckRule(KbdPropertyPanel::NoCheck);
dlg.panel()->setDisplayRow(model,model->rowCount(), modelCols.getGroup(),
QList<int>()<<modelCols.findCol(CN_CTRL_GRP_NAME));
dlg.panel()->setPropertyValue(CN_CTRL_GRP_NAME,Common::getIDData(index));
dlg.panel()->setPropertyEditable(CN_CTRL_GRP_NAME,true);
if(dlg.exec() != QDialog::Accepted)
return;
QString grpName = dlg.panel()->getPropertyData().at(modelCols.findCol(CN_CTRL_GRP_NAME));
if(grpName.isEmpty())
{
onMsg(tr("组名不允许为空!"));
return;
}
bool success = false;
QMap<QString,QString> valueMap;
QMap<QString,QString> filterMap;
valueMap.insert(CN_CTRL_GRP_NAME,grpName);
filterMap.insert(CN_CTRL_GRP_NAME,Common::getIDData(index));
QString retStr = KbdTableDataMgr::getInstance()->startTransaction();
if(retStr.isEmpty())
{
//要显示的列的索引
success = model->modifyRow(valueMap,filterMap);
if(KbdTableDataMgr::getInstance()->commit()?false:((success=false)||true))
onCommitError();
}
else
onStartTransactionError(retStr);
if(!success)
{
onMsg(tr("修改失败"));
return;
}
//刷新控件
m_treeModel->setData(index,grpName);
m_treeModel->setData(index,grpName,Common::IDRole);
onTreeClick(index);
KbdMsgContrl::getInstance()->showMsg(tr("修改成功"),this);
}
void KbdCtrlActWidget::onRemoveTree()
{
QUESTION_REMOVE(this)
QModelIndex index = m_tree->currentIndex();
if(!index.isValid())
return;
//确保选中数字量文本的节点
if(!index.parent().isValid())
{
onMsg(tr("请选中要删除的数字量文本"));
return;
}
KbdTableModel *model = KbdTableDataMgr::getInstance()->createModel(TBL_OPT_CTRL_ACT_DEFINE);
if(model == NULL)
return;
QMap<QString,QString > filterMap;
filterMap.insert(CN_CTRL_GRP_NAME,index.data(Common::IDRole).toString());
bool success = false;
QString retStr = KbdTableDataMgr::getInstance()->startTransaction();
if(retStr.isEmpty())
{
//要显示的列的索引
success = model->removeTableRows(filterMap);
if(KbdTableDataMgr::getInstance()->commit()?false:((success=false)||true))
onCommitError();
}
else
onStartTransactionError(retStr);
if(!success)
{
onMsg(tr("删除失败"));
return;
}
//刷新控件
int row = index.parent().row();
m_tree->reload();
m_tree->expand(m_treeModel->index(row,0));
m_table->setModel(NULL);
KbdMsgContrl::getInstance()->showMsg(tr("删除成功"),this);
}
void KbdCtrlActWidget::onAddTable()
{
KbdTableModel *model = qobject_cast<KbdTableModel *>(m_table->model());
if(model == NULL)
return;
QModelIndex indexTail = m_tree->currentIndex();
if(!indexTail.isValid())
return;
if(!indexTail.parent().isValid())
{
onMsg(tr("请选中对应的控制动作组"));
return;
}
QModelIndex indexSubSystem = indexTail.parent();
if(!indexSubSystem.isValid())
return;
if(model->rowCount() <= 0)
{
onMsg(tr("无法获得分量数"));
return;
}
const ColMgrDataList& modelCols = model->getHeadCols();
QStringList firstRows = model->getRowData(0);
QString valueNum = firstRows.at(modelCols.findCol(CN_VALUE_NUM));
// if(valueNum.toInt() <= model->rowCount())
// {
// onMsg(tr("该控制动作只允许创建")+valueNum+tr("个分量"));
// return;
// }
QStringList rowDatas = model->createDefaultValue();
rowDatas.replace(modelCols.findCol(CN_SUB_SYSTEM),indexSubSystem.data(Common::IDRole).toString());
rowDatas.replace(modelCols.findCol(CN_CTRL_GRP_NAME),indexTail.data(Common::IDRole).toString());
rowDatas.replace(modelCols.findCol(CN_VALUE_NUM),valueNum);
m_table->onAddRowNotCommit(rowDatas);
}
void KbdCtrlActWidget::onSaveTable()
{
if(m_table->saveWithReWrite(false))
{
QAbstractItemModel * m = m_table->model();
if(m)
{
if(m->rowCount() == 0)
{
QModelIndex index = m_tree->currentIndex();
QModelIndex rootIndex = index.parent().isValid()?index.parent():index;
int row = rootIndex.row();
m_table->setModel(NULL);
m_tree->reload();
m_tree->expand(m_treeModel->index(row,0));
}
}
}
}
void KbdCtrlActWidget::onMsg(const QString &msg)
{
N_MessageBox::information(this,tr("消息"),msg);
}
void KbdCtrlActWidget::onCommitError()
{
N_MessageBox::information(this,tr("消息"),tr("此次修改失败,已启动回滚!"));
}
void KbdCtrlActWidget::onStartTransactionError(const QString&erroStr)
{
N_MessageBox::information(this,tr("消息"),erroStr);
}