ImageWidget.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "ImageWidget.h"
  2. #include "ui_ImageWidget.h"
  3. #include <QPainter>
  4. #include <QDebug>
  5. ImageWidget::ImageWidget(QWidget *parent) :
  6. QWidget(parent),
  7. ui(new Ui::ImageWidget)
  8. {
  9. ui->setupUi(this);
  10. }
  11. ImageWidget::~ImageWidget()
  12. {
  13. delete ui;
  14. }
  15. void ImageWidget::setPixmap(const QPixmap& newPixmap) {
  16. this->pixmap = newPixmap;
  17. imageOffset = QPoint(0, 0); // 重置图片偏移量为(0, 0)
  18. setCursor(Qt::ArrowCursor);
  19. update(); // 触发重绘
  20. }
  21. void ImageWidget::setPixmapAndPoint(const QPixmap& pixmap, double previousScaleFactor, qreal scaleFactor, QPoint mousePos) {
  22. this->pixmap = pixmap;
  23. QPointF imagePos = (mousePos - imageOffset) / previousScaleFactor;
  24. imageOffset = mousePos - imagePos * scaleFactor;
  25. update();
  26. }
  27. void ImageWidget::clearPixmap() {
  28. this->pixmap = QPixmap(); // 将 pixmap 设置为空
  29. update(); // 触发重绘
  30. }
  31. void ImageWidget::paintEvent(QPaintEvent *event)
  32. {
  33. QPainter painter(this);
  34. if (!pixmap.isNull())
  35. {
  36. // 限制图片偏移量,确保图片不会被拖动到视图外
  37. int pixmapWidth = pixmap.width();
  38. int pixmapHeight = pixmap.height();
  39. int widgetWidth = width();
  40. int widgetHeight = height();
  41. // 限制横向偏移量,确保图片不会超出左边或右边
  42. if (pixmapWidth < widgetWidth)
  43. {
  44. imageOffset.setX(0); // 如果图片宽度小于控件宽度,居中显示
  45. }
  46. else
  47. {
  48. // 图片左边界不能超出控件左边,右边界不能超出控件右边
  49. imageOffset.setX(qMin(0.0, qMax(static_cast<qreal>(widgetWidth - pixmapWidth), imageOffset.x())));
  50. }
  51. // 限制纵向偏移量,确保图片不会超出上边或下边
  52. if (pixmapHeight < widgetHeight)
  53. {
  54. imageOffset.setY(0); // 如果图片高度小于控件高度,居中显示
  55. }
  56. else
  57. {
  58. // 图片上边界不能超出控件上边,下边界不能超出控件下边
  59. imageOffset.setY(qMin(0.0, qMax(static_cast<qreal>(widgetHeight - pixmapHeight), imageOffset.y())));
  60. }
  61. painter.drawPixmap(imageOffset.x(), imageOffset.y(), pixmap);
  62. QRect targetRect = { 476,476,476,476 };
  63. if (m_nSingleCameraOperationWnd)
  64. {
  65. targetRect = { 794,794,794,794 };
  66. }
  67. drawCross(&painter, targetRect);
  68. //DrawCrossWithScale(&painter, pixmapWidth, pixmapHeight, { 476,300 }, { 300,300 });
  69. }
  70. }
  71. void ImageWidget::mousePressEvent(QMouseEvent *event) {
  72. if (event->button() == Qt::LeftButton) {
  73. lastMousePos = event->pos(); // 记录鼠标按下时的位置
  74. isDragging = true; // 设置正在拖动的标志
  75. setCursor(Qt::OpenHandCursor);
  76. }
  77. }
  78. void ImageWidget::mouseMoveEvent(QMouseEvent *event) {
  79. if (isDragging && (event->buttons() & Qt::LeftButton)) {
  80. QPoint delta = event->pos() - lastMousePos; // 计算鼠标移动的偏移量
  81. imageOffset += delta; // 更新图片的偏移量
  82. lastMousePos = event->pos(); // 更新鼠标位置
  83. update();
  84. }
  85. }
  86. void ImageWidget::mouseReleaseEvent(QMouseEvent *event) {
  87. if (event->button() == Qt::LeftButton) {
  88. isDragging = false; // 重置正在拖动的标志
  89. setCursor(Qt::ArrowCursor);
  90. }
  91. }
  92. void ImageWidget::mouseDoubleClickEvent(QMouseEvent *event){
  93. if (event->type() == QEvent::MouseButtonDblClick) {
  94. // 恢复原始图像
  95. sendDoubleClicksignal();
  96. }
  97. QWidget::mouseDoubleClickEvent(event);
  98. }
  99. void ImageWidget::DrawCrossWithScale(QPainter* painter, double dImgWidth, double dImgHeight, const QSize& sourceImageSize, const QPointF& sourceImagePosition)
  100. {
  101. QPen pen;
  102. pen.setWidth(1);
  103. pen.setBrush(Qt::yellow);
  104. painter->setPen(pen);
  105. const double drX = dImgWidth / 2;
  106. const double drY = dImgHeight / 2;
  107. const int nLineSpace = 20;
  108. const int shortLineLen = 15;
  109. const int longLineLen = 35;
  110. // 计算缩放比例
  111. double power = 1.0;
  112. auto drawLines = [&, power](double start, double end, int step, bool horizontal) {
  113. for (int i = 0; ; ++i) { // 使用无限循环,并通过条件跳出
  114. int increase = i * nLineSpace;
  115. if (increase > std::abs(end)) break; // 检查是否超过边界
  116. int lineLength = (i % 5 == 0 && i != 0) ? longLineLen : shortLineLen;
  117. QPointF ptX1, ptY1, ptX2, ptY2;
  118. if (horizontal) {
  119. // Horizontal lines
  120. ptX1 = QPointF((start + increase - sourceImagePosition.x()) * power, (drY - lineLength - sourceImagePosition.y()) * power);
  121. ptY1 = QPointF((start + increase - sourceImagePosition.x()) * power, (drY + lineLength - sourceImagePosition.y()) * power);
  122. ptX2 = QPointF((start - increase - sourceImagePosition.x()) * power, (drY - lineLength - sourceImagePosition.y()) * power);
  123. ptY2 = QPointF((start - increase - sourceImagePosition.x()) * power, (drY + lineLength - sourceImagePosition.y()) * power);
  124. }
  125. else {
  126. // Vertical lines
  127. ptX1 = QPointF((drX - lineLength - sourceImagePosition.x()) * power, (start + increase - sourceImagePosition.y()) * power);
  128. ptY1 = QPointF((drX + lineLength - sourceImagePosition.x()) * power, (start + increase - sourceImagePosition.y()) * power);
  129. ptX2 = QPointF((drX - lineLength - sourceImagePosition.x()) * power, (start - increase - sourceImagePosition.y()) * power);
  130. ptY2 = QPointF((drX + lineLength - sourceImagePosition.x()) * power, (start - increase - sourceImagePosition.y()) * power);
  131. }
  132. painter->drawLine(ptX1, ptY1);
  133. painter->drawLine(ptX2, ptY2);
  134. }
  135. };
  136. // Draw the cross with scale on both sides of the center
  137. drawLines(dImgWidth / 2.0, dImgWidth, nLineSpace, true); // Right and Left side
  138. drawLines(dImgHeight / 2.0, dImgHeight, nLineSpace, false); // Top and Bottom side
  139. }
  140. void ImageWidget::drawCross(QPainter* painter, QRect rect)
  141. {
  142. QPen pen;
  143. pen.setWidth(3);
  144. pen.setBrush(Qt::yellow);
  145. painter->setPen(pen);
  146. painter->drawLine(rect.width() / 2, 0, rect.width() / 2, rect.height());
  147. painter->drawLine(0, rect.height() / 2, rect.width(), rect.height() / 2);
  148. }