scene_item_base.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 "base/unique_qptr.h"
  9. #include "editor/photo_editor_inner_common.h"
  10. #include <QGraphicsItem>
  11. class QGraphicsSceneHoverEvent;
  12. class QGraphicsSceneMouseEvent;
  13. class QStyleOptionGraphicsItem;
  14. namespace Ui {
  15. class PopupMenu;
  16. } // namespace Ui
  17. namespace Editor {
  18. class NumberedItem : public QGraphicsItem {
  19. public:
  20. enum class Status {
  21. Normal,
  22. Undid,
  23. Removed,
  24. };
  25. enum { Type = UserType + 1 };
  26. using QGraphicsItem::QGraphicsItem;
  27. int type() const override;
  28. void setNumber(int number);
  29. [[nodiscard]] int number() const;
  30. [[nodiscard]] Status status() const;
  31. void setStatus(Status status);
  32. [[nodiscard]] bool isNormalStatus() const;
  33. [[nodiscard]] bool isUndidStatus() const;
  34. [[nodiscard]] bool isRemovedStatus() const;
  35. virtual void save(SaveState state);
  36. virtual void restore(SaveState state);
  37. virtual bool hasState(SaveState state) const;
  38. private:
  39. int _number = 0;
  40. Status _status = Status::Normal;
  41. };
  42. class ItemBase : public NumberedItem {
  43. public:
  44. enum { Type = UserType + 2 };
  45. struct Data {
  46. float64 initialZoom = 0.;
  47. std::shared_ptr<float64> zPtr;
  48. int size = 0;
  49. int x = 0;
  50. int y = 0;
  51. bool flipped = false;
  52. int rotation = 0;
  53. QSize imageSize;
  54. };
  55. ItemBase(Data data);
  56. QRectF boundingRect() const override;
  57. void paint(
  58. QPainter *p,
  59. const QStyleOptionGraphicsItem *option,
  60. QWidget *widget) override;
  61. int type() const override;
  62. bool flipped() const;
  63. void setFlip(bool value);
  64. void updateZoom(float64 zoom);
  65. bool hasState(SaveState state) const override;
  66. void save(SaveState state) override;
  67. void restore(SaveState state) override;
  68. protected:
  69. enum HandleType {
  70. None,
  71. Left,
  72. Right,
  73. };
  74. void mouseMoveEvent(QGraphicsSceneMouseEvent *event) override;
  75. void hoverMoveEvent(QGraphicsSceneHoverEvent *event) override;
  76. void mousePressEvent(QGraphicsSceneMouseEvent *event) override;
  77. void mouseReleaseEvent(QGraphicsSceneMouseEvent *event) override;
  78. void contextMenuEvent(QGraphicsSceneContextMenuEvent *event) override;
  79. void keyPressEvent(QKeyEvent *e) override;
  80. using Action = void(ItemBase::*)();
  81. void performForSelectedItems(Action action);
  82. void actionFlip();
  83. void actionDelete();
  84. void actionDuplicate();
  85. QRectF contentRect() const;
  86. QRectF innerRect() const;
  87. float64 size() const;
  88. float64 horizontalSize() const;
  89. float64 verticalSize() const;
  90. void setAspectRatio(float64 aspectRatio);
  91. virtual void performFlip();
  92. virtual std::shared_ptr<ItemBase> duplicate(Data data) const = 0;
  93. private:
  94. HandleType handleType(const QPointF &pos) const;
  95. QRectF rightHandleRect() const;
  96. QRectF leftHandleRect() const;
  97. bool isHandling() const;
  98. void updateVerticalSize();
  99. void updatePens(QPen pen);
  100. void handleActionKey(not_null<QKeyEvent*> e);
  101. Data generateData() const;
  102. void applyData(const Data &data);
  103. const std::shared_ptr<float64> _lastZ;
  104. const QSize _imageSize;
  105. struct {
  106. QPen select;
  107. QPen selectInactive;
  108. QPen handle;
  109. QPen handleInactive;
  110. } _pens;
  111. base::unique_qptr<Ui::PopupMenu> _menu;
  112. struct {
  113. Data data;
  114. float64 zValue = 0.;
  115. NumberedItem::Status status;
  116. } _saved, _keeped;
  117. struct {
  118. int min = 0;
  119. int max = 0;
  120. } _sizeLimits;
  121. float64 _scaledHandleSize = 1.0;
  122. QMarginsF _scaledInnerMargins;
  123. float64 _horizontalSize = 0;
  124. float64 _verticalSize = 0;
  125. float64 _aspectRatio = 1.0;
  126. HandleType _handle = HandleType::None;
  127. bool _flipped = false;
  128. };
  129. } // namespace Editor