Files
eUi/src/launcher/GUI/uiModule/uiModule_historyListWidget.h
T

201 lines
5.9 KiB
C++

#include <qlistwidget.h>
#include <qlabel.h>
#include <qwidget.h>
#include <qlayout.h>
#include <qgridlayout.h>
#include <qicon.h>
#include <simpleTools/fileManager.h>
#include <qthread.h>
#include <qmutex.h>
#include <opencv.hpp>
#include <qqueue.h>
#include <global.h>
class historyItemWidget : public QWidget
{
public:
QGridLayout* layout = new QGridLayout(this);
historyPack _historyPack;
QLabel* resultLabel = new QLabel(this);
QLabel* fileNameLabel = new QLabel(this);
QLabel* imgShow = new QLabel();
historyItemWidget(QString title, QSize parentSize, historyPack historyPackIn)
{
if (historyPackIn.img.isNull())
return;
_historyPack = historyPackIn;
QFont font(u8"微软雅黑", 12, QFont::Bold);
this->setObjectName("historyItemWidget");
QVBoxLayout* vlayout = new QVBoxLayout();
resultLabel->setText(title);
resultLabel->setFont(font);
resultLabel->setStyleSheet("QLabel { color: white; }");
vlayout->addWidget(resultLabel);
for (auto& str : historyPackIn.text)
{
QString text;
if (str.length() > 28)
text = QString::fromStdString(str).QString::left(28) + "...";
else
text = QString::fromStdString(str);
QLabel* label = new QLabel(text);
label->setFont(font);
label->setStyleSheet("QLabel { color: white; }");
vlayout->addWidget(label);
}
layout->addLayout(vlayout,0,0);
fileNameLabel->setText(QString::fromStdString(fileManager::getFileName(*historyPackIn.filePath)));
fileNameLabel->setFont(font);
fileNameLabel->setStyleSheet("QLabel { color: white; }");
layout->addWidget(fileNameLabel, 1, 0);
int iHeight = historyPackIn.img.size().height();
int pHeight = (parentSize.height() / 10);
double widthScale = iHeight / pHeight;
historyPackIn.img = historyPackIn.img.scaled(historyPackIn.img.size().width() * widthScale, parentSize.height() / 10, Qt::KeepAspectRatio, Qt::SmoothTransformation);
imgShow->setPixmap(historyPackIn.img);
layout->addWidget(imgShow,0,1);
layout->setContentsMargins(0, 0, 0, 0);
}
};
class historyShowListSubThread : public QThread
{
Q_OBJECT
public:
QMutex queueMutex;
QQueue<std::pair<int, std::string>> queue;
private:
void run() override
{
while (queue.size() > 0)
{
queueMutex.lock();
std::pair<int, std::string> data = queue.dequeue();
queueMutex.unlock();
if (data.first == 0)
{
cv::Mat mat = cv::imread(data.second, cv::IMREAD_REDUCED_COLOR_4);
if (mat.empty() == true)
continue;
QPixmap map;
cvMatToQPixMap(mat, map);
historyPack _historyPack;
_historyPack.filePath = std::shared_ptr<std::string>(new std::string(data.second));
_historyPack.img = map;
addDataSignal((data.second.find("OK") != std::string::npos), _historyPack);
}
else if (data.first == 1)
{
cv::Mat mat = cv::imread(data.second, -1);
if (mat.empty() == true)
return;
QPixmap map;
cvMatToQPixMap(mat, map);
emit readImgSignal(map);
}
}
}
public slots:
void asyncReadIcons(std::vector<std::string> files)
{
for (auto file : files)
{
queueMutex.lock();
queue.enqueue(std::make_pair(0, file));
queueMutex.unlock();
this->start();
}
}
void asyncReadImg(std::string filePath)
{
queueMutex.lock();
queue.enqueue(std::make_pair(1, filePath));
queueMutex.unlock();
this->start();
}
signals:
void addDataSignal(bool isOk, historyPack _historyPack);
void readImgSignal(QPixmap img);
};
class historyShowListWidget : public QWidget
{
Q_OBJECT
public:
QLabel* title = new QLabel(u8"历史记录");
QListWidget* showDataListWidget = new QListWidget();
QVBoxLayout* layout = new QVBoxLayout(this);
historyShowListSubThread _historyShowListSubThread;
historyShowListWidget()
{
QPalette palette;
palette.setColor(QPalette::WindowText, Qt::white);
QFont font(u8"微软雅黑", 16, QFont::Bold);
QHBoxLayout* hLayout = new QHBoxLayout();
title->setPalette(palette);
title->setFont(font);
title->setStyleSheet("QLabel { color: white; }");
layout->addWidget(title);
layout->addWidget(showDataListWidget);
this->setStyleSheet("background: transparent;");
QObject::connect(this, &historyShowListWidget::asyncReadImgSignal, &_historyShowListSubThread, &historyShowListSubThread::asyncReadImg);
QObject::connect(this, &historyShowListWidget::asyncReadIconsSignal, &_historyShowListSubThread, &historyShowListSubThread::asyncReadIcons);
QObject::connect(showDataListWidget, &QListWidget::itemClicked, this, &historyShowListWidget::itemClick);
QObject::connect(&_historyShowListSubThread, &historyShowListSubThread::addDataSignal, this, &historyShowListWidget::addData);
}
public slots:
void addImgFromPath(QString path)
{
std::vector<std::string> files;
std::string pathStr = fileManager::normalizePathSeparators(path).toStdString();
fileManager::getAllImages(pathStr, files, "",1);
if (files.size() == 0)
return;
showDataListWidget->clear();
emit asyncReadIconsSignal(files);
}
void addData(bool isOk, historyPack _historyPack)
{
QString title;
if (isOk == true)
title = "OK";
else
title = "NG";
QWidget* widget = new historyItemWidget(title,size(), _historyPack);
QListWidgetItem* item = new QListWidgetItem();
showDataListWidget->addItem(item);
showDataListWidget->setItemWidget(item, widget);
item->setSizeHint(widget->sizeHint());
widget->show();
showDataListWidget->scrollToBottom();
//超过50删除顶层
int size = showDataListWidget->count();
if (size > 50)
showDataListWidget->takeItem(0);
}
void reset()
{
showDataListWidget->clear();
}
private slots:
void itemClick(QListWidgetItem* item)
{
historyItemWidget* widget = (historyItemWidget*)showDataListWidget->itemWidget(item);
std::string path = *widget->_historyPack.filePath;
QString pathQ = QString::fromStdString(path);
emit asyncReadImgSignal(path);
emit itemClickSignal(pathQ);
}
signals:
void asyncReadImgSignal(std::string);
void asyncReadIconsSignal(std::vector<std::string>);
void itemClickSignal(QString path);
};
#pragma once