point_editor.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * bezier_editor.cpp - edit beziers on canvas
  3. * Copyright (C) 2017 caryoscelus
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <QGraphicsItem>
  19. #include <QGraphicsEllipseItem>
  20. #include <2geom/point.h>
  21. #include <widgets/canvas.h>
  22. #include "point_editor.h"
  23. namespace studio {
  24. class PointItem : public QGraphicsEllipseItem {
  25. public:
  26. using Callback = std::function<void(double, double)>;
  27. public:
  28. PointItem(Callback callback) :
  29. QGraphicsEllipseItem(0, 0, 4, 4),
  30. position_callback(callback)
  31. {}
  32. public:
  33. virtual QVariant itemChange(GraphicsItemChange change, QVariant const& value) override {
  34. if (change == ItemPositionHasChanged) {
  35. auto pos = value.toPointF();
  36. position_callback(pos.x(), pos.y());
  37. }
  38. return QGraphicsItem::itemChange(change, value);
  39. }
  40. void set_readonly(bool ro) {
  41. setFlag(QGraphicsItem::ItemIsMovable, !ro);
  42. setFlag(QGraphicsItem::ItemSendsGeometryChanges, !ro);
  43. }
  44. private:
  45. Callback position_callback;
  46. };
  47. PointEditor::PointEditor() {
  48. }
  49. PointEditor::~PointEditor() {
  50. }
  51. void PointEditor::set_canvas(Canvas* canvas) {
  52. CanvasEditor::set_canvas(canvas);
  53. point_item = std::make_unique<PointItem>(
  54. [this](double x, double y) {
  55. save_position(x, y);
  56. }
  57. );
  58. canvas->scene()->addItem(point_item.get());
  59. update_position();
  60. }
  61. void PointEditor::set_node(std::shared_ptr<core::AbstractValue> node) {
  62. NodeEditor::set_node(node);
  63. update_position();
  64. }
  65. void PointEditor::time_changed(core::Time) {
  66. update_position();
  67. }
  68. void PointEditor::update_position() {
  69. if (point_item == nullptr)
  70. return;
  71. if (auto node = dynamic_cast<core::BaseValue<Geom::Point>*>(get_node().get())) {
  72. auto point = node->get(get_time());
  73. point_item->setPos({point.x()-2, point.y()-2});
  74. point_item->set_readonly(!node->can_set());
  75. }
  76. }
  77. void PointEditor::save_position(double x, double y) {
  78. if (auto node = dynamic_cast<core::BaseValue<Geom::Point>*>(get_node().get())) {
  79. if (node->can_set())
  80. node->set({x, y});
  81. }
  82. }
  83. } // namespace studio