imagecropperlabel.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /*************************************************************************
  2. * class: ImageCropperLabel
  3. * author: github@Leopard-C
  4. * email: leopard.c@outlook.com
  5. * last change: 2020-03-06
  6. *************************************************************************/
  7. #ifndef IMAGECROPPERLABEL_H
  8. #define IMAGECROPPERLABEL_H
  9. #include <QLabel>
  10. #include <QPixmap>
  11. #include <QPen>
  12. enum class CropperShape {
  13. UNDEFINED = 0,
  14. RECT = 1,
  15. SQUARE = 2,
  16. FIXED_RECT = 3,
  17. ELLIPSE = 4,
  18. CIRCLE = 5,
  19. FIXED_ELLIPSE = 6
  20. };
  21. enum class OutputShape {
  22. RECT = 0,
  23. ELLIPSE = 1
  24. };
  25. enum class SizeType {
  26. fixedSize = 0,
  27. fitToMaxWidth = 1,
  28. fitToMaxHeight = 2,
  29. fitToMaxWidthHeight = 3,
  30. };
  31. class ImageCropperLabel : public QLabel {
  32. Q_OBJECT
  33. public:
  34. ImageCropperLabel(int width, int height, QWidget* parent);
  35. void setOriginalImage(const QPixmap& pixmap);
  36. void setOutputShape(OutputShape shape) { outputShape = shape; }
  37. QPixmap getCroppedImage();
  38. QPixmap getCroppedImage(OutputShape shape);
  39. /*****************************************
  40. * Set cropper's shape
  41. *****************************************/
  42. void setRectCropper();
  43. void setSquareCropper();
  44. void setEllipseCropper();
  45. void setCircleCropper();
  46. void setFixedRectCropper(QSize size);
  47. void setFixedEllipseCropper(QSize size);
  48. void setCropper(CropperShape shape, QSize size); // not recommended
  49. /*****************************************************************************
  50. * Set cropper's fixed size
  51. *****************************************************************************/
  52. void setCropperFixedSize(int fixedWidth, int fixedHeight);
  53. void setCropperFixedWidth(int fixedWidht);
  54. void setCropperFixedHeight(int fixedHeight);
  55. /*****************************************************************************
  56. * Set cropper's minimum size
  57. * default: the twice of minimum of the edge lenght of drag square
  58. *****************************************************************************/
  59. void setCropperMinimumSize(int minWidth, int minHeight)
  60. { cropperMinimumWidth = minWidth; cropperMinimumHeight = minHeight; }
  61. void setCropperMinimumWidth(int minWidth) { cropperMinimumWidth = minWidth; }
  62. void setCropperMinimumHeight(int minHeight) { cropperMinimumHeight = minHeight; }
  63. /*************************************************
  64. * Set the size, color, visibility of rectangular border
  65. *************************************************/
  66. void setShowRectBorder(bool show) { isShowRectBorder = show; }
  67. QPen getBorderPen() { return borderPen; }
  68. void setBorderPen(const QPen& pen) { borderPen = pen; }
  69. /*************************************************
  70. * Set the size, color of drag square
  71. *************************************************/
  72. void setShowDragSquare(bool show) { isShowDragSquare = show; }
  73. void setDragSquareEdge(int edge) { dragSquareEdge = (edge >= 3 ? edge : 3); }
  74. void setDragSquareColor(const QColor& color) { dragSquareColor = color; }
  75. /*****************************************
  76. * Opacity Effect
  77. *****************************************/
  78. void enableOpacity(bool b = true) { isShowOpacityEffect = b; }
  79. void setOpacity(double newOpacity) { opacity = newOpacity; }
  80. signals:
  81. void croppedImageChanged();
  82. protected:
  83. /*****************************************
  84. * Event
  85. *****************************************/
  86. virtual void paintEvent(QPaintEvent *event) override;
  87. virtual void mousePressEvent(QMouseEvent *e) override;
  88. virtual void mouseMoveEvent(QMouseEvent *e) override;
  89. virtual void mouseReleaseEvent(QMouseEvent *e) override;
  90. private:
  91. /***************************************
  92. * Draw shapes
  93. ***************************************/
  94. void drawFillRect(QPoint centralPoint, int edge, QColor color);
  95. void drawRectOpacity();
  96. void drawEllipseOpacity();
  97. void drawOpacity(const QPainterPath& path); // shadow effect
  98. void drawSquareEdge(bool onlyFourCorners);
  99. /***************************************
  100. * Other utility methods
  101. ***************************************/
  102. int getPosInCropperRect(const QPoint& pt);
  103. bool isPosNearDragSquare(const QPoint& pt1, const QPoint& pt2);
  104. void resetCropperPos();
  105. void changeCursor();
  106. enum {
  107. RECT_OUTSIZD = 0,
  108. RECT_INSIDE = 1,
  109. RECT_TOP_LEFT, RECT_TOP, RECT_TOP_RIGHT, RECT_RIGHT,
  110. RECT_BOTTOM_RIGHT, RECT_BOTTOM, RECT_BOTTOM_LEFT, RECT_LEFT
  111. };
  112. const bool ONLY_FOUR_CORNERS = true;
  113. private:
  114. QPixmap originalImage;
  115. QPixmap tempImage;
  116. bool isShowRectBorder = true;
  117. QPen borderPen;
  118. CropperShape cropperShape = CropperShape::UNDEFINED;
  119. OutputShape outputShape = OutputShape::RECT;
  120. QRect imageRect; // the whole image area in the label (not real size)
  121. QRect cropperRect; // a rectangle frame to choose image area (not real size)
  122. QRect cropperRect_; // cropper rect (real size)
  123. double scaledRate = 1.0;
  124. bool isLButtonPressed = false;
  125. bool isCursorPosCalculated = false;
  126. int cursorPosInCropperRect = RECT_OUTSIZD;
  127. QPoint lastPos;
  128. QPoint currPos;
  129. bool isShowDragSquare = true;
  130. int dragSquareEdge = 8;
  131. QColor dragSquareColor = Qt::white;
  132. int cropperMinimumWidth = dragSquareEdge * 2;
  133. int cropperMinimumHeight = dragSquareEdge * 2;
  134. bool isShowOpacityEffect = false;
  135. double opacity = 0.6;
  136. };
  137. #endif // IMAGECROPPERLABEL_H