bezier_editor.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 <fmt/format.h>
  19. #include <QGraphicsItem>
  20. #include <QDebug>
  21. #include <geom_helpers/knots.h>
  22. #include <widgets/canvas.h>
  23. #include "bezier_editor.h"
  24. using namespace fmt::literals;
  25. namespace studio {
  26. BezierKnotsDisplay::BezierKnotsDisplay() {
  27. init();
  28. }
  29. BezierKnotsDisplay::~BezierKnotsDisplay() {
  30. uninit();
  31. }
  32. void BezierKnotsDisplay::set_canvas(Canvas* canvas) {
  33. uninit();
  34. CanvasEditor::set_canvas(canvas);
  35. init();
  36. }
  37. void BezierKnotsDisplay::set_node(std::shared_ptr<core::AbstractValue> node) {
  38. uninit();
  39. NodeEditor::set_node(node);
  40. init();
  41. }
  42. void BezierKnotsDisplay::time_changed(core::Time) {
  43. uninit();
  44. init();
  45. }
  46. void BezierKnotsDisplay::init() {
  47. if (auto canvas = get_canvas()) {
  48. if (auto scene = canvas->scene()) {
  49. if (auto bezier_node = dynamic_cast<core::BaseValue<Geom::BezierKnots>*>(get_node().get())) {
  50. Geom::BezierKnots path;
  51. try {
  52. path = bezier_node->get(get_core_context()->get_time());
  53. } catch (std::exception const& ex) {
  54. qDebug() << QString::fromStdString("Uncaught exception while getting path: {}"_format(ex.what()));
  55. return;
  56. }
  57. for (auto const& knot : path.knots) {
  58. auto x = knot.pos.x();
  59. auto y = knot.pos.y();
  60. auto e = scene->addEllipse(x-2, y-2, 4, 4);
  61. knot_items.emplace_back(e);
  62. if (!knot.uid.empty()) {
  63. auto e = scene->addText(QString::fromStdString(knot.uid));
  64. e->setX(knot.pos.x());
  65. e->setY(knot.pos.y());
  66. knot_items.emplace_back(e);
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. void BezierKnotsDisplay::uninit() {
  74. if (auto canvas = get_canvas()) {
  75. for (auto const& e : knot_items) {
  76. canvas->scene()->removeItem(e.get());
  77. }
  78. knot_items.clear();
  79. }
  80. }
  81. }