editor_context.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* editor_context.cpp - editor Context
  2. * Copyright (C) 2017-2018 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 <QDebug>
  18. #include <core/document.h>
  19. #include <core/node_tree/exceptions.h>
  20. #include <core/node_tree/node_tree.h>
  21. #include "editor_context.h"
  22. namespace rainynite::studio {
  23. void EditorContext::set_active_node(core::NodeTreeIndex index) {
  24. if (index == active_node)
  25. return;
  26. active_node = index;
  27. changed_active_node()(index);
  28. }
  29. shared_ptr<core::AbstractValue> EditorContext::get_node(core::NodeTreeIndex index) const {
  30. if (!index)
  31. return nullptr;
  32. if (auto node_tree = tree()) {
  33. try {
  34. return node_tree->get_node(index);
  35. } catch (core::NodeTreeError const& ex) {
  36. qWarning() << ex.what();
  37. return nullptr;
  38. }
  39. }
  40. return nullptr;
  41. }
  42. shared_ptr<core::NodeTree> EditorContext::tree() const {
  43. if (auto document = get_context()->get_document()) {
  44. return document->get_tree();
  45. }
  46. return nullptr;
  47. }
  48. shared_ptr<core::ActionStack> EditorContext::action_stack() const {
  49. if (auto document = get_context()->get_document())
  50. return document->get_action_stack();
  51. // TODO: check whether it's ok to return nullptr here
  52. return nullptr;
  53. }
  54. shared_ptr<EditorContext> global_dummy_context() {
  55. static auto context_instance = make_shared<core::Context>();
  56. static auto instance = make_shared<EditorContext>(context_instance);
  57. return instance;
  58. }
  59. } // namespace rainynite::studio