abstract_canvas.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* abstract_canvas.cpp - generic canvas which can be edited with tools
  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 <algorithm>
  18. #include "abstract_canvas.h"
  19. #include "registry.h"
  20. namespace rainynite::studio {
  21. AbstractCanvas::AbstractCanvas(QWidget* parent) :
  22. QGraphicsView(parent),
  23. the_scene(make_unique<QGraphicsScene>())
  24. {
  25. setScene(the_scene.get());
  26. }
  27. void AbstractCanvas::load_registered_tools() {
  28. for (auto factory : get_canvas_tools_by_type(typeid(*this)))
  29. add_tool((*factory)());
  30. }
  31. void AbstractCanvas::add_tool(unique_ptr<CanvasTool> tool) {
  32. named_tools.emplace(tool->name(), tool.get());
  33. tools.push_back(std::move(tool));
  34. }
  35. AbstractCanvas::~AbstractCanvas() {
  36. }
  37. void AbstractCanvas::add_editor(shared_ptr<CanvasEditor> editor) {
  38. editors.push_back(editor);
  39. editor->set_canvas(this);
  40. }
  41. void AbstractCanvas::remove_editor(shared_ptr<CanvasEditor> editor) {
  42. editors.erase(std::remove(editors.begin(), editors.end(), editor), editors.end());
  43. }
  44. void AbstractCanvas::clear_editors() {
  45. editors.clear();
  46. }
  47. vector<string> AbstractCanvas::list_tools() const {
  48. vector<string> result;
  49. std::transform(
  50. tools.begin(),
  51. tools.end(),
  52. std::back_inserter(result),
  53. [](auto const& tool) {
  54. return tool->name();
  55. }
  56. );
  57. return result;
  58. }
  59. void AbstractCanvas::use_tool(string name) {
  60. auto it = named_tools.find(name);
  61. if (it == named_tools.end())
  62. return;
  63. use_tool(it->second);
  64. }
  65. void AbstractCanvas::use_tool(CanvasTool* tool) {
  66. removeEventFilter(current_tool);
  67. installEventFilter(tool);
  68. }
  69. void AbstractCanvas::active_node_changed(shared_ptr<core::AbstractValue> node) {
  70. if (active_node != node) {
  71. // TODO: handle selection
  72. active_node = node;
  73. clear_editors();
  74. add_canvas_node_editor(*this, node);
  75. }
  76. }
  77. void AbstractCanvas::set_context(shared_ptr<EditorContext> context) {
  78. for (auto const& editor : editors) {
  79. if (auto cl = dynamic_cast<ContextListener*>(editor.get())) {
  80. cl->set_context(context);
  81. }
  82. }
  83. }
  84. } // namespace rainynite::studio