rectangle_editor.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* rectangle_editor.cpp - edit rectangles on canvas
  2. * Copyright (C) 2017 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. #include <QGraphicsItem>
  18. #include <QGraphicsRectItem>
  19. #include <geom_helpers/rectangle.h>
  20. #include <generic/node_editor.h>
  21. #include <generic/canvas_editor.h>
  22. #include <widgets/canvas.h>
  23. #include <util/geom.h>
  24. #include <util/pen.h>
  25. namespace rainynite::studio {
  26. class RectangleEditor : public NodeEditor, public CanvasEditor {
  27. public:
  28. virtual ~RectangleEditor() = default;
  29. void setup_canvas() override {
  30. rectangle_item.reset(get_scene()->addRect({}));
  31. rectangle_item->setPen(pens::cosmetic_dash());
  32. update_position();
  33. }
  34. void node_update() override {
  35. update_position();
  36. }
  37. void time_changed(core::Time time) override {
  38. ContextListener::time_changed(time);
  39. update_position();
  40. }
  41. private:
  42. void update_position() {
  43. if (rectangle_item == nullptr)
  44. return;
  45. if (auto node = dynamic_cast<core::BaseValue<Geom::Rectangle>*>(get_node().get())) {
  46. auto rectangle = node->value(get_core_context());
  47. rectangle_item->setRect(
  48. rectangle.pos.x(),
  49. rectangle.pos.y(),
  50. rectangle.size.x(),
  51. rectangle.size.y()
  52. );
  53. }
  54. auto affine = get_transform(*this);
  55. rectangle_item->setTransform(QTransform{util::matrix(affine)});
  56. }
  57. unique_ptr<QGraphicsRectItem> rectangle_item;
  58. };
  59. REGISTER_CANVAS_EDITOR(Canvas, RectangleEditor, Geom::Rectangle);
  60. } // namespace rainynite::studio