canvas.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /*
  2. * canvas.cpp - main canvas widget
  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 <QGraphicsScene>
  20. #include <QGraphicsPixmapItem>
  21. #include <QDebug>
  22. #include <geom_helpers/knots.h>
  23. #include <core/node.h>
  24. #include "canvas.h"
  25. using namespace fmt::literals;
  26. namespace studio {
  27. Canvas::Canvas(QWidget* parent) :
  28. QGraphicsView(parent),
  29. scene(std::make_unique<QGraphicsScene>()),
  30. image(std::make_unique<QGraphicsPixmapItem>())
  31. {
  32. setScene(scene.get());
  33. scene->addItem(image.get());
  34. }
  35. Canvas::~Canvas() {
  36. }
  37. void Canvas::set_main_image(QPixmap const& pixmap) {
  38. image->setPixmap(pixmap);
  39. }
  40. void Canvas::time_changed(core::Time) {
  41. redraw_selected_node();
  42. }
  43. void Canvas::active_node_changed(std::shared_ptr<core::AbstractValue> node) {
  44. active_node = node;
  45. redraw_selected_node();
  46. }
  47. void Canvas::redraw_selected_node() {
  48. for (auto const& e : knot_items) {
  49. scene->removeItem(e.get());
  50. }
  51. knot_items.clear();
  52. if (auto bezier_node = dynamic_cast<core::BaseValue<Geom::BezierKnots>*>(active_node.get())) {
  53. Geom::BezierKnots path;
  54. try {
  55. path = bezier_node->get(get_context()->get_time());
  56. } catch (std::exception const& ex) {
  57. qDebug() << QString::fromStdString("Uncaught exception while getting path: {}"_format(ex.what()));
  58. return;
  59. }
  60. for (auto const& knot : path.knots) {
  61. auto x = knot.pos.x();
  62. auto y = knot.pos.y();
  63. auto e = scene->addEllipse(x-2, y-2, 4, 4);
  64. knot_items.emplace_back(e);
  65. if (!knot.uid.empty()) {
  66. auto e = scene->addText(QString::fromStdString(knot.uid));
  67. e->setX(knot.pos.x());
  68. e->setY(knot.pos.y());
  69. knot_items.emplace_back(e);
  70. }
  71. }
  72. }
  73. }
  74. } // namespace studio