rectangle_editor.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * rectangle_editor.cpp - edit rectangles 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 <QGraphicsRectItem>
  20. #include <geom_helpers/rectangle.h>
  21. #include <widgets/canvas.h>
  22. #include "rectangle_editor.h"
  23. namespace studio {
  24. RectangleEditor::RectangleEditor() {
  25. }
  26. RectangleEditor::~RectangleEditor() {
  27. }
  28. void RectangleEditor::set_canvas(Canvas* canvas) {
  29. CanvasEditor::set_canvas(canvas);
  30. rectangle_item.reset(canvas->scene()->addRect({}));
  31. update_position();
  32. }
  33. void RectangleEditor::set_node(std::shared_ptr<core::AbstractValue> node) {
  34. NodeEditor::set_node(node);
  35. update_position();
  36. }
  37. void RectangleEditor::time_changed(core::Time time) {
  38. ContextListener::time_changed(time);
  39. update_position();
  40. }
  41. void RectangleEditor::update_position() {
  42. if (rectangle_item == nullptr)
  43. return;
  44. if (auto node = dynamic_cast<core::BaseValue<Geom::Rectangle>*>(get_node().get())) {
  45. auto rectangle = node->get(get_time());
  46. rectangle_item->setRect(
  47. rectangle.pos.x(),
  48. rectangle.pos.y(),
  49. rectangle.size.x(),
  50. rectangle.size.y()
  51. );
  52. }
  53. }
  54. } // namespace studio