Files
handleaiuinew/HanddleAI/parm/imagereslistview.cpp
T

359 lines
11 KiB
C++
Raw Normal View History

2024-05-18 23:53:26 +08:00
#include "imagereslistview.h"
#include "qimageview.h"
#include "qimagewidget.h"
#include <QThread>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QDebug>
#include <QHeaderView>
#include <QtConcurrent> //异步线程
#include <QFuture>
#include <QFutureWatcher>
#include "toolkit.h"
ImageResListView::ImageResListView(QWidget* parent) : QWidget(parent)
{
setWindowFlags(Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::MSWindowsFixedSizeDialogHint
| Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
m_Font = this->font();
SavePath.append(QString::fromUtf8("D:\\CCD\\result"));//结果地址 工控机1
// SavePath.append(QString::fromUtf8("C "));
// SavePath.append(QString::fromUtf8("D:/111"));
SavePath.append(QString::fromUtf8("D:\\CCD\\yuantu"));//原图地址 工控机1
// SavePath.append(QString::fromUtf8("\\192.168.1.130\\Ori"));
// SavePath.append(QString::fromUtf8("D:/Ori"));
}
ImageResListView::~ImageResListView()
{
if (m_result_List)
{
delete m_result_List;
m_result_List = nullptr;
}
if (m_view)
{
delete m_view;
m_view = nullptr;
}
}
void ImageResListView::initwidget()
{
m_Font.setFamily("Microsoft YaHei");
m_Font.setPixelSize(35);
QString m_style = "QLineEdit{border-style:outset;background:transparent;color:rgb(0,255,0);}";
// 初始化控件
qgraphicsScene = new QGraphicsScene(this);
m_view = new QImageView(this);
m_view->setScene(qgraphicsScene);//Sets the current scene to scene. If scene is already being viewed, this function does nothing.
m_view->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//关闭滑动条
m_view->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
up_label = new QLineEdit(this);
up_label->setStyleSheet(m_style);
up_label->setAttribute(Qt::WA_TranslucentBackground);
up_label->setFixedSize(200, 200);
up_label->setFont(m_Font);
up_label->setAlignment(Qt::AlignCenter);
up_label->setReadOnly(true);
up_label->setText(ok_str);
labptr.reset(up_label);
down_label = new QLineEdit(this);
down_label->setStyleSheet(m_style);
down_label->setAttribute(Qt::WA_TranslucentBackground);
down_label->setText(QString::fromUtf8("SN: "));
down_label->setFont(m_Font);
down_label->setReadOnly(true);
label_ptr.reset(down_label);
m_Font.setPixelSize(20);
m_result_List = new QListWidget(this);
m_result_List->setFont(m_Font);
m_result_List->setSpacing(5);//设置单元项的间距
m_result_List->setFlow(QListView::LeftToRight);//从左到右横向的排列,图片和文字也是从左到右排列
m_result_List->setViewMode(QListView::IconMode);//左到右横向的排列,上图下文模式
//m_result_List->setViewMode(QListView::ListMode);//子项就会从上到下排列,图片和文字是从左到右排列.
//m_result_List->setFlow(QListView::TopToBottom);//子项就会从上到下排列,图片和文字是从左到右排列.
m_result_List->setMovement(QListView::Static);//把列表设置为固定的(不能移动)
m_result_List->setSelectionBehavior(QAbstractItemView::SelectRows);//整行选中
m_result_List->setSelectionMode(QAbstractItemView::SingleSelection);//单选
m_result_List->setEditTriggers(QAbstractItemView::NoEditTriggers); //禁止编辑表格
m_result_List->setIconSize(QSize(100, 80));//设置图片的大小
m_result_List->setResizeMode(QListView::Adjust);//设置自动适应布局调整
// 设置控件布局
QHBoxLayout* mainWidgetLayout = new QHBoxLayout(this);
QWidget* mainWidget = new QWidget;
mainWidget->setMouseTracking(true);
mainWidgetLayout->addWidget(mainWidget);
mainWidgetLayout->setMargin(0);
QGridLayout* pLayout = new QGridLayout();
pLayout->addWidget(m_view, 0, 0, 12, 16);
pLayout->addWidget(up_label, 0, 14, 2, 2);
pLayout->addWidget(down_label, 10, 0, 3, 16);
pLayout->addWidget(m_result_List, 12, 0, 4, 16);
mainWidget->setLayout(pLayout);
//QVBoxLayout* pvLayout = new QVBoxLayout();
//pvLayout->addWidget(up_label);
//pvLayout->addWidget(m_view);
//pvLayout->addWidget(down_label);
//pvLayout->addWidget(m_result_List);
//pvLayout->setStretchFactor(up_label, 2);
//pvLayout->setStretchFactor(m_view, 12);
//pvLayout->setStretchFactor(down_label, 2);
//pvLayout->setStretchFactor(m_result_List, 4);
//pvLayout->setContentsMargins(3, 3, 3, 3);
//mainWidget->setLayout(pvLayout);
connect(m_result_List, &QListWidget::currentRowChanged, this, &ImageResListView::changeRowList);
connect(this, &ImageResListView::GetShow, this, &ImageResListView::showText);
}
void ImageResListView::mouseMoveEvent(QMouseEvent* event)
{
setCursor(Qt::ArrowCursor);
}
void ImageResListView::addSN(const QString sn)
{
SN = sn;
down_label->setText(SN);
}
void ImageResListView::changeRowList(int row)
{
if (row != -1 && !search_start)
{
QString path;
QImage* image = new QImage();
image->load(str_resultList.at(row));
if (image->isNull())return;
ShowImage(image);
show_index = row;
}
}
void ImageResListView::getCodeSN(const QString res) {
str_resultList.clear();
QStringList pp = res.split(",");
qDebug() << res;
if (pp.size() != 2)return;
addSN(pp.at(0));
qDebug() << pp;
QString SN = pp.at(0).split("_").at(1);
//QString SN = (pp.at(0).split("//").at(1)).split("-").at(0);
//QString index = (pp.at(0).split("//").at(1)).split("-").at(1);
//time2会显示 invalid 数据无效,未根据设定的格式,设置日期
QStringList times = pp.at(0).split("_").at(0).split("-");
//QStringList times = QString("2023-07-27-16").split("-");
QString day_time = QString("%1-%2-%3").arg(times.at(0), times.at(1), times.at(2));
QDateTime datetime = QDateTime::currentDateTime();
QString now = datetime.toString("yyyy-MM-dd");
QString h_time = times.at(3);
for (int i = 0; i < SavePath.size(); i++) {
auto var = SavePath.at(i);
//QString resultDir = QString("%1/%2/%3H/%4").arg(var, day_time, h_time, pp.at(0));
QString resultDir = QString("%1/%2/%3H/%4").arg(var, day_time, h_time, SN);
qDebug() << resultDir;
if (!isFileExist(resultDir)) {
qDebug() << QString::fromUtf8("结果文件夹不存在 ") << resultDir << endl;
continue;
}
//else {
// qDebug() << QString::fromUtf8("结果文件夹不存在 ") << resultDir << endl;
// continue;
//}
QStringList imageList = addSubFolderDirectory(resultDir);//日期下所有时间段
str_resultList << imageList;
}
emit GetShow(pp.at(1));
upListview(str_resultList);
}
void ImageResListView::ShowImage(QImage* image)
{
try
{
m_pImage = new QImageWidget(image);
qgraphicsScene->clear();
int nwith = m_view->width();//获取界面控件Graphics View的宽度
int nheight = m_view->height();//获取界面控件Graphics View的高度
m_pImage->setQGraphicsViewWH(nwith, nheight);//将界面控件Graphics View的width和height传进类m_Image中
qgraphicsScene->addItem(m_pImage);//将QGraphicsItem类对象放进QGraphicsScene中
m_view->setSceneRect(QRectF(-(nwith / 2), -(nheight / 2), nwith, nheight));
// ui->graphicsView->setScene(qgraphicsScene);
m_view->setFocus();//将界面的焦点设置到当前Graphics View控件
if (image)
{
delete image;
image = nullptr;
}
}
catch (QString ev)
{
qDebug() << __FUNCTION__ << " line:" << __LINE__ << " ev:" << ev;
}
}
void ImageResListView::setSavePath(const QStringList& path)
{
SavePath = path;
}
void ImageResListView::clearAll() {
m_result_List->clear();//清理列表
qgraphicsScene->clear();
down_label->setText(QString::fromUtf8("SN: "));
up_label->setText(ok_str);
qApp->processEvents();
str_resultList.clear();
}
QIcon loadThumbnailAsIcon(const QString& filePath, int width, int height) {
// 读取图像文件
QImage originalImage(filePath);
//QImage originalImage = loadThumbnailFast(filePath, width, height, 50);
// 检查图像是否成功加载
if (originalImage.isNull()) {
qWarning("Failed to load image.");
return QIcon(); // 返回一个空的 QIcon
}
// 生成缩略图
QImage thumbnail = originalImage.scaled(width, height, Qt::KeepAspectRatio);
// 将缩略图转换为 QIcon
QIcon thumbnailIcon = QIcon(QPixmap::fromImage(thumbnail));
return thumbnailIcon;
}
void ImageResListView::upListview(const QStringList list)
{
emit waitSearch();
m_result_List->clear();//清理列表
qApp->processEvents();
str_resultList.clear();
QFutureWatcher<void>* pwatcher = nullptr;
pwatcher = new QFutureWatcher<void>;
//把扫描到的信息输出
QFuture<void> future = QtConcurrent::run([=]()
{
search_start = 1;
auto start = std::chrono::high_resolution_clock::now();
for each (auto resultname in list) {
QStringList np = resultname.split("_");
if (np.last().indexOf(".") != -1) {
QString imgpath = resultname;
QString result = np.last().mid(0, np.last().indexOf("."));
//QString station = np.at(np.size() - 5) + "-" + np.at(np.size() - 4);
QString station = resultname.split("_").last();
if (isFileExist(imgpath)) {
QIcon icon = loadThumbnailAsIcon(imgpath,100,120);
// 获取函数执行后的时间点
//QIcon icon(imgpath);
if (!icon.isNull()) {
//存
QListWidgetItem* imgItem = new QListWidgetItem;
imgItem->setTextColor("black");
imgItem->setText(station);
imgItem->setIcon(icon);//图片路径
imgItem->setSizeHint(QSize(100, 120));
if (result.indexOf(ok_str) == -1) {
imgItem->setBackground(ng_color);
}
else {
imgItem->setBackground(ok_color);
}
m_result_List->addItem(imgItem);
str_resultList.append(resultname);
}
}
}
}
search_start = 0;
auto end = std::chrono::high_resolution_clock::now();
// 计算时间差
auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start);
// 输出函数的运行时间
std::cout << "cost: " << duration.count() << " mm" << std::endl;
//耗时操作
});
connect(pwatcher, &QFutureWatcher<void>::finished, this, [=]()
{
//主线程操作
if (list.size()) {
QString path;
QImage* image = new QImage();
if (show_index >= list.size())show_index=0;
image->load(list.at(show_index));
ShowImage(image);
}
emit waitSearch(false);
qApp->processEvents();
});
pwatcher->setFuture(future);
}
void ImageResListView::showText(const QString result)
{
if (result.indexOf(ok_str) == -1) {
up_label->setStyleSheet("QLineEdit{border-style:outset;background:transparent;color:rgb(255,0,0);}");
}
else {
up_label->setStyleSheet("QLineEdit{border-style:outset;background:transparent;color:rgb(0,255,0);}");
}
labptr->setText(result);
}
int ImageResListView::getSearch() {
return search_start;
}
void ImageResListView::stopSearch()
{
search_start = 0;
QThread::msleep(10);
}