You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

366 lines
9.1 KiB
C++

#include "ModelTableView.h"
#include "IWheelModel.h"
#include "qobject.h"
#include <QCheckBox>
#include "qheaderview.h"
#include <QApplication>
#include <QSettings>
#define MINIMGNUM 10
#pragma execution_character_set("utf-8")
enum TEM_MODEL{
emTypeModelSuccess = 0,
emTypeModelWarning,
emTypeModelError
};
ModelsModel::ModelsModel(QStringList lstHeader, QMap<QString, IWheelModel* > *pData)
{
QString strApp = QApplication::applicationDirPath();
QSettings setting(strApp + "\\user\\systemfile.ini", QSettings::IniFormat);
nGlobalMinImgs = setting.value("/MinImgs/MinNum", 10).toInt();
m_lstHeader = lstHeader;
m_pData = pData;
m_list = pData->keys();
m_Enable = true;
m_ChackEnable = true;
}
ModelsModel::~ModelsModel()
{
}
QVariant ModelsModel::headerData(int section, Qt::Orientation orientation, int role /*= Qt::DisplayRole*/) const
{
if (Qt::DisplayRole == role && Qt::Horizontal == orientation) {
if (section >= m_lstHeader.size()) {
return QVariant();
}
QString str = m_lstHeader.at(section);
if ("modelID" == str) {
return tr("型号");
}
else if ("modelID_detect" == str) {
return tr("型号");
}
else if ("count" == str) {
return tr("数量");
}
}
return QVariant();
}
int ModelsModel::rowCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return m_list.size();
}
QVariant ModelsModel::data(const QModelIndex &index, int role /*= Qt::DisplayRole*/) const
{
if (!index.isValid())
return QVariant();
int nCol = index.column();
int nRow = index.row();
if (nRow >= m_list.size()) {
return QVariant();
}
if (Qt::DisplayRole == role) {
QString str = m_lstHeader.at(nCol);
if ("modelID" == str || "modelID_detect" == str) {
QString strModel = m_list.at(nRow);
if (m_pData->contains(strModel))
return m_pData->value(strModel)->getModelID();
}
else if ("count" == str) {
QString strModel = m_list.at(nRow);
if (m_pData->contains(strModel))
return m_pData->value(strModel)->getCount();
}
}
// if (Qt::CheckStateRole == role) {
// QString str = m_lstHeader.at(nCol);
// if ("modelID_detect" == str) {
// if (m_ChackEnable == true)
// return m_pData->value(m_list.at(nRow))->getDetectState() == true ? Qt::Checked : Qt::Unchecked;
// else
// return QVariant();
// }
// }
if (Qt::DecorationRole == role) { //根据训练模板的是否成功标志位 设置列表中型号字符串前面的颜色块 绿色表示成功 红色表示失败 黄色表示没有训练
/*The data to be rendered as a decoration in the form of an icon. (QColor, QIcon or QPixmap)*/
QString str = m_lstHeader.at(nCol);
if ("modelID" == str){
QString strModel = m_list.at(nRow);
if (m_pData->contains(strModel))
{
int nTypeModel = m_pData->value(strModel)->getImageModel();
if (nTypeModel == emTypeModelSuccess)
return QColor(12, 158, 34);
else if (nTypeModel == emTypeModelWarning)
return QColor(207, 213, 77);
else if (nTypeModel == emTypeModelError)
return QColor(239, 32, 32);
else
return QColor(255, 255, 255);
}
}
}
if (Qt::TextColorRole == role){//根据训练模板的图片数量 判断数量是否低于指定的数值,如果低于设定的数值,修改字体颜色 提醒用户
QString str = m_lstHeader.at(nCol);
if ("modelID" == str){
QString strModel = m_list.at(nRow);
if (m_pData->contains(strModel))
{
int nImgNum = m_pData->value(strModel)->getImgCount();
if (nImgNum > 0 && nImgNum < nGlobalMinImgs)
return QColor(255, 0, 0);
else
return QColor(0, 0, 0);
}
}
}
if (Qt::BackgroundColorRole == role) {//根据是否加入训练标志位 设置列表对应元素的背景颜色为淡黄色 方便识别被修改的型号
QString str = m_lstHeader.at(nCol);
if ("modelID" == str) {
QString strModel = m_list.at(nRow);
if (m_pData->contains(strModel))
{
bool bAddTrain = m_pData->value(strModel)->getAddTrainFlag();
if (bAddTrain == false)
return QColor(250, 250, 0,50);
else
return QVariant();
}
}
}
return QVariant();
}
void ModelsModel::setModelList(QStringList strList)
{
m_list = strList;
beginResetModel();//强制刷新
endResetModel();
}
void ModelsModel::updateModels()
{
//m_list = m_pData->keys();
QStringList newlist;
for (int nIndex = 0; nIndex < m_list.size(); nIndex++){
QString str = m_list.at(nIndex);
if (m_pData->contains(str))
newlist.append(str);
}
m_list = newlist;
beginResetModel();//强制刷新
endResetModel();
}
void ModelsModel::update2AllModels()
{
m_list = m_pData->keys();
// QStringList newlist;
// for (int nIndex = 0; nIndex < m_list.size(); nIndex++){
// QString str = m_list.at(nIndex);
// if (m_pData->contains(str))
// newlist.append(str);
// }
// m_list = newlist;
beginResetModel();//强制刷新
endResetModel();
}
void ModelsModel::update2AllModels(QString strExp)
{
m_list = m_pData->keys();
m_list.removeAll(strExp);
beginResetModel();//强制刷新
endResetModel();
}
void ModelsModel::update2CurrentModels()
{
// update();
beginResetModel();//强制刷新
endResetModel();
}
int ModelsModel::columnCount(const QModelIndex &parent /*= QModelIndex()*/) const
{
return m_lstHeader.size();
}
bool ModelsModel::setData(const QModelIndex &index, const QVariant &value, int role /* = Qt::EditRole */)
{
if (!index.isValid())
return false;
int nColumn = index.column();
int nRow = index.row();
if (nRow >= m_list.size()) {
return false;
}
switch (role)
{
case Qt::DisplayRole:
{
if (nColumn == 0)
{
m_pData->value(m_list.at(nRow))->setModelID(value.toString());
emit dataChanged(index, index);
return true;
}
}
case Qt::CheckStateRole:
case Qt::UserRole:
{
if (nColumn == 0)
{
bool B = m_pData->value(m_list.at(nRow))->getDetectState();
m_pData->value(m_list.at(nRow))->setDetectState(1-B);// = value.toBool();
emit dataChanged(index, index);
emit sgChangeState(m_list.at(nRow));
return true;
}
}
default:
return false;
break;
}
return false;
}
void ModelsModel::onStateChanged()
{
}
void ModelsModel::onStateChanged(int state)
{
QModelIndex index;
for (int i = 0; i < rowCount(); ++i)
{
index = this->index(i, 0);
setData(index, state == Qt::Checked, Qt::CheckStateRole);
}
}
void ModelsModel::SetCheckShow(bool nEnable)
{
m_ChackEnable = nEnable;
}
void ModelsModel::SetItemEnable(bool nEnable)
{
m_Enable = nEnable;
}
Qt::ItemFlags ModelsModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return QAbstractItemModel::flags(index);
Qt::ItemFlags nflags;
if (m_Enable)
nflags= Qt::ItemIsEnabled | Qt::ItemIsUserCheckable;//| Qt::ItemIsSelectable
else
{
nflags = QAbstractItemModel::flags(index);
}
return nflags;
}
Q_SLOT void ModelsModel::onValueChange(QString nStr)
{
int nRow = 0;
if (m_pData->contains(nStr))
{
if (m_list.contains(nStr))
nRow = m_list.indexOf(nStr);
emit dataChanged(index(nRow, 0), index(nRow, m_lstHeader.size()-1));
}
}
QString ModelsModel::getModelByRow(int nRow)
{
if (nRow < m_list.size()) {
return m_list.at(nRow);
}
return QString();
}
void ModelsModel::removeModel(QString str)
{
m_list.removeOne(str);
}
ModelsView::ModelsView(QTableView *pView, QMap<QString, IWheelModel* > *p) :m_pModel(NULL)
{
m_pView = pView;
QStringList lstHeader = m_pView->property("wheel_prop_tableview_header").toStringList();
m_pView->setEditTriggers(QAbstractItemView::NoEditTriggers);
m_pView->setAlternatingRowColors(true);
m_pModel = new ModelsModel(lstHeader, p);
m_pView->setModel(m_pModel);
m_pView->horizontalHeader()->setStretchLastSection(true);//设置最后一列自动填满剩余的空间
m_pView->horizontalHeader()->sectionResizeMode(QHeaderView::ResizeToContents);//设置按内容调整列宽
m_pView->setSelectionBehavior(QAbstractItemView::SelectRows);//设置单行选择
m_pView->setAlternatingRowColors(true);//设置表格交替填充背景色
//tableView->verticalHeader()->setDefaultSectionSize(20);设置默认行高
connect(m_pModel, SIGNAL(sgChangeState(QString)), this, SIGNAL(sgItemStateChange(QString)));
connect(this, SIGNAL(sgValueChange(QString)), m_pModel, SLOT(onValueChange(QString)));
connect(m_pView, SIGNAL(clicked(const QModelIndex &)), this, SLOT(SlotListViewItem(const QModelIndex &)));
}
ModelsView::~ModelsView()
{
if (m_pModel){
delete m_pModel;
m_pModel = NULL;
}
}
Q_SLOT void ModelsView::SlotListViewItem(const QModelIndex &nIndex)
{
emit sgSelectModel(m_pModel->getModelByRow(nIndex.row()));
}
void ModelsView::update2CurrentModels()
{
m_pModel->update2CurrentModels();
}
void ModelsView::update2AllModels()
{
m_pModel->update2AllModels();
//m_pModel->removeModel("NG");
}
Q_SLOT void ModelsView::update2AllModels(QString strExp)//
{
m_pModel->update2AllModels(strExp);
}
void ModelsView::setModelList(QStringList strList)
{
for (int nIndex = 0; nIndex < strHidItem.size(); nIndex++)
{
QString str = strHidItem.at(nIndex);
strList.removeAll(str);
}
m_pModel->setModelList(strList);
}
void ModelsView::setEnable(bool b)
{
m_pModel->SetItemEnable(b);
}
void ModelsView::setCheckShow(bool b)
{
m_pModel->SetCheckShow(b);
}
void ModelsView::removeModel(QString str)
{
m_pModel->removeModel(str);
}
Q_SLOT void ModelsView::updateModels()
{
m_pModel->updateModels();
}