#include "ImageWidget.h" #include "ui_ImageWidget.h" #include #include ImageWidget::ImageWidget(QWidget *parent) : QWidget(parent), ui(new Ui::ImageWidget) { ui->setupUi(this); } ImageWidget::~ImageWidget() { delete ui; } void ImageWidget::setPixmap(const QPixmap& newPixmap) { this->pixmap = newPixmap; imageOffset = QPoint(0, 0); // 重置图片偏移量为(0, 0) setCursor(Qt::ArrowCursor); update(); // 触发重绘 } void ImageWidget::setPixmapAndPoint(const QPixmap& pixmap, double previousScaleFactor, qreal scaleFactor, QPoint mousePos) { this->pixmap = pixmap; QPointF imagePos = (mousePos - imageOffset) / previousScaleFactor; imageOffset = mousePos - imagePos * scaleFactor; update(); } void ImageWidget::clearPixmap() { this->pixmap = QPixmap(); // 将 pixmap 设置为空 update(); // 触发重绘 } void ImageWidget::paintEvent(QPaintEvent *event) { QPainter painter(this); if (!pixmap.isNull()) { // 限制图片偏移量,确保图片不会被拖动到视图外 int pixmapWidth = pixmap.width(); int pixmapHeight = pixmap.height(); int widgetWidth = width(); int widgetHeight = height(); // 限制横向偏移量,确保图片不会超出左边或右边 if (pixmapWidth < widgetWidth) { imageOffset.setX(0); // 如果图片宽度小于控件宽度,居中显示 } else { // 图片左边界不能超出控件左边,右边界不能超出控件右边 imageOffset.setX(qMin(0.0, qMax(static_cast(widgetWidth - pixmapWidth), imageOffset.x()))); } // 限制纵向偏移量,确保图片不会超出上边或下边 if (pixmapHeight < widgetHeight) { imageOffset.setY(0); // 如果图片高度小于控件高度,居中显示 } else { // 图片上边界不能超出控件上边,下边界不能超出控件下边 imageOffset.setY(qMin(0.0, qMax(static_cast(widgetHeight - pixmapHeight), imageOffset.y()))); } painter.drawPixmap(imageOffset.x(), imageOffset.y(), pixmap); QRect targetRect = { 476,476,476,476 }; if (m_nSingleCameraOperationWnd) { targetRect = { 794,794,794,794 }; } drawCross(&painter, targetRect); //DrawCrossWithScale(&painter, pixmapWidth, pixmapHeight, { 476,300 }, { 300,300 }); } } void ImageWidget::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { lastMousePos = event->pos(); // 记录鼠标按下时的位置 isDragging = true; // 设置正在拖动的标志 setCursor(Qt::OpenHandCursor); } } void ImageWidget::mouseMoveEvent(QMouseEvent *event) { if (isDragging && (event->buttons() & Qt::LeftButton)) { QPoint delta = event->pos() - lastMousePos; // 计算鼠标移动的偏移量 imageOffset += delta; // 更新图片的偏移量 lastMousePos = event->pos(); // 更新鼠标位置 update(); } } void ImageWidget::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { isDragging = false; // 重置正在拖动的标志 setCursor(Qt::ArrowCursor); } } void ImageWidget::mouseDoubleClickEvent(QMouseEvent *event){ if (event->type() == QEvent::MouseButtonDblClick) { // 恢复原始图像 sendDoubleClicksignal(); } QWidget::mouseDoubleClickEvent(event); } void ImageWidget::DrawCrossWithScale(QPainter* painter, double dImgWidth, double dImgHeight, const QSize& sourceImageSize, const QPointF& sourceImagePosition) { QPen pen; pen.setWidth(1); pen.setBrush(Qt::yellow); painter->setPen(pen); const double drX = dImgWidth / 2; const double drY = dImgHeight / 2; const int nLineSpace = 20; const int shortLineLen = 15; const int longLineLen = 35; // 计算缩放比例 double power = 1.0; auto drawLines = [&, power](double start, double end, int step, bool horizontal) { for (int i = 0; ; ++i) { // 使用无限循环,并通过条件跳出 int increase = i * nLineSpace; if (increase > std::abs(end)) break; // 检查是否超过边界 int lineLength = (i % 5 == 0 && i != 0) ? longLineLen : shortLineLen; QPointF ptX1, ptY1, ptX2, ptY2; if (horizontal) { // Horizontal lines ptX1 = QPointF((start + increase - sourceImagePosition.x()) * power, (drY - lineLength - sourceImagePosition.y()) * power); ptY1 = QPointF((start + increase - sourceImagePosition.x()) * power, (drY + lineLength - sourceImagePosition.y()) * power); ptX2 = QPointF((start - increase - sourceImagePosition.x()) * power, (drY - lineLength - sourceImagePosition.y()) * power); ptY2 = QPointF((start - increase - sourceImagePosition.x()) * power, (drY + lineLength - sourceImagePosition.y()) * power); } else { // Vertical lines ptX1 = QPointF((drX - lineLength - sourceImagePosition.x()) * power, (start + increase - sourceImagePosition.y()) * power); ptY1 = QPointF((drX + lineLength - sourceImagePosition.x()) * power, (start + increase - sourceImagePosition.y()) * power); ptX2 = QPointF((drX - lineLength - sourceImagePosition.x()) * power, (start - increase - sourceImagePosition.y()) * power); ptY2 = QPointF((drX + lineLength - sourceImagePosition.x()) * power, (start - increase - sourceImagePosition.y()) * power); } painter->drawLine(ptX1, ptY1); painter->drawLine(ptX2, ptY2); } }; // Draw the cross with scale on both sides of the center drawLines(dImgWidth / 2.0, dImgWidth, nLineSpace, true); // Right and Left side drawLines(dImgHeight / 2.0, dImgHeight, nLineSpace, false); // Top and Bottom side } void ImageWidget::drawCross(QPainter* painter, QRect rect) { QPen pen; pen.setWidth(3); pen.setBrush(Qt::yellow); painter->setPen(pen); painter->drawLine(rect.width() / 2, 0, rect.width() / 2, rect.height()); painter->drawLine(0, rect.height() / 2, rect.width(), rect.height() / 2); }