阶段备份
This commit is contained in:
@@ -0,0 +1,251 @@
|
||||
#include <appCore.hpp>
|
||||
#include <QGraphicsRectItem>
|
||||
#include <QTimer>
|
||||
#include <QWidget>
|
||||
#include <QVBoxLayout>
|
||||
#include <QLabel>
|
||||
#include <QGraphicsView>
|
||||
#include <QGraphicsScene>
|
||||
#include <QGraphicsPixmapItem>
|
||||
#include <qevent.h>
|
||||
#include <qscrollbar.h>
|
||||
#include <qmutex.h>
|
||||
class FlashingRectItem : public QObject, public QGraphicsRectItem
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
FlashingRectItem(qreal x, qreal y, qreal widthIn, qreal heightIn, QGraphicsItem* parent = nullptr)
|
||||
: QGraphicsRectItem(x, y, widthIn, heightIn, parent)
|
||||
{
|
||||
width = widthIn;
|
||||
height = heightIn;
|
||||
timer = new QTimer(this);
|
||||
|
||||
connect(timer, &QTimer::timeout, this, &FlashingRectItem::toggleVisibility);
|
||||
}
|
||||
void setYellowBord()
|
||||
{
|
||||
setBorder(Qt::yellow, 20);
|
||||
}
|
||||
void setRedBord()
|
||||
{
|
||||
setBorder(Qt::red, 20);
|
||||
}
|
||||
void setGreenBord()
|
||||
{
|
||||
setBorder(Qt::green, 20);
|
||||
}
|
||||
void disableToggleVisibility()
|
||||
{
|
||||
timer->stop();
|
||||
}
|
||||
void enableToggleVisibility()
|
||||
{
|
||||
timer->start(500); // 每500毫秒切换一次可见性
|
||||
}
|
||||
public slots:
|
||||
void toggleVisibility()
|
||||
{
|
||||
setVisible(!isVisible());
|
||||
}
|
||||
|
||||
private:
|
||||
QTimer* timer;
|
||||
int width = 0, height = 0;
|
||||
void setBorder(const QColor& color, int thickness)
|
||||
{
|
||||
QPen pen;
|
||||
pen.setWidth(thickness); // 设置边框厚度
|
||||
pen.setColor(color); // 设置边框颜色
|
||||
setPen(pen);
|
||||
}
|
||||
void setFillColor(const QColor& color)
|
||||
{
|
||||
setBrush(QBrush(color));
|
||||
}
|
||||
};
|
||||
class uiModuleLocationMap :
|
||||
public QGraphicsView
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
QGraphicsScene* scene;
|
||||
uiModuleLocationMap()
|
||||
{
|
||||
//scene = new QGraphicsScene();
|
||||
//this->setScene(scene);
|
||||
mutex.lock();
|
||||
|
||||
this->setFixedHeight(320);
|
||||
setDragMode(QGraphicsView::DragMode::NoDrag);
|
||||
setInteractive(false);
|
||||
setEnabled(false);
|
||||
scene = new QGraphicsScene();
|
||||
this->setScene(scene);
|
||||
this->setObjectName("SonGraphicsView");
|
||||
this->setStyleSheet("QGraphicsView#SonGraphicsView {"
|
||||
"background: transparent;"
|
||||
"background-image: url(:/topBar/icon/waitting.png);"
|
||||
"background-repeat: no-repeat;"
|
||||
"background-position: center center;"
|
||||
"}");
|
||||
mutex.unlock();
|
||||
refreshToCurrentProduct();
|
||||
resetToDefault();
|
||||
}
|
||||
void setImg(QPixmap pixmap)
|
||||
{
|
||||
showImg(pixmap);
|
||||
scale(0.6, 0.6);
|
||||
}
|
||||
void resetImage()
|
||||
{
|
||||
scene->clear();
|
||||
setEnabled(false);
|
||||
}
|
||||
void refreshToCurrentProduct()
|
||||
{
|
||||
auto cData = _core._appConfig._switchProductWidgetDataPtr.getCurrentProductData();
|
||||
QPixmap pixmap(QString::fromStdString(cData.imgPosPath));
|
||||
setImg(pixmap);
|
||||
loadSignRects(cData.productSign);
|
||||
}
|
||||
void loadSignRects(std::unordered_map<int, productSignStruct>&rects)
|
||||
{
|
||||
flashItems.clear();
|
||||
for (auto& data : rects)
|
||||
{
|
||||
FlashingRectItem* _FlashingRectItem = new FlashingRectItem(0,0, data.second.posRect.width, data.second.posRect.height);
|
||||
//_FlashingRectItem->enableToggleVisibility();
|
||||
_FlashingRectItem->disableToggleVisibility();
|
||||
_FlashingRectItem->setVisible(true);
|
||||
_FlashingRectItem->setYellowBord();
|
||||
_FlashingRectItem->setPos(data.second.posRect.x, data.second.posRect.y); // 设置矩形的位置
|
||||
scene->addItem(_FlashingRectItem);
|
||||
flashItems.insert(std::make_pair(data.first,_FlashingRectItem));
|
||||
}
|
||||
centerOn(0, 0);
|
||||
}
|
||||
public slots:
|
||||
void resetToDefault()
|
||||
{
|
||||
for (auto& item : flashItems)
|
||||
{
|
||||
item.second->setYellowBord();
|
||||
item.second->disableToggleVisibility();
|
||||
}
|
||||
}
|
||||
void setProductSignStatus(int pos, int status)//设置产品示意图指定位置的状态
|
||||
{
|
||||
auto item = flashItems.find(pos);
|
||||
if (item == flashItems.end())
|
||||
return;
|
||||
mutex.lock();
|
||||
|
||||
switch (status)
|
||||
{
|
||||
case 0://未检测
|
||||
item->second->setYellowBord();
|
||||
item->second->disableToggleVisibility();
|
||||
break;
|
||||
case 1://正在检测
|
||||
item->second->setYellowBord();
|
||||
item->second->enableToggleVisibility();
|
||||
break;
|
||||
case 2://OK
|
||||
item->second->setGreenBord();
|
||||
item->second->disableToggleVisibility();
|
||||
item->second->setVisible(true);
|
||||
break;
|
||||
case 3://NG
|
||||
item->second->setRedBord();
|
||||
item->second->disableToggleVisibility();
|
||||
item->second->setVisible(true);
|
||||
break;
|
||||
}
|
||||
centerOn(item->second->pos());
|
||||
mutex.unlock();
|
||||
}
|
||||
protected:
|
||||
virtual void keyPressEvent(QKeyEvent* event) override
|
||||
{
|
||||
//if (event->key() == Qt::Key_O) {
|
||||
// this->resetTransform();
|
||||
// this->setSceneRect(m_image.rect());
|
||||
// this->fitInView(QRect(0, 0, m_image.width(), m_image.height()), Qt::KeepAspectRatio);
|
||||
//}
|
||||
}
|
||||
void zoom(QPoint factor)
|
||||
{
|
||||
QRectF FOV = this->mapToScene(this->rect()).boundingRect();
|
||||
QRectF FOVImage = QRectF(FOV.left(), FOV.top(), FOV.width(), FOV.height());
|
||||
float scaleX = static_cast<float>(imageWidth) / FOVImage.width();
|
||||
float scaleY = static_cast<float>(imageHeight) / FOVImage.height();
|
||||
float minScale = scaleX > scaleY ? scaleY : scaleX;
|
||||
float maxScale = scaleX > scaleY ? scaleX : scaleY;
|
||||
if ((factor.y() > 0 && minScale > 100) || (factor.y() < 0 && maxScale < 1)) {
|
||||
return;
|
||||
}
|
||||
if (factor.y() > 0)
|
||||
scale(1.2, 1.2);
|
||||
else
|
||||
scale(0.8, 0.8);
|
||||
}
|
||||
void togglePan(bool pan, const QPoint& startPos = QPoint())
|
||||
{
|
||||
if (pan) {
|
||||
if (m_isPan) {
|
||||
return;
|
||||
}
|
||||
m_isPan = true;
|
||||
m_prevPan = startPos;
|
||||
setCursor(Qt::ClosedHandCursor);
|
||||
}
|
||||
else {
|
||||
if (!m_isPan) {
|
||||
return;
|
||||
}
|
||||
m_isPan = false;
|
||||
m_prevPan = QPoint(0, 0);
|
||||
setCursor(Qt::ArrowCursor);
|
||||
}
|
||||
}
|
||||
void pan(const QPoint& panTo)
|
||||
{
|
||||
auto hBar = horizontalScrollBar();
|
||||
auto vBar = verticalScrollBar();
|
||||
auto delta = panTo - m_prevPan;
|
||||
m_prevPan = panTo;
|
||||
hBar->setValue(hBar->value() - delta.x());
|
||||
vBar->setValue(vBar->value() - delta.y());
|
||||
}
|
||||
private:
|
||||
std::unordered_map<int, FlashingRectItem*>flashItems;
|
||||
void showImg(QPixmap img)
|
||||
{
|
||||
mutex.lock();
|
||||
scene->clear();
|
||||
setEnabled(true);
|
||||
setMouseTracking(true);
|
||||
imgLabel = new QLabel();
|
||||
imgLabel->setMaximumSize(9999, 9999);
|
||||
scene->addWidget(imgLabel);
|
||||
QRect rect = img.rect();
|
||||
imageWidth = rect.width();
|
||||
imageHeight = rect.height();
|
||||
|
||||
imgLabel->setFixedSize(imageWidth, imageHeight);
|
||||
imgLabel->setPixmap(img);
|
||||
setSceneRect(rect);
|
||||
this->fitInView(imgLabel->rect(), Qt::KeepAspectRatioByExpanding);
|
||||
mutex.unlock();
|
||||
}
|
||||
bool m_isPan;
|
||||
QPoint m_prevPan;
|
||||
QLabel* imgLabel;
|
||||
QGraphicsItemGroup* rectGroup = nullptr;
|
||||
int imageWidth = 1; int imageHeight = 1;
|
||||
QMutex mutex;
|
||||
};
|
||||
#pragma once
|
||||
Reference in New Issue
Block a user