bezier_editor.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /* bezier_editor.h - edit beziers on canvas
  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_BEZIER_EDITOR_H_301B627E_5F98_521F_93AD_92571F72CAAC
  18. #define STUDIO_CANVAS_BEZIER_EDITOR_H_301B627E_5F98_521F_93AD_92571F72CAAC
  19. #include <QPen>
  20. #include <geom_helpers/knots.h>
  21. #include <generic/node_editor.h>
  22. #include <generic/canvas_editor.h>
  23. #include <generic/context_listener.h>
  24. #include "point_item.h"
  25. class QGraphicsItem;
  26. class QGraphicsPathItem;
  27. namespace rainynite::studio {
  28. /**
  29. * On-canvas bezier path editor
  30. */
  31. class BezierEditor :
  32. public NodeEditor,
  33. public CanvasEditor,
  34. public PointItemListener
  35. {
  36. public:
  37. BezierEditor();
  38. virtual ~BezierEditor();
  39. void setup_canvas() override;
  40. void node_update() override;
  41. void time_changed(core::Time time_) override;
  42. void redraw();
  43. void init();
  44. void uninit();
  45. void set_appending(bool value) {
  46. appending = value;
  47. }
  48. void set_display_tags(bool display_tags_);
  49. void set_curve_pen(QPen const& pen);
  50. bool canvas_event(QEvent* event) override;
  51. void point_moved(size_t point_id, QPointF const& pos) override;
  52. void point_stopped_moving(size_t point_id) override;
  53. void point_double_clicked(size_t point_id) override;
  54. bool is_readonly() const;
  55. enum BezierPointFlag {
  56. Point = 0,
  57. Tg1,
  58. Tg2,
  59. Count
  60. };
  61. private:
  62. void add_knot_editor(size_t i);
  63. void reset_curve(Geom::BezierKnots const& path);
  64. QGraphicsItem* add_point_editor(size_t i, BezierPointFlag what, QGraphicsItem* parent = nullptr);
  65. void add_tags();
  66. void remove_tags();
  67. shared_ptr<core::BaseValue<Geom::BezierKnots>> get_bezier_node() const;
  68. Geom::BezierKnots get_path() const;
  69. Geom::Point convert_pos(QPoint const& src) const;
  70. pair<BezierPointFlag, size_t> point_id_to_fn(size_t src) const;
  71. size_t point_id_from_fn(BezierPointFlag flag, size_t n) const;
  72. private:
  73. struct EventFilter;
  74. unique_ptr<EventFilter> event_filter;
  75. vector<unique_ptr<QGraphicsItem>> knot_items;
  76. vector<unique_ptr<QGraphicsItem>> tag_items;
  77. unique_ptr<QGraphicsPathItem> curve_item;
  78. ptrdiff_t old_size = -1;
  79. bool display_tags = true;
  80. QPen curve_pen;
  81. bool appending = false;
  82. bool drawing = false;
  83. };
  84. } // namespace rainynite::studio
  85. #endif