This commit is contained in:
Chenwenxuan
2024-03-06 14:54:30 +08:00
commit edac2715f0
1525 changed files with 809982 additions and 0 deletions

View File

@@ -0,0 +1,369 @@
#include "workfileedit.h"
#include <string>
#include "cppcodec/base64_default_rfc4648.hpp"
WorkFileEdit* WorkFileEdit::uniqueInstance = nullptr;
WorkFileEdit* WorkFileEdit::instance()
{
if (!uniqueInstance) {
uniqueInstance = new WorkFileEdit();
}
return uniqueInstance;
}
WorkFileEdit::WorkFileEdit()
{
pWorkFileModel = new QStandardItemModel();
headerList<<"过程名称"<<"参数"<<"文件"<<"启用";
pWorkFileModel->setHorizontalHeaderLabels(headerList);
//pWorkFileSelection = nullptr;
pWorkFileTableView = nullptr;
pSpinBox = nullptr;
pComboBox = nullptr;
}
WorkFileEdit::~WorkFileEdit()
{
if (pComboBox != nullptr)
free(pComboBox);
if (pWorkFileModel != nullptr)
free(pWorkFileModel);
if (pSpinBox != nullptr)
free(pSpinBox);
}
void WorkFileEdit::moveRow(QTableView *tableView, int currentRow, int toRow)
{
if( currentRow == toRow ){
return;
}
QStandardItemModel *model = qobject_cast<QStandardItemModel*>(tableView->model());
if (model == nullptr) {
return;
}
int column = tableView->currentIndex().column();
if(nullptr == model || currentRow < 0 || currentRow > model->rowCount()-1)
{
return ;
}
if( currentRow < toRow ){
//下移需要判断最后一行,不移动
if(currentRow == model->rowCount()-1)
{
return ;
}
}else{
//上移需要判断是否第一行,不移动
if(0 == currentRow)
{
return ;
}
}
QList<QStandardItem *> listItem = model->takeRow(currentRow);
model->insertRow(toRow,listItem);
tableView->setCurrentIndex( model->index(toRow, column) );
tableView->selectRow(toRow);
}
void WorkFileEdit::move_P()
{
if (pWorkFileModel == nullptr)
return;
if (pWorkFileTableView == nullptr)
return;
int row = pWorkFileTableView->currentIndex().row();
int nRowInsert = row - 1;
moveRow(pWorkFileTableView, row, nRowInsert);
}
void WorkFileEdit::move_N()
{
if (pWorkFileModel == nullptr)
return;
if (pWorkFileTableView == nullptr)
return;
int row = pWorkFileTableView->currentIndex().row();
int nRowInsert = row + 1;
moveRow(pWorkFileTableView, row, nRowInsert);
}
void WorkFileEdit::add_row()
{
if (pWorkFileModel == nullptr)
return;
if (pWorkFileTableView == nullptr)
return;
QList<QStandardItem *> items;
QStandardItem *item = new QStandardItem("启用");
item->setCheckable(true);
item->setCheckState(Qt::Checked);
items<<new QStandardItem("")<<new QStandardItem("")<<new QStandardItem("")<<item;
pWorkFileModel->appendRow(items);
}
void WorkFileEdit::delete_row()
{
if (pWorkFileModel == nullptr)
return;
if (pWorkFileTableView == nullptr)
return;
QModelIndex curIndex = pWorkFileTableView->currentIndex();
pWorkFileModel->removeRow(curIndex.row());
}
void WorkFileEdit::insert_row()
{
if (pWorkFileModel == nullptr)
return;
if (pWorkFileTableView == nullptr)
return;
QList<QStandardItem *> items;
QStandardItem *item = new QStandardItem("启用");
item->setCheckable(true);
item->setCheckState(Qt::Checked);
items<<new QStandardItem("")<<new QStandardItem("")<<new QStandardItem("")<<item;
QModelIndex curIndex = pWorkFileTableView->currentIndex();
pWorkFileModel->insertRow(curIndex.row(),items);
}
void WorkFileEdit::iniModelFromStringList(QStringList& aFileContent)
{
//从一个StringList 获取数据初始化数据Model
aFileContent.removeAll("");
int rowCnt=aFileContent.count(); //文本行数第1行是标题
if (rowCnt <= 1)
return;
pWorkFileModel->setRowCount(rowCnt-1); //实际数据行数
//设置表头
QString header=aFileContent.at(0);//第1行是表头
//一个或多个空格、TAB等分隔符隔开的字符串 分解为一个StringList
QStringList headerList=header.split(",",QString::SkipEmptyParts);//"\n",QString::SkipEmptyParts
pWorkFileModel->setHorizontalHeaderLabels(headerList); //设置表头文字
//设置表格数据
QString aText;
QStringList tmpList;
int j;
QStandardItem *aItem;
for (int i=1;i<rowCnt;i++)
{
QString aLineText=aFileContent.at(i); //获取数据区的一行
if (aLineText == "")
continue;
//一个或多个空格、TAB等分隔符隔开的字符串 分解为一个StringList
QStringList tmpList=aLineText.split(",",QString::SkipEmptyParts);//",",QString::SkipEmptyParts
for (j=0;j<tmpList.count()-1;j++) //tmpList的行数等于FixedColumnCount, 固定的
{ //不包含最后一列
aText = tmpList.at(j);
aItem=new QStandardItem(aText.simplified());//创建item
pWorkFileModel->setItem(i-1,j,aItem); //为模型的某个行列位置设置Item
}
aItem=new QStandardItem(headerList.at(j));//最后一列是Checkable,需要设置
//aItem=new QStandardItem();//最后一列是Checkable,设置
aItem->setCheckable(true); //设置为Checkable
//aItem->setTextAlignment(Qt::AlignHCenter);
aText = tmpList.at(j);
if (aText.simplified()=="0")
aItem->setCheckState(Qt::Unchecked); //根据数据设置check状态
else
aItem->setCheckState(Qt::Checked);
pWorkFileModel->setItem(i-1,j,aItem); //为模型的某个行列位置设置Item
}
}
void WorkFileEdit::new_file()
{
WorkFileName = "";
pWorkFileModel->removeRows(0,pWorkFileModel->rowCount());
}
void WorkFileEdit::open_file(QString aFileName)
{
if (aFileName.isEmpty())
return; //如果未选择文件,退出
if ((aFileName.contains(".CSV")) || (aFileName.contains(".csv")))
{
QStringList fFileContent;//文件内容字符串列表
QFile aFile(aFileName); //以文件方式读出
if (aFile.open(QIODevice::ReadOnly)) //以只读文本方式打开文件
{
QTextStream aStream(&aFile); //用文本流读取文件
while (!aStream.atEnd())
{
QString str=aStream.readLine();//读取文件的一行
fFileContent.append(str); //添加到 StringList
}
aFile.close();//关闭文件
WorkFileName = aFileName;
iniModelFromStringList(fFileContent);//从StringList的内容初始化数据模型
}
}
else
{
QString lineStr;
QStringList fFileContent;//文件内容字符串列表
QFile aFile(aFileName); //以文件方式读出
if (aFile.open(QIODevice::ReadOnly)) //以只读文本方式打开文件
{
QTextStream aStream(&aFile); //用文本流读取文件
while (!aStream.atEnd())
{
lineStr += aStream.readLine();
}
aFile.close();//关闭文件
std::string cstr;
cstr = std::string((const char *)lineStr.toLocal8Bit());
using base64 = cppcodec::base64_rfc4648;
std::vector<uint8_t> decoded = base64::decode(cstr);
std::string deStr(decoded.begin(), decoded.end());
QString qstring;
qstring = QString(QString::fromLocal8Bit(deStr.c_str()));
fFileContent = qstring.split("\n");
WorkFileName = aFileName;
iniModelFromStringList(fFileContent);//从StringList的内容初始化数据模型
}
}
}
void WorkFileEdit::delete_file()
{
if (WorkFileName.isEmpty())
return; //如果未选择文件,退出
QFile::remove(WorkFileName);
pWorkFileModel->removeRows(0,pWorkFileModel->rowCount());
}
void WorkFileEdit::save_file(QString aFileName)
{
if (aFileName.isEmpty())
return; //如果未选择文件,退出
QString strData;
//QStringList fFileContent;
QFile aFile(aFileName);
//QList<QStandardItem *> itemList;// = pWorkFileModel->takeRow(1);
QStandardItem * itemtest;
if (aFile.open(QIODevice::WriteOnly)) //以写文本方式打开文件
{
QTextStream aStream(&aFile);
QString lineStr;
lineStr = headerList[0]+","+headerList[1]+","+headerList[2]+","+headerList[3]+"\n";
int rowcount = pWorkFileModel->rowCount();
int columns = pWorkFileModel->columnCount();
for (int i = 0; i < rowcount; i++)
{
for (int j = 0; j < columns-1; j++)
{
{
strData += pWorkFileModel->data(pWorkFileModel->index(i,j)).toString();
//itemtest->data()
// strData += itemtest->data().toString();
strData += ", "; // for .csv file format
}
}
itemtest = pWorkFileModel->takeItem(i,columns-1);
{
QString str;
if (itemtest->checkState())
{
str = "1";
}
else
str = "0";
strData += str;
strData += ", "; // for .csv file format
}
strData[strData.count()-1] = '\n';
lineStr = lineStr + strData;
//aStream<<strData;
strData = "";
}
std::string cstr;
cstr = std::string((const char *)lineStr.toLocal8Bit());
std::string enStr = cppcodec::base64_rfc4648::encode(cstr);
QString qstring;
qstring = QString(QString::fromLocal8Bit(enStr.c_str()));
aStream << qstring;
aFile.close();//关闭文件
}
WorkFileName = aFileName;
open_file(WorkFileName);
}
void WorkFileEdit::SetTableView(QTableView *WorkFileTableView)
{
if (WorkFileTableView == nullptr)
return;
pWorkFileTableView = WorkFileTableView;
pWorkFileTableView->setModel(pWorkFileModel);
}
//void WorkFileEdit::SetItemSelectionMode(QItemSelectionModel *WorkFileSelection)
//{
// pWorkFileSelection = WorkFileSelection;
//}
void WorkFileEdit::SetComboBoxDelegate(ComboBoxDelegate *ComboBox)
{
if (pWorkFileTableView == nullptr)
return;
if (ComboBox == nullptr)
return;
pComboBox = ComboBox;
pWorkFileTableView->setItemDelegateForColumn(0, pComboBox);
}
void WorkFileEdit::SetSpinBoxDelegate(SpinBoxDelegate *SpinBox)
{
if (pWorkFileTableView == nullptr)
return;
if (SpinBox == nullptr)
return;
pSpinBox = SpinBox;
pWorkFileTableView->setItemDelegateForColumn(1, pSpinBox);
pWorkFileTableView->setItemDelegateForColumn(2, pSpinBox);
}
//void WorkFileEdit::SetCheckBoxDelegate(CheckBoxDelegate *CheckBox)
//{
// if (pWorkFileTableView == nullptr)
// return;
// if (CheckBox == nullptr)
// return;
// pCheckBox = CheckBox;
// pWorkFileTableView->setItemDelegateForColumn(3, pCheckBox);
//}
void WorkFileEdit::SetTableViewParam()
{
pWorkFileTableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
//pWorkFileTableView->setSelectionBehavior(QAbstractItemView::SelectItems);
pWorkFileTableView->setSelectionBehavior(QAbstractItemView::SelectRows);//选择一行
}
QString WorkFileEdit::getWorkFileName()
{
return WorkFileName;
}