init
This commit is contained in:
73
device/workfileedit/checkboxdelegate.cpp
Normal file
73
device/workfileedit/checkboxdelegate.cpp
Normal file
@@ -0,0 +1,73 @@
|
||||
#include "checkboxdelegate.h"
|
||||
#include <QCheckBox>
|
||||
|
||||
CheckBoxDelegate::CheckBoxDelegate(QObject *parent)
|
||||
: QItemDelegate(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
CheckBoxDelegate::~CheckBoxDelegate()
|
||||
{
|
||||
|
||||
}
|
||||
void CheckBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
QItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
|
||||
QSize CheckBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
return QItemDelegate::sizeHint(option, index);
|
||||
}
|
||||
|
||||
QWidget *CheckBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
||||
{
|
||||
QCheckBox *editor = new QCheckBox(parent);
|
||||
editor->installEventFilter(const_cast<CheckBoxDelegate *>(this));
|
||||
return editor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return QItemDelegate::createEditor(parent, option, index);
|
||||
}
|
||||
}
|
||||
|
||||
void CheckBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
||||
{
|
||||
QString value = index.model()->data(index, Qt::DisplayRole).toString();
|
||||
QCheckBox *checkbox = static_cast<QCheckBox *>(editor);
|
||||
if (value.toInt() == 1)
|
||||
checkbox->setCheckState(Qt::Checked);
|
||||
else
|
||||
checkbox->setCheckState(Qt::Unchecked);
|
||||
}
|
||||
else
|
||||
{
|
||||
QItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
}
|
||||
|
||||
void CheckBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
||||
{
|
||||
QCheckBox *checkbox = static_cast<QCheckBox *>(editor);
|
||||
|
||||
if (checkbox->isChecked())
|
||||
model->setData(index, 1);
|
||||
else
|
||||
model->setData(index, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
QItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
}
|
||||
31
device/workfileedit/checkboxdelegate.h
Normal file
31
device/workfileedit/checkboxdelegate.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef CHECKBOXDELEGATE_H
|
||||
#define CHECKBOXDELEGATE_H
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QItemDelegate>
|
||||
#include <QModelIndex>
|
||||
#include <QPainter>
|
||||
#include <QWidget>
|
||||
|
||||
#define COMBOXCOL 3
|
||||
class CheckBoxDelegate : public QItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
CheckBoxDelegate(QObject *parent = nullptr);
|
||||
~CheckBoxDelegate();
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
|
||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
void setEditorData(QWidget *editor, const QModelIndex &index) const;
|
||||
void setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const;
|
||||
|
||||
};
|
||||
|
||||
#endif // CHECKBOXDELEGATE_H
|
||||
112
device/workfileedit/comboboxdelegate.cpp
Normal file
112
device/workfileedit/comboboxdelegate.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
#include "comboboxdelegate.h"
|
||||
#include <QComboBox>
|
||||
#include "QStringListModel"
|
||||
|
||||
ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
|
||||
: QItemDelegate(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
ComboBoxDelegate::~ComboBoxDelegate()
|
||||
{
|
||||
|
||||
}
|
||||
void ComboBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
QItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
|
||||
QSize ComboBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
return QItemDelegate::sizeHint(option, index);
|
||||
}
|
||||
|
||||
QWidget *ComboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && index.column() == COMBOXCOL)
|
||||
{
|
||||
QComboBox *editor = new QComboBox(parent);
|
||||
editor->setEditable(true);
|
||||
editor->installEventFilter(const_cast<ComboBoxDelegate *>(this));
|
||||
return editor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return QItemDelegate::createEditor(parent, option, index);
|
||||
}
|
||||
}
|
||||
|
||||
void ComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && index.column() == COMBOXCOL)
|
||||
{
|
||||
QString value = index.model()->data(index, Qt::DisplayRole).toString();
|
||||
|
||||
// QStringListModel* model = new QStringListModel;
|
||||
// QStringList stringlist;
|
||||
// stringlist << "Test1" << "Test2" << "Test3";
|
||||
|
||||
// model->setStringList(stringlist);
|
||||
// ui->comboBox->setModel(model);
|
||||
|
||||
QComboBox *combox = static_cast<QComboBox *>(editor);
|
||||
combox->addItem("上料");
|
||||
combox->addItem("下料");
|
||||
combox->addItem("功率测量");
|
||||
combox->addItem("对零");
|
||||
combox->addItem("寻边");
|
||||
combox->addItem("扫描");
|
||||
combox->addItem("打标");
|
||||
combox->addItem("加工");
|
||||
combox->addItem("晶锭预测量");
|
||||
combox->addItem("小面加工");
|
||||
combox->addItem("DD马达");
|
||||
combox->setCurrentText(value);
|
||||
//combox->setEditable(false);
|
||||
}
|
||||
else
|
||||
{
|
||||
QItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
}
|
||||
|
||||
void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && index.column() == COMBOXCOL)
|
||||
{
|
||||
QComboBox *combox = static_cast<QComboBox *>(editor);
|
||||
model->setData(index, combox->currentText());
|
||||
|
||||
|
||||
int row = model->rowCount();
|
||||
|
||||
for (int i = 0; i < row; i++)
|
||||
{
|
||||
QModelIndex index = model->index(i, 0);
|
||||
QModelIndex index1 = model->index(i, 1);
|
||||
QModelIndex index2 = model->index(i, 2);
|
||||
QString strValue = model->data(index).toString();
|
||||
if (strValue != "加工")
|
||||
{
|
||||
model->setData(index2, "");
|
||||
}
|
||||
if ((strValue == "加工") || (strValue == "功率测量") || (strValue == "DD马达"))
|
||||
{
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
model->setData(index1, "");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
QItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
}
|
||||
34
device/workfileedit/comboboxdelegate.h
Normal file
34
device/workfileedit/comboboxdelegate.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef COMBOBOXDELEGATE_H
|
||||
#define COMBOBOXDELEGATE_H
|
||||
|
||||
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QItemDelegate>
|
||||
#include <QModelIndex>
|
||||
#include <QPainter>
|
||||
#include <QWidget>
|
||||
|
||||
#define COMBOXCOL 0
|
||||
class ComboBoxDelegate : public QItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
ComboBoxDelegate(QObject *parent = nullptr);
|
||||
~ComboBoxDelegate();
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
|
||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
void setEditorData(QWidget *editor, const QModelIndex &index) const;
|
||||
void setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif // COMBOBOXDELEGATE_H
|
||||
67
device/workfileedit/spinboxdelegate.cpp
Normal file
67
device/workfileedit/spinboxdelegate.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "spinboxdelegate.h"
|
||||
#include <QSpinBox>
|
||||
|
||||
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
|
||||
: QItemDelegate(parent)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
SpinBoxDelegate::~SpinBoxDelegate()
|
||||
{
|
||||
|
||||
}
|
||||
void SpinBoxDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
QItemDelegate::paint(painter, option, index);
|
||||
}
|
||||
|
||||
QSize SpinBoxDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
|
||||
{
|
||||
return QItemDelegate::sizeHint(option, index);
|
||||
}
|
||||
|
||||
QWidget *SpinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
||||
{
|
||||
QSpinBox *editor = new QSpinBox(parent);
|
||||
editor->setRange(-180,180);
|
||||
editor->installEventFilter(const_cast<SpinBoxDelegate *>(this));
|
||||
return editor;
|
||||
}
|
||||
else
|
||||
{
|
||||
return QItemDelegate::createEditor(parent, option, index);
|
||||
}
|
||||
}
|
||||
|
||||
void SpinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
||||
{
|
||||
QString value = index.model()->data(index, Qt::DisplayRole).toString();
|
||||
QSpinBox *spinbox = static_cast<QSpinBox *>(editor);
|
||||
spinbox->setValue(value.toInt());
|
||||
}
|
||||
else
|
||||
{
|
||||
QItemDelegate::setEditorData(editor, index);
|
||||
}
|
||||
}
|
||||
|
||||
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid() && (index.column() == COMBOXCOL || index.column() == COMBOXCOL+1))
|
||||
{
|
||||
QSpinBox *spinbox = static_cast<QSpinBox *>(editor);
|
||||
model->setData(index, spinbox->value());
|
||||
}
|
||||
else
|
||||
{
|
||||
QItemDelegate::setModelData(editor, model, index);
|
||||
}
|
||||
}
|
||||
31
device/workfileedit/spinboxdelegate.h
Normal file
31
device/workfileedit/spinboxdelegate.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#ifndef SPINBOXDELEGATE_H
|
||||
#define SPINBOXDELEGATE_H
|
||||
|
||||
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QItemDelegate>
|
||||
#include <QModelIndex>
|
||||
#include <QPainter>
|
||||
#include <QWidget>
|
||||
|
||||
#define COMBOXCOL 1
|
||||
|
||||
class SpinBoxDelegate: public QItemDelegate
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
SpinBoxDelegate(QObject *parent = nullptr);
|
||||
~SpinBoxDelegate();
|
||||
|
||||
void paint(QPainter *painter, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
|
||||
|
||||
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
|
||||
const QModelIndex &index) const;
|
||||
void setEditorData(QWidget *editor, const QModelIndex &index) const;
|
||||
void setModelData(QWidget *editor, QAbstractItemModel *model,
|
||||
const QModelIndex &index) const;
|
||||
};
|
||||
|
||||
#endif // SPINBOXDELEGATE_H
|
||||
369
device/workfileedit/workfileedit.cpp
Normal file
369
device/workfileedit/workfileedit.cpp
Normal 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;
|
||||
}
|
||||
|
||||
|
||||
64
device/workfileedit/workfileedit.h
Normal file
64
device/workfileedit/workfileedit.h
Normal file
@@ -0,0 +1,64 @@
|
||||
#ifndef WORKFILEEDIT_H
|
||||
#define WORKFILEEDIT_H
|
||||
#include <QStyledItemDelegate>
|
||||
#include <QModelIndex>
|
||||
#include <QPainter>
|
||||
#include <QWidget>
|
||||
#include <QStandardItemModel>
|
||||
#include <QItemSelectionModel>
|
||||
#include <QtWidgets/QTableView>
|
||||
#include "comboboxdelegate.h"
|
||||
#include "spinboxdelegate.h"
|
||||
#include "checkboxdelegate.h"
|
||||
#include <QFile>
|
||||
#include <QTextStream>
|
||||
|
||||
|
||||
#define WORKFILEEDIT WorkFileEdit::instance()
|
||||
class WorkFileEdit
|
||||
{
|
||||
|
||||
public:
|
||||
WorkFileEdit();
|
||||
~WorkFileEdit();
|
||||
static WorkFileEdit* instance();
|
||||
void move_P();
|
||||
void move_N();
|
||||
void add_row();
|
||||
void delete_row();
|
||||
void insert_row();
|
||||
|
||||
void new_file();
|
||||
void open_file(QString aFileName);
|
||||
void delete_file();
|
||||
void save_file(QString aFileName);
|
||||
|
||||
void SetTableView(QTableView *WorkFileTableView);
|
||||
//void SetItemSelectionMode(QItemSelectionModel *pWorkFileSelection);
|
||||
void SetComboBoxDelegate(ComboBoxDelegate *pComboBox);
|
||||
void SetSpinBoxDelegate(SpinBoxDelegate *SpinBox);
|
||||
//void SetCheckBoxDelegate(CheckBoxDelegate *CheckBox);
|
||||
void SetTableViewParam();
|
||||
void iniModelFromStringList(QStringList& aFileContent);
|
||||
QString getWorkFileName();
|
||||
private:
|
||||
static WorkFileEdit* uniqueInstance;
|
||||
|
||||
|
||||
//QItemSelectionModel *pWorkFileSelection;
|
||||
QStandardItemModel *pWorkFileModel;
|
||||
QTableView *pWorkFileTableView;
|
||||
ComboBoxDelegate *pComboBox;
|
||||
SpinBoxDelegate *pSpinBox;
|
||||
//CheckBoxDelegate *pCheckBox;
|
||||
QString WorkFileName;
|
||||
QStringList headerList;
|
||||
|
||||
|
||||
void moveRow(QTableView *tableView, int currentRow, int toRow);
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif // WORKFILEEDIT_H
|
||||
Reference in New Issue
Block a user