ImageWidget.cpp 8.7 KB

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