267 lines
9.0 KiB
C++
267 lines
9.0 KiB
C++
|
|
#include "CreateNewTableDlg.h"
|
|||
|
|
#include "DataHelper.h"
|
|||
|
|
#include "LogIO.h"
|
|||
|
|
#include "dataslothelper.h"
|
|||
|
|
extern DEFAULTTABLE *DefauleTable;//[DefTabNum];
|
|||
|
|
extern int DefTabNum;
|
|||
|
|
/*
|
|||
|
|
CCreateNewTableDlg::CCreateNewTableDlg(QWidget *parent)
|
|||
|
|
: QDialog(parent)
|
|||
|
|
{
|
|||
|
|
ui.setupUi(this);
|
|||
|
|
}
|
|||
|
|
*/
|
|||
|
|
CCreateNewTableDlg::~CCreateNewTableDlg()
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
CCreateNewTableDlg::CCreateNewTableDlg(QDialog *parent)
|
|||
|
|
: QDialog(parent)
|
|||
|
|
{
|
|||
|
|
//m_pUI = new Ui_CCreateNewTableDlg();
|
|||
|
|
m_pUI.setupUi(this);
|
|||
|
|
QObject::connect(m_pUI.okbtn, SIGNAL(clicked()), this, SLOT(slotSave()));
|
|||
|
|
QObject::connect(m_pUI.cancelbtn, SIGNAL(clicked()), this, SLOT(slotCancel()));
|
|||
|
|
QObject::connect(m_pUI.comboBox,SIGNAL(currentIndexChanged(int)),this, SLOT(slotChangeTableType(int)));
|
|||
|
|
QObject::connect(m_pUI.tableWidget, SIGNAL(cellClicked(int,int)), this, SLOT(slotGetCell(int,int)));
|
|||
|
|
QObject::connect(m_pUI.pushButton_Add, SIGNAL(clicked()), this, SLOT(slotAdd()));
|
|||
|
|
QObject::connect(m_pUI.pushButton_Delete, SIGNAL(clicked()), this, SLOT(slotDelete()));
|
|||
|
|
QObject::connect(m_pUI.pushButton_Up, SIGNAL(clicked()), this, SLOT(slotUp()));
|
|||
|
|
QObject::connect(m_pUI.pushButton_Down, SIGNAL(clicked()), this, SLOT(slotDown()));
|
|||
|
|
m_pUI.tableWidget->setColumnCount(6);
|
|||
|
|
QStringList header;
|
|||
|
|
header<<"字段名"<<"字段描述"<<"字段单位"<<"单位别名"<<"字段类型"<<"字段长度";
|
|||
|
|
m_pUI.tableWidget->setHorizontalHeaderLabels(header);
|
|||
|
|
m_pUI.tableWidget->horizontalHeader()->sectionResizeMode(QHeaderView::Stretch);
|
|||
|
|
for(int i=0;i<DefTabNum;i++)
|
|||
|
|
{
|
|||
|
|
m_pUI.comboBox->addItem(DefauleTable[i].TableAliasName);//系统缺省表列表
|
|||
|
|
}
|
|||
|
|
m_pUI.comboBox->setCurrentIndex(0);
|
|||
|
|
}
|
|||
|
|
void CCreateNewTableDlg::slotComboxSelectChange(int index)
|
|||
|
|
{
|
|||
|
|
if(! m_pUI.tableWidget->isActiveWindow())
|
|||
|
|
return;
|
|||
|
|
//刷新当前字段内容
|
|||
|
|
|
|||
|
|
//QWidget *widget=m_pUI.tableWidget->cellWidget(row,col);
|
|||
|
|
QComboBox *combox=(QComboBox*)sender();
|
|||
|
|
int row=combox->property("row").toInt();
|
|||
|
|
int col=combox->property("col").toInt();
|
|||
|
|
int colIndex=index;
|
|||
|
|
if(index!=5)//STRING
|
|||
|
|
{
|
|||
|
|
m_pUI.tableWidget->setItem(row,5,new QTableWidgetItem(QString::number(RepSize[index+1])));
|
|||
|
|
m_pUI.tableWidget->item(row,5)->setFlags(
|
|||
|
|
m_pUI.tableWidget->item(row,5)->flags()&(~Qt::ItemIsEditable)
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
m_pUI.tableWidget->item(row,5)->setFlags(
|
|||
|
|
m_pUI.tableWidget->item(row,5)->flags()|Qt::ItemIsEditable|Qt::ItemIsEnabled);
|
|||
|
|
//m_pUI.tableWidget->editItem(m_pUI.tableWidget->item(row,5));
|
|||
|
|
}
|
|||
|
|
//m_pUI.tableWidget->item(row,5)->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEditable);
|
|||
|
|
}
|
|||
|
|
void CCreateNewTableDlg::slotGetCell(int row,int column)
|
|||
|
|
{
|
|||
|
|
if(column!=4)return;
|
|||
|
|
QString oldType=m_pUI.tableWidget->item(row,column)->text();
|
|||
|
|
QComboBox *comboBox = new QComboBox();
|
|||
|
|
for(int i=0;i<11;i++)
|
|||
|
|
{
|
|||
|
|
comboBox->addItem(QString(Rep_STR[i]));
|
|||
|
|
}
|
|||
|
|
connect(comboBox,SIGNAL(currentIndexChanged(int)),this,SLOT(slotComboxSelectChange(int)));
|
|||
|
|
comboBox->setProperty("row",row);
|
|||
|
|
comboBox->setProperty("col",column);
|
|||
|
|
for(int i=0;i<11;i++)
|
|||
|
|
{
|
|||
|
|
if(oldType==QString(Rep_STR[i]))comboBox->setCurrentIndex(i);
|
|||
|
|
}
|
|||
|
|
m_pUI.tableWidget->setCellWidget(row,column, comboBox );
|
|||
|
|
}
|
|||
|
|
void CCreateNewTableDlg::slotChangeTableType(int No)
|
|||
|
|
{
|
|||
|
|
m_pUI.lineEdit_Name->setText(DefauleTable[No].TableName);
|
|||
|
|
m_pUI.lineEdit_Name_2->setText(DefauleTable[No].TableAliasName);
|
|||
|
|
int fNum=DefauleTable[No].ZdNum;
|
|||
|
|
m_pUI.tableWidget->clearContents();
|
|||
|
|
m_pUI.tableWidget->setRowCount(fNum);
|
|||
|
|
m_rowCount=m_pUI.tableWidget->rowCount();
|
|||
|
|
for(int i=0;i<fNum;i++)
|
|||
|
|
{
|
|||
|
|
m_pUI.tableWidget->setItem(i,0,new QTableWidgetItem(QString(DefauleTable[No].tinfo[i].Name)));
|
|||
|
|
m_pUI.tableWidget->setItem(i,1,new QTableWidgetItem(QString(DefauleTable[No].tinfo[i].HZName)));
|
|||
|
|
m_pUI.tableWidget->setItem(i,2,new QTableWidgetItem(QString(DefauleTable[No].tinfo[i].Unit)));
|
|||
|
|
m_pUI.tableWidget->setItem(i,3,new QTableWidgetItem(QString(DefauleTable[No].tinfo[i].HZUnit)));
|
|||
|
|
QString DataType=QString(Rep_STR[DefauleTable[No].tinfo[i].RepCode-1]);
|
|||
|
|
m_pUI.tableWidget->setItem(i,4,new QTableWidgetItem(DataType));
|
|||
|
|
m_pUI.tableWidget->setItem(i,5,new QTableWidgetItem(QString::number(DefauleTable[No].tinfo[i].CodeLength)));
|
|||
|
|
if(DataType=="STRING")
|
|||
|
|
{
|
|||
|
|
m_pUI.tableWidget->editItem(m_pUI.tableWidget->item(i,5));
|
|||
|
|
//m_pUI.tableWidget->item(i,5)->setFlags(m_pUI.tableWidget->item(i,5)->flags()&(Qt::ItemIsEditable));//ItemIsEnabled));
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
m_pUI.tableWidget->item(i,5)->setFlags(Qt::ItemIsSelectable|Qt::NoItemFlags);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
void CCreateNewTableDlg::slotSave()
|
|||
|
|
{
|
|||
|
|
TableName=m_pUI.lineEdit_Name->text().toUpper();
|
|||
|
|
if(TableName=="")
|
|||
|
|
{
|
|||
|
|
QMessageBox::warning(NULL,"警告!","表名不能为空!");return;
|
|||
|
|
}
|
|||
|
|
int fNum=m_pUI.tableWidget->rowCount();
|
|||
|
|
if(!fNum)
|
|||
|
|
{
|
|||
|
|
QMessageBox::warning(NULL,"警告!","没有表字段!");return;
|
|||
|
|
}
|
|||
|
|
CLogIO logio(FileName.toStdString().c_str(),(unsigned int)CLogIO::modeReadWrite);
|
|||
|
|
if(!logio.mFile)
|
|||
|
|
{
|
|||
|
|
QMessageBox::warning(NULL,"警告!",FileName+"\r\n打开井次文件失败!");return ;
|
|||
|
|
}
|
|||
|
|
if(logio.FindObjectIndex(TableName.toStdString().c_str())>=0)
|
|||
|
|
{
|
|||
|
|
QMessageBox::warning(NULL,"警告!","您要产生的表名称已存在!");return;
|
|||
|
|
}
|
|||
|
|
QString TableHzName=m_pUI.lineEdit_Name_2->text();
|
|||
|
|
Slf_OBJECT_FIELD *pField=new Slf_OBJECT_FIELD[fNum];
|
|||
|
|
memset(pField,0,sizeof(Slf_OBJECT_FIELD)*fNum);
|
|||
|
|
for(int i=0;i<fNum;i++)
|
|||
|
|
{
|
|||
|
|
strcpy(pField[i].Name,m_pUI.tableWidget->item(i,0)->text().toStdString().c_str());
|
|||
|
|
strcpy(pField[i].HZName,m_pUI.tableWidget->item(i,1)->text().toStdString().c_str());
|
|||
|
|
strcpy(pField[i].Unit,m_pUI.tableWidget->item(i,2)->text().toStdString().c_str());
|
|||
|
|
strcpy(pField[i].HZUnit,m_pUI.tableWidget->item(i,3)->text().toStdString().c_str());
|
|||
|
|
pField[i].ArrayNum=1;
|
|||
|
|
pField[i].SamplePoint=1;
|
|||
|
|
QString CodeType=m_pUI.tableWidget->item(i,4)->text();
|
|||
|
|
for(int j=0;j<11;j++)
|
|||
|
|
{
|
|||
|
|
if(CodeType==QString(Rep_STR[j]))
|
|||
|
|
{
|
|||
|
|
pField[i].RepCode=j+1;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
pField[i].CodeLength=m_pUI.tableWidget->item(i,5)->text().toInt();
|
|||
|
|
if(QString(pField[i].Name)==""||pField[i].CodeLength<=0)
|
|||
|
|
{
|
|||
|
|
if(QString(pField[i].Name)=="")QMessageBox::warning(NULL,"警告!","字段名不能为空!");
|
|||
|
|
else QMessageBox::warning(NULL,"警告!","字段长度必须是大于0的正整数!");
|
|||
|
|
delete []pField;
|
|||
|
|
logio.Close();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
int table=logio.CreateTable(0,TableName.toLocal8Bit().toStdString().c_str(),TableHzName.toLocal8Bit().toStdString().c_str(),fNum,&pField[0],0);
|
|||
|
|
delete []pField;
|
|||
|
|
logio.CloseTable(table);
|
|||
|
|
logio.Close();
|
|||
|
|
accept();
|
|||
|
|
}
|
|||
|
|
void CCreateNewTableDlg::slotAdd()
|
|||
|
|
{
|
|||
|
|
int count=m_pUI.tableWidget->rowCount();
|
|||
|
|
m_rowCount++;
|
|||
|
|
QList<QTableWidgetSelectionRange> selRange=m_pUI.tableWidget->selectedRanges();
|
|||
|
|
bool isSel=selRange.size()>0;
|
|||
|
|
int index=0;
|
|||
|
|
int sRow=isSel?(selRange.value(0).bottomRow()+1):count+1;
|
|||
|
|
if(!isSel)
|
|||
|
|
{
|
|||
|
|
m_pUI.tableWidget->setRowCount(1+count);
|
|||
|
|
index=sRow-1;
|
|||
|
|
}else
|
|||
|
|
{
|
|||
|
|
m_pUI.tableWidget->insertRow(sRow);
|
|||
|
|
index=sRow;
|
|||
|
|
}
|
|||
|
|
m_pUI.tableWidget->setItem(index,0,new QTableWidgetItem("name"+QString::number(index+1)));
|
|||
|
|
m_pUI.tableWidget->setItem(index,1,new QTableWidgetItem("字段"+QString::number(index+1)));
|
|||
|
|
m_pUI.tableWidget->setItem(index,2,new QTableWidgetItem("NONE"));
|
|||
|
|
m_pUI.tableWidget->setItem(index,3,new QTableWidgetItem(""));
|
|||
|
|
m_pUI.tableWidget->setItem(index,4,new QTableWidgetItem("INT"));
|
|||
|
|
m_pUI.tableWidget->setItem(index,5,new QTableWidgetItem("4"));
|
|||
|
|
m_pUI.tableWidget->item(index,5)->setFlags(Qt::ItemIsEditable);
|
|||
|
|
}
|
|||
|
|
void CCreateNewTableDlg::slotDelete()
|
|||
|
|
{
|
|||
|
|
if(m_rowCount<=1)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
int count=m_pUI.tableWidget->rowCount();
|
|||
|
|
int insertRow=m_rowCount-count;
|
|||
|
|
QList<QTableWidgetSelectionRange> selRanges=m_pUI.tableWidget->selectedRanges();
|
|||
|
|
bool isSel=selRanges.size()>0;
|
|||
|
|
if(!isSel)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
int index=0;
|
|||
|
|
int sRow=0;
|
|||
|
|
int eRow=0;
|
|||
|
|
int delCount=0;
|
|||
|
|
foreach(const QTableWidgetSelectionRange &range,selRanges)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
sRow=range.topRow();
|
|||
|
|
eRow=range.bottomRow();
|
|||
|
|
index=sRow-delCount;
|
|||
|
|
delCount=0;
|
|||
|
|
for(int r=sRow;r<=eRow;r++)
|
|||
|
|
{
|
|||
|
|
--m_rowCount;
|
|||
|
|
delCount++;
|
|||
|
|
m_pUI.tableWidget->removeRow(index);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
void CCreateNewTableDlg::slotUp()
|
|||
|
|
{
|
|||
|
|
//选择一行或者多行往前移动,允许不连续选择,如删除1、2、3、8、10行
|
|||
|
|
//1、把选择的行向上移动一行,移动后被选择的行仍处于选中状态(可以继续上移,实现多步移动)
|
|||
|
|
//2、CurveNameList内容随之改变
|
|||
|
|
int count=m_pUI.tableWidget->rowCount();
|
|||
|
|
int insertRow=m_rowCount-count;
|
|||
|
|
QList<QTableWidgetSelectionRange> selRanges=m_pUI.tableWidget->selectedRanges();
|
|||
|
|
bool isSel=selRanges.size()>0;
|
|||
|
|
if(!isSel)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
DataSlotHelper::instance()->movedUp(m_pUI.tableWidget);
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
void CCreateNewTableDlg::slotDown()
|
|||
|
|
{
|
|||
|
|
//选择一行或者多行往后移动,允许不连续选择,如删除1、2、3、8、10行
|
|||
|
|
//1、把选择的行向上移动一行,移动后被选择的行仍处于选中状态(可以继续上移,实现多步移动)
|
|||
|
|
//2、CurveNameList内容随之改变
|
|||
|
|
int count=m_pUI.tableWidget->rowCount();
|
|||
|
|
int insertRow=m_rowCount-count;
|
|||
|
|
QList<QTableWidgetSelectionRange> selRanges=m_pUI.tableWidget->selectedRanges();
|
|||
|
|
bool isSel=selRanges.size()>0;
|
|||
|
|
if(!isSel)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
DataSlotHelper::instance()->movedDow(m_pUI.tableWidget);
|
|||
|
|
|
|||
|
|
}
|