editor_crop.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "ui/rp_widget.h"
  9. #include "base/flat_map.h"
  10. #include "editor/photo_editor_common.h"
  11. namespace Editor {
  12. // Crop control.
  13. class Crop final : public Ui::RpWidget {
  14. public:
  15. Crop(
  16. not_null<Ui::RpWidget*> parent,
  17. const PhotoModifications &modifications,
  18. const QSize &imageSize,
  19. EditorData type);
  20. void applyTransform(
  21. const QRect &geometry,
  22. int angle,
  23. bool flipped,
  24. const QSizeF &scaledImageSize);
  25. [[nodiscard]] QRect saveCropRect();
  26. [[nodiscard]] style::margins cropMargins() const;
  27. protected:
  28. void mousePressEvent(QMouseEvent *e) override;
  29. void mouseReleaseEvent(QMouseEvent *e) override;
  30. void mouseMoveEvent(QMouseEvent *e) override;
  31. private:
  32. struct InfoAtDown {
  33. QRectF rect;
  34. Qt::Edges edge;
  35. QPoint point;
  36. float64 cropRatio = 0.;
  37. struct Borders {
  38. int left = 0;
  39. int right = 0;
  40. int top = 0;
  41. int bottom = 0;
  42. } borders;
  43. };
  44. void paintPoints(QPainter &p);
  45. void updateEdges();
  46. [[nodiscard]] QPoint pointOfEdge(Qt::Edges e) const;
  47. void setCropPaint(QRectF &&rect);
  48. void convertCropPaintToOriginal();
  49. void computeDownState(const QPoint &p);
  50. void clearDownState();
  51. [[nodiscard]] Qt::Edges mouseState(const QPoint &p);
  52. void performCrop(const QPoint &pos);
  53. void performMove(const QPoint &pos);
  54. const int _pointSize;
  55. const float _pointSizeH;
  56. const style::margins _innerMargins;
  57. const QPoint _offset;
  58. const QMarginsF _edgePointMargins;
  59. const QSize _imageSize;
  60. const EditorData _data;
  61. base::flat_map<Qt::Edges, QRectF> _edges;
  62. struct {
  63. float64 w = 0.;
  64. float64 h = 0.;
  65. } _ratio;
  66. QRectF _cropPaint;
  67. QRectF _cropOriginal;
  68. QRectF _innerRect;
  69. QPainterPath _painterPath;
  70. InfoAtDown _down;
  71. int _angle = 0;
  72. bool _flipped = false;
  73. bool _keepAspectRatio = false;
  74. };
  75. } // namespace Editor