ImageWidget.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. isDragging(false),
  9. imageOffset(0, 0)
  10. {
  11. ui->setupUi(this);
  12. }
  13. ImageWidget::~ImageWidget()
  14. {
  15. delete ui;
  16. }
  17. void ImageWidget::setPixmap(const QPixmap& newPixmap) {
  18. this->pixmap = newPixmap;
  19. imageOffset = QPoint(0, 0); // 重置图片偏移量为(0, 0)
  20. setCursor(Qt::ArrowCursor);
  21. update(); // 触发重绘
  22. }
  23. void ImageWidget::setPixmapAndPoint(const QPixmap& pixmap, double previousScaleFactor, qreal scaleFactor, QPoint mousePos) {
  24. this->pixmap = pixmap;
  25. QPointF imagePos = (mousePos - imageOffset) / previousScaleFactor;
  26. imageOffset = mousePos - imagePos * scaleFactor;
  27. update();
  28. }
  29. void ImageWidget::clearPixmap() {
  30. this->pixmap = QPixmap(); // 将 pixmap 设置为空
  31. update(); // 触发重绘
  32. }
  33. void ImageWidget::paintEvent(QPaintEvent *event) {
  34. QPainter painter(this);
  35. if (!pixmap.isNull()) {
  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. imageOffset.setX(0); // 如果图片宽度小于控件宽度,居中显示
  44. } else {
  45. // 图片左边界不能超出控件左边,右边界不能超出控件右边
  46. imageOffset.setX(qMin(0.0, qMax(static_cast<qreal>(widgetWidth - pixmapWidth), imageOffset.x())));
  47. }
  48. // 限制纵向偏移量,确保图片不会超出上边或下边
  49. if (pixmapHeight < widgetHeight) {
  50. imageOffset.setY(0); // 如果图片高度小于控件高度,居中显示
  51. } else {
  52. // 图片上边界不能超出控件上边,下边界不能超出控件下边
  53. imageOffset.setY(qMin(0.0, qMax(static_cast<qreal>(widgetHeight - pixmapHeight), imageOffset.y())));
  54. }
  55. painter.drawPixmap(imageOffset.x(), imageOffset.y(), pixmap);
  56. }
  57. }
  58. void ImageWidget::mousePressEvent(QMouseEvent *event) {
  59. if (event->button() == Qt::LeftButton) {
  60. lastMousePos = event->pos(); // 记录鼠标按下时的位置
  61. isDragging = true; // 设置正在拖动的标志
  62. setCursor(Qt::OpenHandCursor);
  63. }
  64. }
  65. void ImageWidget::mouseMoveEvent(QMouseEvent *event) {
  66. if (isDragging && (event->buttons() & Qt::LeftButton)) {
  67. QPoint delta = event->pos() - lastMousePos; // 计算鼠标移动的偏移量
  68. imageOffset += delta; // 更新图片的偏移量
  69. lastMousePos = event->pos(); // 更新鼠标位置
  70. update();
  71. }
  72. }
  73. void ImageWidget::mouseReleaseEvent(QMouseEvent *event) {
  74. if (event->button() == Qt::LeftButton) {
  75. isDragging = false; // 重置正在拖动的标志
  76. setCursor(Qt::ArrowCursor);
  77. }
  78. }
  79. void ImageWidget::mouseDoubleClickEvent(QMouseEvent *event){
  80. if (event->type() == QEvent::MouseButtonDblClick) {
  81. // 恢复原始图像
  82. sendDoubleClicksignal();
  83. }
  84. QWidget::mouseDoubleClickEvent(event);
  85. }