ImageWidget.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 = { 493,493,493,493 };
  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. QWidget::paintEvent(event);
  71. }
  72. void ImageWidget::mousePressEvent(QMouseEvent *event)
  73. {
  74. if (event->button() == Qt::LeftButton)
  75. {
  76. lastMousePos = event->pos();
  77. isDragging = true;
  78. setCursor(Qt::BlankCursor);
  79. }
  80. else if (event->button() == Qt::RightButton)
  81. {
  82. unsetCursor();
  83. }
  84. QWidget::mousePressEvent(event);
  85. }
  86. void ImageWidget::mouseMoveEvent(QMouseEvent *event) {
  87. if (isDragging && (event->buttons() & Qt::LeftButton)) {
  88. QPoint delta = event->pos() - lastMousePos; // 计算鼠标移动的偏移量
  89. imageOffset += delta; // 更新图片的偏移量
  90. lastMousePos = event->pos(); // 更新鼠标位置
  91. update();
  92. }
  93. QWidget::mouseMoveEvent(event);
  94. }
  95. void ImageWidget::mouseReleaseEvent(QMouseEvent *event) {
  96. if (event->button() == Qt::LeftButton) {
  97. isDragging = false; // 重置正在拖动的标志
  98. setCursor(Qt::ArrowCursor);
  99. }
  100. QWidget::mouseReleaseEvent(event);
  101. }
  102. void ImageWidget::mouseDoubleClickEvent(QMouseEvent *event){
  103. if (event->type() == QEvent::MouseButtonDblClick) {
  104. // 恢复原始图像
  105. sendDoubleClicksignal();
  106. }
  107. QWidget::mouseDoubleClickEvent(event);
  108. }
  109. void ImageWidget::DrawCrossWithScale(QPainter* painter, double dImgWidth, double dImgHeight, const QSize& sourceImageSize, const QPointF& sourceImagePosition)
  110. {
  111. QPen pen;
  112. pen.setWidth(1);
  113. pen.setBrush(Qt::yellow);
  114. painter->setPen(pen);
  115. const double drX = dImgWidth / 2;
  116. const double drY = dImgHeight / 2;
  117. const int nLineSpace = 20;
  118. const int shortLineLen = 15;
  119. const int longLineLen = 35;
  120. // 计算缩放比例
  121. double power = 1.0;
  122. auto drawLines = [&, power](double start, double end, int step, bool horizontal) {
  123. for (int i = 0; ; ++i) { // 使用无限循环,并通过条件跳出
  124. int increase = i * nLineSpace;
  125. if (increase > std::abs(end)) break; // 检查是否超过边界
  126. int lineLength = (i % 5 == 0 && i != 0) ? longLineLen : shortLineLen;
  127. QPointF ptX1, ptY1, ptX2, ptY2;
  128. if (horizontal) {
  129. // Horizontal lines
  130. ptX1 = QPointF((start + increase - sourceImagePosition.x()) * power, (drY - lineLength - sourceImagePosition.y()) * power);
  131. ptY1 = QPointF((start + increase - sourceImagePosition.x()) * power, (drY + lineLength - sourceImagePosition.y()) * power);
  132. ptX2 = QPointF((start - increase - sourceImagePosition.x()) * power, (drY - lineLength - sourceImagePosition.y()) * power);
  133. ptY2 = QPointF((start - increase - sourceImagePosition.x()) * power, (drY + lineLength - sourceImagePosition.y()) * power);
  134. }
  135. else {
  136. // Vertical lines
  137. ptX1 = QPointF((drX - lineLength - sourceImagePosition.x()) * power, (start + increase - sourceImagePosition.y()) * power);
  138. ptY1 = QPointF((drX + lineLength - sourceImagePosition.x()) * power, (start + increase - sourceImagePosition.y()) * power);
  139. ptX2 = QPointF((drX - lineLength - sourceImagePosition.x()) * power, (start - increase - sourceImagePosition.y()) * power);
  140. ptY2 = QPointF((drX + lineLength - sourceImagePosition.x()) * power, (start - increase - sourceImagePosition.y()) * power);
  141. }
  142. painter->drawLine(ptX1, ptY1);
  143. painter->drawLine(ptX2, ptY2);
  144. }
  145. };
  146. // Draw the cross with scale on both sides of the center
  147. drawLines(dImgWidth / 2.0, dImgWidth, nLineSpace, true); // Right and Left side
  148. drawLines(dImgHeight / 2.0, dImgHeight, nLineSpace, false); // Top and Bottom side
  149. }
  150. void ImageWidget::drawCross(QPainter* painter, QRect rect)
  151. {
  152. QPen pen;
  153. pen.setWidth(3);
  154. pen.setBrush(Qt::yellow);
  155. painter->setPen(pen);
  156. painter->drawLine(rect.width() / 2, 0, rect.width() / 2, rect.height());
  157. painter->drawLine(0, rect.height() / 2, rect.width(), rect.height() / 2);
  158. }