123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- #include "ImageWidget.h"
- #include "ui_ImageWidget.h"
- #include <QPainter>
- #include <QDebug>
- 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<qreal>(widgetWidth - pixmapWidth), imageOffset.x())));
- }
- // 限制纵向偏移量,确保图片不会超出上边或下边
- if (pixmapHeight < widgetHeight)
- {
- imageOffset.setY(0); // 如果图片高度小于控件高度,居中显示
- }
- else
- {
- // 图片上边界不能超出控件上边,下边界不能超出控件下边
- imageOffset.setY(qMin(0.0, qMax(static_cast<qreal>(widgetHeight - pixmapHeight), imageOffset.y())));
- }
- painter.drawPixmap(imageOffset.x(), imageOffset.y(), pixmap);
- QRect targetRect = { 493,493,493,493 };
- if (m_nSingleCameraOperationWnd)
- {
- targetRect = { 794,794,794,794 };
- }
- drawCross(&painter, targetRect);
- //DrawCrossWithScale(&painter, pixmapWidth, pixmapHeight, { 476,300 }, { 300,300 });
- }
- QWidget::paintEvent(event);
- }
- void ImageWidget::mousePressEvent(QMouseEvent *event)
- {
- if (event->button() == Qt::LeftButton)
- {
- lastMousePos = event->pos();
- isDragging = true;
- setCursor(Qt::BlankCursor);
- }
- else if (event->button() == Qt::RightButton)
- {
- unsetCursor();
- }
- QWidget::mousePressEvent(event);
- }
- void ImageWidget::mouseMoveEvent(QMouseEvent *event) {
- if (isDragging && (event->buttons() & Qt::LeftButton)) {
- QPoint delta = event->pos() - lastMousePos; // 计算鼠标移动的偏移量
- imageOffset += delta; // 更新图片的偏移量
- lastMousePos = event->pos(); // 更新鼠标位置
- update();
- }
- QWidget::mouseMoveEvent(event);
- }
- void ImageWidget::mouseReleaseEvent(QMouseEvent *event) {
- if (event->button() == Qt::LeftButton) {
- isDragging = false; // 重置正在拖动的标志
- setCursor(Qt::ArrowCursor);
- }
- QWidget::mouseReleaseEvent(event);
- }
- 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);
- }
|