point_item.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* point_item.h - point item reporting its position changes
  2. * Copyright (C) 2017-2018 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #ifndef STUDIO_CANVAS_EDITORS_POINT_ITEM_H_8B768D2E_AB91_5E39_9D6E_8B2F45174CB7
  18. #define STUDIO_CANVAS_EDITORS_POINT_ITEM_H_8B768D2E_AB91_5E39_9D6E_8B2F45174CB7
  19. #include <QGraphicsEllipseItem>
  20. namespace rainynite::studio {
  21. /**
  22. * On-canvas item for displaying "point" handles.
  23. *
  24. * Basically, it's a circle with the following properties:
  25. * - has its position in center, not lefttop corner
  26. * - automatically scales to stay the same size (NOTE: works for one view only!)
  27. * - supports dragging that can be turned on and off
  28. * - reports its new position when drag is over
  29. */
  30. class AbstractPointItem : public QGraphicsEllipseItem {
  31. public:
  32. AbstractPointItem();
  33. void paint(QPainter* painter, QStyleOptionGraphicsItem const* option, QWidget* widget) override;
  34. void set_readonly(bool ro);
  35. void set_pos(double x, double y);
  36. void set_color(QColor const& color);
  37. protected:
  38. QVariant itemChange(GraphicsItemChange change, QVariant const& value) override;
  39. void mouseReleaseEvent(QGraphicsSceneMouseEvent* event) override;
  40. void mouseDoubleClickEvent(QGraphicsSceneMouseEvent* event) override;
  41. virtual void moved(QPointF const& pos) = 0;
  42. virtual void stopped_moving() = 0;
  43. virtual void double_clicked() = 0;
  44. private:
  45. static const int radius = 3;
  46. bool recursion_protection = false;
  47. };
  48. /**
  49. * Interface for editors that use point items
  50. */
  51. class PointItemListener {
  52. public:
  53. virtual void point_moved(size_t point_id, QPointF const& pos) = 0;
  54. virtual void point_stopped_moving(size_t point_id) = 0;
  55. virtual void point_double_clicked(size_t /*point_id*/) {}
  56. };
  57. /**
  58. * Point item that takes `parent` PointItemListener and calls methods on it.
  59. */
  60. class ListenerPointItem : public AbstractPointItem {
  61. public:
  62. ListenerPointItem(PointItemListener* parent, size_t id_=0) :
  63. p(parent),
  64. id(id_)
  65. {}
  66. protected:
  67. void moved(QPointF const& pos) override {
  68. p->point_moved(id, pos);
  69. }
  70. void stopped_moving() override {
  71. p->point_stopped_moving(id);
  72. }
  73. void double_clicked() override {
  74. p->point_double_clicked(id);
  75. }
  76. private:
  77. observer_ptr<PointItemListener> p;
  78. size_t id;
  79. };
  80. } // namespace rainynite::studio
  81. #endif